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

Logformatter null check #2200

Merged
merged 3 commits into from
Jul 29, 2021
Merged

Conversation

cijothomas
Copy link
Member

@cijothomas cijothomas commented Jul 28, 2021

ilogger.Log(LogLevel.Information, default, "Hello World!", null, null); (note the last param is null, which is the formatter) when combined with IncludeFormattedMessage=true will result in an unhandled exception. This PR check if formatter is not null, before invoking it.

Also removed some netcoreapp2.1 checks. (leftover cleanups)

@cijothomas cijothomas requested a review from a team July 28, 2021 15:03
@codecov
Copy link

codecov bot commented Jul 28, 2021

Codecov Report

Merging #2200 (5b4b8cb) into main (175154e) will increase coverage by 1.60%.
The diff coverage is 62.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2200      +/-   ##
==========================================
+ Coverage   79.36%   80.96%   +1.60%     
==========================================
  Files         217      217              
  Lines        6992     6994       +2     
==========================================
+ Hits         5549     5663     +114     
+ Misses       1443     1331     -112     
Impacted Files Coverage Δ
...tryProtocol/Implementation/MetricItemExtensions.cs 46.75% <50.00%> (+46.75%) ⬆️
...orter.OpenTelemetryProtocol/OtlpMetricsExporter.cs 35.71% <100.00%> (+35.71%) ⬆️
src/OpenTelemetry/Logs/OpenTelemetryLogger.cs 94.11% <100.00%> (ø)
...ZPages/Implementation/ZPagesExporterEventSource.cs 56.25% <0.00%> (-6.25%) ⬇️
...y/Metrics/MetricAggregators/SumMetricAggregator.cs 81.13% <0.00%> (+1.88%) ⬆️
...elemetry/Metrics/Processors/PullMetricProcessor.cs 89.47% <0.00%> (+89.47%) ⬆️

try
{
// Pass null as formatter function
this.logger.Log(LogLevel.Information, default, "Hello World!", null, null);
Copy link
Member Author

Choose a reason for hiding this comment

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

@jviau - Please review!

Copy link

@jviau jviau left a comment

Choose a reason for hiding this comment

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

I am not sure this was a bug to begin with. Many other log implementations all explicitly throw on a null formatter.

ConsoleLogger
EventLogLogger
ApplicationInsightsLogger

But with that said, the aggregate logger doesn't throw. So I am guessing it is up to each logger to decide what to do.

Either way, this change is fine within context of how otel currently behaves.

@@ -5,6 +5,9 @@
* Removes upper constraint for Microsoft.Extensions.Logging
dependencies. ([#2179](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2179))

* Fix bug which caused ILogger.Log calls to throw exception, when the
Copy link
Member

Choose a reason for hiding this comment

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

Do we consider this as a bug?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good qn. After seeing some other implementations, which throw in Log(...formatter=null)., I am thinking we should follow the regular pattern of "not throwing from instrumentation APIs after successful initialization". So Otel logging provider can chose to not throw, end user may still see exception from other providers they have enabled.

And I can reword the changelog to just say:
"OpenTelemetry Logger modified to not throw when formatter is null"

@cijothomas cijothomas merged commit ceaaa40 into main Jul 29, 2021
@cijothomas cijothomas deleted the cijothomas/loggerformatternullcheck branch July 29, 2021 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants