Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reason dimension to exporter and receiver failure metrics #10158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions exporter/exporterhelper/obsexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/grpc/status"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (or *ObsReport) StartTracesOp(ctx context.Context) context.Context {
// EndTracesOp completes the export operation that was started with StartTracesOp.
func (or *ObsReport) EndTracesOp(ctx context.Context, numSpans int, err error) {
numSent, numFailedToSend := toNumItems(numSpans, err)
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeTraces, numSent, numFailedToSend)
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeTraces, numSent, numFailedToSend, err)
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentSpansKey, obsmetrics.FailedToSendSpansKey)
}

Expand All @@ -85,7 +86,7 @@ func (or *ObsReport) StartMetricsOp(ctx context.Context) context.Context {
// StartMetricsOp.
func (or *ObsReport) EndMetricsOp(ctx context.Context, numMetricPoints int, err error) {
numSent, numFailedToSend := toNumItems(numMetricPoints, err)
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeMetrics, numSent, numFailedToSend)
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeMetrics, numSent, numFailedToSend, err)
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentMetricPointsKey, obsmetrics.FailedToSendMetricPointsKey)
}

Expand All @@ -99,7 +100,7 @@ func (or *ObsReport) StartLogsOp(ctx context.Context) context.Context {
// EndLogsOp completes the export operation that was started with StartLogsOp.
func (or *ObsReport) EndLogsOp(ctx context.Context, numLogRecords int, err error) {
numSent, numFailedToSend := toNumItems(numLogRecords, err)
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeLogs, numSent, numFailedToSend)
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeLogs, numSent, numFailedToSend, err)
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentLogRecordsKey, obsmetrics.FailedToSendLogRecordsKey)
}

Expand All @@ -111,7 +112,7 @@ func (or *ObsReport) startOp(ctx context.Context, operationSuffix string) contex
return ctx
}

func (or *ObsReport) recordMetrics(ctx context.Context, dataType component.DataType, sent, failed int64) {
func (or *ObsReport) recordMetrics(ctx context.Context, dataType component.DataType, sent, failed int64, err error) {
if or.level == configtelemetry.LevelNone {
return
}
Expand All @@ -129,7 +130,12 @@ func (or *ObsReport) recordMetrics(ctx context.Context, dataType component.DataT
}

sentMeasure.Add(ctx, sent, metric.WithAttributes(or.otelAttrs...))
failedMeasure.Add(ctx, failed, metric.WithAttributes(or.otelAttrs...))
if err != nil && or.level == configtelemetry.LevelDetailed {
failedMeasure.Add(ctx, failed, metric.WithAttributes(
append(or.otelAttrs, attribute.String("reason", status.Convert(err).Code().String()))...))
} else {
failedMeasure.Add(ctx, failed, metric.WithAttributes(or.otelAttrs...))
}
}

func endSpan(ctx context.Context, err error, numSent, numFailedToSend int64, sentItemsKey, failedToSendItemsKey string) {
Expand Down
12 changes: 9 additions & 3 deletions receiver/receiverhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package receiverhelper // import "go.opentelemetry.io/collector/receiver/receive

import (
"context"
"google.golang.org/grpc/status"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -170,7 +171,7 @@ func (rec *ObsReport) endOp(
span := trace.SpanFromContext(receiverCtx)

if rec.level != configtelemetry.LevelNone {
rec.recordMetrics(receiverCtx, dataType, numAccepted, numRefused)
rec.recordMetrics(receiverCtx, dataType, numAccepted, numRefused, err)
}

// end span according to errors
Expand Down Expand Up @@ -200,7 +201,7 @@ func (rec *ObsReport) endOp(
span.End()
}

func (rec *ObsReport) recordMetrics(receiverCtx context.Context, dataType component.DataType, numAccepted, numRefused int) {
func (rec *ObsReport) recordMetrics(receiverCtx context.Context, dataType component.DataType, numAccepted, numRefused int, err error) {
var acceptedMeasure, refusedMeasure metric.Int64Counter
switch dataType {
case component.DataTypeTraces:
Expand All @@ -215,5 +216,10 @@ func (rec *ObsReport) recordMetrics(receiverCtx context.Context, dataType compon
}

acceptedMeasure.Add(receiverCtx, int64(numAccepted), metric.WithAttributes(rec.otelAttrs...))
refusedMeasure.Add(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
if err != nil && rec.level == configtelemetry.LevelDetailed {
refusedMeasure.Add(receiverCtx, int64(numRefused), metric.WithAttributes(
append(rec.otelAttrs, attribute.String("reason", status.Convert(err).Code().String()))...))
} else {
refusedMeasure.Add(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
}
}