-
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 4 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
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
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
| // <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.Diagnostics; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace OpenTelemetry.Trace | ||
| { | ||
| internal class TracerProviderBuilderHosting : TracerProviderBuilderSdk, IResolvingTracerProviderBuilder | ||
| { | ||
| private readonly IServiceProvider serviceProvider; | ||
|
|
||
| public TracerProviderBuilderHosting(IServiceProvider serviceProvider) | ||
| { | ||
| this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); | ||
| } | ||
|
|
||
| public TracerProviderBuilder AddInstrumentation<T>() | ||
| where T : class | ||
| { | ||
| return this.AddInstrumentation(() => this.ResolveService<T>()); | ||
| } | ||
|
|
||
| public TracerProviderBuilder AddProcessor<T>() | ||
| where T : BaseProcessor<Activity> | ||
| { | ||
| return this.AddProcessor(this.serviceProvider.GetRequiredService<T>()); | ||
| } | ||
|
|
||
| public TracerProviderBuilder SetSampler<T>() | ||
| where T : Sampler | ||
| { | ||
| return this.SetSampler(this.serviceProvider.GetRequiredService<T>()); | ||
| } | ||
|
|
||
| public T ResolveService<T>() | ||
| { | ||
| return this.serviceProvider.GetRequiredService<T>(); | ||
| } | ||
|
|
||
| public T ResolveOptions<T>() | ||
| where T : class, new() | ||
| { | ||
| return this.serviceProvider.GetRequiredService<IOptions<T>>().Value; | ||
| } | ||
| } | ||
| } |
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
56 changes: 56 additions & 0 deletions
56
src/OpenTelemetry.Extensions.Hosting/Trace/TracerProviderBuilderExtensions.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,56 @@ | ||
| // <copyright file="TracerProviderBuilderExtensions.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.Diagnostics; | ||
|
|
||
| namespace OpenTelemetry.Trace | ||
| { | ||
| public static class TracerProviderBuilderExtensions | ||
| { | ||
| public static TracerProviderBuilder AddInstrumentation<T>(this TracerProviderBuilder tracerProviderBuilder) | ||
| where T : class | ||
| { | ||
| if (tracerProviderBuilder is TracerProviderBuilderHosting tracerProviderBuilderHosting) | ||
| { | ||
| tracerProviderBuilderHosting.AddInstrumentation<T>(); | ||
| } | ||
|
|
||
| return tracerProviderBuilder; | ||
| } | ||
|
|
||
| public static TracerProviderBuilder AddProcessor<T>(this TracerProviderBuilder tracerProviderBuilder) | ||
| where T : BaseProcessor<Activity> | ||
| { | ||
| if (tracerProviderBuilder is TracerProviderBuilderHosting tracerProviderBuilderHosting) | ||
| { | ||
| tracerProviderBuilderHosting.AddProcessor<T>(); | ||
| } | ||
|
|
||
| return tracerProviderBuilder; | ||
| } | ||
|
|
||
| public static TracerProviderBuilder SetSampler<T>(this TracerProviderBuilder tracerProviderBuilder) | ||
| where T : Sampler | ||
| { | ||
| if (tracerProviderBuilder is TracerProviderBuilderHosting tracerProviderBuilderHosting) | ||
| { | ||
| tracerProviderBuilderHosting.SetSampler<T>(); | ||
| } | ||
|
|
||
| return tracerProviderBuilder; | ||
| } | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/OpenTelemetry/Trace/IResolvingTracerProviderBuilder.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,26 @@ | ||
| // <copyright file="IResolvingTracerProviderBuilder.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> | ||
|
|
||
| namespace OpenTelemetry.Trace | ||
| { | ||
| public interface IResolvingTracerProviderBuilder | ||
|
CodeBlanch marked this conversation as resolved.
Outdated
|
||
| { | ||
| T ResolveService<T>(); | ||
|
|
||
| T ResolveOptions<T>() | ||
| where T : class, new(); | ||
| } | ||
| } | ||
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
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.