Ilan
Ilan

Reputation: 1

Azure Functions and Opentelemetry

I am trying to get OpenTelemetry working on Azure Functions (Python).

When the library is installed and used, the functions time out at 5 minutes (azure-monitor-opentelemetry==1.1.1).

The functions contain AsgiMiddleware to handle routing, etc.

I suspect that it is linked to this issue: https://github.com/Azure/azure-functions-host/issues/1626.

Is there something I am missing?

I want to see traces in AppInsights. Tried all open telemetry libraries. The function app just times out with 504.

from api.routes.main import app
import azure.functions as func
import os
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.context import attach, detach
from opentelemetry.trace.propagation.tracecontext import \
  TraceContextTextMapPropagator


app_insights_connection = os.getenv('APPLICATIONINSIGHTS_CONNECTION_STRING')

configure_azure_monitor(
    connection_string=app_insights_connection
)

tracer = trace.get_tracer('events')

async def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    functions_current_context = {
        "traceparent": context.trace_context.Traceparent,
        "tracestate": context.trace_context.Tracestate
    }
    parent_context = TraceContextTextMapPropagator().extract(
        carrier=functions_current_context
    )
    token = attach(parent_context)
    response = await func.AsgiMiddleware(app).handle_async(req)
    detach(token)
    return response

Upvotes: 0

Views: 808

Answers (1)

Vivek Vaibhav Shandilya
Vivek Vaibhav Shandilya

Reputation: 2646

For Opentelemetry reference I used document which I suggested in comment.

This worked for me. #Mycode: __init__.py:

from opentelemetry import trace
import os
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
from asgi_tools import App
import azure.functions as func


trace.set_tracer_provider(TracerProvider())
exporter = AzureMonitorTraceExporter(
    connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(exporter))

app = App()

@app.route('/api/HttpTrigger1', methods=['GET', 'POST'])
async def http_trigger_1(request):
    with trace.get_tracer(__name__).start_as_current_span("http-trigger-1-endpoint"):
        return "Hello from HttpTrigger1!"
    
azure_function_app = func.AsgiMiddleware(app).handle_async

async def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    TracerProvider().force_flush()
    return await azure_function_app(req)

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "APPLICATIONINSIGHTS_CONNECTION_STRING":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  }
}

requirements.txt

azure-functions
opentelemetry-api
opentelemetry-sdk
azure-monitor-opentelemetry
asgi_tools

OUTPUT:

enter image description here

enter image description here

enter image description here

Upvotes: 0

Related Questions