From 230ceef1dbef58fc427c2b0c5c1f762d903ed529 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Sat, 16 Dec 2023 17:44:30 -0300 Subject: [PATCH 01/10] docs: `resource-detector` not be tied to tracing --- docs/logs/extending-the-sdk/README.md | 1 + docs/metrics/extending-the-sdk/README.md | 1 + docs/resources/README.md | 20 ++++++++++++++++++++ docs/trace/extending-the-sdk/README.md | 17 +---------------- 4 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 docs/resources/README.md diff --git a/docs/logs/extending-the-sdk/README.md b/docs/logs/extending-the-sdk/README.md index d18b88624f0..f86387ef749 100644 --- a/docs/logs/extending-the-sdk/README.md +++ b/docs/logs/extending-the-sdk/README.md @@ -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) * [References](#references) ## Exporter diff --git a/docs/metrics/extending-the-sdk/README.md b/docs/metrics/extending-the-sdk/README.md index 4c409961d86..c72190bf8cb 100644 --- a/docs/metrics/extending-the-sdk/README.md +++ b/docs/metrics/extending-the-sdk/README.md @@ -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) * [References](#references) ## Exporter diff --git a/docs/resources/README.md b/docs/resources/README.md new file mode 100644 index 00000000000..84d3ce6f536 --- /dev/null +++ b/docs/resources/README.md @@ -0,0 +1,20 @@ +# Resources + +Quick links: + +* [Building your own resource detector](#resource-detector) + +## 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](../trace/extending-the-sdk/MyResourceDetector.cs). diff --git a/docs/trace/extending-the-sdk/README.md b/docs/trace/extending-the-sdk/README.md index 10ad6b661ad..4d6fb215f49 100644 --- a/docs/trace/extending-the-sdk/README.md +++ b/docs/trace/extending-the-sdk/README.md @@ -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) * [Registration extension method guidance for library authors](#registration-extension-method-guidance-for-library-authors) * [References](#references) @@ -340,21 +340,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** From a71638793e886e63672b7f9097a8c5bafb75b99f Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Sat, 16 Dec 2023 18:03:20 -0300 Subject: [PATCH 02/10] fix: removing wrong `resource-detector` bookmark --- docs/logs/extending-the-sdk/README.md | 2 +- docs/metrics/extending-the-sdk/README.md | 2 +- docs/resources/README.md | 49 ++++++++++++++++++++++++ docs/trace/customizing-the-sdk/README.md | 49 +----------------------- docs/trace/extending-the-sdk/README.md | 2 +- 5 files changed, 53 insertions(+), 51 deletions(-) diff --git a/docs/logs/extending-the-sdk/README.md b/docs/logs/extending-the-sdk/README.md index f86387ef749..55ea13dfaf4 100644 --- a/docs/logs/extending-the-sdk/README.md +++ b/docs/logs/extending-the-sdk/README.md @@ -3,7 +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) +* [Building your own resource detector](../../resources/README.md#resource-detector) * [References](#references) ## Exporter diff --git a/docs/metrics/extending-the-sdk/README.md b/docs/metrics/extending-the-sdk/README.md index c72190bf8cb..28df4c467c5 100644 --- a/docs/metrics/extending-the-sdk/README.md +++ b/docs/metrics/extending-the-sdk/README.md @@ -4,7 +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) +* [Building your own resource detector](../../resources/README.md#resource-detector) * [References](#references) ## Exporter diff --git a/docs/resources/README.md b/docs/resources/README.md index 84d3ce6f536..17884be0d8b 100644 --- a/docs/resources/README.md +++ b/docs/resources/README.md @@ -4,6 +4,55 @@ Quick links: * [Building your own resource detector](#resource-detector) +[Resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md) +is the immutable representation of the entity producing the telemetry. If no +`Resource` is explicitly configured, the +[default](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#semantic-attributes-with-sdk-provided-default-value) +is to use a resource indicating this +[Service](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#service) +and [Telemetry +SDK](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#telemetry-sdk). +The `ConfigureResource` method on `TracerProviderBuilder` can be used to +configure the resource on the provider. `ConfigureResource` accepts an `Action` +to configure the `ResourceBuilder`. Multiple calls to `ConfigureResource` can be +made. When the provider is built, it builds the final `Resource` combining all +the `ConfigureResource` calls. There can only be a single `Resource` associated +with a provider. It is not possible to change the resource builder *after* the +provider is built, by calling the `Build()` method on the +`TracerProviderBuilder`. + +`ResourceBuilder` offers various methods to construct resource comprising of +multiple attributes from various sources. Examples include `AddTelemetrySdk()` +which adds [Telemetry +Sdk](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#telemetry-sdk) +resource, and `AddService()` which adds +[Service](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#service) +resource. It also allows adding `ResourceDetector`s. + +Follow [this](#resource-detector) document +to learn about writing custom resource detectors. + +The snippet below shows configuring the `Resource` associated with the provider. + +```csharp +using OpenTelemetry; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; + +var tracerProvider = Sdk.CreateTracerProviderBuilder() + .ConfigureResource(resourceBuilder => resourceBuilder.AddTelemetrySdk()) + .ConfigureResource(resourceBuilder => resourceBuilder.AddService("service-name")) + .Build(); +``` + +It is also possible to configure the `Resource` by using following +environmental variables: + +| Environment variable | Description | +| -------------------------- | -------------------------------------------------- | +| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. See the [Resource SDK specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable) for more details. | +| `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | + ## Resource Detector OpenTelemetry .NET SDK provides a resource detector for detecting resource diff --git a/docs/trace/customizing-the-sdk/README.md b/docs/trace/customizing-the-sdk/README.md index fd16648c820..d7c8edf1c08 100644 --- a/docs/trace/customizing-the-sdk/README.md +++ b/docs/trace/customizing-the-sdk/README.md @@ -285,54 +285,7 @@ writing custom exporters. ### Resource -[Resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md) -is the immutable representation of the entity producing the telemetry. If no -`Resource` is explicitly configured, the -[default](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#semantic-attributes-with-sdk-provided-default-value) -is to use a resource indicating this -[Service](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#service) -and [Telemetry -SDK](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#telemetry-sdk). -The `ConfigureResource` method on `TracerProviderBuilder` can be used to -configure the resource on the provider. `ConfigureResource` accepts an `Action` -to configure the `ResourceBuilder`. Multiple calls to `ConfigureResource` can be -made. When the provider is built, it builds the final `Resource` combining all -the `ConfigureResource` calls. There can only be a single `Resource` associated -with a provider. It is not possible to change the resource builder *after* the -provider is built, by calling the `Build()` method on the -`TracerProviderBuilder`. - -`ResourceBuilder` offers various methods to construct resource comprising of -multiple attributes from various sources. Examples include `AddTelemetrySdk()` -which adds [Telemetry -Sdk](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#telemetry-sdk) -resource, and `AddService()` which adds -[Service](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#service) -resource. It also allows adding `ResourceDetector`s. - -Follow [this](../extending-the-sdk/README.md#resource-detector) document -to learn about writing custom resource detectors. - -The snippet below shows configuring the `Resource` associated with the provider. - -```csharp -using OpenTelemetry; -using OpenTelemetry.Resources; -using OpenTelemetry.Trace; - -var tracerProvider = Sdk.CreateTracerProviderBuilder() - .ConfigureResource(resourceBuilder => resourceBuilder.AddTelemetrySdk()) - .ConfigureResource(resourceBuilder => resourceBuilder.AddService("service-name")) - .Build(); -``` - -It is also possible to configure the `Resource` by using following -environmental variables: - -| Environment variable | Description | -| -------------------------- | -------------------------------------------------- | -| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. See the [Resource SDK specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable) for more details. | -| `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | +Information about resources is shown [here](../../resources/README.md). ### Samplers diff --git a/docs/trace/extending-the-sdk/README.md b/docs/trace/extending-the-sdk/README.md index 4d6fb215f49..425231fd4b4 100644 --- a/docs/trace/extending-the-sdk/README.md +++ b/docs/trace/extending-the-sdk/README.md @@ -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](../../resources/README.md) +* [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) From 8b4d02467f6c1b6bed312a090063860435f49b61 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Mon, 18 Dec 2023 19:42:58 -0300 Subject: [PATCH 03/10] docs: fixing new resource-detector links --- docs/metrics/customizing-the-sdk/README.md | 2 +- docs/trace/customizing-the-sdk/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/metrics/customizing-the-sdk/README.md b/docs/metrics/customizing-the-sdk/README.md index 148d470f0aa..7c0d11cc0e6 100644 --- a/docs/metrics/customizing-the-sdk/README.md +++ b/docs/metrics/customizing-the-sdk/README.md @@ -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. diff --git a/docs/trace/customizing-the-sdk/README.md b/docs/trace/customizing-the-sdk/README.md index 76f4145d2e3..86b5de43744 100644 --- a/docs/trace/customizing-the-sdk/README.md +++ b/docs/trace/customizing-the-sdk/README.md @@ -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. From 686ca7178363d16fa7562de382efb4bb53468210 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Thu, 21 Dec 2023 20:21:57 -0300 Subject: [PATCH 04/10] fix: removing resource section tied to the trace --- docs/resources/README.md | 53 ---------------------------------------- 1 file changed, 53 deletions(-) diff --git a/docs/resources/README.md b/docs/resources/README.md index 17884be0d8b..bacf1a324f5 100644 --- a/docs/resources/README.md +++ b/docs/resources/README.md @@ -1,58 +1,5 @@ # Resources -Quick links: - -* [Building your own resource detector](#resource-detector) - -[Resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md) -is the immutable representation of the entity producing the telemetry. If no -`Resource` is explicitly configured, the -[default](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#semantic-attributes-with-sdk-provided-default-value) -is to use a resource indicating this -[Service](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#service) -and [Telemetry -SDK](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#telemetry-sdk). -The `ConfigureResource` method on `TracerProviderBuilder` can be used to -configure the resource on the provider. `ConfigureResource` accepts an `Action` -to configure the `ResourceBuilder`. Multiple calls to `ConfigureResource` can be -made. When the provider is built, it builds the final `Resource` combining all -the `ConfigureResource` calls. There can only be a single `Resource` associated -with a provider. It is not possible to change the resource builder *after* the -provider is built, by calling the `Build()` method on the -`TracerProviderBuilder`. - -`ResourceBuilder` offers various methods to construct resource comprising of -multiple attributes from various sources. Examples include `AddTelemetrySdk()` -which adds [Telemetry -Sdk](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#telemetry-sdk) -resource, and `AddService()` which adds -[Service](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#service) -resource. It also allows adding `ResourceDetector`s. - -Follow [this](#resource-detector) document -to learn about writing custom resource detectors. - -The snippet below shows configuring the `Resource` associated with the provider. - -```csharp -using OpenTelemetry; -using OpenTelemetry.Resources; -using OpenTelemetry.Trace; - -var tracerProvider = Sdk.CreateTracerProviderBuilder() - .ConfigureResource(resourceBuilder => resourceBuilder.AddTelemetrySdk()) - .ConfigureResource(resourceBuilder => resourceBuilder.AddService("service-name")) - .Build(); -``` - -It is also possible to configure the `Resource` by using following -environmental variables: - -| Environment variable | Description | -| -------------------------- | -------------------------------------------------- | -| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. See the [Resource SDK specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable) for more details. | -| `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. | - ## Resource Detector OpenTelemetry .NET SDK provides a resource detector for detecting resource From a1471319ae02b0c98b14dd1534f1f481f6ce7d67 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Sat, 23 Dec 2023 17:12:03 -0300 Subject: [PATCH 05/10] 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()); +``` From 99e1412e41125972090688f1ccbc46e1f30db62b Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Sat, 23 Dec 2023 17:36:33 -0300 Subject: [PATCH 06/10] fix: markdown lint for resources doc --- docs/resources/README.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/docs/resources/README.md b/docs/resources/README.md index c93a37a1530..f71be62d057 100644 --- a/docs/resources/README.md +++ b/docs/resources/README.md @@ -32,7 +32,8 @@ internal class MyResourceDetector : IResourceDetector } ``` -There are two different ways to add the custom `ResourceDetector` to the OTEL signals, via the `Sdk.Create` approach: +There are two different ways to add the custom `ResourceDetector` to the +OTEL signals, via the `Sdk.Create` approach: ```csharp using System.Diagnostics; @@ -54,19 +55,22 @@ public class Program { using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("OTel.Demo") - .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(new MyResourceDetector())) + .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector( + new MyResourceDetector())) .Build(); using var meterProvider = Sdk.CreateMeterProviderBuilder() - .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(new MyResourceDetector())) + .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector( + new MyResourceDetector())) .Build(); using var loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(options => { - options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddDetector( - new MyResourceDetector())); + options.SetResourceBuilder(ResourceBuilder + .CreateDefault().AddDetector( + new MyResourceDetector())); }); }); @@ -85,7 +89,8 @@ public class Program counter.Add(1, new("tag1", "value1"), new("tag2", "value2")); var logger = loggerFactory.CreateLogger("OTel.Demo"); - logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99); + logger + .LogInformation("Hello from {name} {price}.", "tomato", 2.99); } } ``` @@ -96,8 +101,9 @@ or via `OpenTelemetry.Extensions.Hosting` method: services.AddSingleton(); services.AddOpenTelemetry() - .ConfigureResource(builder => - builder.AddDetector(sp => sp.GetRequiredService())) + .ConfigureResource(builder => builder + .AddDetector(sp => + sp.GetRequiredService())) .WithTracing(builder => builder.AddConsoleExporter()) .WithMetrics(builder => builder.AddConsoleExporter()); ``` From 9630d178b4eeb6ff8547053f14aa8b95435e8a51 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Sat, 23 Dec 2023 17:36:45 -0300 Subject: [PATCH 07/10] fix: add resources doc to solution --- OpenTelemetry.sln | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/OpenTelemetry.sln b/OpenTelemetry.sln index 52835a5b4ca..576b863cc63 100644 --- a/OpenTelemetry.sln +++ b/OpenTelemetry.sln @@ -337,6 +337,11 @@ 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 Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -666,6 +671,7 @@ 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} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521} From a3d3c5d00a17f3976cd2f60b328d4774b7014c0f Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Fri, 12 Jan 2024 23:32:35 -0300 Subject: [PATCH 08/10] chore: `extending-the-sdk` project for resources --- OpenTelemetry.sln | 7 +++ docs/resources/README.md | 4 +- .../extending-the-sdk/MyResourceDetector.cs | 17 ++++++ docs/resources/extending-the-sdk/Program.cs | 60 +++++++++++++++++++ .../extending-the-sdk.csproj | 5 ++ 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 docs/resources/extending-the-sdk/MyResourceDetector.cs create mode 100644 docs/resources/extending-the-sdk/Program.cs create mode 100644 docs/resources/extending-the-sdk/extending-the-sdk.csproj diff --git a/OpenTelemetry.sln b/OpenTelemetry.sln index 3196a2b0c86..f1f6718f04f 100644 --- a/OpenTelemetry.sln +++ b/OpenTelemetry.sln @@ -338,6 +338,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "resources", "resources", "{ 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 @@ -620,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 @@ -673,6 +679,7 @@ Global {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} diff --git a/docs/resources/README.md b/docs/resources/README.md index f71be62d057..059acbc063d 100644 --- a/docs/resources/README.md +++ b/docs/resources/README.md @@ -13,7 +13,7 @@ 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](./extending-the-sdk/MyResourceDetector.cs): ```csharp using OpenTelemetry.Resources; @@ -49,7 +49,7 @@ namespace ExtendingTheSdk; public class Program { private static readonly ActivitySource DemoSource = new("OTel.Demo"); - private static readonly Meter meterDemoSource = new("OTel.Demo"); + private static readonly Meter MeterDemoSource = new("OTel.Demo"); public static void Main() { diff --git a/docs/resources/extending-the-sdk/MyResourceDetector.cs b/docs/resources/extending-the-sdk/MyResourceDetector.cs new file mode 100644 index 00000000000..df40d9b3302 --- /dev/null +++ b/docs/resources/extending-the-sdk/MyResourceDetector.cs @@ -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> + { + new KeyValuePair("key", "val"), + }; + + return new Resource(attributes); + } +} diff --git a/docs/resources/extending-the-sdk/Program.cs b/docs/resources/extending-the-sdk/Program.cs new file mode 100644 index 00000000000..85f1e073a86 --- /dev/null +++ b/docs/resources/extending-the-sdk/Program.cs @@ -0,0 +1,60 @@ +// 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("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); + } +} diff --git a/docs/resources/extending-the-sdk/extending-the-sdk.csproj b/docs/resources/extending-the-sdk/extending-the-sdk.csproj new file mode 100644 index 00000000000..85aab7a7ed3 --- /dev/null +++ b/docs/resources/extending-the-sdk/extending-the-sdk.csproj @@ -0,0 +1,5 @@ + + + + + From cb4807eec43168b314571b2d20b1d919fdfe6e09 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Fri, 12 Jan 2024 23:42:02 -0300 Subject: [PATCH 09/10] fix: remove warnings braces should not be omitted --- docs/resources/extending-the-sdk/Program.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/resources/extending-the-sdk/Program.cs b/docs/resources/extending-the-sdk/Program.cs index 85f1e073a86..77e532a392c 100644 --- a/docs/resources/extending-the-sdk/Program.cs +++ b/docs/resources/extending-the-sdk/Program.cs @@ -51,10 +51,12 @@ public static void Main() var counter = MeterDemoSource.CreateCounter("counter"); for (var i = 0; i < 20000; i++) - counter.Add(1, new("tag1", "value1"), new("tag2", "value2")); + { + counter.Add(1, new KeyValuePair("tag1", "value1"), new KeyValuePair("tag2", "value2")); + } var logger = loggerFactory.CreateLogger("OTel.Demo"); logger - .LogInformation("Hello from {name} {price}.", "tomato", 2.99); + .LogInformation("Hello from {Name} {Price}", "tomato", 2.99); } } From 22b8231b350dc4067eff695cd35318082857ed32 Mon Sep 17 00:00:00 2001 From: Luiz Lelis Date: Sat, 13 Jan 2024 16:18:16 -0300 Subject: [PATCH 10/10] chore: formating code --- docs/resources/extending-the-sdk/Program.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/resources/extending-the-sdk/Program.cs b/docs/resources/extending-the-sdk/Program.cs index 77e532a392c..849bdcc182b 100644 --- a/docs/resources/extending-the-sdk/Program.cs +++ b/docs/resources/extending-the-sdk/Program.cs @@ -33,9 +33,8 @@ public static void Main() { builder.AddOpenTelemetry(options => { - options.SetResourceBuilder(ResourceBuilder - .CreateDefault().AddDetector( - new MyResourceDetector())); + options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddDetector( + new MyResourceDetector())); }); });