-
Notifications
You must be signed in to change notification settings - Fork 792
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[di] Expose a detached LoggerProviderBuilder extension on IServiceCollection which may modify services #4531
Merged
alanwest
merged 3 commits into
open-telemetry:main
from
CodeBlanch:di-configurelogging-withservices
Jun 1, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
81 changes: 81 additions & 0 deletions
81
...penTelemetry.Api.ProviderBuilderExtensions/Logs/LoggerProviderServiceCollectionBuilder.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,81 @@ | ||
// <copyright file="LoggerProviderServiceCollectionBuilder.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 Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Logs; | ||
|
||
internal sealed class LoggerProviderServiceCollectionBuilder : LoggerProviderBuilder, ILoggerProviderBuilder | ||
{ | ||
public LoggerProviderServiceCollectionBuilder(IServiceCollection services) | ||
{ | ||
services.ConfigureOpenTelemetryLoggerProvider((sp, builder) => this.Services = null); | ||
|
||
this.Services = services; | ||
} | ||
|
||
public IServiceCollection? Services { get; set; } | ||
|
||
public LoggerProvider? Provider => null; | ||
|
||
/// <inheritdoc /> | ||
public override LoggerProviderBuilder AddInstrumentation<TInstrumentation>(Func<TInstrumentation> instrumentationFactory) | ||
{ | ||
Guard.ThrowIfNull(instrumentationFactory); | ||
|
||
this.ConfigureBuilderInternal((sp, builder) => | ||
{ | ||
builder.AddInstrumentation(instrumentationFactory); | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public LoggerProviderBuilder ConfigureServices(Action<IServiceCollection> configure) | ||
=> this.ConfigureServicesInternal(configure); | ||
|
||
/// <inheritdoc cref="IDeferredLoggerProviderBuilder.Configure" /> | ||
public LoggerProviderBuilder ConfigureBuilder(Action<IServiceProvider, LoggerProviderBuilder> configure) | ||
=> this.ConfigureBuilderInternal(configure); | ||
|
||
/// <inheritdoc /> | ||
LoggerProviderBuilder IDeferredLoggerProviderBuilder.Configure(Action<IServiceProvider, LoggerProviderBuilder> configure) | ||
=> this.ConfigureBuilderInternal(configure); | ||
|
||
private LoggerProviderBuilder ConfigureBuilderInternal(Action<IServiceProvider, LoggerProviderBuilder> configure) | ||
{ | ||
var services = this.Services | ||
?? throw new NotSupportedException("Builder cannot be configured during LoggerProvider construction."); | ||
|
||
services.ConfigureOpenTelemetryLoggerProvider(configure); | ||
|
||
return this; | ||
} | ||
|
||
private LoggerProviderBuilder ConfigureServicesInternal(Action<IServiceCollection> configure) | ||
{ | ||
Guard.ThrowIfNull(configure); | ||
|
||
var services = this.Services | ||
?? throw new NotSupportedException("Services cannot be configured during LoggerProvider construction."); | ||
|
||
configure(services); | ||
|
||
return this; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// <copyright file="LoggerProviderServiceCollectionBuilder.cs" company="OpenTelemetry Authors"> | ||
// <copyright file="LoggerProviderBuilderBase.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
|
@@ -23,20 +23,17 @@ | |
namespace OpenTelemetry.Logs; | ||
|
||
/// <summary> | ||
/// Contains methods for registering actions into an <see | ||
/// cref="IServiceCollection"/> which will be used to build a <see | ||
/// cref="LoggerProvider"/> once the <see cref="IServiceProvider"/> is | ||
/// available. | ||
/// Contains methods for building <see cref="LoggerProvider"/> instances. | ||
/// </summary> | ||
internal sealed class LoggerProviderServiceCollectionBuilder : LoggerProviderBuilder, ILoggerProviderBuilder | ||
internal sealed class LoggerProviderBuilderBase : LoggerProviderBuilder, ILoggerProviderBuilder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally when this was added you commented that you would prefer it was named |
||
{ | ||
private readonly bool allowBuild; | ||
private IServiceCollection? services; | ||
private readonly LoggerProviderServiceCollectionBuilder innerBuilder; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="LoggerProviderServiceCollectionBuilder"/> class. | ||
/// Initializes a new instance of the <see cref="LoggerProviderBuilderBase"/> class. | ||
/// </summary> | ||
public LoggerProviderServiceCollectionBuilder() | ||
public LoggerProviderBuilderBase() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
|
@@ -46,24 +43,20 @@ public LoggerProviderServiceCollectionBuilder() | |
.TryAddSingleton<LoggerProvider>( | ||
sp => throw new NotSupportedException("Self-contained LoggerProvider cannot be accessed using the application IServiceProvider call Build instead.")); | ||
|
||
services.ConfigureOpenTelemetryLoggerProvider((sp, builder) => this.services = null); | ||
|
||
this.services = services; | ||
this.innerBuilder = new LoggerProviderServiceCollectionBuilder(services); | ||
|
||
this.allowBuild = true; | ||
} | ||
|
||
internal LoggerProviderServiceCollectionBuilder(IServiceCollection services) | ||
internal LoggerProviderBuilderBase(IServiceCollection services) | ||
{ | ||
Guard.ThrowIfNull(services); | ||
|
||
services | ||
.AddOpenTelemetryLoggerProviderBuilderServices() | ||
.TryAddSingleton<LoggerProvider>(sp => new LoggerProviderSdk(sp, ownsServiceProvider: false)); | ||
|
||
services.ConfigureOpenTelemetryLoggerProvider((sp, builder) => this.services = null); | ||
|
||
this.services = services; | ||
this.innerBuilder = new LoggerProviderServiceCollectionBuilder(services); | ||
|
||
this.allowBuild = false; | ||
} | ||
|
@@ -74,23 +67,26 @@ internal LoggerProviderServiceCollectionBuilder(IServiceCollection services) | |
/// <inheritdoc /> | ||
public override LoggerProviderBuilder AddInstrumentation<TInstrumentation>(Func<TInstrumentation> instrumentationFactory) | ||
{ | ||
Guard.ThrowIfNull(instrumentationFactory); | ||
|
||
this.ConfigureBuilderInternal((sp, builder) => | ||
{ | ||
builder.AddInstrumentation(instrumentationFactory); | ||
}); | ||
this.innerBuilder.AddInstrumentation(instrumentationFactory); | ||
|
||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
LoggerProviderBuilder ILoggerProviderBuilder.ConfigureServices(Action<IServiceCollection> configure) | ||
=> this.ConfigureServicesInternal(configure); | ||
{ | ||
this.innerBuilder.ConfigureServices(configure); | ||
|
||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
LoggerProviderBuilder IDeferredLoggerProviderBuilder.Configure(Action<IServiceProvider, LoggerProviderBuilder> configure) | ||
=> this.ConfigureBuilderInternal(configure); | ||
{ | ||
this.innerBuilder.ConfigureBuilder(configure); | ||
|
||
return this; | ||
} | ||
|
||
internal LoggerProvider Build() | ||
{ | ||
|
@@ -99,10 +95,10 @@ internal LoggerProvider Build() | |
throw new NotSupportedException("A LoggerProviderBuilder bound to external service cannot be built directly. Access the LoggerProvider using the application IServiceProvider instead."); | ||
} | ||
|
||
var services = this.services | ||
var services = this.innerBuilder.Services | ||
?? throw new NotSupportedException("LoggerProviderBuilder build method cannot be called multiple times."); | ||
|
||
this.services = null; | ||
this.innerBuilder.Services = null; | ||
|
||
#if DEBUG | ||
bool validateScopes = true; | ||
|
@@ -113,26 +109,4 @@ internal LoggerProvider Build() | |
|
||
return new LoggerProviderSdk(serviceProvider, ownsServiceProvider: true); | ||
} | ||
|
||
private LoggerProviderBuilder ConfigureBuilderInternal(Action<IServiceProvider, LoggerProviderBuilder> configure) | ||
{ | ||
var services = this.services | ||
?? throw new NotSupportedException("Builder cannot be configured during LoggerProvider construction."); | ||
|
||
services.ConfigureOpenTelemetryLoggerProvider(configure); | ||
|
||
return this; | ||
} | ||
|
||
private LoggerProviderBuilder ConfigureServicesInternal(Action<IServiceCollection> configure) | ||
{ | ||
Guard.ThrowIfNull(configure); | ||
|
||
var services = this.services | ||
?? throw new NotSupportedException("Services cannot be configured during LoggerProvider construction."); | ||
|
||
configure(services); | ||
|
||
return this; | ||
} | ||
} |
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.
Merge issue from #4517