Skip to content

Commit

Permalink
Refactor MassTransitHelpers and tests to extensions project
Browse files Browse the repository at this point in the history
  • Loading branch information
nr-ahemsath committed Nov 14, 2023
1 parent feb0bce commit dabf9a6
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

using System;
using MassTransit;
using NewRelic.Agent.Api;
using NewRelic.Agent.Extensions.Providers.Wrapper;

namespace NewRelic.Providers.Wrapper.MassTransitLegacy

namespace NewRelic.Agent.Extensions.Helpers
{
public class MassTransitQueueData
{
Expand Down Expand Up @@ -36,15 +35,5 @@ public static MassTransitQueueData GetQueueData(Uri sourceAddress)
}
return data;
}

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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MassTransit;
using NewRelic.Agent.Api;
using NewRelic.Agent.Extensions.Helpers;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using MethodCall = NewRelic.Agent.Extensions.Providers.Wrapper.MethodCall;

Expand Down Expand Up @@ -87,7 +89,7 @@ public async Task Send(PublishContext context, IPipe<PublishContext> next)
var queueData = MassTransitHelpers.GetQueueData(context.SourceAddress);

var transaction = _agent.CurrentTransaction;
MassTransitHelpers.InsertDistributedTraceHeaders(context.Headers, transaction);
InsertDistributedTraceHeaders(context.Headers, transaction);
var segment = transaction.StartMessageBrokerSegment(mc, queueData.DestinationType, MessageBrokerAction.Produce, MessageBrokerVendorName, queueData.QueueName);

await next.Send(context);
Expand All @@ -104,11 +106,22 @@ public async Task Send(SendContext context, IPipe<SendContext> next)
var queueData = MassTransitHelpers.GetQueueData(context.SourceAddress);

var transaction = _agent.CurrentTransaction;
MassTransitHelpers.InsertDistributedTraceHeaders(context.Headers, transaction);
InsertDistributedTraceHeaders(context.Headers, transaction);
var segment = transaction.StartMessageBrokerSegment(mc, queueData.DestinationType, MessageBrokerAction.Produce, MessageBrokerVendorName, queueData.QueueName);

await next.Send(context);
segment.End();
}

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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using GreenPipes;
using MassTransit;
using NewRelic.Agent.Api;
using NewRelic.Agent.Extensions.Helpers;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using MethodCall = NewRelic.Agent.Extensions.Providers.Wrapper.MethodCall;

Expand Down Expand Up @@ -88,7 +90,7 @@ public async Task Send(PublishContext context, IPipe<PublishContext> next)
var queueData = MassTransitHelpers.GetQueueData(context.SourceAddress);

var transaction = _agent.CurrentTransaction;
MassTransitHelpers.InsertDistributedTraceHeaders(context.Headers, transaction);
InsertDistributedTraceHeaders(context.Headers, transaction);
var segment = transaction.StartMessageBrokerSegment(mc, queueData.DestinationType, MessageBrokerAction.Produce, MessageBrokerVendorName, queueData.QueueName);

await next.Send(context);
Expand All @@ -105,11 +107,22 @@ public async Task Send(SendContext context, IPipe<SendContext> next)
var queueData = MassTransitHelpers.GetQueueData(context.SourceAddress);

var transaction = _agent.CurrentTransaction;
MassTransitHelpers.InsertDistributedTraceHeaders(context.Headers, transaction);
InsertDistributedTraceHeaders(context.Headers, transaction);
var segment = transaction.StartMessageBrokerSegment(mc, queueData.DestinationType, MessageBrokerAction.Produce, MessageBrokerVendorName, queueData.QueueName);

await next.Send(context);
segment.End();
}

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);
}

}
}
2 changes: 0 additions & 2 deletions tests/Agent/UnitTests/Core.UnitTest/Core.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
<ProjectReference Include="$(RootProjectDirectory)\src\Agent\NewRelic\Agent\Extensions\NewRelic.Agent.Extensions\NewRelic.Agent.Extensions.csproj" />
<ProjectReference Include="$(RootProjectDirectory)\src\NewRelic.Core\NewRelic.Core.csproj" />
<ProjectReference Include="$(RootProjectDirectory)\src\Agent\NewRelic\Agent\Parsing\Parsing.csproj" />
<ProjectReference Include="..\..\..\..\src\Agent\NewRelic\Agent\Extensions\Providers\Wrapper\MassTransitLegacy\MassTransitLegacy.csproj" />
<ProjectReference Include="..\..\..\..\src\Agent\NewRelic\Agent\Extensions\Providers\Wrapper\MassTransit\MassTransit.csproj" />
<ProjectReference Include="..\..\NewRelic.Testing.Assertions\NewRelic.Testing.Assertions.csproj" />
<ProjectReference Include="..\NewRelic.Agent.TestUtilities\NewRelic.Agent.TestUtilities.csproj" />
</ItemGroup>
Expand Down
53 changes: 0 additions & 53 deletions tests/Agent/UnitTests/Core.UnitTest/Instrumentation/MassTransit.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System;
using NUnit.Framework;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using NewRelic.Agent.Extensions.Helpers;

namespace Agent.Extensions.Tests.Helpers
{
[TestFixture]
public class MassTransitHelperTests
{

[Test]
[TestCase("rabbitmq://localhost/SomeHostname_MassTransitTest_bus_myqueuename?temporary=true", "myqueuename")]
[TestCase("rabbitmq://localhost/SomeHostname_MassTransitTest_bus_myqueuename", "myqueuename")]
[TestCase("rabbitmq://localhost/bogus", "Unknown")]
[TestCase(null, "Unknown")]
public void GetQueueName(Uri uri, string expectedQueueName)
{
// Act
var queueName = MassTransitHelpers.GetQueueData(uri).QueueName;

// Assert
Assert.AreEqual(expectedQueueName, queueName, "Did not get expected queue name");
}

[Test]
[TestCase("rabbitmq://localhost/NRHXPSQL3_MassTransitTest_bus_myqueuename?temporary=true", MessageBrokerDestinationType.TempQueue)]
[TestCase("rabbitmq://localhost/NRHXPSQL3_MassTransitTest_bus_myqueuename?temporary=false", MessageBrokerDestinationType.Queue)]
[TestCase("rabbitmq://localhost/NRHXPSQL3_MassTransitTest_bus_myqueuename", MessageBrokerDestinationType.Queue)]
[TestCase(null, MessageBrokerDestinationType.Queue)]
public void GetBrokerDestinationType(Uri uri, MessageBrokerDestinationType expectedDestType)
{
// Act
var destType = MassTransitHelpers.GetQueueData(uri).DestinationType;

// Assert
Assert.AreEqual(expectedDestType, destType, "Did not get expected queue type");
}
}

}
Loading

0 comments on commit dabf9a6

Please sign in to comment.