Skip to content

Commit

Permalink
Add Context to the missing CreateMetricsReceiver method
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jun 26, 2020
1 parent f06d74d commit 8b754f7
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 53 deletions.
4 changes: 2 additions & 2 deletions component/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ type ReceiverFactoryOld interface {
// CreateMetricsReceiver creates a metrics receiver based on this config.
// If the receiver type does not support metrics or if the config is not valid
// error will be returned instead.
CreateMetricsReceiver(logger *zap.Logger, cfg configmodels.Receiver,
consumer consumer.MetricsConsumerOld) (MetricsReceiver, error)
CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver,
nextConsumer consumer.MetricsConsumerOld) (MetricsReceiver, error)
}

// ReceiverCreateParams is passed to ReceiverFactory.Create* functions.
Expand Down
6 changes: 1 addition & 5 deletions component/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ func (f *TestReceiverFactory) CreateTraceReceiver(
}

// CreateMetricsReceiver creates a metrics receiver based on this config.
func (f *TestReceiverFactory) CreateMetricsReceiver(
logger *zap.Logger,
cfg configmodels.Receiver,
consumer consumer.MetricsConsumerOld,
) (MetricsReceiver, error) {
func (f *TestReceiverFactory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (MetricsReceiver, error) {
// Not used for this test, just return nil
return nil, nil
}
Expand Down
12 changes: 2 additions & 10 deletions config/example_factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ func (f *ExampleReceiverFactory) createReceiver(cfg configmodels.Receiver) *Exam
}

// CreateMetricsReceiver creates a metrics receiver based on this config.
func (f *ExampleReceiverFactory) CreateMetricsReceiver(
logger *zap.Logger,
cfg configmodels.Receiver,
nextConsumer consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
func (f *ExampleReceiverFactory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {
if cfg.(*ExampleReceiver).FailMetricsCreation {
return nil, configerror.ErrDataTypeIsNotSupported
}
Expand Down Expand Up @@ -244,11 +240,7 @@ func (f *MultiProtoReceiverFactory) CreateTraceReceiver(
}

// CreateMetricsReceiver creates a metrics receiver based on this config.
func (f *MultiProtoReceiverFactory) CreateMetricsReceiver(
logger *zap.Logger,
cfg configmodels.Receiver,
consumer consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
func (f *MultiProtoReceiverFactory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {
// Not used for this test, just return nil
return nil, nil
}
Expand Down
8 changes: 2 additions & 6 deletions receiver/opencensusreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,14 @@ func (f *Factory) CreateTraceReceiver(
}

// CreateMetricsReceiver creates a metrics receiver based on provided config.
func (f *Factory) CreateMetricsReceiver(
logger *zap.Logger,
cfg configmodels.Receiver,
consumer consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
func (f *Factory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {

r, err := f.createReceiver(cfg)
if err != nil {
return nil, err
}

r.metricsConsumer = consumer
r.metricsConsumer = nextConsumer

return r, nil
}
Expand Down
4 changes: 2 additions & 2 deletions receiver/opencensusreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCreateReceiver(t *testing.T) {
assert.NotNil(t, tReceiver)
assert.NoError(t, err)

mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil)
mReceiver, err := factory.CreateMetricsReceiver(context.Background(), zap.NewNop(), cfg, nil)
assert.NotNil(t, mReceiver)
assert.NoError(t, err)
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestCreateMetricReceiver(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sink := new(exportertest.SinkMetricsExporterOld)
tc, err := factory.CreateMetricsReceiver(logger, tt.cfg, sink)
tc, err := factory.CreateMetricsReceiver(context.Background(), logger, tt.cfg, sink)
if (err != nil) != tt.wantErr {
t.Errorf("factory.CreateMetricsReceiver() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
8 changes: 2 additions & 6 deletions receiver/prometheusreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,10 @@ func (f *Factory) CreateTraceReceiver(
}

// CreateMetricsReceiver creates a metrics receiver based on provided config.
func (f *Factory) CreateMetricsReceiver(
logger *zap.Logger,
cfg configmodels.Receiver,
consumer consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
func (f *Factory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {
config := cfg.(*Config)
if config.PrometheusConfig == nil || len(config.PrometheusConfig.ScrapeConfigs) == 0 {
return nil, errNilScrapeConfig
}
return newPrometheusReceiver(logger, config, consumer), nil
return newPrometheusReceiver(logger, config, nextConsumer), nil
}
2 changes: 1 addition & 1 deletion receiver/prometheusreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCreateReceiver(t *testing.T) {

// The default config does not provide scrape_config so we expect that metrics receiver
// creation must also fail.
mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil)
mReceiver, err := factory.CreateMetricsReceiver(context.Background(), zap.NewNop(), cfg, nil)
assert.Equal(t, err, errNilScrapeConfig)
assert.Nil(t, mReceiver)
}
10 changes: 3 additions & 7 deletions receiver/vmmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,15 @@ func (f *Factory) CreateTraceReceiver(
}

// CreateMetricsReceiver creates a metrics receiver based on provided config.
func (f *Factory) CreateMetricsReceiver(
logger *zap.Logger,
config configmodels.Receiver,
consumer consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
func (f *Factory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {

if runtime.GOOS != "linux" {
// TODO: add support for other platforms.
return nil, errors.New("vmmetrics receiver is only supported on linux")
}
cfg := config.(*Config)
rCfg := cfg.(*Config)

vmc, err := NewVMMetricsCollector(cfg.ScrapeInterval, cfg.MountPoint, cfg.ProcessMountPoint, cfg.MetricPrefix, consumer)
vmc, err := NewVMMetricsCollector(rCfg.ScrapeInterval, rCfg.MountPoint, rCfg.ProcessMountPoint, rCfg.MetricPrefix, nextConsumer)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion receiver/vmmetricsreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestCreateReceiver(t *testing.T) {
assert.Equal(t, err, configerror.ErrDataTypeIsNotSupported)
assert.Nil(t, tReceiver)

mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil)
mReceiver, err := factory.CreateMetricsReceiver(context.Background(), zap.NewNop(), cfg, nil)

// Currently vmmetrics receiver is only supported on linux.
if runtime.GOOS != "linux" {
Expand Down
6 changes: 1 addition & 5 deletions receiver/zipkinreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ func (f *Factory) CreateTraceReceiver(
}

// CreateMetricsReceiver creates a metrics receiver based on provided config.
func (f *Factory) CreateMetricsReceiver(
logger *zap.Logger,
cfg configmodels.Receiver,
consumer consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
func (f *Factory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}
2 changes: 1 addition & 1 deletion receiver/zipkinreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCreateReceiver(t *testing.T) {
assert.Nil(t, err, "receiver creation failed")
assert.NotNil(t, tReceiver, "receiver creation failed")

mReceiver, err := factory.CreateMetricsReceiver(zap.NewNop(), cfg, nil)
mReceiver, err := factory.CreateMetricsReceiver(context.Background(), zap.NewNop(), cfg, nil)
assert.Equal(t, err, configerror.ErrDataTypeIsNotSupported)
assert.Nil(t, mReceiver)
}
4 changes: 2 additions & 2 deletions service/builder/receivers_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,13 @@ func createMetricsReceiver(
// If both receiver and consumer are of the old type (can manipulate on OC metrics only),
// use Factory.CreateMetricsReceiver.
if nextConsumer, ok := nextConsumer.(consumer.MetricsConsumerOld); ok {
return factoryOld.CreateMetricsReceiver(logger, cfg, nextConsumer)
return factoryOld.CreateMetricsReceiver(context.Background(), logger, cfg, nextConsumer)
}

// If receiver is of the old type, but downstream consumer is of the new type,
// use NewInternalToOCMetricsConverter compatibility shim to convert metrics from internal format to OC.
metricsConverter := converter.NewOCToInternalMetricsConverter(nextConsumer.(consumer.MetricsConsumer))
return factoryOld.CreateMetricsReceiver(logger, cfg, metricsConverter)
return factoryOld.CreateMetricsReceiver(context.Background(), logger, cfg, metricsConverter)
}

// createLogReceiver creates a log receiver using given factory and next consumer.
Expand Down
6 changes: 1 addition & 5 deletions service/builder/receivers_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,7 @@ func (b *badReceiverFactory) CreateTraceReceiver(
return nil, nil
}

func (b *badReceiverFactory) CreateMetricsReceiver(
_ *zap.Logger,
_ configmodels.Receiver,
_ consumer.MetricsConsumerOld,
) (component.MetricsReceiver, error) {
func (b *badReceiverFactory) CreateMetricsReceiver(ctx context.Context, logger *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {
return nil, nil
}

Expand Down

0 comments on commit 8b754f7

Please sign in to comment.