Skip to content

Commit

Permalink
[chore] Improve otel metrics performance, pre-calculate attribute Set (
Browse files Browse the repository at this point in the history
…#11288)

Depends on
#11293

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Oct 4, 2024
1 parent d547b39 commit 6a8c88d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 36 deletions.
18 changes: 8 additions & 10 deletions exporter/exporterhelper/internal/obsexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ObsReport struct {
tracer trace.Tracer
Signal pipeline.Signal

otelAttrs []attribute.KeyValue
otelAttrs attribute.Set
TelemetryBuilder *metadata.TelemetryBuilder
}

Expand All @@ -41,12 +41,10 @@ func NewExporter(cfg ObsReportSettings) (*ObsReport, error) {
}

return &ObsReport{
spanNamePrefix: ExporterPrefix + cfg.ExporterID.String(),
tracer: cfg.ExporterCreateSettings.TracerProvider.Tracer(cfg.ExporterID.String()),
Signal: cfg.Signal,
otelAttrs: []attribute.KeyValue{
attribute.String(ExporterKey, cfg.ExporterID.String()),
},
spanNamePrefix: ExporterPrefix + cfg.ExporterID.String(),
tracer: cfg.ExporterCreateSettings.TracerProvider.Tracer(cfg.ExporterID.String()),
Signal: cfg.Signal,
otelAttrs: attribute.NewSet(attribute.String(ExporterKey, cfg.ExporterID.String())),
TelemetryBuilder: telemetryBuilder,
}, nil
}
Expand Down Expand Up @@ -118,8 +116,8 @@ func (or *ObsReport) recordMetrics(ctx context.Context, signal pipeline.Signal,
failedMeasure = or.TelemetryBuilder.ExporterSendFailedLogRecords
}

sentMeasure.Add(ctx, sent, metric.WithAttributes(or.otelAttrs...))
failedMeasure.Add(ctx, failed, metric.WithAttributes(or.otelAttrs...))
sentMeasure.Add(ctx, sent, metric.WithAttributeSet(or.otelAttrs))
failedMeasure.Add(ctx, failed, metric.WithAttributeSet(or.otelAttrs))
}

func endSpan(ctx context.Context, err error, numSent, numFailedToSend int64, sentItemsKey, failedToSendItemsKey string) {
Expand Down Expand Up @@ -155,5 +153,5 @@ func (or *ObsReport) RecordEnqueueFailure(ctx context.Context, signal pipeline.S
enqueueFailedMeasure = or.TelemetryBuilder.ExporterEnqueueFailedLogRecords
}

enqueueFailedMeasure.Add(ctx, failed, metric.WithAttributes(or.otelAttrs...))
enqueueFailedMeasure.Add(ctx, failed, metric.WithAttributeSet(or.otelAttrs))
}
14 changes: 8 additions & 6 deletions processor/processorhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"go.opentelemetry.io/collector/processor/processorhelper/internal/metadata"
)

const signalKey = "otel.signal"

// Deprecated: [v0.111.0] no longer needed. To be removed in future.
func BuildCustomMetricName(configType, metric string) string {
componentPrefix := internal.ProcessorMetricPrefix
Expand Down Expand Up @@ -44,7 +46,7 @@ func NewObsReport(_ ObsReportSettings) (*ObsReport, error) {
}

type obsReport struct {
otelAttrs []attribute.KeyValue
otelAttrs attribute.Set
telemetryBuilder *metadata.TelemetryBuilder
}

Expand All @@ -54,15 +56,15 @@ func newObsReport(set processor.Settings, signal pipeline.Signal) (*obsReport, e
return nil, err
}
return &obsReport{
otelAttrs: []attribute.KeyValue{
otelAttrs: attribute.NewSet(
attribute.String(internal.ProcessorKey, set.ID.String()),
attribute.String("otel.signal", signal.String()),
},
attribute.String(signalKey, signal.String()),
),
telemetryBuilder: telemetryBuilder,
}, nil
}

func (or *obsReport) recordInOut(ctx context.Context, incoming, outgoing int) {
or.telemetryBuilder.ProcessorIncomingItems.Add(ctx, int64(incoming), metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.ProcessorOutgoingItems.Add(ctx, int64(outgoing), metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.ProcessorIncomingItems.Add(ctx, int64(incoming), metric.WithAttributeSet(or.otelAttrs))
or.telemetryBuilder.ProcessorOutgoingItems.Add(ctx, int64(outgoing), metric.WithAttributeSet(or.otelAttrs))
}
10 changes: 5 additions & 5 deletions receiver/receiverhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ObsReport struct {
longLivedCtx bool
tracer trace.Tracer

otelAttrs []attribute.KeyValue
otelAttrs attribute.Set
telemetryBuilder *metadata.TelemetryBuilder
}

Expand Down Expand Up @@ -60,10 +60,10 @@ func newReceiver(cfg ObsReportSettings) (*ObsReport, error) {
longLivedCtx: cfg.LongLivedCtx,
tracer: cfg.ReceiverCreateSettings.TracerProvider.Tracer(cfg.ReceiverID.String()),

otelAttrs: []attribute.KeyValue{
otelAttrs: attribute.NewSet(
attribute.String(internal.ReceiverKey, cfg.ReceiverID.String()),
attribute.String(internal.TransportKey, cfg.Transport),
},
),
telemetryBuilder: telemetryBuilder,
}, nil
}
Expand Down Expand Up @@ -207,6 +207,6 @@ func (rec *ObsReport) recordMetrics(receiverCtx context.Context, signal pipeline
refusedMeasure = rec.telemetryBuilder.ReceiverRefusedLogRecords
}

acceptedMeasure.Add(receiverCtx, int64(numAccepted), metric.WithAttributes(rec.otelAttrs...))
refusedMeasure.Add(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
acceptedMeasure.Add(receiverCtx, int64(numAccepted), metric.WithAttributeSet(rec.otelAttrs))
refusedMeasure.Add(receiverCtx, int64(numRefused), metric.WithAttributeSet(rec.otelAttrs))
}
10 changes: 5 additions & 5 deletions receiver/scraperhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type obsReport struct {
scraper component.ID
tracer trace.Tracer

otelAttrs []attribute.KeyValue
otelAttrs attribute.Set
telemetryBuilder *metadata.TelemetryBuilder
}

Expand All @@ -47,10 +47,10 @@ func newScraper(cfg obsReportSettings) (*obsReport, error) {
scraper: cfg.Scraper,
tracer: cfg.ReceiverCreateSettings.TracerProvider.Tracer(cfg.Scraper.String()),

otelAttrs: []attribute.KeyValue{
otelAttrs: attribute.NewSet(
attribute.String(internal.ReceiverKey, cfg.ReceiverID.String()),
attribute.String(internal.ScraperKey, cfg.Scraper.String()),
},
),
telemetryBuilder: telemetryBuilder,
}, nil
}
Expand Down Expand Up @@ -103,6 +103,6 @@ func (s *obsReport) EndMetricsOp(
}

