Skip to content

Commit

Permalink
[chore] use mdatagen scraperhelper (open-telemetry#10095)
Browse files Browse the repository at this point in the history
Follows
open-telemetry#10094

---------

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
codeboten authored and andrzej-stencel committed May 27, 2024
1 parent 867e727 commit 8a0ab17
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 42 deletions.
2 changes: 2 additions & 0 deletions receiver/scraperhelper/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

// Package scraperhelper provides utilities for scrapers.
package scraperhelper // import "go.opentelemetry.io/collector/receiver/scraperhelper"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions receiver/scraperhelper/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions receiver/scraperhelper/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type: scraperhelper

status:
class: pkg
stability:
beta: [traces, metrics, logs]
distributions: [core, contrib]

telemetry:
metrics:
scraper_scraped_metric_points:
enabled: true
description: Number of metric points successfully scraped.
unit: 1
sum:
value_type: int
monotonic: true

scraper_errored_metric_points:
enabled: true
description: Number of metric points that were unable to be scraped.
unit: 1
sum:
value_type: int
monotonic: true
52 changes: 12 additions & 40 deletions receiver/scraperhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ import (
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/multierr"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scrapererror"
)

var (
scraperScope = obsmetrics.Scope + obsmetrics.SpanNameSep + obsmetrics.ScraperKey
"go.opentelemetry.io/collector/receiver/scraperhelper/internal/metadata"
)

// ObsReport is a helper to add observability to a scraper.
Expand All @@ -34,9 +30,8 @@ type ObsReport struct {

logger *zap.Logger

otelAttrs []attribute.KeyValue
scrapedMetricsPoints metric.Int64Counter
erroredMetricsPoints metric.Int64Counter
otelAttrs []attribute.KeyValue
telemetryBuilder *metadata.TelemetryBuilder
}

// ObsReportSettings are settings for creating an ObsReport.
Expand All @@ -52,7 +47,11 @@ func NewObsReport(cfg ObsReportSettings) (*ObsReport, error) {
}

func newScraper(cfg ObsReportSettings) (*ObsReport, error) {
scraper := &ObsReport{
telemetryBuilder, err := metadata.NewTelemetryBuilder(cfg.ReceiverCreateSettings.TelemetrySettings)
if err != nil {
return nil, err
}
return &ObsReport{
level: cfg.ReceiverCreateSettings.TelemetrySettings.MetricsLevel,
receiverID: cfg.ReceiverID,
scraper: cfg.Scraper,
Expand All @@ -63,35 +62,8 @@ func newScraper(cfg ObsReportSettings) (*ObsReport, error) {
attribute.String(obsmetrics.ReceiverKey, cfg.ReceiverID.String()),
attribute.String(obsmetrics.ScraperKey, cfg.Scraper.String()),
},
}

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

return scraper, nil
}

func (s *ObsReport) createOtelMetrics(cfg ObsReportSettings) error {
meter := cfg.ReceiverCreateSettings.MeterProvider.Meter(scraperScope)

var errors, err error

s.scrapedMetricsPoints, err = meter.Int64Counter(
obsmetrics.ScraperPrefix+obsmetrics.ScrapedMetricPointsKey,
metric.WithDescription("Number of metric points successfully scraped."),
metric.WithUnit("1"),
)
errors = multierr.Append(errors, err)

s.erroredMetricsPoints, err = meter.Int64Counter(
obsmetrics.ScraperPrefix+obsmetrics.ErroredMetricPointsKey,
metric.WithDescription("Number of metric points that were unable to be scraped."),
metric.WithUnit("1"),
)
errors = multierr.Append(errors, err)

return errors
telemetryBuilder: telemetryBuilder,
}, nil
}

// StartMetricsOp is called when a scrape operation is started. The
Expand Down Expand Up @@ -144,6 +116,6 @@ func (s *ObsReport) EndMetricsOp(
}

func (s *ObsReport) recordMetrics(scraperCtx context.Context, numScrapedMetrics, numErroredMetrics int) {
s.scrapedMetricsPoints.Add(scraperCtx, int64(numScrapedMetrics), metric.WithAttributes(s.otelAttrs...))
s.erroredMetricsPoints.Add(scraperCtx, int64(numErroredMetrics), metric.WithAttributes(s.otelAttrs...))
s.telemetryBuilder.ScraperScrapedMetricPoints.Add(scraperCtx, int64(numScrapedMetrics), metric.WithAttributes(s.otelAttrs...))
s.telemetryBuilder.ScraperErroredMetricPoints.Add(scraperCtx, int64(numErroredMetrics), metric.WithAttributes(s.otelAttrs...))
}

0 comments on commit 8a0ab17

Please sign in to comment.