Skip to content

Commit

Permalink
Add sampling strategies flag to OTEL collector
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <ploffay@redhat.com>
  • Loading branch information
pavolloffay committed Apr 27, 2020
1 parent b99114e commit f8094e3
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 4 deletions.
8 changes: 8 additions & 0 deletions cmd/opentelemetry-collector/app/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (
"flag"

"github.com/open-telemetry/opentelemetry-collector/config"
otelJaegerreceiver "github.com/open-telemetry/opentelemetry-collector/receiver/jaegerreceiver"
"github.com/open-telemetry/opentelemetry-collector/service/defaultcomponents"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/cassandra"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/elasticsearch"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/kafka"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/receiver/jaegerreceiver"
storageCassandra "github.com/jaegertracing/jaeger/plugin/storage/cassandra"
storageEs "github.com/jaegertracing/jaeger/plugin/storage/es"
storageKafka "github.com/jaegertracing/jaeger/plugin/storage/kafka"
Expand Down Expand Up @@ -56,6 +58,12 @@ func Components(v *viper.Viper) config.Factories {
factories.Exporters[kafkaExp.Type()] = kafkaExp
factories.Exporters[cassandraExp.Type()] = cassandraExp
factories.Exporters[esExp.Type()] = esExp

otelJRec := factories.Receivers["jaeger"].(*otelJaegerreceiver.Factory)
factories.Receivers["jaeger"] = &jaegerreceiver.Factory{
Wrapped: otelJRec,
Viper: v,
}
return factories
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jaegerreceiver

import (
"context"

"github.com/open-telemetry/opentelemetry-collector/component"
"github.com/open-telemetry/opentelemetry-collector/config/configerror"
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
"github.com/open-telemetry/opentelemetry-collector/consumer"
"github.com/open-telemetry/opentelemetry-collector/receiver/jaegerreceiver"
"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/plugin/sampling/strategystore/static"
)

// Factory wraps jaegerreceiver.Factory and makes the default config configurable via viper.
// For instance this enables using flags as default values in the config object.
type Factory struct {
// Wrapped is Jaeger receiver.
Wrapped *jaegerreceiver.Factory
// Viper is used to get configuration values for default configuration
Viper *viper.Viper
}

var _ component.ReceiverFactoryOld = (*Factory)(nil)

// Type gets the type of exporter.
func (f *Factory) Type() string {
return f.Wrapped.Type()
}

// CreateDefaultConfig returns default configuration of Factory.
// This function implements OTEL component.BaseFactory interface.
func (f *Factory) CreateDefaultConfig() configmodels.Receiver {
cfg := f.Wrapped.CreateDefaultConfig().(*jaegerreceiver.Config)
strategyFile := f.Viper.GetString(static.SamplingStrategiesFile)
// if remote sampling struct is not nil the factory will use default values for endpoints and enable remote sampling
// the problem is that flag will always return some default value
// so we cannot distinguish when it should be enabled and when not.
// Using default values makes sense when a component is enabled
var samplingConf *jaegerreceiver.RemoteSamplingConfig
if strategyFile != "" {
samplingConf = &jaegerreceiver.RemoteSamplingConfig{
StrategyFile: strategyFile,
}
}
cfg.RemoteSampling = samplingConf
return cfg
}

// CreateTraceReceiver creates Jaeger receiver trace receiver.
// This function implements OTEL component.ReceiverFactory interface.
func (f *Factory) CreateTraceReceiver(
ctx context.Context,
log *zap.Logger,
cfg configmodels.Receiver,
nextConsumer consumer.TraceConsumerOld,
) (component.TraceReceiver, error) {
return f.Wrapped.CreateTraceReceiver(ctx, log, cfg, nextConsumer)
}

// CustomUnmarshaler creates custom unmarshaller for Jaeger receiver config.
// This function implements component.ReceiverFactoryBase interface.
func (f *Factory) CustomUnmarshaler() component.CustomUnmarshaler {
return f.Wrapped.CustomUnmarshaler()
}

// CreateMetricsReceiver creates a metrics receiver based on provided config.
// This function implements component.ReceiverFactory.
func (f *Factory) CreateMetricsReceiver(
_ *zap.Logger,
_ configmodels.Receiver,
_ consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jaegerreceiver

import (
"testing"

"github.com/open-telemetry/opentelemetry-collector/config/configerror"
"github.com/open-telemetry/opentelemetry-collector/receiver/jaegerreceiver"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/plugin/sampling/strategystore/static"
)

func TestDefaultValueFromViper(t *testing.T) {
v := viper.New()
v.Set(static.SamplingStrategiesFile, "config.json")
jr := &jaegerreceiver.Factory{}

f := &Factory{
Wrapped: jr,
Viper: v,
}

cfg := f.CreateDefaultConfig().(*jaegerreceiver.Config)
assert.Equal(t, "config.json", cfg.RemoteSampling.StrategyFile)
}

func TestType(t *testing.T) {
f := &Factory{
Wrapped: &jaegerreceiver.Factory{},
}
assert.Equal(t, "jaeger", f.Type())
}

func TestCreateMetricsExporter(t *testing.T) {
f := &Factory{
Wrapped: &jaegerreceiver.Factory{},
}
mReceiver, err := f.CreateMetricsReceiver(zap.NewNop(), nil, nil)
assert.Equal(t, err, configerror.ErrDataTypeIsNotSupported)
assert.Nil(t, mReceiver)
}
2 changes: 1 addition & 1 deletion cmd/opentelemetry-collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/census-instrumentation/opencensus-proto v0.2.1
github.com/jaegertracing/jaeger v1.17.0
github.com/magiconair/properties v1.8.1
github.com/open-telemetry/opentelemetry-collector v0.3.1-0.20200408203355-0e1b2e323d39
github.com/open-telemetry/opentelemetry-collector v0.3.1-0.20200420175007-048d7d257824
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.5.0
Expand Down
3 changes: 3 additions & 0 deletions cmd/opentelemetry-collector/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ github.com/open-telemetry/opentelemetry-collector v0.2.8-0.20200323151927-794a2b
github.com/open-telemetry/opentelemetry-collector v0.2.8-0.20200323151927-794a2b689bd9/go.mod h1:7Sxe+ROKhJSqPZqsKnvb/y5EbEsjbkDTbQ2sS7cF9rY=
github.com/open-telemetry/opentelemetry-collector v0.3.1-0.20200408203355-0e1b2e323d39 h1:D8QuOoh7ImcWA/o0RJ8s7M4QojCWSgiK9LBhYONDD1E=
github.com/open-telemetry/opentelemetry-collector v0.3.1-0.20200408203355-0e1b2e323d39/go.mod h1:aZAL+YwTtk+1YkTj8dDgTvv06dU8twzOdRowRtBfEKo=
github.com/open-telemetry/opentelemetry-collector v0.3.1-0.20200420175007-048d7d257824 h1:3FftRuw4Hqe1X2mG1z+0nJHyhjsAOrE6qZXXB45B86Q=
github.com/open-telemetry/opentelemetry-collector v0.3.1-0.20200420175007-048d7d257824/go.mod h1:aZAL+YwTtk+1YkTj8dDgTvv06dU8twzOdRowRtBfEKo=
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200206071824-8310c432e51c h1:nDOtl6j2Ei16tlnx/o4qKEelpHtGoZ9ArwU+tux4Ia8=
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200206071824-8310c432e51c/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200211051721-ff5f19c6217d h1:hZcHR0at6tb3jBjaPHlfLr6yK7rTrA8xGCS6jlUSLcU=
Expand Down Expand Up @@ -1129,6 +1131,7 @@ github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c/go.mod h1:kJ9mm
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
github.com/shirou/gopsutil v2.20.3+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt3d2aYa0SiNms/hFqC9qJYolM=
Expand Down
4 changes: 4 additions & 0 deletions cmd/opentelemetry-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/elasticsearch"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/kafka"
jconfig "github.com/jaegertracing/jaeger/pkg/config"
"github.com/jaegertracing/jaeger/plugin/sampling/strategystore/static"
"github.com/jaegertracing/jaeger/plugin/storage"
)

Expand Down Expand Up @@ -88,6 +90,7 @@ func main() {
collectorApp.AddFlags,
jflags.AddConfigFileFlag,
storageFlags,
static.AddFlags,
)

// parse flags to propagate Jaeger config file flag value to viper
Expand All @@ -104,6 +107,7 @@ func main() {
// getOTELConfigFile returns name of OTEL config file.
func getOTELConfigFile() string {
f := &flag.FlagSet{}
f.SetOutput(ioutil.Discard)
builder.Flags(f)
// parse flags to bind the value
f.Parse(os.Args[1:])
Expand Down
7 changes: 4 additions & 3 deletions plugin/sampling/strategystore/static/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
)

const (
samplingStrategiesFile = "sampling.strategies-file"
// SamplingStrategiesFile defines configuration name for sampling strategies file.
SamplingStrategiesFile = "sampling.strategies-file"
)

// Options holds configuration for the static sampling strategy store.
Expand All @@ -32,11 +33,11 @@ type Options struct {

// AddFlags adds flags for Options
func AddFlags(flagSet *flag.FlagSet) {
flagSet.String(samplingStrategiesFile, "", "The path for the sampling strategies file in JSON format. See sampling documentation to see format of the file")
flagSet.String(SamplingStrategiesFile, "", "The path for the sampling strategies file in JSON format. See sampling documentation to see format of the file")
}

// InitFromViper initializes Options with properties from viper
func (opts *Options) InitFromViper(v *viper.Viper) *Options {
opts.StrategiesFile = v.GetString(samplingStrategiesFile)
opts.StrategiesFile = v.GetString(SamplingStrategiesFile)
return opts
}

0 comments on commit f8094e3

Please sign in to comment.