Skip to content

Commit

Permalink
Extract service bus related values out to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
russcam committed Mar 24, 2021
1 parent 0c81080 commit b0de537
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AzureMessagingServiceBusDiagnosticListener(IApmAgent agent) : base(agent)
{
_realAgent = agent as ApmAgent;
_service = Service.GetDefaultService(agent.ConfigurationReader, agent.Logger);
_service.Framework = new Framework { Name = "AzureServiceBus" };
_service.Framework = new Framework { Name = ServiceBus.SegmentName };
}

protected override void HandleOnNext(KeyValuePair<string, object> kv)
Expand All @@ -48,36 +48,24 @@ protected override void HandleOnNext(KeyValuePair<string, object> kv)
case "ServiceBusSender.Send.Start":
OnSendStart(kv, "SEND");
break;
case "ServiceBusSender.Send.Stop":
OnStop();
break;
case "ServiceBusSender.Send.Exception":
OnException(kv);
break;
case "ServiceBusSender.Schedule.Start":
OnSendStart(kv, "SCHEDULE");
break;
case "ServiceBusSender.Schedule.Stop":
OnStop();
break;
case "ServiceBusSender.Schedule.Exception":
OnException(kv);
break;
case "ServiceBusReceiver.Receive.Start":
OnReceiveStart(kv, "RECEIVE");
break;
case "ServiceBusReceiver.Receive.Stop":
OnStop();
break;
case "ServiceBusReceiver.Receive.Exception":
OnException(kv);
break;
case "ServiceBusReceiver.ReceiveDeferred.Start":
OnReceiveStart(kv, "RECEIVEDEFERRED");
break;
case "ServiceBusSender.Send.Stop":
case "ServiceBusSender.Schedule.Stop":
case "ServiceBusReceiver.Receive.Stop":
case "ServiceBusReceiver.ReceiveDeferred.Stop":
OnStop();
break;
case "ServiceBusSender.Send.Exception":
case "ServiceBusSender.Schedule.Exception":
case "ServiceBusReceiver.Receive.Exception":
case "ServiceBusReceiver.ReceiveDeferred.Exception":
OnException(kv);
break;
Expand Down Expand Up @@ -112,14 +100,13 @@ private void OnReceiveStart(KeyValuePair<string, object> kv, string action)
return;

var transactionName = queueName is null
? $"AzureServiceBus {action}"
: $"AzureServiceBus {action} from {queueName}";
? $"{ServiceBus.SegmentName} {action}"
: $"{ServiceBus.SegmentName} {action} from {queueName}";

var transaction = ApmAgent.Tracer.StartTransaction(transactionName, "messaging");
var transaction = ApmAgent.Tracer.StartTransaction(transactionName, ServiceBus.Type);
transaction.Context.Service = _service;

// transaction creation will create an activity, so use this as the key.
// TODO: change when existing activity is used.
var activityId = Activity.Current.Id;

if (!_processingSegments.TryAdd(activityId, transaction))
Expand All @@ -128,7 +115,7 @@ private void OnReceiveStart(KeyValuePair<string, object> kv, string action)
"Could not add {Action} transaction {TransactionId} for activity {ActivityId} to tracked segments",
action,
transaction.Id,
activity.Id);
activityId);
}
}

Expand Down Expand Up @@ -186,18 +173,18 @@ private void OnSendStart(KeyValuePair<string, object> kv, string action)
return;

var spanName = queueName is null
? $"AzureServiceBus {action}"
: $"AzureServiceBus {action} to {queueName}";
? $"{ServiceBus.SegmentName} {action}"
: $"{ServiceBus.SegmentName} {action} to {queueName}";

