Skip to content

Commit

Permalink
Merge pull request #8 from osstotalsoft/feature/RDINC-847-OpenTracing
Browse files Browse the repository at this point in the history
OpenTracing - Messaging semantic conventions
  • Loading branch information
fraliv13 authored Apr 18, 2019
2 parents 0c0a81a + 469ce08 commit fdfb762
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
8 changes: 8 additions & 0 deletions src/Messaging/NBB.Messaging.OpenTracing/MessagingTags.cs
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using OpenTracing.Propagation;
using OpenTracing.Tag;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -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>(T message, CancellationToken cancellationToken, Action<MessagingEnvelope> customizer = null, string topicName = null)
Expand All @@ -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<string, object>(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;
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/Messaging/NBB.Messaging.OpenTracing/SpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<string, object>(3)
{
{LogFields.Event, Tags.Error.Key},
{LogFields.ErrorKind, exception.GetType().Name},
{LogFields.ErrorObject, exception}
});

span.SetTag(Tags.Error, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,34 +24,28 @@ public OpenTracingMiddleware(ITracer tracer)
public async Task Invoke(MessagingEnvelope data, CancellationToken cancellationToken, Func<Task> 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
{
await next();
}
catch (Exception exception)
{
scope.Span.Log(new Dictionary<string, object>(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;
}
}
}

0 comments on commit fdfb762

Please sign in to comment.