-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dependency injection support (#1889)
- Loading branch information
1 parent
5f1acbf
commit 0208fd6
Showing
26 changed files
with
710 additions
and
312 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void |
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 @@ | ||
OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void |
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
7 changes: 6 additions & 1 deletion
7
src/OpenTelemetry.Extensions.Hosting/.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,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 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 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>>(); | ||
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.