-
Notifications
You must be signed in to change notification settings - Fork 785
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
[Logs] Support dependency injection in logging build-up #3504
Merged
CodeBlanch
merged 35 commits into
open-telemetry:main
from
CodeBlanch:loggerprovider-dependencyinjection
Aug 6, 2022
Merged
Changes from 23 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
8d9bb6c
Support dependency injection in logging. Add AddOpenTelemetryEventSou…
CodeBlanch a3616bb
Merge branch 'main' into loggerprovider-dependencyinjection
CodeBlanch 0836b09
CHANGELOG update.
CodeBlanch 013ab59
Bug fixes and a test.
CodeBlanch 9ee35f2
More fixes and more tests.
CodeBlanch c0fe823
Tweak comments for clarity.
CodeBlanch 5b4ffc1
Added OpenTelemetryLoggerOptions.Services xml detail remarks.
CodeBlanch a1fe108
More tests.
CodeBlanch d2ba768
More tests.
CodeBlanch a8f8238
Test fix.
CodeBlanch 6daf110
More tests.
CodeBlanch 71cdfbf
Tests and fixes.
CodeBlanch 0c73626
Warning cleanup.
CodeBlanch c5c93e0
Added resource test.
CodeBlanch 3632366
Smooth out multiple resource configurations.
CodeBlanch e78734c
Resource chaining fix.
CodeBlanch 8aeb08e
Remove throw for additional SetResourceBuilder calls.
CodeBlanch 570ab37
Warning fixes.
CodeBlanch 813bacc
Moved OpenTelemetryEventSourceLogEmitter extension to OpenTelemetryLo…
CodeBlanch 6ffd623
Tweaks.
CodeBlanch fad5a6c
Switched from static to builder pattern.
CodeBlanch d477d8d
Tweaks.
CodeBlanch 2c4195d
More tests.
CodeBlanch ce2a932
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch 6284ff1
Fix CHANGELOG for release.
CodeBlanch d416e1b
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch 2b26cb0
Tweaks.
CodeBlanch 280ad5d
Test updates.
CodeBlanch 251ff81
Added a detached configuration option.
CodeBlanch 5f2b5b5
Prevent double registration to be consistent with tracer builder patt…
CodeBlanch 28301d8
API review.
CodeBlanch e389a5d
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch 7cc2865
Warning cleanup.
CodeBlanch 8ecfdaf
Build fixes.
CodeBlanch 7057343
Test fix.
CodeBlanch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/OpenTelemetry.Extensions.EventSource/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
#nullable enable | ||
OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter | ||
OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter.OpenTelemetryEventSourceLogEmitter(OpenTelemetry.Logs.OpenTelemetryLoggerProvider! openTelemetryLoggerProvider, System.Func<string!, System.Diagnostics.Tracing.EventLevel?>! shouldListenToFunc, bool disposeProvider = true) -> void | ||
OpenTelemetry.Logs.OpenTelemetryEventSourceLoggerOptionsExtensions | ||
override OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter.Dispose() -> void | ||
static OpenTelemetry.Logs.OpenTelemetryEventSourceLoggerOptionsExtensions.AddEventSourceLogEmitter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! options, System.Func<string!, System.Diagnostics.Tracing.EventLevel?>! shouldListenToFunc) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! |
2 changes: 2 additions & 0 deletions
2
src/OpenTelemetry.Extensions.EventSource/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
#nullable enable | ||
OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter | ||
OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter.OpenTelemetryEventSourceLogEmitter(OpenTelemetry.Logs.OpenTelemetryLoggerProvider! openTelemetryLoggerProvider, System.Func<string!, System.Diagnostics.Tracing.EventLevel?>! shouldListenToFunc, bool disposeProvider = true) -> void | ||
OpenTelemetry.Logs.OpenTelemetryEventSourceLoggerOptionsExtensions | ||
override OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter.Dispose() -> void | ||
static OpenTelemetry.Logs.OpenTelemetryEventSourceLoggerOptionsExtensions.AddEventSourceLogEmitter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! options, System.Func<string!, System.Diagnostics.Tracing.EventLevel?>! shouldListenToFunc) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/OpenTelemetry.Extensions.EventSource/OpenTelemetryEventSourceLoggerOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// <copyright file="OpenTelemetryEventSourceLoggerOptionsExtensions.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Logs | ||
{ | ||
/// <summary> | ||
/// Contains extension methods for registering OpenTelemetry EventSource utilities into logging services. | ||
/// </summary> | ||
public static class OpenTelemetryEventSourceLoggerOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Registers an <see cref="EventListener"/> which will convert <see | ||
/// cref="EventSource"/> events into OpenTelemetry logs. | ||
/// </summary> | ||
/// <param name="options"><see | ||
/// cref="OpenTelemetryLoggerOptions"/>.</param> | ||
/// <param name="shouldListenToFunc">Callback function used to decide if | ||
/// events should be captured for a given <see | ||
/// cref="EventSource.Name"/>. Return <see langword="null"/> if no | ||
/// events should be captured.</param> | ||
/// <returns>Supplied <see cref="OpenTelemetryLoggerOptions"/> for | ||
/// chaining calls.</returns> | ||
public static OpenTelemetryLoggerOptions AddEventSourceLogEmitter( | ||
this OpenTelemetryLoggerOptions options, | ||
Func<string, EventLevel?> shouldListenToFunc) | ||
{ | ||
Guard.ThrowIfNull(options); | ||
Guard.ThrowIfNull(shouldListenToFunc); | ||
|
||
options.ConfigureServices(services => services.TryAddSingleton<EventSourceManager>()); | ||
|
||
options.ConfigureProvider((sp, provider) => | ||
{ | ||
var manager = sp.GetRequiredService<EventSourceManager>(); | ||
|
||
manager.Emitters.Add( | ||
new OpenTelemetryEventSourceLogEmitter(provider, shouldListenToFunc, disposeProvider: false)); | ||
}); | ||
|
||
return options; | ||
} | ||
|
||
internal sealed class EventSourceManager : IDisposable | ||
{ | ||
public List<OpenTelemetryEventSourceLogEmitter> Emitters { get; } = new(); | ||
|
||
public void Dispose() | ||
{ | ||
foreach (var emitter in this.Emitters) | ||
{ | ||
emitter.Dispose(); | ||
} | ||
|
||
this.Emitters.Clear(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels a little off to have some things configured via the options like
IncludeFormattedMessage
and other things configured via methods off the builder.For metrics we have some similar options like max streams that we set using builder methods:
This PR is growing, so maybe a follow up? Though a follow up would mean the potential for some breaking changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya this was something I went back and forth on. The bools suck because you can't chain them which kind of breaks the builder pattern. That's why I added the delegate. We could add "Set" methods to preserve the chaining but some downsides to that: More public API and maybe confusing to have properties with get/set and also "Set" methods?
@utpilla @cijothomas @reyang Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it all started from
SetSampler
.I personally would find it slightly more user-friendly with
builder.SetSomething
if the number of things to be set is small enough, and the things to be set are fairly complex (e.g. very hard to be configuration-based). I would be less happy if there are too many simple options that I have to chain the builder calls:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, now that we started talking about configuration, do you think
ConfigureResource
should beSetResource
? 😆There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reyang You are going to love this, we actually have
SetResourceBuilder
&ConfigureResource
both today 🤣 ConfigureResource was a recent addition.Subtle differences between the two.
Set
clobbers anything that was configured.Configure
is additive. For users configuring their host, I would expect them to callSet
(but they could alsoConfigure
). For libraries/extension authors, they should callConfigure
.We should probably obsolete the
Set
version because inConfigure
you can callClear
on the builder if you really wanted to reset it. (Pretty sure, didn't double-check aClear
exists.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would vote for eventually getting rid of
ConfigureXyz
(or at least use it carefully) because "configure" is a very general term that can have several different meanings (e.g. it could be additive and commutative; it could be additive but not commutative; it could be idempotent...).If we're trying to be pedantic, "Append" is probably good for Processor and Exporter (coz it is not commutative due to the sequential processing behavior), "Set" is probably good for Sampler, "Configure" is probably good for anything that takes a complex "XyzOptions" object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the delegate and replaced it with 3 new "Set" methods. Don't disagree with any of this, it just seems like a bigger conversation we need to have beyond this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, I would suggest that we scope this out for now. Maybe revisit it after this PR.