func (s *obsReport) recordMetrics(scraperCtx context.Context, numScrapedMetrics, numErroredMetrics int) {
s.telemetryBuilder.ScraperScrapedMetricPoints.Add(scraperCtx, int64(numScrapedMetrics), metric.WithAttributes(s.otelAttrs...))
s.telemetryBuilder.ScraperErroredMetricPoints.Add(scraperCtx, int64(numErroredMetrics), metric.WithAttributes(s.otelAttrs...))
s.telemetryBuilder.ScraperScrapedMetricPoints.Add(scraperCtx, int64(numScrapedMetrics), metric.WithAttributeSet(s.otelAttrs))
s.telemetryBuilder.ScraperErroredMetricPoints.Add(scraperCtx, int64(numErroredMetrics), metric.WithAttributeSet(s.otelAttrs))
}
15 changes: 7 additions & 8 deletions service/telemetry/internal/otelinit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ const (

var (
// GRPCUnacceptableKeyValues is a list of high cardinality grpc attributes that should be filtered out.
GRPCUnacceptableKeyValues = []attribute.KeyValue{
GRPCUnacceptableKeyValues = attribute.NewSet(
attribute.String(semconv.AttributeNetSockPeerAddr, ""),
attribute.String(semconv.AttributeNetSockPeerPort, ""),
attribute.String(semconv.AttributeNetSockPeerName, ""),
}
)

// HTTPUnacceptableKeyValues is a list of high cardinality http attributes that should be filtered out.
HTTPUnacceptableKeyValues = []attribute.KeyValue{
HTTPUnacceptableKeyValues = attribute.NewSet(
attribute.String(semconv.AttributeNetHostName, ""),
attribute.String(semconv.AttributeNetHostPort, ""),
}
)

errNoValidMetricExporter = errors.New("no valid metric exporter")
)
Expand Down Expand Up @@ -123,18 +123,17 @@ func disableHighCardinalityViews(disableHighCardinality bool) []sdkmetric.View {
sdkmetric.NewView(
sdkmetric.Instrument{Scope: instrumentation.Scope{Name: GRPCInstrumentation}},
sdkmetric.Stream{
AttributeFilter: cardinalityFilter(GRPCUnacceptableKeyValues...),
AttributeFilter: cardinalityFilter(GRPCUnacceptableKeyValues),
}),
sdkmetric.NewView(
sdkmetric.Instrument{Scope: instrumentation.Scope{Name: HTTPInstrumentation}},
sdkmetric.Stream{
AttributeFilter: cardinalityFilter(HTTPUnacceptableKeyValues...),
AttributeFilter: cardinalityFilter(HTTPUnacceptableKeyValues),
}),
}
}

func cardinalityFilter(kvs ...attribute.KeyValue) attribute.Filter {
filter := attribute.NewSet(kvs...)
func cardinalityFilter(filter attribute.Set) attribute.Filter {
return func(kv attribute.KeyValue) bool {
return !filter.HasValue(kv.Key)
}
Expand Down
4 changes: 2 additions & 2 deletions service/telemetry/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ func createTestMetrics(t *testing.T, mp metric.MeterProvider) {

grpcExampleCounter, err := mp.Meter(otelinit.GRPCInstrumentation).Int64Counter(metricPrefix + grpcPrefix + counterName)
require.NoError(t, err)
grpcExampleCounter.Add(context.Background(), 11, metric.WithAttributes(otelinit.GRPCUnacceptableKeyValues...))
grpcExampleCounter.Add(context.Background(), 11, metric.WithAttributeSet(otelinit.GRPCUnacceptableKeyValues))

httpExampleCounter, err := mp.Meter(otelinit.HTTPInstrumentation).Int64Counter(metricPrefix + httpPrefix + counterName)
require.NoError(t, err)
httpExampleCounter.Add(context.Background(), 10, metric.WithAttributes(otelinit.HTTPUnacceptableKeyValues...))
httpExampleCounter.Add(context.Background(), 10, metric.WithAttributeSet(otelinit.HTTPUnacceptableKeyValues))
}

func getMetricsFromPrometheus(t *testing.T, handler http.Handler) map[string]*io_prometheus_client.MetricFamily {
Expand Down

0 comments on commit 6a8c88d

Please sign in to comment.