Skip to content

Commit 093f0b2

Browse files
committed
exporter: add metrics recording helper for Exporters
Updates #64
1 parent 986666f commit 093f0b2

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

internal/observability.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,38 @@ import (
3232
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
3333
)
3434

35-
var tagKeyInterceptorName, _ = tag.NewKey("opencensus_interceptor")
35+
var (
36+
tagKeyInterceptorName, _ = tag.NewKey("opencensus_interceptor")
37+
tagKeyExporterName, _ = tag.NewKey("opencensus_exporter")
38+
)
39+
3640
var mReceivedSpans = stats.Int64("oc.io/interceptor/received_spans", "Counts the number of spans received by the interceptor", "1")
3741

42+
var itemsDistribution = view.Distribution(
43+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90,
44+
100, 150, 200, 250, 300, 450, 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000,
45+
)
46+
3847
var ViewReceivedSpansInterceptor = &view.View{
3948
Name: "oc.io/interceptor/received_spans",
4049
Description: "The number of spans received by the interceptor",
4150
Measure: mReceivedSpans,
42-
Aggregation: view.Distribution(
43-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90,
44-
100, 150, 200, 250, 300, 450, 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000,
45-
),
46-
TagKeys: []tag.Key{tagKeyInterceptorName},
51+
Aggregation: itemsDistribution,
52+
TagKeys: []tag.Key{tagKeyInterceptorName},
53+
}
54+
55+
var mExportedSpans = stats.Int64("oc.io/interceptor/exported_spans", "Counts the number of exported spans", "1")
56+
var ViewExportedSpans = &view.View{
57+
Name: "oc.io/interceptor/exported_spans",
58+
Description: "Tracks the number of exported spans",
59+
Measure: mExportedSpans,
60+
Aggregation: itemsDistribution,
61+
TagKeys: []tag.Key{tagKeyExporterName},
4762
}
4863

4964
var AllViews = []*view.View{
5065
ViewReceivedSpansInterceptor,
66+
ViewExportedSpans,
5167
}
5268

5369
// ContextWithInterceptorName adds the tag "opencensus_interceptor" and the name of the
@@ -73,6 +89,16 @@ func NewReceivedSpansRecorderStreaming(lifetimeCtx context.Context, interceptorN
7389
}
7490
}
7591

92+
// NewExportedSpansRecorder creates a helper function that'll add the name of the
93+
// creating exporter as a tag value in the context that will be used to count the
94+
// the number of spans exported.
95+
func NewExportedSpansRecorder(exporterName string) func(context.Context, *commonpb.Node, []*tracepb.Span) {
96+
return func(ctx context.Context, ni *commonpb.Node, spans []*tracepb.Span) {
97+
ctx, _ = tag.New(ctx, tag.Upsert(tagKeyExporterName, exporterName))
98+
stats.Record(ctx, mExportedSpans.M(int64(len(spans))))
99+
}
100+
}
101+
76102
// GRPCServerWithObservabilityEnabled creates a gRPC server that at a bare minimum has
77103
// the OpenCensus ocgrpc server stats handler enabled for tracing and stats.
78104
// Use it instead of invoking grpc.NewServer directly.

0 commit comments

Comments
 (0)