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

[sdk-logs] Improve OpenTelemetryLogger.IsEnabled #5375

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

reyang
Copy link
Member

@reyang reyang commented Feb 21, 2024

This change should be almost transparent to the user, unless the user is relying on the side effect:

if (logger.IsEnabled(level))
{
    // code with side effect
}

With this change, it will be possible to save some CPU time if it is expensive to prepare the logging arguments:

if (logger.IsEnabled(level))
{
    var expensive = GetSomethingExpensive();
    logger.LogSomething(expensive);
}

The logic was originally written by me in #1329.

It was later refactored (with no change to the actual logic) https://github.com/open-telemetry/opentelemetry-dotnet/pull/1869/files#r588851416.

@reyang reyang marked this pull request as ready for review February 21, 2024 04:38
@reyang reyang requested a review from a team February 21, 2024 04:38
@@ -111,7 +110,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsEnabled(LogLevel logLevel)
{
return logLevel != LogLevel.None;
return logLevel != LogLevel.None && !Sdk.SuppressInstrumentation;
Copy link
Member

Choose a reason for hiding this comment

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

it was never clear to me why the LogLevel.None check was needed.. If the LogLevel.None is used, the ILogger infra should not be even sending the log to this provider at all (maybe thats just my interpretation!)

Copy link
Member

Choose a reason for hiding this comment

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

@cijothomas

It does appear somewhat standard: https://github.com/dotnet/runtime/blob/703f6e846027681a052970929989bd36e81636cd/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs#L73-L76

But you are correct the LoggerFactory also does a fast-check up front: https://github.com/dotnet/runtime/blob/703f6e846027681a052970929989bd36e81636cd/src/libraries/Microsoft.Extensions.Logging/src/LoggerInformation.cs#L32-L35

My guess would be it is because someone might swap out the default ILoggerFactory with their own implementation which might not have the logic?

Copy link
Member

Choose a reason for hiding this comment

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

Yes it appears standard in the sense - every logging provider I have seen has that logic! I wasn't expecting that providers have to do that job, instead handled by ILogger itself.!

Copy link
Member

@CodeBlanch CodeBlanch left a comment

Choose a reason for hiding this comment

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

LGTM

@CodeBlanch CodeBlanch added pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package logs Logging signal related labels Feb 21, 2024
@CodeBlanch CodeBlanch changed the title Improve OpenTelemetryLogger.IsEnabled [sdk-logs] Improve OpenTelemetryLogger.IsEnabled Feb 21, 2024
@CodeBlanch CodeBlanch merged commit 5bcc805 into open-telemetry:main Feb 21, 2024
37 checks passed
@reyang reyang deleted the reyang/log-enabled branch February 21, 2024 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logs Logging signal related pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants