-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto-instrumentation for MassTransit v7 and v8.
- Loading branch information
1 parent
c0e06e9
commit 2a1060a
Showing
14 changed files
with
579 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/MassTransit/Instrumentation.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- | ||
Copyright 2020 New Relic Corporation. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
<extension xmlns="urn:newrelic-extension"> | ||
|
||
<instrumentation> | ||
|
||
<tracerFactory name="TransportConfigWrapper"> | ||
|
||
<match assemblyName="MassTransit" className="MassTransit.Configuration.TransportRegistrationBusFactory`1" minVersion="8.0.0"> | ||
<exactMethodMatcher methodName="CreateBus" /> | ||
</match> | ||
</tracerFactory> | ||
|
||
</instrumentation> | ||
</extension> |
24 changes: 24 additions & 0 deletions
24
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/MassTransit/MassTransit.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks> | ||
<AssemblyName>NewRelic.Providers.Wrapper.MassTransit</AssemblyName> | ||
<RootNamespace>NewRelic.Providers.Wrapper.MassTransit</RootNamespace> | ||
<Description>MassTransit Wrapper Provider for New Relic .NET Agent</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MassTransit.Abstractions" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="Instrumentation.xml"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(RootProjectDirectory)\src\NewRelic.Core\NewRelic.Core.csproj" /> | ||
<ProjectReference Include="..\..\..\NewRelic.Agent.Extensions\NewRelic.Agent.Extensions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
47 changes: 47 additions & 0 deletions
47
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/MassTransit/MassTransitHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using MassTransit; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
|
||
namespace NewRelic.Providers.Wrapper.MassTransit | ||
{ | ||
public class MassTransitHelpers | ||
{ | ||
private static string[] GetQueueData(Uri sourceAddress) | ||
{ | ||
// rabbitmq://localhost/NRHXPSQL3_MassTransitTest_bus_iyeoyyge44oc7yijbdp5i1opfd?temporary=true | ||
var items = sourceAddress.AbsoluteUri.Split('_'); | ||
return items[items.Length - 1].Split('?'); | ||
} | ||
|
||
public static string GetQueue(Uri sourceAddress) | ||
{ | ||
var queueData = GetQueueData(sourceAddress); | ||
return queueData[0]; | ||
} | ||
|
||
public static MessageBrokerDestinationType GetBrokerDestinationType(Uri sourceAddress) | ||
{ | ||
var queueData = GetQueueData(sourceAddress); | ||
if (queueData.Length == 2 && queueData[1] == "temporary=true") | ||
{ | ||
return MessageBrokerDestinationType.TempQueue; | ||
} | ||
|
||
return MessageBrokerDestinationType.Queue; | ||
} | ||
|
||
public static void InsertDistributedTraceHeaders(SendHeaders headers, ITransaction transaction) | ||
{ | ||
var setHeaders = new Action<SendHeaders, string, string>((carrier, key, value) => | ||
{ | ||
carrier.Set(key, value); | ||
}); | ||
|
||
transaction.InsertDistributedTraceHeaders(headers, setHeaders); | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/MassTransit/NewRelicFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using MassTransit; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
using MethodCall = NewRelic.Agent.Extensions.Providers.Wrapper.MethodCall; | ||
|
||
namespace NewRelic.Providers.Wrapper.MassTransit | ||
{ | ||
public class NewRelicFilter : IFilter<ConsumeContext>, IFilter<PublishContext>, IFilter<SendContext> | ||
{ | ||
private const string SendMethodName = "Send"; | ||
|
||
private Method _consumeMethod; | ||
private Method _publishMethod; | ||
private Method _sendMethod; | ||
|
||
private Agent.Api.IAgent _agent; | ||
|
||
public NewRelicFilter(Agent.Api.IAgent agent) | ||
{ | ||
_agent = agent; | ||
} | ||
|
||
public void Probe(ProbeContext context) | ||
{ | ||
context.CreateFilterScope("newrelic-scope"); | ||
} | ||
|
||
public async Task Send(ConsumeContext context, IPipe<ConsumeContext> next) | ||
{ | ||
_ = _consumeMethod ??= new Method(context.GetType(), SendMethodName, | ||
context.GetType().FullName + "," + next.GetType().FullName); | ||
|
||
var mc = new MethodCall(_consumeMethod, context, default(string[])); | ||
|
||
var destName = MassTransitHelpers.GetQueue(context.SourceAddress); | ||
|
||
var transaction = _agent.CreateTransaction( | ||
destinationType: MassTransitHelpers.GetBrokerDestinationType(context.SourceAddress), | ||
brokerVendorName: "MassTransit", | ||
destination: destName); | ||
|
||
transaction.AttachToAsync(); | ||
|
||
transaction.AcceptDistributedTraceHeaders(context.Headers, GetHeaderValue, TransportType.AMQP); | ||
|
||
var segment = transaction.StartMessageBrokerSegment(mc, MessageBrokerDestinationType.Queue, MessageBrokerAction.Consume, "MassTransit", destName); | ||
|
||
await next.Send(context); | ||
segment.End(); | ||
transaction.End(); | ||
|
||
IEnumerable<string> GetHeaderValue(Headers carrier, string key) | ||
{ | ||
var headers = carrier.GetAll(); | ||
if (headers != null) | ||
{ | ||
var headerValues = new List<string>(); | ||
foreach (var item in headers) | ||
{ | ||
if (item.Key.Equals(key, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
headerValues.Add(item.Value.ToString()); | ||
} | ||
} | ||
|
||
return headerValues; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public async Task Send(PublishContext context, IPipe<PublishContext> next) | ||
{ | ||
_ = _publishMethod ??= new Method(context.GetType(), SendMethodName, | ||
context.GetType().FullName + "," + next.GetType().FullName); | ||
|
||
var mc = new MethodCall(_publishMethod, context, default(string[])); | ||
|
||
var destName = MassTransitHelpers.GetQueue(context.SourceAddress); | ||
var destType = MassTransitHelpers.GetBrokerDestinationType(context.SourceAddress); | ||
|
||
var transaction = _agent.CurrentTransaction; | ||
MassTransitHelpers.InsertDistributedTraceHeaders(context.Headers, transaction); | ||
var segment = transaction.StartMessageBrokerSegment(mc, destType, MessageBrokerAction.Produce, "MassTransit", destName); | ||
|
||
await next.Send(context); | ||
segment.End(); | ||
} | ||
|
||
public async Task Send(SendContext context, IPipe<SendContext> next) | ||
{ | ||
_ = _sendMethod ??= new Method(context.GetType(), SendMethodName, | ||
context.GetType().FullName + "," + next.GetType().FullName); | ||
|
||
var mc = new MethodCall(_sendMethod, context, default(string[])); | ||
|
||
var destName = MassTransitHelpers.GetQueue(context.SourceAddress); | ||
var destType = MassTransitHelpers.GetBrokerDestinationType(context.SourceAddress); | ||
|
||
var transaction = _agent.CurrentTransaction; | ||
MassTransitHelpers.InsertDistributedTraceHeaders(context.Headers, transaction); | ||
var segment = transaction.StartMessageBrokerSegment(mc, destType, MessageBrokerAction.Produce, "MassTransit", destName); | ||
|
||
await next.Send(context); | ||
segment.End(); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...gent/NewRelic/Agent/Extensions/Providers/Wrapper/MassTransit/NewRelicPipeSpecification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using MassTransit.Configuration; | ||
using MassTransit; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace NewRelic.Providers.Wrapper.MassTransit | ||
{ | ||
public class NewRelicPipeSpecification : IPipeSpecification<ConsumeContext>, IPipeSpecification<PublishContext>, IPipeSpecification<SendContext> | ||
{ | ||
Agent.Api.IAgent _agent; | ||
|
||
public NewRelicPipeSpecification(Agent.Api.IAgent agent) | ||
{ | ||
_agent = agent; | ||
} | ||
|
||
public IEnumerable<ValidationResult> Validate() | ||
{ | ||
return Enumerable.Empty<ValidationResult>(); | ||
} | ||
|
||
public void Apply(IPipeBuilder<ConsumeContext> builder) | ||
{ | ||
builder.AddFilter(new NewRelicFilter(_agent)); | ||
} | ||
|
||
public void Apply(IPipeBuilder<PublishContext> builder) | ||
{ | ||
builder.AddFilter(new NewRelicFilter(_agent)); | ||
} | ||
|
||
public void Apply(IPipeBuilder<SendContext> builder) | ||
{ | ||
builder.AddFilter(new NewRelicFilter(_agent)); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/MassTransit/TransportConfigWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using MassTransit; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
using NewRelic.SystemExtensions; | ||
|
||
namespace NewRelic.Providers.Wrapper.MassTransit | ||
{ | ||
public class TransportConfigWrapper : IWrapper | ||
{ | ||
private const string WrapperName = "TransportConfigWrapper"; | ||
public bool IsTransactionRequired => false; | ||
|
||
public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo) | ||
{ | ||
return new CanWrapResponse(WrapperName.Equals(methodInfo.RequestedWrapperName)); | ||
} | ||
|
||
public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, Agent.Api.IAgent agent, ITransaction transaction) | ||
{ | ||
// This will be run for each bus. Each bus gets one transport. | ||
// We can support more than on transport with this setup. | ||
var configurator = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs<IBusFactoryConfigurator>(0); | ||
|
||
var spec = new NewRelicPipeSpecification(agent); | ||
|
||
configurator.ConfigurePublish(cfg => cfg.AddPipeSpecification(spec)); | ||
configurator.ConfigureSend(cfg => cfg.AddPipeSpecification(spec)); | ||
configurator.AddPipeSpecification(spec); | ||
|
||
return Delegates.NoOp; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/MassTransitLegacy/Instrumentation.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- | ||
Copyright 2020 New Relic Corporation. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
<extension xmlns="urn:newrelic-extension"> | ||
|
||
<instrumentation> | ||
|
||
<tracerFactory name="TransportConfigLegacyWrapper"> | ||
|
||
<match assemblyName="MassTransit" className="MassTransit.Registration.TransportRegistrationBusFactory" maxVersion="7.99.0"> | ||
<exactMethodMatcher methodName="CreateBus" /> | ||
</match> | ||
</tracerFactory> | ||
|
||
</instrumentation> | ||
</extension> |
Oops, something went wrong.