Skip to content

Commit

Permalink
Fix Azure SDK activity sources, remove custom worker activity from sa…
Browse files Browse the repository at this point in the history
…mples, fix nits in docs,
  • Loading branch information
lmolkova committed Oct 9, 2023
1 parent ad970d7 commit 3c09093
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ return await app.RunAsync();
2. Add the following user-secret to the MyApp orchestrator project (using the unique namespace you created above):
```shell
C:\git\aspire\samples\eShopLite\AppHost> dotnet user-secrets set Aspire.Azure.Messaging.ServiceBus:Namespace <ServiceBus namespace host>
C:\git\aspire\samples\eShopLite\AppHost> dotnet user-secrets set Aspire:Azure:Messaging:ServiceBus:Namespace <ServiceBus namespace host>
```
- You can do the same in VS by right-clicking AppHost in the Solution Explorer -> "Manage User Secrets" and add
Expand All @@ -103,7 +103,7 @@ C:\git\aspire\samples\eShopLite\AppHost> dotnet user-secrets set Aspire.Azure.Me
}
```
- The `<ServiceBus namespace host>` is labeled in the portal UI as "Host name" and looks similar to "yournamespacename.servicebus.windows.net"
- The `<ServiceBus namespace host>` is labeled in the portal UI as "Host name". Include host name only and omit "servicebus.windows.net".
## Load the Sample Application
Expand Down
43 changes: 18 additions & 25 deletions samples/eShopLite/OrderProcessor/OrderProcessingWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ namespace OrderProcessor;

public class OrderProcessingWorker : BackgroundService
{
public static readonly string ActivitySourceName = "Worker";

public static ActivitySource ActivitySource { get; } = new ActivitySource(ActivitySourceName);

private readonly ILogger<OrderProcessingWorker> _logger;
private readonly IConfiguration _config;
private readonly ServiceBusClient? _client;
Expand Down Expand Up @@ -53,32 +49,29 @@ public override async Task StopAsync(CancellationToken cancellationToken)

private Task ProcessMessageAsync(ProcessMessageEventArgs args)
{
using (var activity = ActivitySource.StartActivity("order-processor.worker"))
{
_logger.LogInformation($"Processing Order at: {DateTime.UtcNow}");
_logger.LogInformation($"Processing Order at: {DateTime.UtcNow}");

var message = args.Message;
var message = args.Message;

if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("""
MessageId:{MessageId}
MessageBody:{Body}
""", message.MessageId, message.Body);
}
var order = message.Body.ToObjectFromJson<Order>();
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("""
MessageId:{MessageId}
MessageBody:{Body}
""", message.MessageId, message.Body);
}
var order = message.Body.ToObjectFromJson<Order>();

activity?.AddTag("order-id", order.Id);
activity?.AddTag("product-count", order.Items.Count);
Activity.Current?.AddTag("order-id", order.Id);
Activity.Current?.AddTag("product-count", order.Items.Count);

_logger.LogInformation("""
OrderId:{Id}
BuyerId:{BuyerId}
ProductCount:{Count}
""", order.Id, order.BuyerId, order.Items.Count);
_logger.LogInformation("""
OrderId:{Id}
BuyerId:{BuyerId}
ProductCount:{Count}
""", order.Id, order.BuyerId, order.Items.Count);

return Task.CompletedTask;
}
return Task.CompletedTask;
}

private Task ProcessErrorAsync(ProcessErrorEventArgs arg)
Expand Down
4 changes: 1 addition & 3 deletions samples/eShopLite/OrderProcessor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
}

builder.Services.AddHostedService<OrderProcessingWorker>();
// ensure the OrderProcessingWorker's Activities participate in tracing
builder.Services.AddOpenTelemetry()
.WithTracing(traceBuilder => traceBuilder.AddSource(OrderProcessingWorker.ActivitySourceName));
builder.Services.AddOpenTelemetry();

var host = builder.Build();
host.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public static void AddKeyedAzureServiceBus(

private sealed class MessageBusComponent : AzureComponent<AzureMessagingServiceBusSettings, ServiceBusClient, ServiceBusClientOptions>
{
protected override string[] ActivitySourceNames => ["Azure.Messaging.ServiceBus", "Azure.Messaging.ServiceBus.ServiceBusReceiver", "Azure.Messaging.ServiceBus.ServiceBusSender", "Azure.Messaging.ServiceBus.ServiceBusProcessor"];
// TODO: Remove "Azure.Messaging.ServiceBus" method when https://github.com/Azure/azure-sdk-for-net/issues/39166 is fixed
protected override string[] ActivitySourceNames => ["Azure.Messaging.ServiceBus.*", "Azure.Messaging.ServiceBus"];

protected override IAzureClientBuilder<ServiceBusClient, ServiceBusClientOptions> AddClient<TBuilder>(TBuilder azureFactoryBuilder, AzureMessagingServiceBusSettings settings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void AddKeyedAzureBlobService(

private sealed class BlobStorageComponent : AzureComponent<AzureStorageBlobsSettings, BlobServiceClient, BlobClientOptions>
{
protected override string[] ActivitySourceNames => ["Azure.Storage.Blobs.BlobContainerClient"];
protected override string[] ActivitySourceNames => ["Azure.Storage.Blobs.*"];

protected override IAzureClientBuilder<BlobServiceClient, BlobClientOptions> AddClient<TBuilder>(TBuilder azureFactoryBuilder, AzureStorageBlobsSettings settings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void AddKeyedAzureQueueService(

private sealed class StorageQueueComponent : AzureComponent<AzureStorageQueuesSettings, QueueServiceClient, QueueClientOptions>
{
protected override string[] ActivitySourceNames => ["Azure.Storage.Queues.QueueClient"];
protected override string[] ActivitySourceNames => ["Azure.Storage.Queues.*"];

protected override IAzureClientBuilder<QueueServiceClient, QueueClientOptions> AddClient<TBuilder>(TBuilder azureFactoryBuilder, AzureStorageQueuesSettings settings)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Common/AzureComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal abstract class AzureComponent<TSettings, TClient, TClientOptions>
where TClient : class
where TClientOptions : class
{
protected virtual string[] ActivitySourceNames => new[] { $"{typeof(TClient).Namespace}.{typeof(TClient).Name}" };
protected virtual string[] ActivitySourceNames => new[] { $"{typeof(TClient).Namespace}.*" };

// There would be no need for Get* methods if TSettings had a common base type or if it was implementing a shared interface.
// TSettings is a public type and we don't have a shared package yet, but we may reconsider the approach in near future.
Expand Down

0 comments on commit 3c09093

Please sign in to comment.