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 6 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
26 changes: 26 additions & 0 deletions docs/trace/extending-the-sdk/MyResourceDectectorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// <copyright file="MyResourceDectectorExtensions.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 OpenTelemetry.Resources;

internal static class MyResourceDectectorExtensions
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
internal static ResourceBuilder AddMyResourceDetector(this ResourceBuilder resourceBuilder)
{
resourceBuilder.AddDetector(new MyResourceDetector());
return resourceBuilder;
}
}
55 changes: 55 additions & 0 deletions docs/trace/extending-the-sdk/MyResourceDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <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;
using System.Collections.Generic;
using OpenTelemetry.Resources;

internal class MyResourceDetector : IResourceDetector
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
public const string EnVarkey = "myEnVarkey";

public Resource Detect()
{
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
var resource = Resource.Empty;
if (this.LoadString(EnVarkey, out string envAttributeVal))
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
resource = new Resource(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(EnVarkey, envAttributeVal),
});
}

return resource;
}

internal bool LoadString(string envVarKey, out string result)
{
result = null;

try
{
result = Environment.GetEnvironmentVariable(envVarKey);
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}.", ex);
return false;
}

return !string.IsNullOrEmpty(result);
}
}
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().AddMyResourceDetector())
.AddProcessor(new MyProcessor("ProcessorA"))
.AddProcessor(new MyProcessor("ProcessorB"))
.AddProcessor(new SimpleActivityExportProcessor(new MyExporter("ExporterX")))
Expand Down
11 changes: 11 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 resourceDetector](#detector)
* [References](#references)

## Exporter
Expand Down Expand Up @@ -293,6 +294,16 @@ class MySampler : Sampler

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

## ResourceDetector
Custom ResourceDetector 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
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