forked from mrpmorris/Morris.Azure.Functions.OpenTelemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cron and Servicebus Trigger Handlers, Fix some issues with the Http Trigger Handler, and downgrade the azure function packages as there's an issue with current versions around the Opentelemetry context (see Azure/azure-functions-dotnet-worker#2543)
- Loading branch information
1 parent
075fa25
commit 5e213fa
Showing
6 changed files
with
151 additions
and
46 deletions.
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
21 changes: 21 additions & 0 deletions
21
Source/Morris.Azure.Functions.OpenTelemetry/CronTriggerHandler.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,21 @@ | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Middleware; | ||
using OpenTelemetry.Trace; | ||
using System.Diagnostics; | ||
|
||
namespace Morris.Azure.Functions.OpenTelemetry; | ||
|
||
public class CronTriggerHandler : ITriggerHandler | ||
{ | ||
public async Task<Activity?> HandleAsync(ActivitySource activitySource, TriggerParameterInfo triggerParameterInfo, FunctionContext context, FunctionExecutionDelegate next) | ||
{ | ||
string activityName = context.FunctionDefinition.Name; | ||
Activity? activity = activitySource.StartActivity(name: activityName, kind: ActivityKind.Server); | ||
|
||
var schedule = ((TimerTriggerAttribute)triggerParameterInfo.BindingAttribute).Schedule; | ||
activity?.SetTag(TraceSemanticConventions.AttributeFaasCron, schedule); | ||
|
||
await next(context); | ||
return activity; | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
Source/Morris.Azure.Functions.OpenTelemetry/ServiceBusTriggerHandler.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,62 @@ | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Middleware; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Context.Propagation; | ||
using OpenTelemetry.Trace; | ||
using System.Diagnostics; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Morris.Azure.Functions.OpenTelemetry; | ||
|
||
public class ServiceBusTriggerHandler : ITriggerHandler | ||
{ | ||
public async Task<Activity?> HandleAsync(ActivitySource activitySource, TriggerParameterInfo triggerParameterInfo, FunctionContext context, FunctionExecutionDelegate next) | ||
{ | ||
string? applicationPropertiesJson = context.BindingContext.BindingData["ApplicationProperties"]?.ToString(); | ||
ApplicationProperties? applicationProperties = null; | ||
|
||
if (applicationPropertiesJson != null) | ||
{ | ||
applicationProperties = JsonSerializer.Deserialize<ApplicationProperties>(applicationPropertiesJson); | ||
} | ||
|
||
ActivityContext currentActivityContext = Activity.Current?.Context ?? new ActivityContext(); | ||
var propagationContext = new PropagationContext(currentActivityContext, Baggage.Current); | ||
|
||
PropagationContext newPropagationContext = Propagators | ||
.DefaultTextMapPropagator | ||
.Extract(context: propagationContext, carrier: applicationProperties, getter: ExtractContextFromApplicationProperties); | ||
|
||
string activityName = context.FunctionDefinition.Name; | ||
Activity? activity = null; | ||
|
||
if (newPropagationContext.ActivityContext.TraceFlags == ActivityTraceFlags.Recorded) | ||
{ | ||
activity = activitySource.StartActivity(name: activityName, kind: ActivityKind.Server, newPropagationContext.ActivityContext); | ||
} | ||
else | ||
{ | ||
activity = activitySource.StartActivity(name: activityName, kind: ActivityKind.Server); | ||
} | ||
|
||
activity?.SetTag("messaging.system", "servicebus"); | ||
activity?.SetTag("messaging.operation.type", "receive"); | ||
activity?.SetTag("messaging.servicebus.message.enqueued_time", context.BindingContext.BindingData["EnqueuedTimeUtc"]?.ToString()); | ||
activity?.SetTag("messaging.message.id", context.BindingContext.BindingData["MessageId"]?.ToString()); | ||
activity?.SetTag("messaging.message.sequence_number", context.BindingContext.BindingData["SequenceNumber"]?.ToString()); | ||
activity?.SetTag("messaging.servicebus.message.delivery_count", context.BindingContext.BindingData["DeliveryCount"]?.ToString()); | ||
|
||
await next(context); | ||
return activity; | ||
} | ||
|
||
internal class ApplicationProperties() | ||
{ | ||
[JsonPropertyName("Diagnostic-Id")] | ||
public string? DiagnosticId { get; set; } | ||
} | ||
|
||
internal static IEnumerable<string> ExtractContextFromApplicationProperties(ApplicationProperties? applicationProperties, string key) => | ||
key == "traceparent" && applicationProperties?.DiagnosticId != null ? new List<string>() { applicationProperties.DiagnosticId } : []; | ||
} |