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

Support Azure.Mesasging.ServiceBus (Track2) SDK #2593

Merged
merged 3 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## VNext
- [LOGGING: Make TelemetryConfiguration configurable in ApplicationInsightsLoggingBuilderExtensions](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1944)
- [Added support for distributed tracing with Azure.Messaging.ServiceBus](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2593)

## Version 2.21.0-beta1
- [Support IPv6 in request headers](https://github.com/microsoft/ApplicationInsights-dotnet/issues/2521)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,147 @@ public void AzureClientSpansAreCollectedForEventHubsException()
}
}

[DataRow("producer")]
[DataRow("client")]
[DataTestMethod]
public void AzureServiceBusSpansAreCollectedAsDependency(string kind)
{
using (var listener = new DiagnosticListener("Azure.SomeClient"))
using (var module = new DependencyTrackingTelemetryModule())
{
module.Initialize(this.configuration);

Activity sendActivity = new Activity("Azure.SomeClient.Method")
.AddTag("kind", kind)
.AddTag("az.namespace", "Microsoft.ServiceBus")
.AddTag("component", "servicebus")
.AddTag("peer.address", "amqps://my.servicebus.windows.net/")
.AddTag("message_bus.destination", "queueName");

listener.StartActivity(sendActivity, null);
listener.StopActivity(sendActivity, null);

var telemetry = this.sentItems.Last() as DependencyTelemetry;

Assert.IsNotNull(telemetry);
Assert.AreEqual("SomeClient.Method", telemetry.Name);
if (kind == "producer")
{
Assert.AreEqual("Queue Message | Azure Service Bus", telemetry.Type);
}
else
{
Assert.AreEqual("Azure Service Bus", telemetry.Type);
}

Assert.IsTrue(telemetry.Success.Value);
Assert.IsNull(telemetry.Context.Operation.ParentId);
Assert.AreEqual(sendActivity.StartTimeUtc, telemetry.Timestamp);
Assert.AreEqual(sendActivity.Duration, telemetry.Duration);
Assert.AreEqual(sendActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
Assert.AreEqual(sendActivity.SpanId.ToHexString(), telemetry.Id);
Assert.AreEqual("amqps://my.servicebus.windows.net/queueName", telemetry.Target);
}
}

[DataRow("server")]
[DataRow("consumer")]
[DataTestMethod]
public void AzureServiceBusSpansAreCollectedAsRequest(string kind)
{
using (var listener = new DiagnosticListener("Azure.SomeClient"))
using (var module = new DependencyTrackingTelemetryModule())
{
module.Initialize(this.configuration);

Activity sendActivity = new Activity("Azure.SomeClient.Process")
.AddTag("kind", kind)
.AddTag("az.namespace", "Microsoft.ServiceBus")
.AddTag("component", "servicebus")
.AddTag("peer.address", "amqps://my.servicebus.windows.net")
.AddTag("message_bus.destination", "queueName");

listener.StartActivity(sendActivity, null);
listener.StopActivity(sendActivity, null);

var telemetry = this.sentItems.Last() as RequestTelemetry;

Assert.IsNotNull(telemetry);
Assert.AreEqual("SomeClient.Process", telemetry.Name);
Assert.AreEqual("amqps://my.servicebus.windows.net/queueName", telemetry.Source);
Assert.IsTrue(telemetry.Success.Value);

Assert.IsNull(telemetry.Context.Operation.ParentId);
Assert.AreEqual(sendActivity.StartTimeUtc, telemetry.Timestamp);
Assert.AreEqual(sendActivity.Duration, telemetry.Duration);
Assert.AreEqual(sendActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
Assert.AreEqual(sendActivity.SpanId.ToHexString(), telemetry.Id);
Assert.IsFalse(telemetry.Metrics.Any());
}
}

[DataRow("producer")]
[DataRow("client")]
[DataRow("server")]
[DataRow("consumer")]
[DataTestMethod]
public void AzureServiceBusSpansAreCollectedError(string kind)
{
using (var listener = new DiagnosticListener("Azure.SomeClient"))
using (var module = new DependencyTrackingTelemetryModule())
{
module.Initialize(this.configuration);

var exception = new InvalidOperationException();
Activity sendActivity = new Activity("Azure.SomeClient.Method")
.AddTag("peer.address", "amqps://my.servicebus.windows.net")
.AddTag("message_bus.destination", "queueName")
.AddTag("kind", kind)
.AddTag("az.namespace", "Microsoft.ServiceBus");

listener.StartActivity(sendActivity, null);
listener.Write("Azure.SomeClient.Send.Exception", exception);
listener.StopActivity(sendActivity, null);

var telemetry = this.sentItems.Last();

Assert.IsNotNull(telemetry);
Assert.IsNull(telemetry.Context.Operation.ParentId);
Assert.AreEqual(sendActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);

OperationTelemetry operation = telemetry as OperationTelemetry;
Assert.IsFalse(operation.Success.Value);
Assert.AreEqual(exception.ToInvariantString(), operation.Properties["Error"]);
Assert.AreEqual(sendActivity.SpanId.ToHexString(), operation.Id);
Assert.AreEqual("SomeClient.Method", operation.Name);

if (kind == "producer" || kind == "client" || kind == "internal")
{
Assert.IsTrue(telemetry is DependencyTelemetry);
DependencyTelemetry dependency = telemetry as DependencyTelemetry;
Assert.AreEqual(string.Empty, dependency.Data);
Assert.AreEqual(string.Empty, dependency.ResultCode);
Assert.AreEqual("amqps://my.servicebus.windows.net/queueName", dependency.Target);
if (kind == "producer")
{
Assert.AreEqual("Queue Message | Azure Service Bus", dependency.Type);
}
else
{
Assert.AreEqual("Azure Service Bus", dependency.Type);
}
}
else
{
Assert.IsTrue(telemetry is RequestTelemetry);
RequestTelemetry request = telemetry as RequestTelemetry;
Assert.AreEqual(string.Empty, request.ResponseCode);
Assert.AreEqual("amqps://my.servicebus.windows.net/queueName", request.Source);
}
}
}


private T TrackOperation<T>(
DiagnosticListener listener,
string activityName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public override void OnEvent(KeyValuePair<string, object> evnt, DiagnosticListen
telemetry = new DependencyTelemetry { Type = type };
}

if (type != null && type.EndsWith(RemoteDependencyConstants.AzureEventHubs, StringComparison.Ordinal))
if (IsMessagingDependency(type))
{
SetEventHubsProperties(currentActivity, telemetry);
SetMessagingProperties(currentActivity, telemetry);
}

if (this.linksPropertyFetcher.Fetch(evnt.Value) is IEnumerable<Activity> activityLinks)
Expand Down Expand Up @@ -225,6 +225,13 @@ private static string GetType(Activity currentActivity)
break;

case "component":
// old tag populated for back-compat, if az.namespace is set - ignore it.
if (component == null)
{
component = tag.Value;
}

break;
case "az.namespace":
component = tag.Value;
break;
Expand All @@ -233,9 +240,11 @@ private static string GetType(Activity currentActivity)

if (component == "eventhubs" || component == "Microsoft.EventHub")
{
return kind == null
? RemoteDependencyConstants.AzureEventHubs
: string.Concat(kind, " | ", RemoteDependencyConstants.AzureEventHubs);
component = RemoteDependencyConstants.AzureEventHubs;
}
else if (component == "Microsoft.ServiceBus")
{
component = RemoteDependencyConstants.AzureServiceBus;
}

if (component != null)
Expand Down Expand Up @@ -310,10 +319,16 @@ private static void SetHttpProperties(Activity activity, DependencyTelemetry dep
}
}

private static void SetEventHubsProperties(Activity activity, OperationTelemetry telemetry)
private static bool IsMessagingDependency(string dependencyType)
{
return dependencyType != null && (dependencyType.EndsWith(RemoteDependencyConstants.AzureEventHubs, StringComparison.Ordinal) ||
dependencyType.EndsWith(RemoteDependencyConstants.AzureServiceBus, StringComparison.Ordinal));
}

private static void SetMessagingProperties(Activity activity, OperationTelemetry telemetry)
{
string endpoint = null;
string queueName = null;
string entityName = null;

foreach (var tag in activity.Tags)
{
Expand All @@ -323,32 +338,32 @@ private static void SetEventHubsProperties(Activity activity, OperationTelemetry
}
else if (tag.Key == "message_bus.destination")
{
queueName = tag.Value;
entityName = tag.Value;
}
}

if (endpoint == null || queueName == null)
if (endpoint == null || entityName == null)
{
return;
}

// Target uniquely identifies the resource, we use both: queueName and endpoint
// Target uniquely identifies the resource, we use both: entityName and endpoint
// with schema used for SQL-dependencies
string separator = "/";
if (endpoint.EndsWith(separator, StringComparison.Ordinal))
{
separator = string.Empty;
}

string eventHubInfo = string.Concat(endpoint, separator, queueName);
string brokerInfo = string.Concat(endpoint, separator, entityName);

if (telemetry is DependencyTelemetry dependency)
{
dependency.Target = eventHubInfo;
dependency.Target = brokerInfo;
}
else if (telemetry is RequestTelemetry request)
{
request.Source = eventHubInfo;
request.Source = brokerInfo;
}
}

Expand Down