Skip to content

Commit

Permalink
[DependencyInjection] Introduce new package and refactor SDK (#3923)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Dec 8, 2022
1 parent f0f5158 commit 9836d3a
Show file tree
Hide file tree
Showing 84 changed files with 3,115 additions and 2,048 deletions.
19 changes: 12 additions & 7 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "redaction", "docs\logs\reda
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.OpenTelemetryProtocol.Grpc", "src\OpenTelemetry.Exporter.OpenTelemetryProtocol.Grpc\OpenTelemetry.Exporter.OpenTelemetryProtocol.Grpc.csproj", "{7263001A-49F8-4C3C-AAA8-998F12DAAF64}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.Exporter.Console.Tests", "test\OpenTelemetry.Exporter.Console.Tests\OpenTelemetry.Exporter.Console.Tests.csproj", "{011E70E1-152A-47BB-AF83-12DD12B125ED}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.Console.Tests", "test\OpenTelemetry.Exporter.Console.Tests\OpenTelemetry.Exporter.Console.Tests.csproj", "{011E70E1-152A-47BB-AF83-12DD12B125ED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "getting-started-jaeger", "docs\trace\getting-started-jaeger\getting-started-jaeger.csproj", "{329AD438-6D15-4432-99BE-B0E85F00B3CB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.DependencyInjection", "src\OpenTelemetry.Extensions.DependencyInjection\OpenTelemetry.Extensions.DependencyInjection.csproj", "{171A87CB-393C-4296-913F-E704CD8CEAE9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.DependencyInjection.Tests", "test\OpenTelemetry.Extensions.DependencyInjection.Tests\OpenTelemetry.Extensions.DependencyInjection.Tests.csproj", "{662476AA-5875-4E74-B992-DDF309168EFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -500,10 +502,14 @@ Global
{011E70E1-152A-47BB-AF83-12DD12B125ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{011E70E1-152A-47BB-AF83-12DD12B125ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{011E70E1-152A-47BB-AF83-12DD12B125ED}.Release|Any CPU.Build.0 = Release|Any CPU
{329AD438-6D15-4432-99BE-B0E85F00B3CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{329AD438-6D15-4432-99BE-B0E85F00B3CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{329AD438-6D15-4432-99BE-B0E85F00B3CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{329AD438-6D15-4432-99BE-B0E85F00B3CB}.Release|Any CPU.Build.0 = Release|Any CPU
{171A87CB-393C-4296-913F-E704CD8CEAE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{171A87CB-393C-4296-913F-E704CD8CEAE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{171A87CB-393C-4296-913F-E704CD8CEAE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{171A87CB-393C-4296-913F-E704CD8CEAE9}.Release|Any CPU.Build.0 = Release|Any CPU
{662476AA-5875-4E74-B992-DDF309168EFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{662476AA-5875-4E74-B992-DDF309168EFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{662476AA-5875-4E74-B992-DDF309168EFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{662476AA-5875-4E74-B992-DDF309168EFB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -541,7 +547,6 @@ Global
{9A07D215-90AC-4BAF-BCDB-73D74FD3A5C5} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
{5FDAF679-DE5A-4C73-A49B-8ABCF2399229} = {77C7929A-2EED-4AA6-8705-B5C443C8AA0F}
{A2DF46DE-50D7-4887-8C9D-4BD79CA19FAA} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
{329AD438-6D15-4432-99BE-B0E85F00B3CB} = {5B7FB835-3FFF-4BC2-99C5-A5B5FAE3C818}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521}
Expand Down
1 change: 1 addition & 0 deletions build/Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<MicrosoftCodeAnalysisAnalyzersPkgVer>[3.3.3]</MicrosoftCodeAnalysisAnalyzersPkgVer>
<MicrosoftCodeCoveragePkgVer>[17.3.0]</MicrosoftCodeCoveragePkgVer>
<MicrosoftExtensionsConfigurationEnvironmentVariablesPkgVer>[3.1.0,)</MicrosoftExtensionsConfigurationEnvironmentVariablesPkgVer>
<MicrosoftExtensionsDependencyInjectionAbstractionsPkgVer>[3.1.0,)</MicrosoftExtensionsDependencyInjectionAbstractionsPkgVer>
<MicrosoftExtensionsHostingAbstractionsPkgVer>[2.1.0,)</MicrosoftExtensionsHostingAbstractionsPkgVer>
<MicrosoftExtensionsLoggingPkgVer>[3.1.0,)</MicrosoftExtensionsLoggingPkgVer>
<MicrosoftExtensionsLoggingConfigurationPkgVer>[3.1.0,)</MicrosoftExtensionsLoggingConfigurationPkgVer>
Expand Down
58 changes: 33 additions & 25 deletions docs/trace/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,24 @@ var appBuilder = WebApplication.CreateBuilder(args);

appBuilder.Services.AddSingleton<MyCustomService>();

appBuilder.Services.AddOpenTelemetryTracing(builder => builder
.AddProcessor<MyCustomProcessor>();
appBuilder.Services.AddOpenTelemetry()
.WithTracing(builder => builder
.AddProcessor<MyCustomProcessor>())
.StartWithHost();
```

When using the `AddOpenTelemetryTracing` method the `TracerProvider` does not
own its `IServiceCollection` and instead registers into an existing collection
(typically the collection used is the one managed by the application host). The
`TracerProviderBuilder` will be able to access all services registered into that
collection. For lifecycle management, an [IHostedService
When using the `AddOpenTelemetry` & `WithTracing` extension methods the
`TracerProvider` does not own its `IServiceCollection` and instead registers
into an existing collection (typically the collection used is the one managed by
the application host). The `TracerProviderBuilder` will be able to access all
services registered into that collection. For lifecycle management, the
`StartWithHost` registers an [IHostedService
](https://learn.microsoft.com/dotnet/api/microsoft.extensions.hosting.ihostedservice)
is used to automatically start the `TracerProvider` when the host starts and the
host will automatically shutdown and dispose the `TracerProvider` when it is
shutdown.
which is used to automatically start the `TracerProvider` when the host starts
and the host will automatically shutdown and dispose the `TracerProvider` when
it is shutdown.

**Note:** Multiple calls to `AddOpenTelemetryTracing` will configure the same
**Note:** Multiple calls to `WithTracing` will configure the same
`TracerProvider`. Only a single `TraceProvider` may exist in an
`IServiceCollection` \ `IServiceProvider`.

Expand All @@ -451,15 +454,17 @@ shutdown.
```csharp
var appBuilder = WebApplication.CreateBuilder(args);

appBuilder.Services.AddOpenTelemetryTracing(builder => builder
.ConfigureBuilder((sp, builder) =>
{
builder.AddProcessor(
new MyCustomProcessor(
// Note: This example uses the final IServiceProvider once it is available.
sp.GetRequiredService<MyCustomService>(),
sp.GetRequiredService<IOptions<MyOptions>>().Value));
}));
appBuilder.Services.AddOpenTelemetry()
.WithTracing(builder => builder
.ConfigureBuilder((sp, builder) =>
{
builder.AddProcessor(
new MyCustomProcessor(
// Note: This example uses the final IServiceProvider once it is available.
sp.GetRequiredService<MyCustomService>(),
sp.GetRequiredService<IOptions<MyOptions>>().Value));
}))
.StartWithHost();
```

**Note:** `ConfigureBuilder` is an advanced API and is expected to be used
Expand Down Expand Up @@ -612,8 +617,9 @@ var appBuilder = WebApplication.CreateBuilder(args);
appBuilder.Services.Configure<JaegerExporterOptions>(
appBuilder.Configuration.GetSection("OpenTelemetry:Jaeger"));

appBuilder.Services.AddOpenTelemetryTracing(
builder => builder.AddJaegerExporter());
appBuilder.Services.AddOpenTelemetry()
.WithTracing(builder => builder.AddJaegerExporter())
.StartWithHost();
```

The OpenTelemetry .NET SDK supports running multiple `TracerProvider`s inside
Expand Down Expand Up @@ -654,7 +660,9 @@ appBuilder.Services.Configure<JaegerExporterOptions>(
"JaegerSecondary",
appBuilder.Configuration.GetSection("OpenTelemetry:JaegerSecondary"));

appBuilder.Services.AddOpenTelemetryTracing(builder => builder
.AddJaegerExporter(name: "JaegerPrimary", configure: null)
.AddJaegerExporter(name: "JaegerSecondary", configure: null));
appBuilder.Services.AddOpenTelemetry()
.WithTracing(builder => builder
.AddJaegerExporter(name: "JaegerPrimary", configure: null)
.AddJaegerExporter(name: "JaegerSecondary", configure: null))
.StartWithHost();
```
13 changes: 6 additions & 7 deletions docs/trace/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ the target type for registration extension methods.

The following example shows how a library might enable tracing and metric
support using an `IServiceCollection` extension by calling
`ConfigureOpenTelemetryTracing`.
`ConfigureOpenTelemetryTracerProvider`.

```csharp
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -563,7 +563,7 @@ namespace Microsoft.Extensions.DependencyInjection
}

// Configure OpenTelemetry tracing.
services.ConfigureOpenTelemetryTracing(builder => builder.ConfigureBuilder((sp, builder) =>
services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<MyLibraryOptions>>().Get(name);
if (options.EnableTracing)
Expand All @@ -573,7 +573,7 @@ namespace Microsoft.Extensions.DependencyInjection
}));

// Configure OpenTelemetry metrics.
services.ConfigureOpenTelemetryMetrics(builder => builder.ConfigureBuilder((sp, builder) =>
services.ConfigureOpenTelemetryMeterProvider((sp, builder) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<MyLibraryOptions>>().Get(name);
if (options.EnableMetrics)
Expand Down Expand Up @@ -614,13 +614,12 @@ single `AddMyLibrary` extension to configure the library itself and optionally
turn on OpenTelemetry integration for multiple signals (tracing & metrics in
this case).

**Note:** `ConfigureOpenTelemetryTracing` does not automatically start
OpenTelemetry. The host is responsible for either calling
`AddOpenTelemetryTracing` in the
**Note:** `ConfigureOpenTelemetryTracerProvider` does not automatically start
OpenTelemetry. The host is responsible for either calling `StartWithHost` in the
[OpenTelemetry.Extensions.Hosting](../../../src/OpenTelemetry.Extensions.Hosting/README.md)
package, calling `Build` when using the `Sdk.CreateTracerProviderBuilder`
method, or by accessing the `TracerProvider` from the `IServiceCollection` where
`ConfigureOpenTelemetryTracing` was performed.
`ConfigureOpenTelemetryTracerProvider` was performed.

When providing `IServiceCollection` registration extensions:

Expand Down
Loading

0 comments on commit 9836d3a

Please sign in to comment.