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

[FEATURE REQ] Support e2e tracing for CloudEvents in Microsoft.Azure.WebJobs.Extensions.EventGrid #25722

Closed
lmolkova opened this issue Dec 6, 2021 · 1 comment
Labels
Client This issue points to a problem in the data-plane of the library. Functions Monitor - Exporter Monitor OpenTelemetry Exporter Monitor Monitor, Monitor Ingestion, Monitor Query
Milestone

Comments

@lmolkova
Copy link
Member

lmolkova commented Dec 6, 2021

Library name

Microsoft.Azure.WebJobs.Extensions.EventGrid

Please describe the feature.

EventGrid client SDKs populate distributed tracing extension with trace context. To leverage it and enable proper e2e tracing in Functions and WebJobs, the extension should read this context and continue the trace

Below is the code users need to write now (specific to Application Insights, but Microsoft.Azure.WebJobs.Extensions.EventGrid can use System.Diagnostics.Activity only and Functions already do the trick with Application Insights).

For IAsyncCollector<CloudEvent> overload, it gets tricky and we'd have to use ActivitySource to properly populate links.
Users would likely want to be able to extract context for individual events, it's tracked here Azure/azure-sdk#3319

private readonly TelemetryClient telemetryClient;
public Function1(TelemetryClient telemetryClient)
{
    this.telemetryClient = telemetryClient;
}

[FunctionName("Function1")]
public void Run([EventGridTrigger]CloudEvent evnt, ILogger log)
{
    using (var processingOperation = telemetryClient.StartOperation<RequestTelemetry>(ExtractActivity(evnt)))
    {
        // do something
        log.LogInformation("Processing  CloudEvent... {event}", evnt.Data.ToString());
    }
}

private static Activity ExtractActivity(CloudEvent evnt)
{
    // read distributed tracing extension
    // https://github.com/cloudevents/spec/blob/v1.0.1/extensions/distributed-tracing.md
    string traceparentStr = null;
    string tracestateStr = null;
    if (evnt.ExtensionAttributes.TryGetValue("traceparent", out var traceparent)
        && traceparent is string)
    {
        traceparentStr = (string) traceparent;
        if (evnt.ExtensionAttributes.TryGetValue("tracestate", out var tracestate)
            && tracestate is string)
        {
            tracestateStr = (string)tracestate;
        }

    }

    return ExtractActivity("CloudEvents Process " + evnt.Type, traceparentStr, tracestateStr);
}

private static Activity ExtractActivity(string activityName, string traceparent, string tracestate)
{
    var activity = new Activity(activityName);

    if (!string.IsNullOrEmpty(traceparent))
    {
        // parse traceparent according to https://www.w3.org/TR/trace-context/
        if (traceparent.StartsWith("00-") && traceparent.Length == 55)
        {
            // preserve function execution as parent and add link context from the event
            // assuming we do this inside the extension code, we should just make this context parent 
           // or use `ActivitySource` with proper links support if possible
            activity.AddTag("_MS.links",
                $"[{{\"operation_Id\":\"{traceparent.Substring(3, 32)}\",\"id\":\"{traceparent.Substring(36, 16)}\"}}]");
        }

        activity.TraceStateString = tracestate;
    }

    return activity;
}
@ghost ghost added the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Dec 6, 2021
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Dec 6, 2021
@jsquire jsquire added the Client This issue points to a problem in the data-plane of the library. label Dec 6, 2021
@jsquire jsquire added this to the Backlog milestone May 19, 2022
@JoshLove-msft
Copy link
Member

This was added in #25944

@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2023
@scottaddie scottaddie added Monitor Monitor, Monitor Ingestion, Monitor Query and removed Monitor - ApplicationInsights labels Sep 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. Functions Monitor - Exporter Monitor OpenTelemetry Exporter Monitor Monitor, Monitor Ingestion, Monitor Query
Projects
Development

No branches or pull requests

4 participants