-
Notifications
You must be signed in to change notification settings - Fork 55
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
Nservicebus7 feature branch #857
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fe7a936
Adds NServiceBus 6/7 message receive instrumentation. (#830)
vuqtran88 6ed4990
Trigger CI checks on nservicebus7-feature-branch PRs
JcolemanNR 6ba3be7
Adds PipelineWrapper for message send/publish instrumentation. (#832)
vuqtran88 c579261
Migrates NServiceBus 5 tests to use ConsoleMF app (#833)
jaffinito 4d5fdbf
Merge in main
jaffinito 41a48ad
Merge remote-tracking branch 'origin/main' into nservicebus7-feature-…
JcolemanNR 2db4b0e
Adds NServiceBus to CoreComponents in ArtifactBuilder (#851)
jaffinito 3115cbc
Merge branch 'main' of github.com:newrelic/newrelic-dotnet-agent into…
jaffinito d6aabbf
Add Integration tests for NServiceBus 6/7 (#852)
JcolemanNR f7b0f7f
Fix issue with bypasser (#856)
jaffinito 2041fd6
Merge branch 'main' of github.com:newrelic/newrelic-dotnet-agent into…
jaffinito 43b87e7
Merge main and update changelog
jaffinito 1f697b0
NSB: Fix issue with Handler transactions being ended by GC (#858)
JcolemanNR File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
80 changes: 80 additions & 0 deletions
80
...t/NewRelic/Agent/Extensions/Providers/Wrapper/NServiceBus/LoadHandlersConnectorWrapper.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,80 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
|
||
namespace NewRelic.Providers.Wrapper.NServiceBus | ||
{ | ||
/// <summary> | ||
/// This wrapper instruments message receive for NServiceBus v6+ library. | ||
/// </summary> | ||
public class LoadHandlersConnectorWrapper : IWrapper | ||
{ | ||
private const string BrokerVendorName = "NServiceBus"; | ||
private const string WrapperName = "LoadHandlersConnectorWrapper"; | ||
|
||
public bool IsTransactionRequired => false; | ||
|
||
public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo) | ||
{ | ||
return new CanWrapResponse(WrapperName.Equals(methodInfo.RequestedWrapperName)); | ||
} | ||
|
||
public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, | ||
IAgent agent, ITransaction transaction) | ||
{ | ||
var incomingLogicalMessageContext = instrumentedMethodCall.MethodCall.MethodArguments[0]; | ||
|
||
var message = NServiceBusHelpers.GetMessageFromIncomingLogicalMessageContext(incomingLogicalMessageContext); | ||
if (message == null) | ||
{ | ||
throw new NullReferenceException("logicalMessage"); | ||
} | ||
|
||
var queueName = NServiceBusHelpers.TryGetQueueNameLoadHandlersConnector(message); | ||
|
||
//If the transaction does not exist. | ||
if (!transaction.IsValid) | ||
{ | ||
transaction = agent.CreateTransaction( | ||
destinationType: MessageBrokerDestinationType.Queue, | ||
brokerVendorName: BrokerVendorName, | ||
destination: queueName); | ||
|
||
transaction.AttachToAsync(); | ||
transaction.DetachFromPrimary(); //Remove from thread-local type storage | ||
} | ||
|
||
var segment = transaction.StartMessageBrokerSegment(instrumentedMethodCall.MethodCall, MessageBrokerDestinationType.Queue, MessageBrokerAction.Consume, BrokerVendorName, queueName); | ||
|
||
var headers = NServiceBusHelpers.GetHeadersFromIncomingLogicalMessageContext(incomingLogicalMessageContext); | ||
NServiceBusHelpers.ProcessHeaders(headers, agent); | ||
|
||
void OnComplete(Task task) | ||
{ | ||
if (task == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (task.Status == TaskStatus.Faulted) | ||
{ | ||
transaction.NoticeError(task.Exception); | ||
} | ||
|
||
if (task.Status == TaskStatus.RanToCompletion | ||
|| task.Status == TaskStatus.Canceled | ||
|| task.Status == TaskStatus.Faulted) | ||
{ | ||
segment.End(); | ||
transaction.End(); | ||
} | ||
} | ||
|
||
return Delegates.GetAsyncDelegateFor<Task>(agent, segment, false, OnComplete); | ||
} | ||
} | ||
} |
12 changes: 4 additions & 8 deletions
12
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/NServiceBus/NServiceBus.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
205 changes: 205 additions & 0 deletions
205
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/NServiceBus/NServiceBusHelpers.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,205 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
using NewRelic.Reflection; | ||
|
||
namespace NewRelic.Providers.Wrapper.NServiceBus | ||
{ | ||
public class NServiceBusHelpers | ||
{ | ||
private static Func<object, Dictionary<string, string>> _getHeadersSendMessageFunc; | ||
private static Func<object, Dictionary<string, string>> _getHeadersPipelineFunc; | ||
private static Func<object, Dictionary<string, string>> _getHeadersReceiveMessageFunc; | ||
|
||
private static Func<object, object> _getIncomingLogicalMessageFunc; | ||
|
||
private static Func<object, object> _getMessageFromIncomingLogicalMessageContextFunc; | ||
private static Func<object, Dictionary<string, string>> _getHeadersFromIncomingLogicalMessageContextFunc; | ||
|
||
private static Func<object, object> _getMessageFromOutgoingSendContextFunc; | ||
private static Func<object, object> _getMessageFromOutgoingPublishContextFunc; | ||
|
||
public const string OutgoingSendContextTypeName = "NServiceBus.OutgoingSendContext"; | ||
public const string OutgoingPublishContextTypeName = "NServiceBus.OutgoingPublishContext"; | ||
|
||
|
||
private static Func<object, Type> _getMessageTypeSendMessageFunc; | ||
private static Func<object, Type> _getMessageTypePipelineFunc; | ||
private static Func<object, Type> _getMessageTypeReceiveMessageFunc; | ||
private static Func<object, Type> _getMessageTypeLoadHandlersConnectorFunc; | ||
|
||
#region Wrapper Specific Helpers - does not depend on other bypasser results | ||
|
||
public static object GetMessageFromOutgoingContext(object outgoingContext) | ||
{ | ||
if (outgoingContext.GetType().FullName == OutgoingSendContextTypeName) | ||
{ | ||
var getMessageFromOutgoingSendContextFunc = _getMessageFromOutgoingSendContextFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(outgoingContext.GetType(), "Message"); | ||
return getMessageFromOutgoingSendContextFunc(outgoingContext); | ||
} | ||
else if (outgoingContext.GetType().FullName == OutgoingPublishContextTypeName) | ||
{ | ||
var getMessageFromOutgoingPublishContextFunc = _getMessageFromOutgoingPublishContextFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(outgoingContext.GetType(), "Message"); | ||
return getMessageFromOutgoingPublishContextFunc(outgoingContext); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static object GetMessageFromIncomingLogicalMessageContext(object incomingLogicalMessageContext) | ||
{ | ||
var getLogicalMessageContextFunc = _getMessageFromIncomingLogicalMessageContextFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(incomingLogicalMessageContext.GetType(), "Message"); | ||
return getLogicalMessageContextFunc(incomingLogicalMessageContext); | ||
} | ||
|
||
|
||
public static Dictionary<string, string> GetHeadersFromIncomingLogicalMessageContext(object incomingLogicalMessageContext) | ||
{ | ||
var getHeadersFromIncomingLogicalMessageContextFunc = _getHeadersFromIncomingLogicalMessageContextFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Dictionary<string, string>>(incomingLogicalMessageContext.GetType(), "Headers"); | ||
return getHeadersFromIncomingLogicalMessageContextFunc(incomingLogicalMessageContext); | ||
} | ||
|
||
public static object GetIncomingLogicalMessage(object incomingContext) | ||
{ | ||
var getLogicalMessageFunc = _getIncomingLogicalMessageFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(incomingContext.GetType(), "IncomingLogicalMessage"); | ||
return getLogicalMessageFunc(incomingContext); | ||
} | ||
|
||
public static Dictionary<string, string> GetHeadersReceiveMessage(object logicalMessage) | ||
{ | ||
var getHeadersReceiveMessageFunc = _getHeadersReceiveMessageFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Dictionary<string, string>>(logicalMessage.GetType(), "Headers"); | ||
return getHeadersReceiveMessageFunc(logicalMessage); | ||
} | ||
|
||
#endregion | ||
|
||
// receive - load; no bypaser in here | ||
public static void ProcessHeaders(Dictionary<string, string> headers, IAgent agent) | ||
{ | ||
if (headers == null) | ||
{ | ||
return; | ||
} | ||
|
||
agent.CurrentTransaction.AcceptDistributedTraceHeaders(headers, GetHeaderValue, TransportType.HTTP); | ||
|
||
static IEnumerable<string> GetHeaderValue(Dictionary<string, string> carrier, string key) | ||
{ | ||
if (carrier != null) | ||
{ | ||
foreach (var item in carrier) | ||
{ | ||
if (item.Key.Equals(key, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return new string[] { item.Value }; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public static void CreateOutboundHeadersSendMessage(IAgent agent, object logicalMessage) | ||
{ | ||
var getHeadersSendMessageFunc = _getHeadersSendMessageFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Dictionary<string, string>>(logicalMessage.GetType(), "Headers"); | ||
CreateOutboundHeaders(agent, logicalMessage, getHeadersSendMessageFunc); | ||
} | ||
|
||
public static void CreateOutboundHeadersPipeline(IAgent agent, object logicalMessage) | ||
{ | ||
var getHeadersPipelineFunc = _getHeadersPipelineFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Dictionary<string, string>>(logicalMessage.GetType(), "Headers"); | ||
CreateOutboundHeaders(agent, logicalMessage, getHeadersPipelineFunc); | ||
} | ||
|
||
private static void CreateOutboundHeaders(IAgent agent, object logicalMessage, Func<object, Dictionary<string, string>> getHeaders) | ||
{ | ||
// We don't need to check if headers are null since we will create the headers object if its null | ||
// create action for use later | ||
var setHeaders = new Action<object, string, string>((carrier, key, value) => | ||
{ | ||
var headers = getHeaders(logicalMessage); | ||
if (headers == null) | ||
{ | ||
headers = new Dictionary<string, string>(); | ||
SetHeaders(carrier, headers); | ||
} | ||
else if (headers is IReadOnlyDictionary<string, object>) | ||
{ | ||
headers = new Dictionary<string, string>(headers); | ||
SetHeaders(carrier, headers); | ||
} | ||
|
||
headers[key] = value; | ||
}); | ||
|
||
agent.CurrentTransaction.InsertDistributedTraceHeaders(logicalMessage, setHeaders); | ||
|
||
static void SetHeaders(object logicalMessage, Dictionary<string, string> headers) | ||
{ | ||
// Unlike the GetHeaders function, we can't cache this action. It is only valid for the specific logicalMessage object instance provided. | ||
var action = VisibilityBypasser.Instance.GeneratePropertySetter<Dictionary<string, string>>(logicalMessage, "Headers"); | ||
action(headers); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns a metric name based on the type of message. The source/destination queue isn't always known (depending on the circumstances) and in some cases isn't even relevant. The message type is always known and is always relevant. | ||
/// </summary> | ||
/// <param name="logicalMessage"></param> | ||
/// <returns></returns> | ||
public static string TryGetQueueNameSendMessage(object logicalMessage) | ||
{ | ||
var getMessageTypeFunc = _getMessageTypeSendMessageFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Type>(logicalMessage.GetType(), "MessageType"); | ||
return TryGetQueueName(logicalMessage, getMessageTypeFunc); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a metric name based on the type of message. The source/destination queue isn't always known (depending on the circumstances) and in some cases isn't even relevant. The message type is always known and is always relevant. | ||
/// </summary> | ||
/// <param name="logicalMessage"></param> | ||
/// <returns></returns> | ||
public static string TryGetQueueNamePipeline(object logicalMessage) | ||
{ | ||
var getMessageTypeFunc = _getMessageTypePipelineFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Type>(logicalMessage.GetType(), "MessageType"); | ||
return TryGetQueueName(logicalMessage, getMessageTypeFunc); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a metric name based on the type of message. The source/destination queue isn't always known (depending on the circumstances) and in some cases isn't even relevant. The message type is always known and is always relevant. | ||
/// </summary> | ||
/// <param name="logicalMessage"></param> | ||
/// <returns></returns> | ||
public static string TryGetQueueNameReceiveMessage(object logicalMessage) | ||
{ | ||
var getMessageTypeFunc = _getMessageTypeReceiveMessageFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Type>(logicalMessage.GetType(), "MessageType"); | ||
return TryGetQueueName(logicalMessage, getMessageTypeFunc); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a metric name based on the type of message. The source/destination queue isn't always known (depending on the circumstances) and in some cases isn't even relevant. The message type is always known and is always relevant. | ||
/// </summary> | ||
/// <param name="logicalMessage"></param> | ||
/// <returns></returns> | ||
public static string TryGetQueueNameLoadHandlersConnector(object logicalMessage) | ||
{ | ||
var getMessageTypeFunc = _getMessageTypeLoadHandlersConnectorFunc ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<Type>(logicalMessage.GetType(), "MessageType"); | ||
return TryGetQueueName(logicalMessage, getMessageTypeFunc); | ||
} | ||
|
||
private static string TryGetQueueName(object logicalMessage, Func<object, Type> getMessageType) | ||
{ | ||
var messageType = getMessageType(logicalMessage); | ||
|
||
if (messageType == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return messageType.FullName; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will fix in a later PR..