Skip to content

Commit

Permalink
Add better documentation for IResourceDetector and OltpExporter confi…
Browse files Browse the repository at this point in the history
…guration (#4230)

Co-authored-by: Cijo Thomas <[email protected]>
Co-authored-by: Mikel Blanchard <[email protected]>
  • Loading branch information
3 people authored Mar 31, 2023
1 parent 6918866 commit c5ddf5a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ and environment variables.
The `OtlpExporterOptions` type setters take precedence over the environment variables.

This can be achieved by providing an `Action<OtlpExporterOptions>` delegate to the
`AddOtlpExporter()` method.
`AddOtlpExporter()` method or using `AddOptions<OtlpExporterOptions>()`.

If additional services from the dependency injection are required, they can be
configured like this:

```csharp
services.AddOptions<OtlpExporterOptions>().Configure<Service>((opts, svc) => {
// ...
});
```

TODO: Show metrics specific configuration (i.e MetricReaderOptions).

Expand Down
35 changes: 35 additions & 0 deletions src/OpenTelemetry.Extensions.Hosting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,41 @@ app.Run();
A fully functional example can be found
[here](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/examples/AspNetCore).

### Resources

To dynamically add resources at startup from the dependency injection you can
provide an `IResourceDetector`.
To make use of it add it to the dependency injection and they you can use the
`ISerivceProvider` add it to OpenTelemetry:

```csharp
public class MyResourceDetector : IResourceDetector
{
private readonly IWebHostEnvironment webHostEnvironment;

public MyResourceDetector(IWebHostEnvironment webHostEnvironment)
{
this.webHostEnvironment = webHostEnvironment;
}

public Resource Detect()
{
return ResourceBuilder.CreateEmpty()
.AddService(serviceName: this.webHostEnvironment.ApplicationName)
.AddAttributes(new Dictionary<string, object> { ["host.environment"] = this.webHostEnvironment.EnvironmentName })
.Build();
}
}

services.AddSingleton<MyResourceDetector>();

services.AddOpenTelemetry()
.ConfigureResource(builder =>
builder.AddDetector(sp => sp.GetRequiredService<MyResourceDetector>()))
.WithTracing(builder => builder.AddConsoleExporter())
.WithMetrics(builder => builder.AddConsoleExporter());
```

## Migrating from pre-release versions of OpenTelemetry.Extensions.Hosting

Pre-release versions (all versions prior to 1.4.0) of
Expand Down

0 comments on commit c5ddf5a

Please sign in to comment.