From a1471319ae02b0c98b14dd1534f1f481f6ce7d67 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Sat, 23 Dec 2023 17:12:03 -0300 Subject: [PATCH] docs: snippets to add custom `ResourceDetector` --- docs/resources/README.md | 89 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/docs/resources/README.md b/docs/resources/README.md index bacf1a324f5..c93a37a1530 100644 --- a/docs/resources/README.md +++ b/docs/resources/README.md @@ -13,4 +13,91 @@ Custom resource detectors can be implemented: [OpenTelemetry](../../src/OpenTelemetry/README.md) package), and implement the `Detect` method. -A demo ResourceDetector is shown [here](../trace/extending-the-sdk/MyResourceDetector.cs). +A demo `ResourceDetector` is shown [here](../trace/extending-the-sdk/MyResourceDetector.cs): + +```csharp +using OpenTelemetry.Resources; + +internal class MyResourceDetector : IResourceDetector +{ + public Resource Detect() + { + var attributes = new List> + { + new KeyValuePair("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("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(); + + services.AddOpenTelemetry() + .ConfigureResource(builder => + builder.AddDetector(sp => sp.GetRequiredService())) + .WithTracing(builder => builder.AddConsoleExporter()) + .WithMetrics(builder => builder.AddConsoleExporter()); +```