| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package scraperhelper // import "go.opentelemetry.io/collector/scraper/scraperhelper" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
|
| 10 |
"go.opentelemetry.io/otel/attribute" |
| 11 |
"go.opentelemetry.io/otel/codes" |
| 12 |
"go.opentelemetry.io/otel/metric" |
| 13 |
"go.uber.org/zap" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/component" |
| 16 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 17 |
"go.opentelemetry.io/collector/pipeline" |
| 18 |
"go.opentelemetry.io/collector/scraper" |
| 19 |
"go.opentelemetry.io/collector/scraper/scrapererror" |
| 20 |
"go.opentelemetry.io/collector/scraper/scraperhelper/internal/metadata" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
// scraperKey used to identify scrapers in metrics and traces. |
| 25 |
scraperKey = "scraper" |
| 26 |
// scrapedMetricPointsKey used to identify metric points scraped by the |
| 27 |
// Collector. |
| 28 |
scrapedMetricPointsKey = "scraped_metric_points" |
| 29 |
// erroredMetricPointsKey used to identify metric points errored (i.e. |
| 30 |
// unable to be scraped) by the Collector. |
| 31 |
erroredMetricPointsKey = "errored_metric_points" |
| 32 |
|
| 33 |
spanNameSep = "/" |
| 34 |
// receiverKey used to identify receivers in metrics and traces. |
| 35 |
receiverKey = "receiver" |
| 36 |
// FormatKey used to identify the format of the data received. |
| 37 |
formatKey = "format" |
| 38 |
) |
| 39 |
|
| 40 |
func wrapObsMetrics(sc scraper.Metrics, receiverID, scraperID component.ID, set component.TelemetrySettings) (scraper.Metrics, error) { |
| 41 |
telemetryBuilder, errBuilder := metadata.NewTelemetryBuilder(set) |
| 42 |
if errBuilder != nil { |
| 43 |
return nil, errBuilder |
| 44 |
} |
| 45 |
|
| 46 |
tracer := metadata.Tracer(set) |
| 47 |
spanName := scraperKey + spanNameSep + scraperID.String() + spanNameSep + "ScrapeMetrics" |
| 48 |
otelAttrs := metric.WithAttributeSet(attribute.NewSet( |
| 49 |
attribute.String(receiverKey, receiverID.String()), |
| 50 |
attribute.String(scraperKey, scraperID.String()), |
| 51 |
)) |
| 52 |
|
| 53 |
scraperFuncs := func(ctx context.Context) (pmetric.Metrics, error) { |
| 54 |
ctx, span := tracer.Start(ctx, spanName) |
| 55 |
defer span.End() |
| 56 |
|
| 57 |
md, err := sc.ScrapeMetrics(ctx) |
| 58 |
numScrapedMetrics := 0 |
| 59 |
numErroredMetrics := 0 |
| 60 |
if err != nil { |
| 61 |
set.Logger.Error("Error scraping metrics", zap.Error(err)) |
| 62 |
var partialErr scrapererror.PartialScrapeError |
| 63 |
if errors.As(err, &partialErr) { |
| 64 |
numErroredMetrics = partialErr.Failed |
| 65 |
numScrapedMetrics = md.MetricCount() |
| 66 |
} |
| 67 |
} else { |
| 68 |
numScrapedMetrics = md.MetricCount() |
| 69 |
} |
| 70 |
|
| 71 |
telemetryBuilder.ScraperScrapedMetricPoints.Add(ctx, int64(numScrapedMetrics), otelAttrs) |
| 72 |
telemetryBuilder.ScraperErroredMetricPoints.Add(ctx, int64(numErroredMetrics), otelAttrs) |
| 73 |
|
| 74 |
// end span according to errors |
| 75 |
if span.IsRecording() { |
| 76 |
span.SetAttributes( |
| 77 |
attribute.String(formatKey, pipeline.SignalMetrics.String()), |
| 78 |
attribute.Int64(scrapedMetricPointsKey, int64(numScrapedMetrics)), |
| 79 |
attribute.Int64(erroredMetricPointsKey, int64(numErroredMetrics)), |
| 80 |
) |
| 81 |
|
| 82 |
if err != nil { |
| 83 |
span.SetStatus(codes.Error, err.Error()) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return md, err |
| 88 |
} |
| 89 |
|
| 90 |
return scraper.NewMetrics(scraperFuncs, scraper.WithStart(sc.Start), scraper.WithShutdown(sc.Shutdown)) |
| 91 |
} |