-
Notifications
You must be signed in to change notification settings - Fork 893
Dependency injection support #1889
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
Merged
cijothomas
merged 19 commits into
open-telemetry:main
from
CodeBlanch:dependency-injection
Mar 26, 2021
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ad49139
Dependency injection support.
CodeBlanch ee986e2
Added DI-aware instrumentation methods.
CodeBlanch 186fa2f
Renamed jaeger keys to match options.
CodeBlanch b99da7d
Added ConfigureOpenTelemetryTracing.
CodeBlanch 50212ac
Code review.
CodeBlanch 960545e
Merge remote-tracking branch 'upstream/main' into dependency-injection
CodeBlanch 1f75aac
Code review & tweaks.
CodeBlanch 670aa36
Fallback logic for when IOptions is present, but not registered.
CodeBlanch d5e2482
Code review.
CodeBlanch c5cf8da
Removed build from IDeferredTracerBuilder.
CodeBlanch f114261
Code review.
CodeBlanch beaeb93
Tweaks.
CodeBlanch 3ff8258
Merging from main.
CodeBlanch 3f264b6
Lint fix.
CodeBlanch 415f133
Bug fixes.
CodeBlanch 9f28e45
Put back internal ctor on TracerProviderBuilder.
CodeBlanch 3892b58
Moved build to bottom of file. Fixed up warnings.
CodeBlanch bba6131
Merging from main.
CodeBlanch 0ce3374
Restored changelog text.
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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void |
This file contains hidden or 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 @@ | ||
| OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
7 changes: 6 additions & 1 deletion
7
src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt
This file contains hidden or 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,9 @@ | ||
| Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions | ||
| OpenTelemetry.Trace.TracerProviderBuilderExtensions | ||
| static Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions.AddOpenTelemetryTracing(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection | ||
| static Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions.AddOpenTelemetryTracing(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<OpenTelemetry.Trace.TracerProviderBuilder> configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection | ||
| static Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions.AddOpenTelemetryTracing(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection | ||
| static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddInstrumentation<T>(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
| static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor<T>(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
| static OpenTelemetry.Trace.TracerProviderBuilderExtensions.Build(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, System.IServiceProvider serviceProvider) -> OpenTelemetry.Trace.TracerProvider | ||
| static OpenTelemetry.Trace.TracerProviderBuilderExtensions.Configure(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
| static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler<T>(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder |
This file contains hidden or 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
126 changes: 126 additions & 0 deletions
126
src/OpenTelemetry.Extensions.Hosting/Implementation/TracerProviderBuilderHosting.cs
This file contains hidden or 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,126 @@ | ||
| // <copyright file="TracerProviderBuilderHosting.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; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace OpenTelemetry.Trace | ||
| { | ||
| /// <summary> | ||
| /// A <see cref="TracerProviderBuilderBase"/> with support for deferred initialization using <see cref="IServiceProvider"/> for dependency injection. | ||
| /// </summary> | ||
| internal class TracerProviderBuilderHosting : TracerProviderBuilderBase, IDeferredTracerProviderBuilder | ||
| { | ||
| private readonly List<InstrumentationFactory> instrumentationFactories = new List<InstrumentationFactory>(); | ||
| private readonly List<Type> processorTypes = new List<Type>(); | ||
| private readonly List<Action<IServiceProvider, TracerProviderBuilder>> configurationActions = new List<Action<IServiceProvider, TracerProviderBuilder>>(); | ||
|
Contributor
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. I like having generic configuration actions like this. |
||
| private Type samplerType; | ||
|
|
||
| public TracerProviderBuilderHosting(IServiceCollection services) | ||
| { | ||
| this.Services = services ?? throw new ArgumentNullException(nameof(services)); | ||
| } | ||
|
|
||
| public IServiceCollection Services { get; } | ||
|
|
||
| public TracerProviderBuilder AddInstrumentation<TInstrumentation>( | ||
| Func<IServiceProvider, TInstrumentation> instrumentationFactory) | ||
| where TInstrumentation : class | ||
| { | ||
| if (instrumentationFactory == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(instrumentationFactory)); | ||
| } | ||
|
|
||
| this.instrumentationFactories.Add( | ||
| new InstrumentationFactory( | ||
| typeof(TInstrumentation).Name, | ||
| "semver:" + typeof(TInstrumentation).Assembly.GetName().Version, | ||
| typeof(TInstrumentation))); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public TracerProviderBuilder AddProcessor<T>() | ||
| where T : BaseProcessor<Activity> | ||
| { | ||
| this.processorTypes.Add(typeof(T)); | ||
| return this; | ||
| } | ||
|
|
||
| public TracerProviderBuilder SetSampler<T>() | ||
| where T : Sampler | ||
| { | ||
| this.samplerType = typeof(T); | ||
| return this; | ||
| } | ||
|
|
||
| public TracerProviderBuilder Configure(Action<IServiceProvider, TracerProviderBuilder> configure) | ||
| { | ||
| if (configure == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(configure)); | ||
| } | ||
|
|
||
| this.configurationActions.Add(configure); | ||
| return this; | ||
| } | ||
|
|
||
| public TracerProvider Build(IServiceProvider serviceProvider) | ||
| { | ||
| foreach (InstrumentationFactory instrumentationFactory in this.instrumentationFactories) | ||
| { | ||
| this.AddInstrumentation( | ||
| instrumentationFactory.Name, | ||
| instrumentationFactory.Version, | ||
| () => serviceProvider.GetRequiredService(instrumentationFactory.Type)); | ||
| } | ||
|
|
||
| foreach (Type processorType in this.processorTypes) | ||
| { | ||
| this.AddProcessor((BaseProcessor<Activity>)serviceProvider.GetRequiredService(processorType)); | ||
| } | ||
|
|
||
| if (this.samplerType != null) | ||
| { | ||
| this.SetSampler((Sampler)serviceProvider.GetRequiredService(this.samplerType)); | ||
| } | ||
|
|
||
| foreach (Action<IServiceProvider, TracerProviderBuilder> configureAction in this.configurationActions) | ||
| { | ||
| configureAction(serviceProvider, this); | ||
| } | ||
|
|
||
| return this.Build(); | ||
| } | ||
|
|
||
| private readonly struct InstrumentationFactory | ||
| { | ||
| public readonly string Name; | ||
| public readonly string Version; | ||
| public readonly Type Type; | ||
|
|
||
| internal InstrumentationFactory(string name, string version, Type type) | ||
| { | ||
| this.Name = name; | ||
| this.Version = version; | ||
| this.Type = type; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.