var span = currentSegment.StartSpan(spanName, "messaging", "azureservicebus", action.ToLowerInvariant());
var span = currentSegment.StartSpan(spanName, ServiceBus.Type, ServiceBus.SubType, action.ToLowerInvariant());
span.Context.Destination = new Destination
{
Address = destinationAddress,
Service = new Destination.DestinationService
{
Name = "azureservicebus",
Resource = queueName is null ? "azureservicebus" : $"azureservicebus/{queueName}",
Type = "messaging"
Name = ServiceBus.SubType,
Resource = queueName is null ? ServiceBus.SubType : $"{ServiceBus.SubType}/{queueName}",
Type = ServiceBus.Type
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Elastic.Apm.Azure.ServiceBus.Tests" Key="$(ExposedPublicKey)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Elastic.Apm\Elastic.Apm.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MicrosoftAzureServiceBusDiagnosticListener(IApmAgent agent) : base(agent)
{
_realAgent = agent as ApmAgent;
_service = Service.GetDefaultService(agent.ConfigurationReader, agent.Logger);
_service.Framework = new Framework { Name = "AzureServiceBus" };
_service.Framework = new Framework { Name = ServiceBus.SegmentName };
}

protected override void HandleOnNext(KeyValuePair<string, object> kv)
Expand Down Expand Up @@ -92,21 +92,18 @@ private void OnReceiveStart(KeyValuePair<string, object> kv, string action, Prop
return;
}

var activity = Activity.Current;
var queueName = cachedProperties.Fetch(kv.Value,"Entity") as string;

if (MatchesIgnoreMessageQueues(queueName))
return;

var transactionName = queueName is null
? $"AzureServiceBus {action}"
: $"AzureServiceBus {action} from {queueName}";
? $"{ServiceBus.SegmentName} {action}"
: $"{ServiceBus.SegmentName} {action} from {queueName}";

var transaction = ApmAgent.Tracer.StartTransaction(transactionName, "messaging");
var transaction = ApmAgent.Tracer.StartTransaction(transactionName, ServiceBus.Type);
transaction.Context.Service = _service;

// transaction creation will create an activity, so use this as the key.
// TODO: change when existing activity is used.
var activityId = Activity.Current.Id;

if (!_processingSegments.TryAdd(activityId, transaction))
Expand All @@ -115,7 +112,7 @@ private void OnReceiveStart(KeyValuePair<string, object> kv, string action, Prop
"Could not add {Action} transaction {TransactionId} for activity {ActivityId} to tracked segments",
action,
transaction.Id,
activity.Id);
activityId);
}
}

