From e3041e3b489f7ac7cbbb32b5df1b21bc6290eb73 Mon Sep 17 00:00:00 2001 From: Anthony J Mirabella Date: Wed, 29 Sep 2021 19:11:28 -0400 Subject: [PATCH 1/2] obsreport: wire up ReceiverCreateSettings for scrapers using obsreport Signed-off-by: Anthony J Mirabella --- obsreport/obsreport_scraper.go | 9 +++++---- obsreport/obsreport_test.go | 9 +++++---- receiver/scraperhelper/scraper.go | 18 +++++++++++++----- receiver/scraperhelper/scrapercontroller.go | 10 ++++++---- .../scraperhelper/scrapercontroller_test.go | 15 +++------------ 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/obsreport/obsreport_scraper.go b/obsreport/obsreport_scraper.go index 8ead166a3dd..01775862dcf 100644 --- a/obsreport/obsreport_scraper.go +++ b/obsreport/obsreport_scraper.go @@ -19,10 +19,10 @@ import ( "go.opencensus.io/stats" "go.opencensus.io/tag" - "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/configtelemetry" "go.opentelemetry.io/collector/internal/obsreportconfig" @@ -40,8 +40,9 @@ type Scraper struct { // ScraperSettings are settings for creating a Scraper. type ScraperSettings struct { - ReceiverID config.ComponentID - Scraper config.ComponentID + ReceiverID config.ComponentID + Scraper config.ComponentID + ReceiverCreateSettings component.ReceiverCreateSettings } // NewScraper creates a new Scraper. @@ -52,7 +53,7 @@ func NewScraper(cfg ScraperSettings) *Scraper { mutators: []tag.Mutator{ tag.Upsert(obsmetrics.TagKeyReceiver, cfg.ReceiverID.String(), tag.WithTTL(tag.TTLNoPropagation)), tag.Upsert(obsmetrics.TagKeyScraper, cfg.Scraper.String(), tag.WithTTL(tag.TTLNoPropagation))}, - tracer: otel.GetTracerProvider().Tracer(cfg.Scraper.String()), + tracer: cfg.ReceiverCreateSettings.TracerProvider.Tracer(cfg.Scraper.String()), } } diff --git a/obsreport/obsreport_test.go b/obsreport/obsreport_test.go index 3bffcde04e0..39ea41d798e 100644 --- a/obsreport/obsreport_test.go +++ b/obsreport/obsreport_test.go @@ -200,9 +200,6 @@ func TestScrapeMetricsDataOp(t *testing.T) { require.NoError(t, err) defer set.Shutdown(context.Background()) - otel.SetTracerProvider(set.TracerProvider) - defer otel.SetTracerProvider(trace.NewNoopTracerProvider()) - parentCtx, parentSpan := set.TracerProvider.Tracer("test").Start(context.Background(), t.Name()) defer parentSpan.End() @@ -212,7 +209,11 @@ func TestScrapeMetricsDataOp(t *testing.T) { {items: 15, err: nil}, } for i := range params { - scrp := NewScraper(ScraperSettings{ReceiverID: receiver, Scraper: scraper}) + scrp := NewScraper(ScraperSettings{ + ReceiverID: receiver, + Scraper: scraper, + ReceiverCreateSettings: set.ToReceiverCreateSettings(), + }) ctx := scrp.StartMetricsOp(parentCtx) assert.NotNil(t, ctx) scrp.EndMetricsOp(ctx, params[i].items, params[i].err) diff --git a/receiver/scraperhelper/scraper.go b/receiver/scraperhelper/scraper.go index ef52a93f4ed..c0a305954f5 100644 --- a/receiver/scraperhelper/scraper.go +++ b/receiver/scraperhelper/scraper.go @@ -44,7 +44,7 @@ type Scraper interface { // ID returns the scraper id. ID() config.ComponentID - Scrape(context.Context, config.ComponentID) (pdata.Metrics, error) + Scrape(context.Context, config.ComponentID, component.ReceiverCreateSettings) (pdata.Metrics, error) } type baseScraper struct { @@ -101,8 +101,12 @@ func NewMetricsScraper( return ms } -func (ms metricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.Metrics, error) { - scrp := obsreport.NewScraper(obsreport.ScraperSettings{ReceiverID: receiverID, Scraper: ms.ID()}) +func (ms metricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID, set component.ReceiverCreateSettings) (pdata.Metrics, error) { + scrp := obsreport.NewScraper(obsreport.ScraperSettings{ + ReceiverID: receiverID, + Scraper: ms.ID(), + ReceiverCreateSettings: set, + }) ctx = scrp.StartMetricsOp(ctx) metrics, err := ms.ScrapeMetrics(ctx) count := 0 @@ -147,8 +151,12 @@ func NewResourceMetricsScraper( return rms } -func (rms resourceMetricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.Metrics, error) { - scrp := obsreport.NewScraper(obsreport.ScraperSettings{ReceiverID: receiverID, Scraper: rms.ID()}) +func (rms resourceMetricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID, set component.ReceiverCreateSettings) (pdata.Metrics, error) { + scrp := obsreport.NewScraper(obsreport.ScraperSettings{ + ReceiverID: receiverID, + Scraper: rms.ID(), + ReceiverCreateSettings: set, + }) ctx = scrp.StartMetricsOp(ctx) resourceMetrics, err := rms.ScrapeResourceMetrics(ctx) diff --git a/receiver/scraperhelper/scrapercontroller.go b/receiver/scraperhelper/scrapercontroller.go index 857c2918f57..89ae09d54d5 100644 --- a/receiver/scraperhelper/scrapercontroller.go +++ b/receiver/scraperhelper/scrapercontroller.go @@ -85,13 +85,14 @@ type controller struct { done chan struct{} terminated chan struct{} - obsrecv *obsreport.Receiver + obsrecv *obsreport.Receiver + recvSettings component.ReceiverCreateSettings } // NewScraperControllerReceiver creates a Receiver with the configured options, that can control multiple scrapers. func NewScraperControllerReceiver( cfg *ScraperControllerSettings, - logger *zap.Logger, + set component.ReceiverCreateSettings, nextConsumer consumer.Metrics, options ...ScraperControllerOption, ) (component.Receiver, error) { @@ -105,12 +106,13 @@ func NewScraperControllerReceiver( sc := &controller{ id: cfg.ID(), - logger: logger, + logger: set.Logger, collectionInterval: cfg.CollectionInterval, nextConsumer: nextConsumer, done: make(chan struct{}), terminated: make(chan struct{}), obsrecv: obsreport.NewReceiver(obsreport.ReceiverSettings{ReceiverID: cfg.ID(), Transport: ""}), + recvSettings: set, } for _, op := range options { @@ -180,7 +182,7 @@ func (sc *controller) scrapeMetricsAndReport(ctx context.Context) { metrics := pdata.NewMetrics() for _, scraper := range sc.scrapers { - md, err := scraper.Scrape(ctx, sc.id) + md, err := scraper.Scrape(ctx, sc.id, sc.recvSettings) if err != nil { sc.logger.Error("Error scraping metrics", zap.Error(err), zap.Stringer("scraper", scraper.ID())) diff --git a/receiver/scraperhelper/scrapercontroller_test.go b/receiver/scraperhelper/scrapercontroller_test.go index 606397cd2ce..2e2948adce9 100644 --- a/receiver/scraperhelper/scrapercontroller_test.go +++ b/receiver/scraperhelper/scrapercontroller_test.go @@ -22,13 +22,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/codes" sdktrace "go.opentelemetry.io/otel/sdk/trace" - "go.opentelemetry.io/otel/sdk/trace/tracetest" - "go.opentelemetry.io/otel/trace" "go.uber.org/multierr" - "go.uber.org/zap" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" @@ -186,11 +182,6 @@ func TestScrapeController(t *testing.T) { for _, test := range testCases { t.Run(test.name, func(t *testing.T) { - sr := new(tracetest.SpanRecorder) - tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) - otel.SetTracerProvider(tp) - defer otel.SetTracerProvider(trace.NewNoopTracerProvider()) - set, err := obsreporttest.SetupRecordedMetricsTest() require.NoError(t, err) defer set.Shutdown(context.Background()) @@ -215,7 +206,7 @@ func TestScrapeController(t *testing.T) { cfg = test.scraperControllerSettings } - mr, err := NewScraperControllerReceiver(cfg, zap.NewNop(), nextConsumer, options...) + mr, err := NewScraperControllerReceiver(cfg, set.ToReceiverCreateSettings(), nextConsumer, options...) if test.expectedNewErr != "" { assert.EqualError(t, err, test.expectedNewErr) return @@ -256,7 +247,7 @@ func TestScrapeController(t *testing.T) { assert.GreaterOrEqual(t, sink.DataPointCount(), iterations) } - spans := sr.Ended() + spans := set.SpanRecorder.Ended() assertReceiverSpan(t, spans) assertReceiverViews(t, sink) assertScraperSpan(t, test.scrapeErr, spans) @@ -429,7 +420,7 @@ func TestSingleScrapePerTick(t *testing.T) { receiver, err := NewScraperControllerReceiver( cfg, - zap.NewNop(), + componenttest.NewNopReceiverCreateSettings(), new(consumertest.MetricsSink), AddScraper(NewMetricsScraper("", tsm.scrape)), AddScraper(NewResourceMetricsScraper(config.NewComponentID("scraper"), tsrm.scrape)), From 2274289bef8673289bd255dab9ea06e1226a0969 Mon Sep 17 00:00:00 2001 From: Anthony J Mirabella Date: Wed, 29 Sep 2021 19:33:30 -0400 Subject: [PATCH 2/2] Ensure TracerProvider is available to scraper receiver for now Signed-off-by: Anthony J Mirabella --- receiver/scraperhelper/scrapercontroller_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/receiver/scraperhelper/scrapercontroller_test.go b/receiver/scraperhelper/scrapercontroller_test.go index 2e2948adce9..4d0b4467827 100644 --- a/receiver/scraperhelper/scrapercontroller_test.go +++ b/receiver/scraperhelper/scrapercontroller_test.go @@ -22,8 +22,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/codes" sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/trace" "go.uber.org/multierr" "go.opentelemetry.io/collector/component" @@ -184,6 +186,8 @@ func TestScrapeController(t *testing.T) { t.Run(test.name, func(t *testing.T) { set, err := obsreporttest.SetupRecordedMetricsTest() require.NoError(t, err) + otel.SetTracerProvider(set.TracerProvider) + defer otel.SetTracerProvider(trace.NewNoopTracerProvider()) defer set.Shutdown(context.Background()) initializeChs := make([]chan bool, test.scrapers+test.resourceScrapers)