@@ -29,14 +29,19 @@ import (
2929 cejs "github.com/cloudevents/sdk-go/protocol/nats_jetstream/v2"
3030 "github.com/cloudevents/sdk-go/v2/binding"
3131
32- "go.opencensus.io/trace"
32+ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
33+ "go.opentelemetry.io/otel"
34+ "go.opentelemetry.io/otel/metric"
35+ "go.opentelemetry.io/otel/trace"
36+ "k8s.io/apimachinery/pkg/types"
37+
3338 "knative.dev/eventing-natss/pkg/tracing"
3439
3540 "github.com/nats-io/nats.go"
3641 "go.uber.org/zap"
37- eventingchannels "knative.dev/eventing/pkg/channel"
38- "knative.dev/eventing/pkg/channel/fanout"
3942 "knative.dev/eventing/pkg/kncloudevents"
43+ "knative.dev/eventing/pkg/observability"
44+ eventingtracing "knative.dev/eventing/pkg/tracing"
4045 "knative.dev/pkg/logging"
4146)
4247
@@ -64,8 +69,10 @@ func init() {
6469
6570type PullConsumer struct {
6671 dispatcher * kncloudevents.Dispatcher
67- reporter eventingchannels.StatsReporter
6872 channelNamespace string
73+ channelName string
74+ dispatchDuration metric.Float64Histogram
75+ tracer trace.Tracer
6976
7077 natsConsumer * nats.Subscription
7178 natsConsumerInfo * nats.ConsumerInfo
@@ -81,7 +88,7 @@ type PullConsumer struct {
8188 closed chan struct {}
8289}
8390
84- func NewPullConsumer (ctx context.Context , consumer * nats.Subscription , subscription Subscription , dispatcher * kncloudevents.Dispatcher , reporter eventingchannels. StatsReporter , channelConfig * ChannelConfig ) (* PullConsumer , error ) {
91+ func NewPullConsumer (ctx context.Context , consumer * nats.Subscription , subscription Subscription , dispatcher * kncloudevents.Dispatcher , channelConfig * ChannelConfig ) (* PullConsumer , error ) {
8592 consumerInfo , err := consumer .ConsumerInfo ()
8693 if err != nil {
8794 return nil , fmt .Errorf ("failed to get consumer info: %w" , err )
@@ -90,10 +97,14 @@ func NewPullConsumer(ctx context.Context, consumer *nats.Subscription, subscript
9097 logger := logging .FromContext (ctx )
9198 updatePullSubscriptionConfig (channelConfig , & subscription )
9299
93- return & PullConsumer {
100+ tp := otel .GetTracerProvider ()
101+ mp := otel .GetMeterProvider ()
102+
103+ c := & PullConsumer {
94104 dispatcher : dispatcher ,
95- reporter : reporter ,
96105 channelNamespace : channelConfig .Namespace ,
106+ channelName : channelConfig .Name ,
107+ tracer : tp .Tracer (scopeName ),
97108
98109 natsConsumer : consumer ,
99110 natsConsumerInfo : consumerInfo ,
@@ -104,7 +115,20 @@ func NewPullConsumer(ctx context.Context, consumer *nats.Subscription, subscript
104115
105116 closing : make (chan struct {}),
106117 closed : make (chan struct {}),
107- }, nil
118+ }
119+
120+ meter := mp .Meter (scopeName )
121+ c .dispatchDuration , err = meter .Float64Histogram (
122+ "kn.eventing.dispatch.duration" ,
123+ metric .WithDescription ("The duration to dispatch the event" ),
124+ metric .WithUnit ("s" ),
125+ metric .WithExplicitBucketBoundaries (latencyBounds ... ),
126+ )
127+ if err != nil {
128+ return nil , err
129+ }
130+
131+ return c , nil
108132}
109133
110134func (c * PullConsumer ) ConsumerType () ConsumerType {
@@ -234,16 +258,30 @@ func (c *PullConsumer) handleMessage(ctx context.Context, msg *nats.Msg) (err er
234258 event := tracing .ConvertNatsMsgToEvent (c .logger .Desugar (), msg )
235259 additionalHeaders := tracing .ConvertEventToHttpHeader (event )
236260
237- sc , ok := tracing .ParseSpanContext (event )
238- var span * trace.Span
239- if ! ok {
240- c .logger .Warn ("Cannot parse the spancontext, creating a new span" )
241- ctx , span = trace .StartSpan (ctx , jsmChannel + "-" + string (c .sub .UID ))
242- } else {
243- ctx , span = trace .StartSpanWithRemoteParent (ctx , jsmChannel + "-" + string (c .sub .UID ), sc )
244- }
245-
246- defer span .End ()
261+ ctx = observability .WithChannelLabels (ctx , types.NamespacedName {Name : c .channelName , Namespace : c .channelNamespace })
262+ ctx = observability .WithMessagingLabels (
263+ ctx ,
264+ eventingtracing .SubscriptionMessagingDestination (
265+ types.NamespacedName {
266+ Name : c .sub .Name ,
267+ Namespace : c .sub .Namespace ,
268+ },
269+ ),
270+ "send" ,
271+ )
272+ ctx = observability .WithMinimalEventLabels (ctx , event )
273+
274+ ctx = tracing .ParseSpanContext (ctx , event )
275+ ctx , span := c .tracer .Start (ctx , jsmChannel + "-" + string (c .sub .UID ))
276+ defer func () {
277+ if span .IsRecording () {
278+ // add full event labels here so that they only populate the span, and not any metrics
279+ ctx = observability .WithEventLabels (ctx , event )
280+ labeler , _ := otelhttp .LabelerFromContext (ctx )
281+ span .SetAttributes (labeler .Get ()... )
282+ }
283+ span .End ()
284+ }()
247285
248286 te := TypeExtractorTransformer ("" )
249287
@@ -261,10 +299,13 @@ func (c *PullConsumer) handleMessage(ctx context.Context, msg *nats.Msg) (err er
261299 WithHeader (additionalHeaders ),
262300 )
263301
264- _ = fanout .ParseDispatchResultAndReportMetrics (fanout .NewDispatchResult (err , dispatchExecutionInfo ), c .reporter , eventingchannels.ReportArgs {
265- Ns : c .channelNamespace ,
266- EventType : string (te ),
267- })
302+ labeler , _ := otelhttp .LabelerFromContext (ctx )
303+
304+ c .dispatchDuration .Record (
305+ ctx ,
306+ dispatchExecutionInfo .Duration .Seconds (),
307+ metric .WithAttributes (labeler .Get ()... ),
308+ )
268309
269310 if err != nil {
270311 logger .Errorw ("failed to forward message to downstream subscriber" ,
0 commit comments