Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obsreport: wire up ReceiverCreateSettings for scrapers using obsreport #4150

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions obsreport/obsreport_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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()),
}
}

Expand Down
9 changes: 5 additions & 4 deletions obsreport/obsreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions receiver/scraperhelper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 6 additions & 4 deletions receiver/scraperhelper/scrapercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()))

Expand Down
15 changes: 5 additions & 10 deletions receiver/scraperhelper/scrapercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import (
"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"
Expand Down Expand Up @@ -186,13 +184,10 @@ 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)
otel.SetTracerProvider(set.TracerProvider)
defer otel.SetTracerProvider(trace.NewNoopTracerProvider())
defer set.Shutdown(context.Background())

initializeChs := make([]chan bool, test.scrapers+test.resourceScrapers)
Expand All @@ -215,7 +210,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
Expand Down Expand Up @@ -256,7 +251,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)
Expand Down Expand Up @@ -429,7 +424,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)),
Expand Down