Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publicize required methods to allow for custom implementations of resource detectors. #2949

Merged
merged 20 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/trace/extending-the-sdk/MyResourceDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="MyResourceDetector.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>

using System.Collections.Generic;
using OpenTelemetry.Resources;

internal class MyResourceDetector : IResourceDetector
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
public Resource Detect()
{
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
var attributes = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("key", "val"),
};

return new Resource(attributes);
}
}
2 changes: 2 additions & 0 deletions docs/trace/extending-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

public class Program
Expand All @@ -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()))
Copy link
Member

@reyang reyang Mar 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related to this PR, this line seems complex, maybe it could be simplified to ResourceBuilder.AddDetector(new MyResourceDetector())? (by tweaking the static method)

.AddProcessor(new MyProcessor("ProcessorA"))
.AddProcessor(new MyProcessor("ProcessorB"))
.AddProcessor(new SimpleActivityExportProcessor(new MyExporter("ExporterX")))
Expand Down
16 changes: 16 additions & 0 deletions docs/trace/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
OpenTelemetry.Resources.ResourceBuilder.AddDetector(OpenTelemetry.Resources.IResourceDetector resourceDetector) -> OpenTelemetry.Resources.ResourceBuilder
OpenTelemetry.Trace.BatchExportActivityProcessorOptions
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void
override OpenTelemetry.BaseExportProcessor<T>.OnForceFlush(int timeoutMilliseconds) -> bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
OpenTelemetry.Resources.ResourceBuilder.AddDetector(OpenTelemetry.Resources.IResourceDetector resourceDetector) -> OpenTelemetry.Resources.ResourceBuilder
OpenTelemetry.Trace.BatchExportActivityProcessorOptions
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void
override OpenTelemetry.BaseExportProcessor<T>.OnForceFlush(int timeoutMilliseconds) -> bool
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Resources/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Resource
/// Initializes a new instance of the <see cref="Resource"/> class.
/// </summary>
/// <param name="attributes">An <see cref="IEnumerable{T}"/> of attributes that describe the resource.</param>
internal Resource(IEnumerable<KeyValuePair<string, object>> attributes)
public Resource(IEnumerable<KeyValuePair<string, object>> attributes)
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
if (attributes == null)
{
Expand Down
4 changes: 1 addition & 3 deletions src/OpenTelemetry/Resources/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down