Skip to content

Commit b538b9b

Browse files
authored
Publicize required methods to allow for custom implementations of resource detectors. (#2949)
1 parent 1afbb7c commit b538b9b

File tree

8 files changed

+59
-4
lines changed

8 files changed

+59
-4
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// <copyright file="MyResourceDetector.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using System.Collections.Generic;
18+
using OpenTelemetry.Resources;
19+
20+
internal class MyResourceDetector : IResourceDetector
21+
{
22+
public Resource Detect()
23+
{
24+
var attributes = new List<KeyValuePair<string, object>>
25+
{
26+
new KeyValuePair<string, object>("key", "val"),
27+
};
28+
29+
return new Resource(attributes);
30+
}
31+
}

docs/trace/extending-the-sdk/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System.Diagnostics;
1818
using OpenTelemetry;
19+
using OpenTelemetry.Resources;
1920
using OpenTelemetry.Trace;
2021

2122
public class Program
@@ -27,6 +28,7 @@ public static void Main()
2728
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
2829
.SetSampler(new MySampler())
2930
.AddSource("OTel.Demo")
31+
.SetResourceBuilder(ResourceBuilder.CreateEmpty().AddDetector(new MyResourceDetector()))
3032
.AddProcessor(new MyProcessor("ProcessorA"))
3133
.AddProcessor(new MyProcessor("ProcessorB"))
3234
.AddProcessor(new SimpleActivityExportProcessor(new MyExporter("ExporterX")))

docs/trace/extending-the-sdk/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* [Building your own instrumentation library](#instrumentation-library)
55
* [Building your own processor](#processor)
66
* [Building your own sampler](#sampler)
7+
* [Building your own resource detector](#resource-detector)
78
* [References](#references)
89

910
## Exporter
@@ -293,6 +294,21 @@ class MySampler : Sampler
293294

294295
A demo sampler is shown [here](./MySampler.cs).
295296

297+
## Resource Detector
298+
299+
OpenTelemetry .NET SDK provides a resource detector for detecting
300+
resource information from the `OTEL_RESOURCE_ATTRIBUTES` and
301+
`OTEL_SERVICE_NAME` environment variables.
302+
303+
Custom resource detectors can be implemented:
304+
305+
* ResourceDetectors should inherit from
306+
`OpenTelemetry.Resources.IResourceDetector`, (which belongs
307+
to the [OpenTelemetry](../../../src/OpenTelemetry/README.md)
308+
package), and implement the `Detect` method.
309+
310+
A demo ResourceDetector is shown [here](./MyResourceDetector.cs).
311+
296312
## References
297313

298314
* [Exporter

src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ OpenTelemetry.ReadOnlyTagCollection.GetEnumerator() -> OpenTelemetry.ReadOnlyTag
114114
OpenTelemetry.ReadOnlyTagCollection.ReadOnlyTagCollection() -> void
115115
OpenTelemetry.Resources.IResourceDetector
116116
OpenTelemetry.Resources.IResourceDetector.Detect() -> OpenTelemetry.Resources.Resource
117+
OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
118+
OpenTelemetry.Resources.ResourceBuilder.AddDetector(OpenTelemetry.Resources.IResourceDetector resourceDetector) -> OpenTelemetry.Resources.ResourceBuilder
117119
OpenTelemetry.Trace.BatchExportActivityProcessorOptions
118120
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void
119121
override OpenTelemetry.BaseExportProcessor<T>.OnForceFlush(int timeoutMilliseconds) -> bool

src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ OpenTelemetry.ReadOnlyTagCollection.GetEnumerator() -> OpenTelemetry.ReadOnlyTag
114114
OpenTelemetry.ReadOnlyTagCollection.ReadOnlyTagCollection() -> void
115115
OpenTelemetry.Resources.IResourceDetector
116116
OpenTelemetry.Resources.IResourceDetector.Detect() -> OpenTelemetry.Resources.Resource
117+
OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
118+
OpenTelemetry.Resources.ResourceBuilder.AddDetector(OpenTelemetry.Resources.IResourceDetector resourceDetector) -> OpenTelemetry.Resources.ResourceBuilder
117119
OpenTelemetry.Trace.BatchExportActivityProcessorOptions
118120
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void
119121
override OpenTelemetry.BaseExportProcessor<T>.OnForceFlush(int timeoutMilliseconds) -> bool

src/OpenTelemetry/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
* Publicize required methods to allow for custom implementations of resource
6+
detectors.
7+
([2949](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2949/))
8+
59
* Make `IResourceDetector` public to allow custom implementations of resource
610
detectors.
711
([2897](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2897))

src/OpenTelemetry/Resources/Resource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class Resource
3535
/// Initializes a new instance of the <see cref="Resource"/> class.
3636
/// </summary>
3737
/// <param name="attributes">An <see cref="IEnumerable{T}"/> of attributes that describe the resource.</param>
38-
internal Resource(IEnumerable<KeyValuePair<string, object>> attributes)
38+
public Resource(IEnumerable<KeyValuePair<string, object>> attributes)
3939
{
4040
if (attributes == null)
4141
{

src/OpenTelemetry/Resources/ResourceBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ public Resource Build()
8686
return finalResource;
8787
}
8888

89-
// Internal until spec is finalized.
90-
// https://github.com/open-telemetry/oteps/blob/master/text/0111-auto-resource-detection.md
91-
internal ResourceBuilder AddDetector(IResourceDetector resourceDetector)
89+
public ResourceBuilder AddDetector(IResourceDetector resourceDetector)
9290
{
9391
Guard.ThrowIfNull(resourceDetector);
9492

0 commit comments

Comments
 (0)