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

Using OpenTelemetry Sdk with Azure Functions #1803

Closed
utpilla opened this issue Feb 6, 2021 · 11 comments
Closed

Using OpenTelemetry Sdk with Azure Functions #1803

utpilla opened this issue Feb 6, 2021 · 11 comments
Labels
documentation Documentation related

Comments

@utpilla
Copy link
Contributor

utpilla commented Feb 6, 2021

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 (Azure/azure-functions-host#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)

@utpilla utpilla added the enhancement New feature or request label Feb 6, 2021
@cijothomas cijothomas added documentation Documentation related and removed enhancement New feature or request labels Feb 6, 2021
@oising
Copy link

oising commented Mar 16, 2021

Now that we have out of process net5 support, will this start working (since s.d.ds is part of net5?) -- @cijothomas

@cijothomas
Copy link
Member

Now that we have out of process net5 support, will this start working (since s.d.ds is part of net5?) -- @cijothomas

I am not sure if I understood your question. Could you elaborate?

@oising
Copy link

oising commented Mar 16, 2021

Now that we have out of process net5 support, will this start working (since s.d.ds is part of net5?) -- @cijothomas

I am not sure if I understood your question. Could you elaborate?

I guess it's more about this: Azure/azure-functions-host#7135 -- but since you're here :)

The functions v2 runtime is in-process, and as such, we're stuck with using system.diagnostics.diagnosticsource 4.7, ergo OTEL instrumentation libraries for HttpClient, SqlClient etc do not work. Now that net 5.0 is out of process, and we provide the runtime ourselves (right?) does this mean that we can use system.diagnostics.diagnosticsource 5.x and OTEL will work correctly in a functions v3 context?

@cijothomas
Copy link
Member

I cannot comment on whether Functions V3 (or V2) addresses this. The owners of Azure Functions has to make that clarification. I'll still check with Functions team and post back in this thread if I hear anything.

@josh-endries
Copy link

josh-endries commented 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.

@valdisiljuconoks
Copy link

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;
    }
}

@smmaster
Copy link

can someone please provide a full working example of an azure function ?

@valdisiljuconoks
Copy link

can someone please provide a full working example of an azure function ?

This is repo with my experiments so far. Tried to model some "distributed" system in field of mine: e-commerce. https://github.com/valdisiljuconoks/otel-experiment/tree/master/application/Commerce.Batch

@smmaster
Copy link

can someone please provide a full working example of an azure function ?

This is repo with my experiments so far. Tried to model some "distributed" system in field of mine: e-commerce. https://github.com/valdisiljuconoks/otel-experiment/tree/master/application/Commerce.Batch

Can't access the repo, is it public?

@cijothomas
Copy link
Member

https://github.com/cijothomas/FunctionsOpenTelemetry/blob/master/FunctionsOpenTelemetry/Startup.cs - Note that this is not approved or reviewed by Functions experts, so not sure if any Functions specific things are required. It does work in my testing.

@agr
Copy link

agr commented May 4, 2023

Is it supposed to work locally with function runner from Azure function core tools? Mine fails unable to find System.Diagnostics.DiagnosticSource assembly whenever the code touches any OpenTelemetry object.
Disregard. I'm having some build issues that prevent the DLL to end up in proper location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation related
Projects
None yet
Development

No branches or pull requests

7 participants