From b538b9b9d42da38acdb4c79255fcbe607507084e Mon Sep 17 00:00:00 2001 From: Yun-Ting Lin Date: Wed, 2 Mar 2022 18:05:57 -0800 Subject: [PATCH] Publicize required methods to allow for custom implementations of resource detectors. (#2949) --- .../extending-the-sdk/MyResourceDetector.cs | 31 +++++++++++++++++++ docs/trace/extending-the-sdk/Program.cs | 2 ++ docs/trace/extending-the-sdk/README.md | 16 ++++++++++ .../.publicApi/net461/PublicAPI.Unshipped.txt | 2 ++ .../netstandard2.0/PublicAPI.Unshipped.txt | 2 ++ src/OpenTelemetry/CHANGELOG.md | 4 +++ src/OpenTelemetry/Resources/Resource.cs | 2 +- .../Resources/ResourceBuilder.cs | 4 +-- 8 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 docs/trace/extending-the-sdk/MyResourceDetector.cs diff --git a/docs/trace/extending-the-sdk/MyResourceDetector.cs b/docs/trace/extending-the-sdk/MyResourceDetector.cs new file mode 100644 index 00000000000..d78f7c8ffc6 --- /dev/null +++ b/docs/trace/extending-the-sdk/MyResourceDetector.cs @@ -0,0 +1,31 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System.Collections.Generic; +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/trace/extending-the-sdk/Program.cs b/docs/trace/extending-the-sdk/Program.cs index d22781033f5..f518fe140f8 100644 --- a/docs/trace/extending-the-sdk/Program.cs +++ b/docs/trace/extending-the-sdk/Program.cs @@ -16,6 +16,7 @@ using System.Diagnostics; using OpenTelemetry; +using OpenTelemetry.Resources; using OpenTelemetry.Trace; public class Program @@ -27,6 +28,7 @@ public static void Main() using var tracerProvider = Sdk.CreateTracerProviderBuilder() .SetSampler(new MySampler()) .AddSource("OTel.Demo") + .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(new MyResourceDetector())) .AddProcessor(new MyProcessor("ProcessorA")) .AddProcessor(new MyProcessor("ProcessorB")) .AddProcessor(new SimpleActivityExportProcessor(new MyExporter("ExporterX"))) diff --git a/docs/trace/extending-the-sdk/README.md b/docs/trace/extending-the-sdk/README.md index 3234dcba210..923be6682c2 100644 --- a/docs/trace/extending-the-sdk/README.md +++ b/docs/trace/extending-the-sdk/README.md @@ -4,6 +4,7 @@ * [Building your own instrumentation library](#instrumentation-library) * [Building your own processor](#processor) * [Building your own sampler](#sampler) +* [Building your own resource detector](#resource-detector) * [References](#references) ## Exporter @@ -293,6 +294,21 @@ 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). + ## References * [Exporter diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index bee50adc683..3e246c44335 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -114,6 +114,8 @@ OpenTelemetry.ReadOnlyTagCollection.GetEnumerator() -> OpenTelemetry.ReadOnlyTag OpenTelemetry.ReadOnlyTagCollection.ReadOnlyTagCollection() -> void OpenTelemetry.Resources.IResourceDetector OpenTelemetry.Resources.IResourceDetector.Detect() -> OpenTelemetry.Resources.Resource +OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable> attributes) -> void +OpenTelemetry.Resources.ResourceBuilder.AddDetector(OpenTelemetry.Resources.IResourceDetector resourceDetector) -> OpenTelemetry.Resources.ResourceBuilder OpenTelemetry.Trace.BatchExportActivityProcessorOptions OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void override OpenTelemetry.BaseExportProcessor.OnForceFlush(int timeoutMilliseconds) -> bool diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index bee50adc683..3e246c44335 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -114,6 +114,8 @@ OpenTelemetry.ReadOnlyTagCollection.GetEnumerator() -> OpenTelemetry.ReadOnlyTag OpenTelemetry.ReadOnlyTagCollection.ReadOnlyTagCollection() -> void OpenTelemetry.Resources.IResourceDetector OpenTelemetry.Resources.IResourceDetector.Detect() -> OpenTelemetry.Resources.Resource +OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable> attributes) -> void +OpenTelemetry.Resources.ResourceBuilder.AddDetector(OpenTelemetry.Resources.IResourceDetector resourceDetector) -> OpenTelemetry.Resources.ResourceBuilder OpenTelemetry.Trace.BatchExportActivityProcessorOptions OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void override OpenTelemetry.BaseExportProcessor.OnForceFlush(int timeoutMilliseconds) -> bool diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index ef6dcfff080..7fc2ded97a5 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Publicize required methods to allow for custom implementations of resource + detectors. + ([2949](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2949/)) + * Make `IResourceDetector` public to allow custom implementations of resource detectors. ([2897](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2897)) diff --git a/src/OpenTelemetry/Resources/Resource.cs b/src/OpenTelemetry/Resources/Resource.cs index 1ec6bda90fc..28207ac1fab 100644 --- a/src/OpenTelemetry/Resources/Resource.cs +++ b/src/OpenTelemetry/Resources/Resource.cs @@ -35,7 +35,7 @@ public class Resource /// Initializes a new instance of the class. /// /// An of attributes that describe the resource. - internal Resource(IEnumerable> attributes) + public Resource(IEnumerable> attributes) { if (attributes == null) { diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs index 43a605dfe34..3d48c248af7 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilder.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs @@ -86,9 +86,7 @@ public Resource Build() return finalResource; } - // Internal until spec is finalized. - // https://github.com/open-telemetry/oteps/blob/master/text/0111-auto-resource-detection.md - internal ResourceBuilder AddDetector(IResourceDetector resourceDetector) + public ResourceBuilder AddDetector(IResourceDetector resourceDetector) { Guard.ThrowIfNull(resourceDetector);