@@ -28,80 +28,88 @@ import (
2828 "go.opencensus.io/stats/view"
2929 "go.opencensus.io/tag"
3030 "go.opencensus.io/trace"
31-
32- commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
33- tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
3431)
3532
3633var (
37- tagKeyReceiverName , _ = tag .NewKey ("opencensus_receiver" )
38- tagKeyExporterName , _ = tag .NewKey ("opencensus_exporter" )
39- )
40-
41- var mReceivedSpans = stats .Int64 ("oc.io/receiver/received_spans" , "Counts the number of spans received by the receiver" , "1" )
34+ tagKeyReceiver , _ = tag .NewKey ("oc_receiver" )
35+ mReceiverReceivedSpans = stats .Int64 ("oc.io/receiver/received_spans" , "Counts the number of spans received by the receiver" , "1" )
36+ mReceiverDroppedSpans = stats .Int64 ("oc.io/receiver/dropped_spans" , "Counts the number of spans dropped by the receiver" , "1" )
4237
43- var itemsDistribution = view . Distribution (
44- 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 ,
45- 100 , 150 , 200 , 250 , 300 , 450 , 500 , 600 , 700 , 800 , 900 , 1000 , 1200 , 1400 , 1600 , 1800 , 2000 ,
38+ tagKeyExporter , _ = tag . NewKey ( "oc_exporter" )
39+ mExporterReceivedSpans = stats . Int64 ( "oc.io/exporter/received_spans" , "Counts the number of spans received by the exporter" , "1" )
40+ mExporterDroppedSpans = stats . Int64 ( "oc.io/exporter/dropped_spans" , "Counts the number of spans received by the exporter" , "1" )
4641)
4742
48- // ViewReceivedSpansReceiver defines the view for the received spans metric.
49- var ViewReceivedSpansReceiver = & view.View {
50- Name : "oc.io/receiver/received_spans" ,
51- Description : "The number of spans received by the receiver" ,
52- Measure : mReceivedSpans ,
53- Aggregation : itemsDistribution ,
54- TagKeys : []tag.Key {tagKeyReceiverName },
43+ // ViewReceiverReceivedSpans defines the view for the receiver received spans metric.
44+ var ViewReceiverReceivedSpans = & view.View {
45+ Name : mReceiverReceivedSpans . Name () ,
46+ Description : mReceiverReceivedSpans . Description () ,
47+ Measure : mReceiverReceivedSpans ,
48+ Aggregation : view . Sum () ,
49+ TagKeys : []tag.Key {tagKeyReceiver },
5550}
5651
57- var mExportedSpans = stats .Int64 ("oc.io/receiver/exported_spans" , "Counts the number of exported spans" , "1" )
52+ // ViewReceiverDroppedSpans defines the view for the receiver dropped spans metric.
53+ var ViewReceiverDroppedSpans = & view.View {
54+ Name : mReceiverDroppedSpans .Name (),
55+ Description : mReceiverDroppedSpans .Description (),
56+ Measure : mReceiverDroppedSpans ,
57+ Aggregation : view .Sum (),
58+ TagKeys : []tag.Key {tagKeyReceiver },
59+ }
60+
61+ // ViewExporterReceivedSpans defines the view for the exporter received spans metric.
62+ var ViewExporterReceivedSpans = & view.View {
63+ Name : mExporterReceivedSpans .Name (),
64+ Description : mExporterReceivedSpans .Description (),
65+ Measure : mExporterReceivedSpans ,
66+ Aggregation : view .Sum (),
67+ TagKeys : []tag.Key {tagKeyReceiver , tagKeyExporter },
68+ }
5869
59- // ViewExportedSpans defines the view for exported spans metric.
60- var ViewExportedSpans = & view.View {
61- Name : "oc.io/receiver/exported_spans" ,
62- Description : "Tracks the number of exported spans" ,
63- Measure : mExportedSpans ,
64- Aggregation : itemsDistribution ,
65- TagKeys : []tag.Key {tagKeyExporterName },
70+ // ViewExporterDroppedSpans defines the view for the exporter dropped spans metric.
71+ var ViewExporterDroppedSpans = & view.View {
72+ Name : mExporterDroppedSpans . Name () ,
73+ Description : mExporterDroppedSpans . Description () ,
74+ Measure : mExporterDroppedSpans ,
75+ Aggregation : view . Sum () ,
76+ TagKeys : []tag.Key {tagKeyReceiver , tagKeyExporter },
6677}
6778
6879// AllViews has the views for the metrics provided by the agent.
6980var AllViews = []* view.View {
70- ViewReceivedSpansReceiver ,
71- ViewExportedSpans ,
81+ ViewReceiverReceivedSpans ,
82+ ViewReceiverDroppedSpans ,
83+ ViewExporterReceivedSpans ,
84+ ViewExporterDroppedSpans ,
7285}
7386
74- // ContextWithReceiverName adds the tag "opencensus_receiver" and the name of the
75- // receiver as the value, and returns the newly created context.
87+ // ContextWithReceiverName adds the tag "oc_receiver" and the name of the receiver as the value,
88+ // and returns the newly created context. For receivers that can receive multiple signals it is
89+ // recommended to encode the signal as suffix (e.g. "oc_trace" and "oc_metrics").
7690func ContextWithReceiverName (ctx context.Context , receiverName string ) context.Context {
77- ctx , _ = tag .New (ctx , tag .Upsert (tagKeyReceiverName , receiverName ))
91+ ctx , _ = tag .New (ctx , tag .Upsert (tagKeyReceiver , receiverName ))
7892 return ctx
7993}
8094
81- // NewReceivedSpansRecorderStreaming creates a function that uses a context created
82- // from the name of the receiver to record the number of the spans received
83- // by the receiver.
84- func NewReceivedSpansRecorderStreaming (lifetimeCtx context.Context , receiverName string ) func (* commonpb.Node , []* tracepb.Span ) {
85- // We create and reuse this context because for streaming RPCs e.g. with gRPC
86- // the context doesn't change, so it is more useful for avoid expensively adding
87- // keys on each invocation. We can create the context once and then reuse it
88- // when recording measurements.
89- ctx := ContextWithReceiverName (lifetimeCtx , receiverName )
90-
91- return func (ni * commonpb.Node , spans []* tracepb.Span ) {
92- // TODO: (@odeke-em) perhaps also record information from the node?
93- stats .Record (ctx , mReceivedSpans .M (int64 (len (spans ))))
94- }
95+ // RecordTraceReceiverMetrics records the number of the spans received and dropped by the receiver.
96+ // Use it with a context.Context generated using ContextWithReceiverName().
97+ func RecordTraceReceiverMetrics (ctxWithTraceReceiverName context.Context , receivedSpans int , droppedSpans int ) {
98+ stats .Record (ctxWithTraceReceiverName , mReceiverReceivedSpans .M (int64 (receivedSpans )), mReceiverDroppedSpans .M (int64 (droppedSpans )))
9599}
96100
97- // NewExportedSpansRecorder creates a helper function that'll add the name of the
98- // creating exporter as a tag value in the context that will be used to count the
99- // the number of spans exported.
100- func NewExportedSpansRecorder (exporterName string ) func (context.Context , * commonpb.Node , []* tracepb.Span ) {
101- return func (ctx context.Context , ni * commonpb.Node , spans []* tracepb.Span ) {
102- ctx , _ = tag .New (ctx , tag .Upsert (tagKeyExporterName , exporterName ))
103- stats .Record (ctx , mExportedSpans .M (int64 (len (spans ))))
104- }
101+ // ContextWithExporterName adds the tag "oc_exporter" and the name of the exporter as the value,
102+ // and returns the newly created context. For exporters that can export multiple signals it is
103+ // recommended to encode the signal as suffix (e.g. "oc_trace" and "oc_metrics").
104+ func ContextWithExporterName (ctx context.Context , exporterName string ) context.Context {
105+ ctx , _ = tag .New (ctx , tag .Upsert (tagKeyExporter , exporterName ))
106+ return ctx
107+ }
108+
109+ // RecordTraceExporterMetrics records the number of the spans received and dropped by the exporter.
110+ // Use it with a context.Context generated using ContextWithExporterName().
111+ func RecordTraceExporterMetrics (ctx context.Context , receivedSpans int , droppedSpans int ) {
112+ stats .Record (ctx , mExporterReceivedSpans .M (int64 (receivedSpans )), mExporterDroppedSpans .M (int64 (droppedSpans )))
105113}
106114
107115// GRPCServerWithObservabilityEnabled creates a gRPC server that at a bare minimum has
0 commit comments