Skip to content

Commit

Permalink
scraperhelper: fix case when returned pdata is empty (#3520)
Browse files Browse the repository at this point in the history
On error pdata.Metrics may not be initialized so guard against it. Update test
to return uninitialized object on error.
  • Loading branch information
jrcamp authored Jun 29, 2021
1 parent cfe32ae commit cbee3b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
34 changes: 10 additions & 24 deletions receiver/scraperhelper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/model/pdata"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/receiver/scrapererror"
)

// ScrapeMetrics scrapes metrics.
Expand Down Expand Up @@ -106,12 +107,11 @@ func (ms metricsScraper) Scrape(ctx context.Context, receiverID config.Component
ctx = scrp.StartMetricsOp(ctx)
metrics, err := ms.ScrapeMetrics(ctx)
count := 0
if err == nil {
count = metrics.Len()
}
md := pdata.NewMetrics()
if metrics.Len() > 0 {
md := pdata.Metrics{}
if err == nil || scrapererror.IsPartialScrapeError(err) {
md = pdata.NewMetrics()
metrics.MoveAndAppendTo(md.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())
count = md.MetricCount()
}
scrp.EndMetricsOp(ctx, count, err)
return md, err
Expand Down Expand Up @@ -153,29 +153,15 @@ func (rms resourceMetricsScraper) Scrape(ctx context.Context, receiverID config.
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)
}

md := pdata.NewMetrics()
if resourceMetrics.Len() > 0 {
count := 0
md := pdata.Metrics{}
if err == nil || scrapererror.IsPartialScrapeError(err) {
md = pdata.NewMetrics()
resourceMetrics.MoveAndAppendTo(md.ResourceMetrics())
count = md.MetricCount()
}

scrp.EndMetricsOp(ctx, count, err)
return md, err
}

func metricCount(resourceMetrics pdata.ResourceMetricsSlice) int {
count := 0

for i := 0; i < resourceMetrics.Len(); i++ {
ilm := resourceMetrics.At(i).InstrumentationLibraryMetrics()
for j := 0; j < ilm.Len(); j++ {
count += ilm.At(j).Metrics().Len()
}
}

return count
}
4 changes: 2 additions & 2 deletions receiver/scraperhelper/scrapercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (ts *testScrapeMetrics) scrape(_ context.Context) (pdata.MetricSlice, error
ts.ch <- ts.timesScrapeCalled

if ts.err != nil {
return pdata.NewMetricSlice(), ts.err
return pdata.MetricSlice{}, ts.err
}

return singleMetric(), nil
Expand All @@ -85,7 +85,7 @@ func (ts *testScrapeResourceMetrics) scrape(_ context.Context) (pdata.ResourceMe
ts.ch <- ts.timesScrapeCalled

if ts.err != nil {
return pdata.NewResourceMetricsSlice(), ts.err
return pdata.ResourceMetricsSlice{}, ts.err
}

return singleResourceMetric(), nil
Expand Down

0 comments on commit cbee3b3

Please sign in to comment.