Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated MassTransit instrumentation to OpenTelemetry 1.0 #85

Merged
merged 5 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,62 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using OpenTelemetry.Instrumentation;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Contrib.Instrumentation.MassTransit.Implementation
{
internal class MassTransitDiagnosticListener : ListenerHandler
{
private readonly ActivitySourceAdapter activitySource;
internal static readonly AssemblyName AssemblyName = typeof(MassTransitDiagnosticListener).Assembly.GetName();
internal static readonly string ActivitySourceName = AssemblyName.Name;
internal static readonly Version Version = AssemblyName.Version;
internal static readonly ActivitySource ActivitySource = new ActivitySource(ActivitySourceName, Version.ToString());

private readonly MassTransitInstrumentationOptions options;

public MassTransitDiagnosticListener(ActivitySourceAdapter activitySource, MassTransitInstrumentationOptions options)
: base("MassTransit")
public MassTransitDiagnosticListener(string name, MassTransitInstrumentationOptions options)
: base(name)
{
this.activitySource = activitySource;
this.options = options;
}

public override void OnStartActivity(Activity activity, object payload)
{
if (this.options.TracedOperations != null && !this.options.TracedOperations.Contains(activity.OperationName))
{
MassTransitInstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
return;
}

activity.DisplayName = this.GetDisplayName(activity);

this.activitySource.Start(activity, this.GetActivityKind(activity));
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
ActivityInstrumentationHelper.SetKindProperty(activity, this.GetActivityKind(activity));
}

public override void OnStopActivity(Activity activity, object payload)
{
if (this.options.TracedOperations != null && !this.options.TracedOperations.Contains(activity.OperationName))
{
MassTransitInstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: RequestIsFilteredOut doesn't sound the apt name here.

activity.IsAllDataRequested = false;
return;
}

if (activity.IsAllDataRequested)
{
this.TransformMassTransitTags(activity);
try
{
this.TransformMassTransitTags(activity);
}
catch (Exception ex)
{
MassTransitInstrumentationEventSource.Log.EnrichmentException(ex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This error message seem to indicate an error occuring in some user defined Enrich function. Given this is an internal transformation error, please rename the method and log message accordingly.

}
}

this.activitySource.Stop(activity);
}

private string GetDisplayName(Activity activity)
Expand Down Expand Up @@ -94,8 +108,8 @@ private void TransformMassTransitTags(Activity activity)

this.RenameTag(activity, TagName.MessageId, SemanticConventions.AttributeMessagingMessageId);
this.RenameTag(activity, TagName.ConversationId, SemanticConventions.AttributeMessagingConversationId);
this.RenameTag(activity, TagName.InitiatorId, SemanticConventions.AttributeMessagingMassTransitInitiatorId);
this.RenameTag(activity, TagName.CorrelationId, SemanticConventions.AttributeMessagingMassTransitCorrelationId);
this.RenameTag(activity, TagName.InitiatorId, MassTransitSemanticConventions.AttributeMessagingMassTransitInitiatorId);
this.RenameTag(activity, TagName.CorrelationId, MassTransitSemanticConventions.AttributeMessagingMassTransitCorrelationId);

activity.SetTag(TagName.SourceAddress, null);
}
Expand All @@ -105,8 +119,8 @@ private void TransformMassTransitTags(Activity activity)

this.RenameTag(activity, TagName.MessageId, SemanticConventions.AttributeMessagingMessageId);
this.RenameTag(activity, TagName.ConversationId, SemanticConventions.AttributeMessagingConversationId);
this.RenameTag(activity, TagName.InitiatorId, SemanticConventions.AttributeMessagingMassTransitInitiatorId);
this.RenameTag(activity, TagName.CorrelationId, SemanticConventions.AttributeMessagingMassTransitCorrelationId);
this.RenameTag(activity, TagName.InitiatorId, MassTransitSemanticConventions.AttributeMessagingMassTransitInitiatorId);
this.RenameTag(activity, TagName.CorrelationId, MassTransitSemanticConventions.AttributeMessagingMassTransitCorrelationId);

activity.SetTag(TagName.MessageId, null);

Expand All @@ -116,7 +130,7 @@ private void TransformMassTransitTags(Activity activity)
}
else if (activity.OperationName == OperationName.Consumer.Consume)
{
this.RenameTag(activity, TagName.ConsumerType, SemanticConventions.AttributeMessagingMassTransitConsumerType);
this.RenameTag(activity, TagName.ConsumerType, MassTransitSemanticConventions.AttributeMessagingMassTransitConsumerType);
}
else if (activity.OperationName == OperationName.Consumer.Handle)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <copyright file="MassTransitInstrumentationEventSource.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Diagnostics.Tracing;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Contrib.Instrumentation.MassTransit.Implementation
{
/// <summary>
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Instrumentation-MassTransit")]
internal class MassTransitInstrumentationEventSource : EventSource
{
public static MassTransitInstrumentationEventSource Log = new MassTransitInstrumentationEventSource();

[Event(1, Message = "Request is filtered out.", Level = EventLevel.Verbose)]
public void RequestIsFilteredOut(string eventName)
{
this.WriteEvent(1, eventName);
}

[NonEvent]
public void EnrichmentException(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
this.EnrichmentException(ex.ToInvariantString());
}
}

[Event(2, Message = "Enrich threw exception. Exception {0}.", Level = EventLevel.Error)]
public void EnrichmentException(string exception)
{
this.WriteEvent(2, exception);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <copyright file="MassTransitSemanticConventions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Contrib.Instrumentation.MassTransit.Implementation
{
internal class MassTransitSemanticConventions
{
public const string AttributeMessagingMassTransitInitiatorId = "messaging.masstransit.initiator_id";
public const string AttributeMessagingMassTransitCorrelationId = "messaging.masstransit.correlation_id";
public const string AttributeMessagingMassTransitConsumerType = "messaging.masstransit.consumer_type";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,25 @@
using System;
using OpenTelemetry.Contrib.Instrumentation.MassTransit.Implementation;
using OpenTelemetry.Instrumentation;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Contrib.Instrumentation.MassTransit
{
internal class MassTransitInstrumentation : IDisposable
{
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
internal const string MassTransitDiagnosticListenerName = "MassTransit";

/// <summary>
/// Initializes a new instance of the <see cref="MassTransitInstrumentation"/> class.
/// </summary>
/// <param name="activitySource">ActivitySource adapter instance.</param>
public MassTransitInstrumentation(ActivitySourceAdapter activitySource)
: this(activitySource, new MassTransitInstrumentationOptions())
{
}
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;

/// <summary>
/// Initializes a new instance of the <see cref="MassTransitInstrumentation"/> class.
/// </summary>
/// <param name="activitySource">ActivitySource adapter instance.</param>
/// <param name="options">Instrumentation options.</param>
public MassTransitInstrumentation(ActivitySourceAdapter activitySource, MassTransitInstrumentationOptions options)
public MassTransitInstrumentation(MassTransitInstrumentationOptions options)
{
var diagnosticListener = new MassTransitDiagnosticListener(activitySource, options);
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(diagnosticListener, null);
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
name => new MassTransitDiagnosticListener(name, options),
listener => listener.Name == MassTransitDiagnosticListenerName,
null);
this.diagnosticSourceSubscriber.Subscribe();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<Description>OpenTelemetry instrumentation for MassTransit</Description>
<PackageTags>$(PackageTags);distributed-tracing;MassTransit</PackageTags>
<MinVerTagPrefix>Instrumentation.MassTransit-</MinVerTagPrefix>
<IncludeSharedInstrumentationSource>true</IncludeSharedInstrumentationSource>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="0.7.0-beta.1" />
<PackageReference Include="OpenTelemetry" Version="1.0.2-alpha.0.29" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using OpenTelemetry.Contrib.Instrumentation.MassTransit;
using OpenTelemetry.Contrib.Instrumentation.MassTransit.Implementation;

namespace OpenTelemetry.Trace
{
Expand All @@ -42,7 +43,15 @@ public static TracerProviderBuilder AddMassTransitInstrumentation(
var options = new MassTransitInstrumentationOptions();
configureMassTransitInstrumentationOptions?.Invoke(options);

return builder.AddInstrumentation(activitySource => new MassTransitInstrumentation(activitySource, options));
builder.AddInstrumentation(() => new MassTransitInstrumentation(options));
builder.AddSource(MassTransitDiagnosticListener.ActivitySourceName);

builder.AddLegacySource(OperationName.Consumer.Consume);
builder.AddLegacySource(OperationName.Consumer.Handle);
builder.AddLegacySource(OperationName.Transport.Send);
builder.AddLegacySource(OperationName.Transport.Receive);

return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public async Task ShouldMapMassTransitTagsForConsumeMessageToOpenTelemetrySpecif
Assert.NotNull(expectedMessageContext);
Assert.Equal("OpenTelemetry.Contrib.Instrumentation.MassTransit.Tests.TestConsumer process", actualActivity.DisplayName);
Assert.Equal(ActivityKind.Internal, actualActivity.Kind);
Assert.Equal("OpenTelemetry.Contrib.Instrumentation.MassTransit.Tests.TestConsumer", actualActivity.GetTagValue(SemanticConventions.AttributeMessagingMassTransitConsumerType)?.ToString());
Assert.Equal("OpenTelemetry.Contrib.Instrumentation.MassTransit.Tests.TestConsumer", actualActivity.GetTagValue(MassTransitSemanticConventions.AttributeMessagingMassTransitConsumerType)?.ToString());

Assert.Null(actualActivity.GetTagValue(TagName.SpanKind));
Assert.Null(actualActivity.GetTagValue(TagName.PeerService));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<Description>Unit test project for OpenTelemetry MassTransit instrumentation</Description>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net461</TargetFrameworks>
<IncludeSharedTestSource>true</IncludeSharedTestSource>
</PropertyGroup>

<ItemGroup>
Expand Down