-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Logs] Support dependency injection in logging build-up (#3504)
* Support dependency injection in logging. Add AddOpenTelemetryEventSourceLogEmitter extension. * CHANGELOG update. * Bug fixes and a test. * More fixes and more tests. * Tweak comments for clarity. * Added OpenTelemetryLoggerOptions.Services xml detail remarks. * More tests. * More tests. * Test fix. * More tests. * Tests and fixes. * Warning cleanup. * Added resource test. * Smooth out multiple resource configurations. * Resource chaining fix. * Remove throw for additional SetResourceBuilder calls. * Warning fixes. * Moved OpenTelemetryEventSourceLogEmitter extension to OpenTelemetryLoggerOptions. * Tweaks. * Switched from static to builder pattern. * Tweaks. * More tests. * Fix CHANGELOG for release. * Tweaks. * Test updates. * Added a detached configuration option. * Prevent double registration to be consistent with tracer builder pattern. * API review. * Warning cleanup. * Build fixes. * Test fix.
- Loading branch information
1 parent
72f4e07
commit 952c3b1
Showing
31 changed files
with
1,450 additions
and
361 deletions.
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
Oops, something went wrong.