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

LogLevel configuration (appSettings.json) for OpenTelemetry provider not working on Functions app using HostBuilder without defaults #5702

Closed
Arash-Sabet opened this issue Jun 19, 2024 · 14 comments
Labels
question Further information is requested

Comments

@Arash-Sabet
Copy link

What is the question?

One would normally follow the following pattern to set the log level in their application:

"Logging": {
    "LogLevel": {
      "Default": "Error",
      "Microsoft.AspNetCore": "Error"
    }

Apparently OpenTelemetryLoggerProvider has OpenTelemetry alias and hence we must be able to configure it like the following entry but it's not working:

"OpenTelemetry": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.AspNetCore": "Warning"
      }
    }

Are we missing anything in the configuration?

Additional context

No response

@Arash-Sabet Arash-Sabet added the question Further information is requested label Jun 19, 2024
@cijothomas
Copy link
Member

Log filtering is completely part of ILogger infra, nothing to do with OpenTelemetry itself, as long as you got the alias "OpenTelemetry" correctly (which you have already using correctly!)

If you share a minimal repro app, we can help see if there is anything wrong with the setup.
"but it's not working" is not clear, could you elaborate, and share the repro app.

@Arash-Sabet
Copy link
Author

Thanks @cijothomas I just invited to a repo, Please simply replace the ApplicationInsights configuration with a proper one.

@cijothomas
Copy link
Member

@Arash-Sabet Please take a look at https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/logs/getting-started-aspnetcore and make incrementation changes to it till it reproduces the issue you are facing, and share that. Please use ConsoleExporter only to verify if logs are filtered or not. This will be easy for us to investigate and not

I won't have the bandwidth to investigate any custom projects that requires ApplicationInsights or anything not shipped from this repo.

@Arash-Sabet
Copy link
Author

@cijothomas Putting the Application Insights thing aside, the console does not produce logs consistent with the configuration in the appsettings.json config file.

@cijothomas
Copy link
Member

Yes I understand that, but i need to get a minimal repro to investigate.! Also, repeating my original comment below - most likely its an ILogger configuration issue, nothing specific to OTel. Happy to help, if you show a minimal repro app.

Log filtering is completely part of ILogger infra, nothing to do with OpenTelemetry itself, as long as you got the alias "OpenTelemetry" correctly (which you have already using correctly!)

@Arash-Sabet
Copy link
Author

@cijothomas Thanks, I just minimized the repo and committed the code. I got rid of all application insights stuff. Please note that this is a dotnet-isolated function app with one Web API exposed to simply call it to view the output to the console.

@Arash-Sabet
Copy link
Author

@cijothomas just wondering, did you have a chance to run the minimal project I shared with you?

@cijothomas
Copy link
Member

Not yet. I also suggest to share the repro right here for anyone looking at this to see, as opposed to just me having access!
The repro should be 2-3 files at max - the .csproj, the program.cs and optionally one more file, if needed. See https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/logs/getting-started-console for an example you can start off, and modify till you repro.

(I don't have much experience with Functions specifics, so I won't be of much use, unless you modify the repro to be extremely minimal)

@Arash-Sabet
Copy link
Author

@cijothomas I made the repository public and everybody must have access to it now. It's found here: https://github.com/Arash-Sabet/FunctionAppOpenTelemetryPOC

(I don't have much experience with Functions specifics, so I won't be of much use, unless you modify the repro to be extremely minimal)

I cannot reduce the number of files in the function app as the project is already at its very minimum number of required files to run successfully. If I eliminate even one file, the function won't run. Simply open the solution file in VS 2022 and hit F5 to debug or Ctrl+F5 to run the application.

@CodeBlanch
Copy link
Member

CodeBlanch commented Jun 20, 2024

@Arash-Sabet

Did you nest your "OpenTelemetry" section under "Logging"? Here is an example:

https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/AspNetCore/appsettings.json#L1-L11

Something like this should work automatically if you are using a host:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    },
    "OpenTelemetry": {
      "LogLevel": {
         "Default": "Information"
      },
      "IncludeFormattedMessage": true,
      "IncludeScopes": true,
      "ParseStateValues": true
    }
  }
}

@Arash-Sabet
Copy link
Author

Arash-Sabet commented Jun 20, 2024

@CodeBlanch Thanks for the reply. I used the json payload you shared in the appsettings.json as is and it dot work. To be more specific,, changing OpenTelemetry.LogLevel.Default = Error did not filter out the information and warning messages. Have you tried this settings against the repo I shared?

@CodeBlanch
Copy link
Member

Something like this should work automatically if you are using a host:

So the issue is you are NOT really using a host. You are building a host manually. Seems like a quirk of functions bootstrap.

Here is where configuration is bound in a WebApplicationBuilder: https://github.com/dotnet/aspnetcore/blob/c40405fe48c999eb3fcb38416d5bdf4cb52ba012/src/DefaultBuilder/src/WebApplicationBuilder.cs#L316

Here is the generic host equivalent: https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L294

You have a couple options...

Bind configuration manually

-   .ConfigureServices(services =>
+   .ConfigureServices((context, services) =>
    {
+       services.AddLogging(logging => logging.AddConfiguration(context.Configuration.GetSection("Logging")));

        services
            .AddOpenTelemetry()
            .ConfigureResource(r => r.AddService("POC.Func.App"))
            .WithLogging(logging => logging.AddConsoleExporter());
    })

Enable host defaults

var host = new HostBuilder()
+   .ConfigureDefaults(args) // Calls into the code linked above to set up defaults including configuration binding
    .ConfigureFunctionsWebApplication()

Don't ask me what other side effects that will have 😄

@CodeBlanch CodeBlanch changed the title How to set log levels in Open Telemetry dotnet LogLevel configuration (appSettings.json) for OpenTelemetry provider not working on Functions app using HostBuilder without defaults Jun 20, 2024
@Arash-Sabet
Copy link
Author

Thanks @CodeBlanch I will keep you posted as soon as I try your recent approach.

@Arash-Sabet
Copy link
Author

@CodeBlanch @cijothomas The solution worked. Thanks for your support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants