Skip to content

Commit

Permalink
update API to use MustNewScraper
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-osman3 committed Nov 3, 2022
1 parent d134a4d commit 63bb239
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
45 changes: 30 additions & 15 deletions obsreport/obsreport_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/trace"
"go.uber.org/multierr"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -64,12 +65,26 @@ type ScraperSettings struct {
ReceiverCreateSettings component.ReceiverCreateSettings
}

// NewScraper creates a new Scraper.
// Deprecated: [v0.64.0] use MustNewScraper.
func NewScraper(cfg ScraperSettings) *Scraper {
return newScraper(cfg, featuregate.GetRegistry())
scr, err := newScraper(cfg, featuregate.GetRegistry())
if err != nil && cfg.ReceiverCreateSettings.Logger != nil {
cfg.ReceiverCreateSettings.Logger.Warn("Error creating an obsreport.Scraper", zap.Error(err))
}
return scr
}

// MustNewReceiver creates a new Scraper.
func MustNewScraper(cfg ScraperSettings) *Scraper {
scr, err := newScraper(cfg, featuregate.GetRegistry())
if err != nil {
panic(err)
}

return scr
}

func newScraper(cfg ScraperSettings, registry *featuregate.Registry) *Scraper {
func newScraper(cfg ScraperSettings, registry *featuregate.Registry) (*Scraper, error) {
scraper := &Scraper{
level: cfg.ReceiverCreateSettings.TelemetrySettings.MetricsLevel,
receiverID: cfg.ReceiverID,
Expand All @@ -87,36 +102,36 @@ func newScraper(cfg ScraperSettings, registry *featuregate.Registry) *Scraper {
},
}

scraper.createOtelMetrics(cfg)
return scraper
if err := scraper.createOtelMetrics(cfg); err != nil {
return nil, err
}

return scraper, nil
}

func (s *Scraper) createOtelMetrics(cfg ScraperSettings) {
func (s *Scraper) createOtelMetrics(cfg ScraperSettings) error {
if !s.useOtelForMetrics {
return
return nil
}
meter := cfg.ReceiverCreateSettings.MeterProvider.Meter(scraperScope)

var err error
handleError := func(metricName string, err error) {
if err != nil {
s.logger.Warn("failed to create otel instrument", zap.Error(err), zap.String("metric", metricName))
}
}
var errors, err error

s.scrapedMetricsPoints, err = meter.SyncInt64().Counter(
obsmetrics.ScraperPrefix+obsmetrics.ScrapedMetricPointsKey,
instrument.WithDescription("Number of metric points successfully scraped."),
instrument.WithUnit(unit.Dimensionless),
)
handleError(obsmetrics.ScraperPrefix+obsmetrics.ScrapedMetricPointsKey, err)
errors = multierr.Append(errors, err)

s.erroredMetricsPoints, err = meter.SyncInt64().Counter(
obsmetrics.ScraperPrefix+obsmetrics.ErroredMetricPointsKey,
instrument.WithDescription("Number of metric points that were unable to be scraped."),
instrument.WithUnit(unit.Dimensionless),
)
handleError(obsmetrics.ScraperPrefix+obsmetrics.ErroredMetricPointsKey, err)
errors = multierr.Append(errors, err)

return errors
}

// StartMetricsOp is called when a scrape operation is started. The
Expand Down
3 changes: 2 additions & 1 deletion obsreport/obsreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ func TestScrapeMetricsDataOp(t *testing.T) {
{items: 15, err: nil},
}
for i := range params {
scrp := newScraper(ScraperSettings{
scrp, err := newScraper(ScraperSettings{
ReceiverID: receiver,
Scraper: scraper,
ReceiverCreateSettings: tt.ToReceiverCreateSettings(),
}, registry)
require.NoError(t, err)
ctx := scrp.StartMetricsOp(parentCtx)
assert.NotNil(t, ctx)
scrp.EndMetricsOp(ctx, params[i].items, params[i].err)
Expand Down
2 changes: 1 addition & 1 deletion obsreport/obsreporttest/obsreporttest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCheckScraperMetricsViews(t *testing.T) {
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

s := obsreport.NewScraper(obsreport.ScraperSettings{
s := obsreport.MustNewScraper(obsreport.ScraperSettings{
ReceiverID: receiver,
Scraper: scraper,
ReceiverCreateSettings: tt.ToReceiverCreateSettings(),
Expand Down

0 comments on commit 63bb239

Please sign in to comment.