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

Change call pattern for scraper #3327

Merged
merged 3 commits into from
Jun 17, 2021
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Rename `service.Application` to `service.Collector` (#3268)
- Provide case sensitivity in config yaml mappings by using Koanf instead of Viper (#3337)

## 💡 Enhancements 💡

- Change obsreport helpers for scraper to use the same pattern as Processor/Exporter (#3327)

## v0.28.0 Beta

## 🛑 Breaking changes 🛑
Expand Down
34 changes: 26 additions & 8 deletions obsreport/obsreport_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,40 @@ func ScraperContext(
return ctx
}

// StartMetricsScrapeOp is called when a scrape operation is started. The
// Scraper is a helper to add observability to a component.Scraper.
type Scraper struct {
receiverID config.ComponentID
scraper config.ComponentID
}

// ScraperSettings are settings for creating a Scraper.
type ScraperSettings struct {
ReceiverID config.ComponentID
Scraper config.ComponentID
}

// NewScraper creates a new Scraper.
func NewScraper(cfg ScraperSettings) *Scraper {
return &Scraper{
receiverID: cfg.ReceiverID,
scraper: cfg.Scraper,
}
}

// StartMetricsOp is called when a scrape operation is started. The
// returned context should be used in other calls to the obsreport functions
// dealing with the same scrape operation.
func StartMetricsScrapeOp(
func (s *Scraper) StartMetricsOp(
scraperCtx context.Context,
receiverID config.ComponentID,
scraper config.ComponentID,
) context.Context {
spanName := obsmetrics.ScraperPrefix + receiverID.String() + obsmetrics.NameSep + scraper.String() + obsmetrics.ScraperMetricsOperationSuffix
spanName := obsmetrics.ScraperPrefix + s.receiverID.String() + obsmetrics.NameSep + s.scraper.String() + obsmetrics.ScraperMetricsOperationSuffix
ctx, _ := trace.StartSpan(scraperCtx, spanName)
return ctx
}

// EndMetricsScrapeOp completes the scrape operation that was started with
// StartMetricsScrapeOp.
func EndMetricsScrapeOp(
// EndMetricsOp completes the scrape operation that was started with
// StartMetricsOp.
func (s *Scraper) EndMetricsOp(
scraperCtx context.Context,
numScrapedMetrics int,
err error,
Expand Down
5 changes: 3 additions & 2 deletions obsreport/obsreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,11 @@ func TestScrapeMetricsDataOp(t *testing.T) {
errParams := []error{partialErrFake, errFake, nil}
scrapedMetricPts := []int{23, 29, 15}
for i, err := range errParams {
ctx := StartMetricsScrapeOp(receiverCtx, receiver, scraper)
scrp := NewScraper(ScraperSettings{ReceiverID: receiver, Scraper: scraper})
ctx := scrp.StartMetricsOp(receiverCtx)
assert.NotNil(t, ctx)

EndMetricsScrapeOp(
scrp.EndMetricsOp(
ctx,
scrapedMetricPts[i],
err)
Expand Down
10 changes: 6 additions & 4 deletions receiver/scraperhelper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ func NewMetricsScraper(

func (ms metricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.MetricSlice, error) {
ctx = obsreport.ScraperContext(ctx, receiverID, ms.ID())
ctx = obsreport.StartMetricsScrapeOp(ctx, receiverID, ms.ID())
scrp := obsreport.NewScraper(obsreport.ScraperSettings{ReceiverID: receiverID, Scraper: ms.ID()})
ctx = scrp.StartMetricsOp(ctx)
metrics, err := ms.ScrapeMetrics(ctx)
count := 0
if err == nil {
count = metrics.Len()
}
obsreport.EndMetricsScrapeOp(ctx, count, err)
scrp.EndMetricsOp(ctx, count, err)
return metrics, err
}

Expand Down Expand Up @@ -158,13 +159,14 @@ func NewResourceMetricsScraper(

func (rms resourceMetricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.ResourceMetricsSlice, error) {
ctx = obsreport.ScraperContext(ctx, receiverID, rms.ID())
ctx = obsreport.StartMetricsScrapeOp(ctx, receiverID, rms.ID())
scrp := obsreport.NewScraper(obsreport.ScraperSettings{ReceiverID: receiverID, Scraper: rms.ID()})
ctx = scrp.StartMetricsOp(ctx)
resourceMetrics, err := rms.ScrapeResourceMetrics(ctx)
count := 0
if err == nil {
count = metricCount(resourceMetrics)
}
obsreport.EndMetricsScrapeOp(ctx, count, err)
scrp.EndMetricsOp(ctx, count, err)
return resourceMetrics, err
}

Expand Down