Skip to content

Commit

Permalink
docs: snippets to add custom ResourceDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
luizhlelis committed Dec 23, 2023
1 parent 6ff7dce commit a147131
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion docs/resources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<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());
```

0 comments on commit a147131

Please sign in to comment.