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

Add better documentation for IResourceDetector and OltpExporter configuration #4230

Merged
merged 27 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
145ff7f
Added IServiceProvider option to callbacks for hosting extensions
cromefire Feb 24, 2023
5c86fc1
Added changelog entry
cromefire Feb 24, 2023
c6dc65d
Reverted changes to metrics and tracing
cromefire Feb 24, 2023
b7f6156
Removed unneeded stuff from the public API
cromefire Feb 24, 2023
12ef20c
Swapped params
cromefire Feb 24, 2023
5da158c
Added more documentation on the option configuration
cromefire Feb 24, 2023
796cad8
Made the AddDetector variant for the ResolvingResourceDetector availa…
cromefire Feb 24, 2023
dbbe47f
Added more documentation to the IResourceDetector
cromefire Feb 24, 2023
7e5ef07
Fixed APIs
cromefire Feb 24, 2023
b53a42c
Fixed Changelog
cromefire Feb 24, 2023
2c242c5
Removed unneeded version of ConfigureResource
cromefire Feb 24, 2023
7415513
Removed extra formatting
cromefire Feb 24, 2023
c68f85b
Merge remote-tracking branch 'upsteam/main' into service-provider-cal…
cromefire Feb 25, 2023
2847aa6
Reverted public API change
cromefire Mar 6, 2023
2b4831b
Reverted note
cromefire Mar 6, 2023
261ea06
Reverted API doc changes
cromefire Mar 6, 2023
c7c6d09
Merge remote-tracking branch 'upsteam/main' into service-provider-cal…
cromefire Mar 6, 2023
290d486
Improved resource detector docs
cromefire Mar 6, 2023
ba2edeb
Satisfied markdownlint
cromefire Mar 6, 2023
11a81a3
Merge branch 'main' into service-provider-callbacks
cromefire Mar 6, 2023
138fff0
Merge branch 'main' into service-provider-callbacks
cijothomas Mar 7, 2023
f7f0488
Merge branch 'main' into service-provider-callbacks
cromefire Mar 7, 2023
6d81fe4
Added dependency injection to sample
cromefire Mar 30, 2023
9a189f2
Merge remote-tracking branch 'origin/service-provider-callbacks' into…
cromefire Mar 30, 2023
31fc495
Merge branch 'main' into service-provider-callbacks
cromefire Mar 30, 2023
afef252
Merge branch 'main' into service-provider-callbacks
CodeBlanch Mar 31, 2023
db3d00f
Merge branch 'main' into service-provider-callbacks
CodeBlanch Mar 31, 2023
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
Expand Up @@ -67,7 +67,14 @@ 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) => {
// ...
});
```
cromefire marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
22 changes: 22 additions & 0 deletions src/OpenTelemetry.Extensions.Hosting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ 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
{
public Resource Detect()
{
return ResourceBuilder.CreateEmpty()
.AddService("your service name")
.Build();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we made this slightly more interesting by showing something being injected?

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();
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The environment is injected, I can add something else too if you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cromefire I don't see any changes on the diff did you forget to push maybe? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I'm just too stupid to use the GitHub app... Will fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


services.AddSingleton<MyResourceDetector>();

services.AddOpenTelemetry()
.ConfigureResource(builder =>
builder.AddDetector(sp => sp!.GetRequiredService<MyResourceDetector>()))
WithTracing(builder => builder.AddConsoleExporter());
cromefire marked this conversation as resolved.
Show resolved Hide resolved
```

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

Pre-release versions (all versions prior to 1.4.0) of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func<IServiceProvider, IResourceDetector!>! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func<IServiceProvider, IResourceDetector!>! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func<IServiceProvider, IResourceDetector!>! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func<IServiceProvider, IResourceDetector!>! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder!
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Released 2023-Feb-10
could be removed.
([#4174](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4174))

* Made the `IServiceProvider` variant of `AddDetector` publicly available.
([#4230](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4230))

## 1.4.0-rc.3

Released 2023-Feb-01
Expand Down
3 changes: 1 addition & 2 deletions src/OpenTelemetry/Resources/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ public ResourceBuilder AddDetector(IResourceDetector resourceDetector)
/// </remarks>
/// <param name="resourceDetectorFactory">Resource detector factory.</param>
/// <returns>Supplied <see cref="ResourceBuilder"/> for call chaining.</returns>
// Note: This API may be made public if there is a need for it.
internal ResourceBuilder AddDetector(Func<IServiceProvider?, IResourceDetector> resourceDetectorFactory)
public ResourceBuilder AddDetector(Func<IServiceProvider?, IResourceDetector> resourceDetectorFactory)
{
Guard.ThrowIfNull(resourceDetectorFactory);

Expand Down