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

docs: resource-detector should not be tied to tracing #5188

Merged
merged 18 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
13 changes: 13 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "experimental-apis", "experi
docs\diagnostics\experimental-apis\README.md = docs\diagnostics\experimental-apis\README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "resources", "resources", "{A115CE4C-71A8-4B95-96A5-C1DF46FD94C2}"
ProjectSection(SolutionItems) = preProject
docs\resources\README.md = docs\resources\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "extending-the-sdk", "docs\resources\extending-the-sdk\extending-the-sdk.csproj", "{7BE494FC-4B0D-4340-A62A-9C9F3E7389FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -615,6 +622,10 @@ Global
{B4856711-6D4C-4246-A686-49458D4C1301}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4856711-6D4C-4246-A686-49458D4C1301}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4856711-6D4C-4246-A686-49458D4C1301}.Release|Any CPU.Build.0 = Release|Any CPU
{7BE494FC-4B0D-4340-A62A-9C9F3E7389FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7BE494FC-4B0D-4340-A62A-9C9F3E7389FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BE494FC-4B0D-4340-A62A-9C9F3E7389FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BE494FC-4B0D-4340-A62A-9C9F3E7389FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -667,6 +678,8 @@ Global
{B4856711-6D4C-4246-A686-49458D4C1301} = {5B7FB835-3FFF-4BC2-99C5-A5B5FAE3C818}
{52AF6D7D-9E66-4234-9A2C-5D16C6F22B40} = {7C87CAF9-79D7-4C26-9FFB-F3F1FB6911F1}
{17A22B0E-6EC3-4A39-B955-0A486AD06699} = {52AF6D7D-9E66-4234-9A2C-5D16C6F22B40}
{A115CE4C-71A8-4B95-96A5-C1DF46FD94C2} = {7C87CAF9-79D7-4C26-9FFB-F3F1FB6911F1}
{7BE494FC-4B0D-4340-A62A-9C9F3E7389FE} = {A115CE4C-71A8-4B95-96A5-C1DF46FD94C2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521}
Expand Down
1 change: 1 addition & 0 deletions docs/logs/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [Building your own exporter](#exporter)
* [Building your own processor](#processor)
* [Building your own sampler](#sampler)
* [Building your own resource detector](../../resources/README.md#resource-detector)
* [References](#references)

## Exporter
Expand Down
2 changes: 1 addition & 1 deletion docs/metrics/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ It is recommended to model attributes that are static throughout the lifetime of
the process as Resources, instead of adding them as attributes(tags) on each
measurement.

Follow [this](../../trace/extending-the-sdk/README.md#resource-detector) document
Follow [this](../../resources/README.md#resource-detector) document
to learn about writing custom resource detectors.

The snippet below shows configuring the `Resource` associated with the provider.
Expand Down
1 change: 1 addition & 0 deletions docs/metrics/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [Building your own reader](#reader)
* [Building your own exemplar filter](#exemplarfilter)
* [Building your own exemplar reservoir](#exemplarreservoir)
* [Building your own resource detector](../../resources/README.md#resource-detector)
* [References](#references)

## Exporter
Expand Down
109 changes: 109 additions & 0 deletions docs/resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Resources

Copy link
Member

Choose a reason for hiding this comment

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

it would be good to give a background on what is resource etc. here in a follow up.

## Resource Detector

OpenTelemetry .NET SDK provides a resource detector for detecting resource
information from the `OTEL_RESOURCE_ATTRIBUTES` and `OTEL_SERVICE_NAME`
environment variables.

Custom resource detectors can be implemented:

* ResourceDetectors should inherit from
`OpenTelemetry.Resources.IResourceDetector`, (which belongs to the
[OpenTelemetry](../../src/OpenTelemetry/README.md) package), and implement
the `Detect` method.

A demo `ResourceDetector` is shown [here](./extending-the-sdk/MyResourceDetector.cs):

```csharp
using OpenTelemetry.Resources;

internal class MyResourceDetector : IResourceDetector
{
public Resource Detect()
{
var attributes = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("key", "val"),
};

return new Resource(attributes);
}
}
```

There are two different ways to add the custom `ResourceDetector` to the
OTEL signals, via the `Sdk.Create` approach:

```csharp
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace ExtendingTheSdk;

public class Program
{
private static readonly ActivitySource DemoSource = new("OTel.Demo");
private static readonly Meter MeterDemoSource = new("OTel.Demo");

public static void Main()
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("OTel.Demo")
.SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(
new MyResourceDetector()))
.Build();

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(
new MyResourceDetector()))
.Build();

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceBuilder
.CreateDefault().AddDetector(
new MyResourceDetector()));
});
});

using (var foo = DemoSource.StartActivity("Foo"))
{
using (var bar = DemoSource.StartActivity("Bar"))
{
using (var baz = DemoSource.StartActivity("Baz"))
{
}
}
}

var counter = MeterDemoSource.CreateCounter<long>("counter");
for (var i = 0; i < 20000; i++)
counter.Add(1, new("tag1", "value1"), new("tag2", "value2"));

var logger = loggerFactory.CreateLogger("OTel.Demo");
logger
.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
}
}
```

or via `OpenTelemetry.Extensions.Hosting` method:

```csharp
services.AddSingleton<MyResourceDetector>();

services.AddOpenTelemetry()
.ConfigureResource(builder => builder
.AddDetector(sp =>
sp.GetRequiredService<MyResourceDetector>()))
.WithTracing(builder => builder.AddConsoleExporter())
.WithMetrics(builder => builder.AddConsoleExporter());
```
17 changes: 17 additions & 0 deletions docs/resources/extending-the-sdk/MyResourceDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.Resources;

internal class MyResourceDetector : IResourceDetector
{
public Resource Detect()
{
var attributes = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("key", "val"),
};

return new Resource(attributes);
}
}
61 changes: 61 additions & 0 deletions docs/resources/extending-the-sdk/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace ExtendingTheSdk;

public class Program
{
private static readonly ActivitySource DemoSource = new("OTel.Demo");
private static readonly Meter MeterDemoSource = new("OTel.Demo");

public static void Main()
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("OTel.Demo")
.SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(
new MyResourceDetector()))
.Build();

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(
new MyResourceDetector()))
.Build();

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddDetector(
new MyResourceDetector()));
});
});

using (var foo = DemoSource.StartActivity("Foo"))
{
using (var bar = DemoSource.StartActivity("Bar"))
{
using (var baz = DemoSource.StartActivity("Baz"))
{
}
}
}

var counter = MeterDemoSource.CreateCounter<long>("counter");
for (var i = 0; i < 20000; i++)
{
counter.Add(1, new KeyValuePair<string, object?>("tag1", "value1"), new KeyValuePair<string, object?>("tag2", "value2"));
}

var logger = loggerFactory.CreateLogger("OTel.Demo");
logger
.LogInformation("Hello from {Name} {Price}", "tomato", 2.99);
}
}
5 changes: 5 additions & 0 deletions docs/resources/extending-the-sdk/extending-the-sdk.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion docs/trace/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ It is recommended to model attributes that are static throughout the lifetime of
the process as Resources, instead of adding them as attributes(tags) on each
`Activity`.

Follow [this](../extending-the-sdk/README.md#resource-detector) document
Follow [this](../../resources/README.md#resource-detector) document
to learn about writing custom resource detectors.

The snippet below shows configuring the `Resource` associated with the provider.
Expand Down
17 changes: 1 addition & 16 deletions docs/trace/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Quick links:
* [Building your own instrumentation library](#instrumentation-library)
* [Building your own processor](#processor)
* [Building your own sampler](#sampler)
* [Building your own resource detector](#resource-detector)
* [Building your own resource detector](../../resources/README.md#resource-detector)
* [Registration extension method guidance for library authors](#registration-extension-method-guidance-for-library-authors)
* [References](#references)

Expand Down Expand Up @@ -341,21 +341,6 @@ class MySampler : Sampler

A demo sampler is shown [here](./MySampler.cs).

## Resource Detector

OpenTelemetry .NET SDK provides a resource detector for detecting resource
information from the `OTEL_RESOURCE_ATTRIBUTES` and `OTEL_SERVICE_NAME`
environment variables.

Custom resource detectors can be implemented:

* ResourceDetectors should inherit from
`OpenTelemetry.Resources.IResourceDetector`, (which belongs to the
[OpenTelemetry](../../../src/OpenTelemetry/README.md) package), and implement
the `Detect` method.

A demo ResourceDetector is shown [here](./MyResourceDetector.cs).

## Registration extension method guidance for library authors

> [!NOTE]
Expand Down
Loading