-
Notifications
You must be signed in to change notification settings - Fork 765
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
230ceef
docs: `resource-detector` not be tied to tracing
luizhlelis a716387
fix: removing wrong `resource-detector` bookmark
luizhlelis 02a4580
Merge branch 'main' into main
luizhlelis 8b4d024
docs: fixing new resource-detector links
luizhlelis 686ca71
fix: removing resource section tied to the trace
luizhlelis fbd84b6
Merge branch 'main' into main
luizhlelis 6ff7dce
Merge branch 'main' into main
luizhlelis a147131
docs: snippets to add custom `ResourceDetector`
luizhlelis 99e1412
fix: markdown lint for resources doc
luizhlelis 9630d17
fix: add resources doc to solution
luizhlelis 4a13e6c
Merge branch 'main' into main
CodeBlanch 492593a
Merge remote-tracking branch 'upstream/main'
luizhlelis e784a10
Merge remote-tracking branch 'upstream/main'
luizhlelis a3d3c5d
chore: `extending-the-sdk` project for resources
luizhlelis cb4807e
fix: remove warnings braces should not be omitted
luizhlelis 22b8231
chore: formating code
luizhlelis 3edf8a7
Merge remote-tracking branch 'upstream/main'
luizhlelis 733f01c
Merge branch 'main' into main
alanwest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Resources | ||
|
||
## 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()); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.