-
Notifications
You must be signed in to change notification settings - Fork 773
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 4 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.OpenTelemetryEventSourceServiceCollectionExtensions | ||
override OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter.Dispose() -> void | ||
static OpenTelemetry.Logs.OpenTelemetryEventSourceServiceCollectionExtensions.AddOpenTelemetryEventSourceLogEmitter(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Func<string!, System.Diagnostics.Tracing.EventLevel?>! shouldListenToFunc) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! |
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.OpenTelemetryEventSourceServiceCollectionExtensions | ||
override OpenTelemetry.Logs.OpenTelemetryEventSourceLogEmitter.Dispose() -> void | ||
static OpenTelemetry.Logs.OpenTelemetryEventSourceServiceCollectionExtensions.AddOpenTelemetryEventSourceLogEmitter(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Func<string!, System.Diagnostics.Tracing.EventLevel?>! shouldListenToFunc) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! |
80 changes: 80 additions & 0 deletions
80
...enTelemetry.Extensions.EventSource/OpenTelemetryEventSourceServiceCollectionExtensions.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,80 @@ | ||
// <copyright file="OpenTelemetryEventSourceServiceCollectionExtensions.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 application services. | ||
/// </summary> | ||
public static class OpenTelemetryEventSourceServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Registers into application services an <see cref="EventListener"/> | ||
/// which will convert <see cref="EventSource"/> events into | ||
/// OpenTelemetry logs. | ||
/// </summary> | ||
/// <param name="services"><see cref="IServiceCollection"/>.</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="IServiceCollection"/> for chaining calls.</returns> | ||
public static IServiceCollection AddOpenTelemetryEventSourceLogEmitter( | ||
this IServiceCollection services, | ||
Func<string, EventLevel?> shouldListenToFunc) | ||
{ | ||
Guard.ThrowIfNull(services); | ||
Guard.ThrowIfNull(shouldListenToFunc); | ||
|
||
services.TryAddSingleton<EventSourceManager>(); | ||
|
||
services.Configure<OpenTelemetryLoggerOptions>(options => | ||
{ | ||
options.Configure((sp, provider) => | ||
{ | ||
var manager = sp.GetRequiredService<EventSourceManager>(); | ||
|
||
manager.Emitters.Add( | ||
new OpenTelemetryEventSourceLogEmitter(provider, shouldListenToFunc, disposeProvider: false)); | ||
}); | ||
}); | ||
|
||
return services; | ||
} | ||
|
||
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
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.
Would a static method on
Sdk
make sense? LikeSdk.CreateMeterProviderBuilder
andSdk.CreateTracerProviderBuilder
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.
Good idea I like it! At the moment the mechanics are slightly different in that there is no
Build
method forOpenTelemetryLoggerProvider
. I could introduce a small classLoggerProviderBuilder
so it matches more closely what we have to metrics + traces. Thoughts on that?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.
If folks aren't thrilled by that idea, then I think
Sdk.CreateLoggerProvider
(i.e., w/oBuilder
) wouldn't be terrible.But, consistency would have a nice feel to it, so my vote is a simple class enabling
Sdk.CreateLoggerProviderBuilder(...).Build()
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.
@alanwest OK this now has more of a builder pattern. Updated the description. LMK what you think!