Skip to content
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

Moving IDeferredTracerProviderBuilder to API #2100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
abstract OpenTelemetry.Trace.TracerProviderBuilder.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Configure(System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
abstract OpenTelemetry.Trace.TracerProviderBuilder.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Configure(System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ please check the latest changes

## Unreleased

* Added `IDeferredTracerProviderBuilder`.
([#2058](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2100))

## 1.1.0-beta4

Released 2021-Jun-09
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
// </copyright>

using System;
#if NET461_OR_GREATER || NETSTANDARD2_0
using Microsoft.Extensions.DependencyInjection;
#endif

namespace OpenTelemetry.Trace
{
Expand All @@ -28,13 +25,6 @@ namespace OpenTelemetry.Trace
/// </summary>
public interface IDeferredTracerProviderBuilder
{
#if NET461_OR_GREATER || NETSTANDARD2_0
/// <summary>
/// Gets the application <see cref="IServiceCollection"/>.
/// </summary>
IServiceCollection Services { get; }
#endif

/// <summary>
/// Register a callback action to configure the <see
/// cref="TracerProviderBuilder"/> once the application <see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddInstrumentation<T>
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.GetServices(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> Microsoft.Extensions.DependencyInjection.IServiceCollection
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler<T>(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Added `GetServices` extension.
([#2058](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2100))

## 1.0.0-rc5

Released 2021-Jun-09
Expand Down
15 changes: 6 additions & 9 deletions src/OpenTelemetry.Extensions.Hosting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,18 @@ from the application `IServiceCollection` so any services registered in the

Library authors may want to configure the OpenTelemetry `TracerProvider` and
register application services to provide more complex features. This can be
accomplished concisely by casting the `TracerProviderBuilder` into an
`IDeferredTracerProviderBuilder` instance in an extension method like this:
accomplished concisely by using the `TracerProviderBuilder.GetServices`
extension method inside of a more general `TracerProviderBuilder` configuration
extension like this:

```csharp
public static class MyLibraryExtensions
{
public static TracerProviderBuilder AddMyFeature(this TracerProviderBuilder tracerProviderBuilder)
{
if (!(tracerProviderBuilder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder))
{
throw new NotSupportedException(
"MyFeature requires an IDeferredTracerProviderBuilder instance.");
}

deferredTracerProviderBuilder.Services
(tracerProviderBuilder.GetServices()
?? throw new NotSupportedException(
"MyFeature requires a hosting TracerProviderBuilder instance."))
.AddHostedService<MyHostedService>()
.AddSingleton<MyService>()
.AddSingleton<MyProcessor>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ public static TracerProviderBuilder Configure(this TracerProviderBuilder tracerP
return tracerProviderBuilder;
}

/// <summary>
/// Gets the application <see cref="IServiceCollection"/> attached to
/// the <see cref="TracerProviderBuilder"/>.
/// </summary>
/// <param name="tracerProviderBuilder"><see cref="TracerProviderBuilder"/>.</param>
/// <returns><see cref="IServiceCollection"/> or <see langword="null"/>
/// if services are unavailable.</returns>
public static IServiceCollection GetServices(this TracerProviderBuilder tracerProviderBuilder)
{
if (tracerProviderBuilder is TracerProviderBuilderHosting tracerProviderBuilderHosting)
{
return tracerProviderBuilderHosting.Services;
}

return null;
}

/// <summary>
/// Run the configured actions to initialize the <see cref="TracerProvider"/>.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
OpenTelemetry.Trace.IDeferredTracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Configure(System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void
OpenTelemetry.Trace.TracerProviderBuilderBase
OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrumentationName, string instrumentationVersion, System.Func<object> instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
Expand Down
2 changes: 0 additions & 2 deletions src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
OpenTelemetry.Trace.IDeferredTracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Configure(System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void
OpenTelemetry.Trace.TracerProviderBuilderBase
OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrumentationName, string instrumentationVersion, System.Func<object> instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
Expand Down
3 changes: 0 additions & 3 deletions src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
OpenTelemetry.Trace.IDeferredTracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Configure(System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Services.get -> Microsoft.Extensions.DependencyInjection.IServiceCollection
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void
OpenTelemetry.Trace.TracerProviderBuilderBase
OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrumentationName, string instrumentationVersion, System.Func<object> instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
OpenTelemetry.Trace.IDeferredTracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Configure(System.Action<System.IServiceProvider, OpenTelemetry.Trace.TracerProviderBuilder> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Trace.IDeferredTracerProviderBuilder.Services.get -> Microsoft.Extensions.DependencyInjection.IServiceCollection
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void
OpenTelemetry.Trace.TracerProviderBuilderBase
OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrumentationName, string instrumentationVersion, System.Func<object> instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ please check the latest changes

## Unreleased

* Moved `IDeferredTracerProviderBuilder` to API library.
([#2058](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2100))

## 1.1.0-beta4

Released 2021-Jun-09
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ public void AddOpenTelemetryTracerProvider_BadArgs_NullServiceCollection()
}));
}

[Fact]
public void AddOpenTelemetryTracerProvider_GetServicesExtension()
{
var services = new ServiceCollection();
services.AddOpenTelemetryTracing(builder => AddMyFeature(builder));

using var serviceProvider = services.BuildServiceProvider();

var tracerProvider = (TracerProviderSdk)serviceProvider.GetRequiredService<TracerProvider>();

Assert.True(tracerProvider.Sampler is TestSampler);
}

[Fact]
public void AddOpenTelemetryTracerProvider_NestedConfigureCallbacks()
{
Expand Down Expand Up @@ -199,6 +212,14 @@ public void AddOpenTelemetryTracerProvider_Idempotent()
Assert.True(testInstrumentation2.Disposed);
}

private static TracerProviderBuilder AddMyFeature(TracerProviderBuilder tracerProviderBuilder)
{
(tracerProviderBuilder.GetServices() ?? throw new NotSupportedException("MyFeature requires a hosting TracerProviderBuilder instance."))
.AddSingleton<TestSampler>();

return tracerProviderBuilder.SetSampler<TestSampler>();
}

internal class TestInstrumentation : IDisposable
{
public bool Disposed { get; private set; }
Expand Down