question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using OpenTelemetry Sdk with Azure Functions

See original GitHub issue

For using OpenTelemetry Sdk with Azure Functions, AddOpenTelemetryTracing extension method cannot be used. This is because Azure Functions has restrictions on running background hosted services. The right way to configure it for Functions is below:

var openTelemetry = Sdk.CreateTracerProviderBuilder().
                AddSource("MyActivitySourceName")
                .SetSampler(new AlwaysOnSampler())
                .AddConsoleExporter()
                .Build();
            builder.Services.AddSingleton(openTelemetry);

There is also the issue (https://github.com/Azure/azure-functions-host/issues/7135) of Azure Functions not supporting DiagnosticListener callbacks when using System.Diagnostics.DiagnosticSource (> 4.7.0). Therefore, OpenTelemetry instrumentations (like HttpClient, SqlClient etc.) will not work in Azure Functions as the instrumentations use System.Diagnostics.DiagnosticSource (= 5.0.1)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
valdisiljuconokscommented, Dec 2, 2021

Seems to be working. I’m using the following setup in functions (v4 runtime + in-process):

[assembly: FunctionsStartup(typeof(Startup))]

namespace FunctionApp1;

internal class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddOpenTelemetryTracing(b =>
        {
            b.AddHttpClientInstrumentation();
            b.AddSource("AzureFunctionsOpenTelemetry");
            b.AddOtlpExporter(options => options.Endpoint = new Uri("http://localhost:4317"));
        });

        builder.AddOpenTelemetry(b =>
        {
            b.IncludeFormattedMessage = true;
            b.IncludeScopes = true;
            b.ParseStateValues = true;
            b.AddOtlpExporter(options => options.Endpoint = new Uri("http://localhost:4317"));
        });
    }
}

public static class OpenTelemetryLoggingExtensions
{
    public static IFunctionsHostBuilder AddOpenTelemetry(
        this IFunctionsHostBuilder builder,
        Action<OpenTelemetryLoggerOptions> configure = null)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, OpenTelemetryLoggerProvider>());

        if (configure != null)
        {
            builder.Services.Configure(configure);
        }

        return builder;
    }
}
3reactions
josh-endriescommented, Aug 4, 2021

Just a note that using OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation with a .NET 5 isolated runtime Azure Function does seem to work, at least somewhat. If I set the trace ID in an ActivityContext and start my own Activity, then call HttpClient.GetAsync within the activity scope, that outgoing request contains the traceparent header that contains the trace ID.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enable Azure Monitor OpenTelemetry for .NET, Java, Node ...
This article provides guidance on how to enable Azure Monitor on applications by using OpenTelemetry.
Read more >
OpenTelemetry & correlation support for Azure Functions
I have a project written in .net 6. I use Azure Function to send data to Service bus and MediatR. How can I...
Read more >
Boost your Serverless Observability with OpenTelemetry ...
OpenTelemetry provides a powerful and flexible way to collect telemetry data from your applications, and Azure Monitor provides a comprehensive ...
Read more >
Set up OpenTelemetry monitoring for Azure Functions on ...
Dynatrace uses OpenTelemetry to monitor Azure Functions invocations. For that purpose, Dynatrace provides language-specific packages, such as Dynatrace.
Read more >
OpenTelemetry Support for Azure Functions (Isolated ...
Currently, OpenTelemetry is supported on the following SDKs: NodeJS · NextJS · Python · Ruby · Java .NET · Go. You can configure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found