From e7e93d74fc57f9fd7965c44d50db052dcdd9b97a Mon Sep 17 00:00:00 2001 From: fraliv13 <5892139+fraliv13@users.noreply.github.com> Date: Thu, 18 Apr 2019 16:52:47 +0300 Subject: [PATCH 1/2] RDINC-847 OpenTracing - Messaging semantic conventions --- .../MessagingTags.cs | 8 +++++ .../OpenTracingPublisherDecorator.cs | 31 +++++++++---------- .../SpanExtensions.cs | 23 ++++++++++++++ .../Subscriber/OpenTracingMiddleware.cs | 27 ++++++---------- 4 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 src/Messaging/NBB.Messaging.OpenTracing/MessagingTags.cs create mode 100644 src/Messaging/NBB.Messaging.OpenTracing/SpanExtensions.cs diff --git a/src/Messaging/NBB.Messaging.OpenTracing/MessagingTags.cs b/src/Messaging/NBB.Messaging.OpenTracing/MessagingTags.cs new file mode 100644 index 00000000..a9ca6866 --- /dev/null +++ b/src/Messaging/NBB.Messaging.OpenTracing/MessagingTags.cs @@ -0,0 +1,8 @@ +namespace NBB.Messaging.OpenTracing +{ + public static class MessagingTags + { + public const string ComponentMessaging = "NBB.Messaging"; + public const string CorrelationId = "nbb.correlation_id"; + } +} diff --git a/src/Messaging/NBB.Messaging.OpenTracing/Publisher/OpenTracingPublisherDecorator.cs b/src/Messaging/NBB.Messaging.OpenTracing/Publisher/OpenTracingPublisherDecorator.cs index fd472dcf..f2ea762b 100644 --- a/src/Messaging/NBB.Messaging.OpenTracing/Publisher/OpenTracingPublisherDecorator.cs +++ b/src/Messaging/NBB.Messaging.OpenTracing/Publisher/OpenTracingPublisherDecorator.cs @@ -6,7 +6,6 @@ using OpenTracing.Propagation; using OpenTracing.Tag; using System; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -16,11 +15,13 @@ public class OpenTracingPublisherDecorator : IMessageBusPublisher { private readonly IMessageBusPublisher _inner; private readonly ITracer _tracer; + private readonly ITopicRegistry _topicRegistry; - public OpenTracingPublisherDecorator(IMessageBusPublisher inner, ITracer tracer) + public OpenTracingPublisherDecorator(IMessageBusPublisher inner, ITracer tracer,ITopicRegistry topicRegistry) { _inner = inner; _tracer = tracer; + _topicRegistry = topicRegistry; } public Task PublishAsync(T message, CancellationToken cancellationToken, Action customizer = null, string topicName = null) @@ -36,28 +37,24 @@ void NewCustomizer(MessagingEnvelope outgoingEnvelope) customizer?.Invoke(outgoingEnvelope); } - string operationName = $"Publisher {message.GetType().GetPrettyName()}"; + var formattedTopicName = _topicRegistry.GetTopicForName(topicName) ?? + _topicRegistry.GetTopicForMessageType(message.GetType()); + var operationName = $"Publisher {message.GetType().GetPrettyName()}"; + using (var scope = _tracer.BuildSpan(operationName) - .WithTag(Tags.Component, "NBB.Messaging.Publisher") - .WithTag(Tags.SpanKind, Tags.SpanKindServer) - .WithTag("correlationId", CorrelationManager.GetCorrelationId()?.ToString()) - .StartActive(finishSpanOnDispose: true)) + .WithTag(Tags.Component, MessagingTags.ComponentMessaging) + .WithTag(Tags.SpanKind, Tags.SpanKindProducer) + .WithTag(Tags.MessageBusDestination, formattedTopicName) + .WithTag(MessagingTags.CorrelationId, CorrelationManager.GetCorrelationId()?.ToString()) + .StartActive(true)) { try { - return _inner.PublishAsync(message, cancellationToken, NewCustomizer); + return _inner.PublishAsync(message, cancellationToken, NewCustomizer, topicName); } catch (Exception exception) { - scope.Span.Log(new Dictionary(3) - { - { LogFields.Event, Tags.Error.Key }, - { LogFields.ErrorKind, exception.GetType().Name }, - { LogFields.ErrorObject, exception } - }); - - scope.Span.SetTag(Tags.Error, true); - + scope.Span.SetException(exception); throw; } } diff --git a/src/Messaging/NBB.Messaging.OpenTracing/SpanExtensions.cs b/src/Messaging/NBB.Messaging.OpenTracing/SpanExtensions.cs new file mode 100644 index 00000000..20b0642c --- /dev/null +++ b/src/Messaging/NBB.Messaging.OpenTracing/SpanExtensions.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OpenTracing; +using OpenTracing.Tag; + +namespace NBB.Messaging.OpenTracing +{ + public static class SpanExtensions + { + public static void SetException(this ISpan span, Exception exception) + { + span.Log(new Dictionary(3) + { + {LogFields.Event, Tags.Error.Key}, + {LogFields.ErrorKind, exception.GetType().Name}, + {LogFields.ErrorObject, exception} + }); + + span.SetTag(Tags.Error, true); + } + } +} diff --git a/src/Messaging/NBB.Messaging.OpenTracing/Subscriber/OpenTracingMiddleware.cs b/src/Messaging/NBB.Messaging.OpenTracing/Subscriber/OpenTracingMiddleware.cs index 0d72e682..a544a9ba 100644 --- a/src/Messaging/NBB.Messaging.OpenTracing/Subscriber/OpenTracingMiddleware.cs +++ b/src/Messaging/NBB.Messaging.OpenTracing/Subscriber/OpenTracingMiddleware.cs @@ -4,7 +4,6 @@ using OpenTracing.Propagation; using OpenTracing.Tag; using System; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using NBB.Core.Abstractions; @@ -25,14 +24,17 @@ public OpenTracingMiddleware(ITracer tracer) public async Task Invoke(MessagingEnvelope data, CancellationToken cancellationToken, Func next) { var extractedSpanContext = _tracer.Extract(BuiltinFormats.TextMap, new TextMapExtractAdapter(data.Headers)); - string operationName = $"Subscriber {data.Payload.GetType().GetPrettyName()}"; + string operationName = $"Subscriber {data.Payload.GetType().GetPrettyName()}"; - using(var scope = _tracer.BuildSpan(operationName) - .AsChildOf(extractedSpanContext) - .WithTag(Tags.Component, "NBB.Messaging.Subscriber") - .WithTag(Tags.SpanKind, Tags.SpanKindServer) + using (var scope = _tracer.BuildSpan(operationName) + .AddReference(References.FollowsFrom, extractedSpanContext) + .WithTag(Tags.Component, "NBB.Messaging") + .WithTag(Tags.SpanKind, Tags.SpanKindConsumer) + .WithTag(Tags.PeerService, + data.Headers.TryGetValue(MessagingHeaders.Source, out var value) ? value : default) .WithTag("correlationId", CorrelationManager.GetCorrelationId()?.ToString()) - .StartActive(finishSpanOnDispose: true)) { + .StartActive(true)) + { try { @@ -40,19 +42,10 @@ public async Task Invoke(MessagingEnvelope data, CancellationToken cancellationT } catch (Exception exception) { - scope.Span.Log(new Dictionary(3) - { - { LogFields.Event, Tags.Error.Key }, - { LogFields.ErrorKind, exception.GetType().Name }, - { LogFields.ErrorObject, exception } - }); - - scope.Span.SetTag(Tags.Error, true); - + scope.Span.SetException(exception); throw; } } - return; } } } From 469ce08eccf3614fcbf365764894b63467d50d80 Mon Sep 17 00:00:00 2001 From: fraliv13 <5892139+fraliv13@users.noreply.github.com> Date: Thu, 18 Apr 2019 17:44:35 +0300 Subject: [PATCH 2/2] RDING-847-OpenTracing: Raised C# language version --- .../NBB.Messaging.OpenTracing/NBB.Messaging.OpenTracing.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Messaging/NBB.Messaging.OpenTracing/NBB.Messaging.OpenTracing.csproj b/src/Messaging/NBB.Messaging.OpenTracing/NBB.Messaging.OpenTracing.csproj index b46c7497..789bea7d 100644 --- a/src/Messaging/NBB.Messaging.OpenTracing/NBB.Messaging.OpenTracing.csproj +++ b/src/Messaging/NBB.Messaging.OpenTracing/NBB.Messaging.OpenTracing.csproj @@ -2,6 +2,7 @@ netstandard2.0 + 7.3