-
Notifications
You must be signed in to change notification settings - Fork 780
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
[sdk-logs] Add LoggerProviderBuilderSdk to SDK #4447
Merged
CodeBlanch
merged 1 commit into
open-telemetry:main
from
CodeBlanch:sdk-logs-loggerproviderbuildersdk
Apr 26, 2023
Merged
Changes from all commits
Commits
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
133 changes: 133 additions & 0 deletions
133
src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.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,133 @@ | ||
// <copyright file="LoggerProviderBuilderSdk.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> | ||
|
||
#nullable enable | ||
|
||
using System.Diagnostics; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Resources; | ||
|
||
namespace OpenTelemetry.Logs; | ||
|
||
/// <summary> | ||
/// Stores state used to build a <see cref="LoggerProvider"/>. | ||
/// </summary> | ||
internal sealed class LoggerProviderBuilderSdk : LoggerProviderBuilder, ILoggerProviderBuilder | ||
{ | ||
private const string DefaultInstrumentationVersion = "1.0.0.0"; | ||
|
||
private readonly IServiceProvider serviceProvider; | ||
private LoggerProviderSdk? loggerProvider; | ||
|
||
public LoggerProviderBuilderSdk(IServiceProvider serviceProvider) | ||
{ | ||
this.serviceProvider = serviceProvider; | ||
} | ||
|
||
public List<InstrumentationRegistration> Instrumentation { get; } = new(); | ||
|
||
public ResourceBuilder? ResourceBuilder { get; private set; } | ||
|
||
public LoggerProvider? Provider => this.loggerProvider; | ||
|
||
public List<BaseProcessor<Activity>> Processors { get; } = new(); | ||
|
||
public void RegisterProvider(LoggerProviderSdk loggerProvider) | ||
{ | ||
Debug.Assert(loggerProvider != null, "loggerProvider was null"); | ||
|
||
if (this.loggerProvider != null) | ||
{ | ||
throw new NotSupportedException("LoggerProvider cannot be accessed while build is executing."); | ||
} | ||
|
||
this.loggerProvider = loggerProvider; | ||
} | ||
|
||
public override LoggerProviderBuilder AddInstrumentation<TInstrumentation>( | ||
Func<TInstrumentation> instrumentationFactory) | ||
{ | ||
Debug.Assert(instrumentationFactory != null, "instrumentationFactory was null"); | ||
|
||
this.Instrumentation.Add( | ||
new InstrumentationRegistration( | ||
typeof(TInstrumentation).Name, | ||
typeof(TInstrumentation).Assembly.GetName().Version?.ToString() ?? DefaultInstrumentationVersion, | ||
instrumentationFactory!()!)); | ||
|
||
return this; | ||
} | ||
|
||
public LoggerProviderBuilder ConfigureResource(Action<ResourceBuilder> configure) | ||
{ | ||
Debug.Assert(configure != null, "configure was null"); | ||
|
||
var resourceBuilder = this.ResourceBuilder ??= ResourceBuilder.CreateDefault(); | ||
|
||
configure!(resourceBuilder); | ||
|
||
return this; | ||
} | ||
|
||
public LoggerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder) | ||
{ | ||
Debug.Assert(resourceBuilder != null, "resourceBuilder was null"); | ||
|
||
this.ResourceBuilder = resourceBuilder; | ||
|
||
return this; | ||
} | ||
|
||
public LoggerProviderBuilder AddProcessor(BaseProcessor<Activity> processor) | ||
{ | ||
Debug.Assert(processor != null, "processor was null"); | ||
|
||
this.Processors.Add(processor!); | ||
|
||
return this; | ||
} | ||
|
||
public LoggerProviderBuilder ConfigureBuilder(Action<IServiceProvider, LoggerProviderBuilder> configure) | ||
{ | ||
Debug.Assert(configure != null, "configure was null"); | ||
|
||
configure!(this.serviceProvider, this); | ||
|
||
return this; | ||
} | ||
|
||
public LoggerProviderBuilder ConfigureServices(Action<IServiceCollection> configure) | ||
{ | ||
throw new NotSupportedException("Services cannot be configured after ServiceProvider has been created."); | ||
} | ||
|
||
LoggerProviderBuilder IDeferredLoggerProviderBuilder.Configure(Action<IServiceProvider, LoggerProviderBuilder> configure) | ||
=> this.ConfigureBuilder(configure); | ||
|
||
internal readonly struct InstrumentationRegistration | ||
{ | ||
public readonly string Name; | ||
public readonly string Version; | ||
public readonly object Instance; | ||
|
||
internal InstrumentationRegistration(string name, string version, object instance) | ||
{ | ||
this.Name = name; | ||
this.Version = version; | ||
this.Instance = instance; | ||
} | ||
} | ||
} |
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.
Not related to this PR but could we share this struct among the different provider SDKs?
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.
Totally. Or just remove it, I don't think we use this data for anything?