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

Added distributed tracing instrumentation #201

Merged
merged 7 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Executors;
Expand All @@ -20,6 +21,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.RabbitMQ
{
internal sealed class RabbitMQListener : IListener, IScaleMonitor<RabbitMQTriggerMetrics>
{
#pragma warning disable SA1000
private static readonly ActivitySource Source = new("Microsoft.Azure.WebJobs.Extensions.RabbitMQ");
t-manyadav marked this conversation as resolved.
Show resolved Hide resolved
#pragma warning restore SA1000
private readonly ITriggeredFunctionExecutor executor;
private readonly string queueName;
private readonly ushort prefetchCount;
Expand Down Expand Up @@ -82,6 +86,8 @@ public Task StartAsync(CancellationToken cancellationToken)

this.consumer.Received += async (model, ea) =>
{
Activity activity = StartActivity(ea); // create activity object

t-manyadav marked this conversation as resolved.
Show resolved Hide resolved
var input = new TriggeredFunctionData() { TriggerValue = ea };
t-manyadav marked this conversation as resolved.
Show resolved Hide resolved
FunctionResult result = await this.executor.TryExecuteAsync(input, cancellationToken).ConfigureAwait(false);

Expand All @@ -100,6 +106,8 @@ public Task StartAsync(CancellationToken cancellationToken)
this.RepublishMessages(ea);
}
}

activity?.Stop();
};

this.consumerTag = this.rabbitMQModel.BasicConsume(queue: this.queueName, autoAck: false, consumer: this.consumer);
Expand Down Expand Up @@ -152,6 +160,32 @@ public ScaleStatus GetScaleStatus(ScaleStatusContext<RabbitMQTriggerMetrics> con
return this.GetScaleStatusCore(context.WorkerCount, context.Metrics?.ToArray());
}

internal static Activity StartActivity(BasicDeliverEventArgs ea)
{
Activity activity;
if (ea.BasicProperties.Headers != null && ea.BasicProperties.Headers.ContainsKey("traceparent"))
{
// check if traceId present in header
byte[] traceParentIdInBytes = ea.BasicProperties.Headers["traceparent"] as byte[];
string traceparentId = Encoding.Default.GetString(traceParentIdInBytes);
t-manyadav marked this conversation as resolved.
Show resolved Hide resolved
activity = Source.StartActivity("RabbitMQ.function.trigger", ActivityKind.Consumer, traceparentId);
t-manyadav marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
// create new traceId, StartActivity will create and start new activity
activity = Source.StartActivity("RabbitMQ.function.trigger", ActivityKind.Server);
if (ea.BasicProperties.Headers == null)
t-manyadav marked this conversation as resolved.
Show resolved Hide resolved
{
ea.BasicProperties.Headers = new Dictionary<string, object>();
}

byte[] traceParentIdInBytes = Encoding.Default.GetBytes(activity?.Id);
t-manyadav marked this conversation as resolved.
Show resolved Hide resolved
ea.BasicProperties.Headers["traceparent"] = traceParentIdInBytes; // add trace-id to header
}

return activity;
}

internal void CreateHeadersAndRepublish(BasicDeliverEventArgs ea)
{
if (ea.BasicProperties.Headers == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="6.0.0" />
<PackageReference Include="System.Json" Version="4.7.1" />
</ItemGroup>

Expand Down