Expand Down Expand Up @@ -160,19 +157,19 @@ private void OnSendStart(KeyValuePair<string, object> kv, string action, Propert
return;

var spanName = queueName is null
? $"AzureServiceBus {action}"
: $"AzureServiceBus {action} to {queueName}";
? $"{ServiceBus.SegmentName} {action}"
: $"{ServiceBus.SegmentName} {action} to {queueName}";

var span = currentSegment.StartSpan(spanName, "messaging", "azureservicebus", action.ToLowerInvariant());
var span = currentSegment.StartSpan(spanName, ServiceBus.Type, ServiceBus.SubType, action.ToLowerInvariant());

span.Context.Destination = new Destination
{
Address = destinationAddress?.AbsoluteUri,
Service = new Destination.DestinationService
{
Name = "azureservicebus",
Resource = queueName is null ? "azureservicebus" : $"azureservicebus/{queueName}",
Type = "messaging"
Name = ServiceBus.SubType,
Resource = queueName is null ? ServiceBus.SubType : $"{ServiceBus.SubType}/{queueName}",
Type = ServiceBus.Type
}
};

Expand Down
14 changes: 14 additions & 0 deletions src/Elastic.Apm.Azure.ServiceBus/ServiceBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to Elasticsearch B.V under
// one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

namespace Elastic.Apm.Azure.ServiceBus
{
internal static class ServiceBus
{
public const string SegmentName = "AzureServiceBus";
public const string SubType = "azureservicebus";
public const string Type = "messaging";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ await _agent.Tracer.CaptureTransaction("Send AzureServiceBus Message", "message"
_sender.Spans.Should().HaveCount(1);
var span = _sender.FirstSpan;

span.Name.Should().Be($"AzureServiceBus SEND to {scope.QueueName}");
span.Type.Should().Be("messaging");
span.Subtype.Should().Be("azureservicebus");
span.Name.Should().Be($"{ServiceBus.SegmentName} SEND to {scope.QueueName}");
span.Type.Should().Be(ServiceBus.Type);
span.Subtype.Should().Be(ServiceBus.SubType);
span.Action.Should().Be("send");
span.Context.Destination.Should().NotBeNull();
var destination = span.Context.Destination;

destination.Address.Should().Be(_environment.ServiceBusConnectionStringProperties.FullyQualifiedNamespace);
destination.Service.Name.Should().Be("azureservicebus");
destination.Service.Resource.Should().Be($"azureservicebus/{scope.QueueName}");
destination.Service.Type.Should().Be("messaging");
destination.Service.Name.Should().Be(ServiceBus.SubType);
destination.Service.Resource.Should().Be($"{ServiceBus.SubType}/{scope.QueueName}");
destination.Service.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand All @@ -79,17 +79,17 @@ await _agent.Tracer.CaptureTransaction("Send AzureServiceBus Message", "message"
_sender.Spans.Should().HaveCount(1);
var span = _sender.FirstSpan;

span.Name.Should().Be($"AzureServiceBus SEND to {scope.TopicName}");
span.Type.Should().Be("messaging");
span.Subtype.Should().Be("azureservicebus");
span.Name.Should().Be($"{ServiceBus.SegmentName} SEND to {scope.TopicName}");
span.Type.Should().Be(ServiceBus.Type);
span.Subtype.Should().Be(ServiceBus.SubType);
span.Action.Should().Be("send");
span.Context.Destination.Should().NotBeNull();
var destination = span.Context.Destination;

destination.Address.Should().Be(_environment.ServiceBusConnectionStringProperties.FullyQualifiedNamespace);
destination.Service.Name.Should().Be("azureservicebus");
destination.Service.Resource.Should().Be($"azureservicebus/{scope.TopicName}");
destination.Service.Type.Should().Be("messaging");
destination.Service.Name.Should().Be(ServiceBus.SubType);
destination.Service.Resource.Should().Be($"{ServiceBus.SubType}/{scope.TopicName}");
destination.Service.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand All @@ -110,17 +110,17 @@ await sender.ScheduleMessageAsync(
_sender.Spans.Should().HaveCount(1);
var span = _sender.FirstSpan;

span.Name.Should().Be($"AzureServiceBus SCHEDULE to {scope.QueueName}");
span.Type.Should().Be("messaging");
span.Subtype.Should().Be("azureservicebus");
span.Name.Should().Be($"{ServiceBus.SegmentName} SCHEDULE to {scope.QueueName}");
span.Type.Should().Be(ServiceBus.Type);
span.Subtype.Should().Be(ServiceBus.SubType);
span.Action.Should().Be("schedule");
span.Context.Destination.Should().NotBeNull();
var destination = span.Context.Destination;

destination.Address.Should().Be(_environment.ServiceBusConnectionStringProperties.FullyQualifiedNamespace);
destination.Service.Name.Should().Be("azureservicebus");
destination.Service.Resource.Should().Be($"azureservicebus/{scope.QueueName}");
destination.Service.Type.Should().Be("messaging");
destination.Service.Name.Should().Be(ServiceBus.SubType);
destination.Service.Resource.Should().Be($"{ServiceBus.SubType}/{scope.QueueName}");
destination.Service.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand All @@ -141,17 +141,17 @@ await sender.ScheduleMessageAsync(
_sender.Spans.Should().HaveCount(1);
var span = _sender.FirstSpan;

span.Name.Should().Be($"AzureServiceBus SCHEDULE to {scope.TopicName}");
span.Type.Should().Be("messaging");
span.Subtype.Should().Be("azureservicebus");
span.Name.Should().Be($"{ServiceBus.SegmentName} SCHEDULE to {scope.TopicName}");
span.Type.Should().Be(ServiceBus.Type);
span.Subtype.Should().Be(ServiceBus.SubType);
span.Action.Should().Be("schedule");
span.Context.Destination.Should().NotBeNull();
var destination = span.Context.Destination;

destination.Address.Should().Be(_environment.ServiceBusConnectionStringProperties.FullyQualifiedNamespace);
destination.Service.Name.Should().Be("azureservicebus");
destination.Service.Resource.Should().Be($"azureservicebus/{scope.TopicName}");
destination.Service.Type.Should().Be("messaging");
destination.Service.Name.Should().Be(ServiceBus.SubType);
destination.Service.Resource.Should().Be($"{ServiceBus.SubType}/{scope.TopicName}");
destination.Service.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand All @@ -172,8 +172,8 @@ await sender.SendMessageAsync(
_sender.Transactions.Should().HaveCount(1);
var transaction = _sender.FirstTransaction;

transaction.Name.Should().Be($"AzureServiceBus RECEIVE from {scope.QueueName}");
transaction.Type.Should().Be("messaging");
transaction.Name.Should().Be($"{ServiceBus.SegmentName} RECEIVE from {scope.QueueName}");
transaction.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand All @@ -195,8 +195,8 @@ await sender.SendMessageAsync(
_sender.Transactions.Should().HaveCount(1);
var transaction = _sender.FirstTransaction;

transaction.Name.Should().Be($"AzureServiceBus RECEIVE from {scope.TopicName}/Subscriptions/{scope.SubscriptionName}");
transaction.Type.Should().Be("messaging");
transaction.Name.Should().Be($"{ServiceBus.SegmentName} RECEIVE from {scope.TopicName}/Subscriptions/{scope.SubscriptionName}");
transaction.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand All @@ -222,12 +222,12 @@ await sender.SendMessageAsync(
_sender.Transactions.Should().HaveCount(2);

var transaction = _sender.FirstTransaction;
transaction.Name.Should().Be($"AzureServiceBus RECEIVE from {scope.QueueName}");
transaction.Type.Should().Be("messaging");
transaction.Name.Should().Be($"{ServiceBus.SegmentName} RECEIVE from {scope.QueueName}");
transaction.Type.Should().Be(ServiceBus.Type);

var secondTransaction = _sender.Transactions[1];
secondTransaction.Name.Should().Be($"AzureServiceBus RECEIVEDEFERRED from {scope.QueueName}");
secondTransaction.Type.Should().Be("messaging");
secondTransaction.Name.Should().Be($"{ServiceBus.SegmentName} RECEIVEDEFERRED from {scope.QueueName}");
secondTransaction.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand All @@ -252,12 +252,12 @@ await sender.SendMessageAsync(
_sender.Transactions.Should().HaveCount(2);

var transaction = _sender.FirstTransaction;
transaction.Name.Should().Be($"AzureServiceBus RECEIVE from {scope.TopicName}/Subscriptions/{scope.SubscriptionName}");
transaction.Type.Should().Be("messaging");
transaction.Name.Should().Be($"{ServiceBus.SegmentName} RECEIVE from {scope.TopicName}/Subscriptions/{scope.SubscriptionName}");
transaction.Type.Should().Be(ServiceBus.Type);

var secondTransaction = _sender.Transactions[1];
secondTransaction.Name.Should().Be($"AzureServiceBus RECEIVEDEFERRED from {scope.TopicName}/Subscriptions/{scope.SubscriptionName}");
secondTransaction.Type.Should().Be("messaging");
secondTransaction.Name.Should().Be($"{ServiceBus.SegmentName} RECEIVEDEFERRED from {scope.TopicName}/Subscriptions/{scope.SubscriptionName}");
secondTransaction.Type.Should().Be(ServiceBus.Type);
}

[AzureCredentialsFact]
Expand Down
Loading

0 comments on commit b0de537

Please sign in to comment.