Skip to content

Add support to send trace information to the WebJobs extension #540

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

Merged
merged 12 commits into from
May 1, 2025

Conversation

bachuv
Copy link
Contributor

@bachuv bachuv commented Apr 8, 2025

This PR introduces support for propagating trace information to the WebJobs extension by getting the traceparent and tracestate information in the start_new method by accessing the "current span" information. traceparent and tracestate are then forwarded to the post_async_request method, which now accepts trace_parent and trace_state parameters in http_utils.py to include them in the outgoing request. trace_parent and trace_state parameters are now set as the headers in the POST request to the WebJobs extension.

In the example below, I used the HTTP trigger's traceparent and tracestate as the parent context of a new span that I created. I then set that new span as the current span before calling start_new().

Note: I followed the Azure Function OpenTelemetry set up in this example which is also emitting spans for storage requests and that's why there are spans for storage operations.

import azure.functions as func
import azure.durable_functions as df
import opentelemetry.context as context_api

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.trace import SpanKind
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

configure_azure_monitor()
tracer = trace.get_tracer(__name__)

myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)

# An HTTP-triggered function with a Durable Functions client binding
@myApp.route(route="orchestrators/{functionName}")
@myApp.durable_client_input(client_name="client")
async def http_start(req: func.HttpRequest, client, context: func.Context):
    function_name = req.route_params.get('functionName')

    # Extract the traceparent and tracestate from the context
    parent = {
        "traceparent": context.trace_context.trace_parent,
        "tracestate": context.trace_context.trace_state
    }
    parent_context = TraceContextTextMapPropagator().extract(parent)

    # Create a new span with the extracted parent context
    with tracer.start_as_current_span(
            "myCustomSpan",
            context=parent_context,
            kind=SpanKind.CLIENT) as span:
        
        span.set_attribute("az.namespace", "app")

        instance_id = await client.start_new(function_name)
        response = client.create_check_status_response(req, instance_id)
    return response
image

@bachuv bachuv requested a review from davidmrdavid as a code owner April 8, 2025 21:19
@bachuv bachuv requested a review from cgillum as a code owner April 23, 2025 18:25
Copy link
Collaborator

@andystaples andystaples left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments

Copy link
Member

@cgillum cgillum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with @andystaples feedback. Otherwise, I'm good with these changes.

@bachuv bachuv requested a review from nytian as a code owner April 25, 2025 23:11
Copy link
Collaborator

@andystaples andystaples left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@bachuv bachuv merged commit f655a1a into dev May 1, 2025
2 checks passed
@bachuv bachuv deleted the vabachu/add-trace-info branch May 1, 2025 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants