Skip to content

Commit

Permalink
Container resource detectors + manage resource detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek committed Apr 6, 2023
1 parent 44a24c4 commit 826715c
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .cspell/other.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ MASSTRANSIT
HTTPCLIENT
GRPCNETCLIENT
ENTITYFRAMEWORKCORE
ENVIRONMENTALVARIABLES
TELEMETRYSDK
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h
ensuring back off instead of crashing, improving overall stability.
- The environment variable `OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED` could be
used to enable or disable the rule engine.
- Support for Container resource detector.
- Support for enabling well known resource detectors such by
- `OTEL_DOTNET_AUTO_RESOURCE_DETECTOR_ENABLED`
- `OTEL_DOTNET_AUTO_{0}_RESOURCE_DETECTOR_ENABLED`.

### Changed

Expand Down
24 changes: 20 additions & 4 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,23 @@ However, if given setting supports it, then:

A resource is the immutable representation of the entity producing the telemetry.
See [Resource semantic conventions](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions)
for more details.
for more details. Resource are detected by resource detectors.

| Environment variable | Description | Default value | Status |
|--------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------|
| `OTEL_DOTNET_AUTO_RESOURCE_DETECTOR_ENABLED` | Enables all resource detectors. | `true` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `OTEL_DOTNET_AUTO_{0}_RESOURCE_DETECTOR_ENABLED` | Configuration pattern for enabling a specific resource detector, where `{0}` is the uppercase id of the resource detector you want to enable. Overrides `OTEL_DOTNET_AUTO_RESOURCE_DETECTOR_ENABLED`. | `true` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |

Supported resource detectors:

| ID | Description | Documentation | Status |
|--------------------------|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
| `ENVIRONMENTALVARIABLES` | Environment variables detector | See /[1/] | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `TELEMETRYSDK` | Telemetry SDK detector | [Telemetry SDK resources documentation](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md?plain=1#L117) | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `CONTAINER` | Container detector | [Container resource detector documentation](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.ResourceDetectors.Container/README.md) | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |

/[1/] `ENVIRONMENTALVARIABLES` detector is using following environmental variables
to detect resources:

| Environment variable | Description | Default value | Status |
|----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
Expand Down Expand Up @@ -337,9 +353,9 @@ or when the deployment changes or the Automatic Instrumentation
library is upgraded. Once validated, there's no need to revalidate
the rules when the application restarts.

| Environment variable | Description | Default value | Status |
|--------------------------------------- |---------------------|----------------|-----------------------------------------------------------------------------------------------------------------------------------|
| `OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED` | Enables RuleEngine. | `true` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| Environment variable | Description | Default value | Status |
|----------------------------------------|---------------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------|
| `OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED` | Enables RuleEngine. | `true` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |

## .NET CLR Profiler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<dependency id="OpenTelemetry.Instrumentation.EntityFrameworkCore" version="1.0.0-beta.6" />
<dependency id="OpenTelemetry.Instrumentation.MySqlData" version="1.0.0-beta.6" />
<dependency id="OpenTelemetry.Instrumentation.StackExchangeRedis" version="1.0.0-rc9.8" />
<dependency id="OpenTelemetry.Extensions.Docker" version="1.0.0-beta.1" />
</group>
<group targetFramework=".NETFramework4.6.2">
<dependency id="OpenTelemetry" version="1.4.0" />
Expand All @@ -56,6 +57,7 @@
<dependency id="OpenTelemetry.Instrumentation.Wcf" version="1.0.0-rc.9" />
<dependency id="OpenTelemetry.Shims.OpenTracing" version="1.0.0-rc9.14" />
<dependency id="OpenTelemetry.Instrumentation.AspNet" version="1.0.0-rc9.8" />
<dependency id="OpenTelemetry.Extensions.Docker" version="1.0.0-beta.1" />
</group>
</dependencies>
<frameworkAssemblies>
Expand Down
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageVersion Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc9.14" />
<PackageVersion Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.8" />
<PackageVersion Include="OpenTelemetry.Shims.OpenTracing" Version="1.0.0-rc9.14" />
<PackageVersion Include="OpenTelemetry.Extensions.Docker" Version="1.0.0-beta.2" />
</ItemGroup>

<!-- Versions from CommonExcludedAssets.props -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace OpenTelemetry.AutoInstrumentation.Configurations;

internal static class ConfigurationExtensions
{
public static IList<TEnum> ParseEnabledEnumList<TEnum>(this Configuration source, bool enabledByDefault, string enabledConfigurationTemplate)
public static IReadOnlyList<TEnum> ParseEnabledEnumList<TEnum>(this Configuration source, bool enabledByDefault, string enabledConfigurationTemplate)
where TEnum : struct, Enum, IConvertible
{
var allConfigurations = Enum.GetValues(typeof(TEnum)).Cast<TEnum>().ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ internal static class ConfigurationKeys
/// </summary>
public const string InstrumentationEnabled = "OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED";

/// <summary>
/// Configuration key for disabling all trace instrumentations.
/// </summary>
public const string ResourceDetectorEnabled = "OTEL_DOTNET_AUTO_RESOURCE_DETECTOR_ENABLED";

/// <summary>
/// Configuration key template for disabled trace instrumentations.
/// </summary>
public const string EnabledResourceDetectorTemplate = "OTEL_DOTNET_AUTO_{0}_RESOURCE_DETECTOR_ENABLED";

/// <summary>
/// Configuration keys for traces.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ internal class GeneralSettings : Settings
/// <summary>
/// Gets the list of plugins represented by <see cref="Type.AssemblyQualifiedName"/>.
/// </summary>
public IList<string> Plugins { get; private set; } = new List<string>();
public IList<string> Plugins { get; } = new List<string>();

/// <summary>
/// Gets the list of enabled resource detectors.
/// </summary>
public IReadOnlyList<ResourceDetector> EnabledResourceDetectors { get; private set; } = new List<ResourceDetector>();

/// <summary>
/// Gets a value indicating whether the <see cref="AppDomain.UnhandledException"/> event should trigger
Expand All @@ -46,6 +51,12 @@ protected override void OnLoad(Configuration configuration)
}
}

var resourceDetectorsEnabledByDefault = configuration.GetBool(ConfigurationKeys.ResourceDetectorEnabled) ?? true;

EnabledResourceDetectors = configuration.ParseEnabledEnumList<ResourceDetector>(
enabledByDefault: resourceDetectorsEnabledByDefault,
enabledConfigurationTemplate: ConfigurationKeys.EnabledResourceDetectorTemplate);

FlushOnUnhandledException = configuration.GetBool(ConfigurationKeys.FlushOnUnhandledException) ?? false;
SetupSdk = configuration.GetBool(ConfigurationKeys.SetupSdk) ?? true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal class LogSettings : Settings
/// <summary>
/// Gets the list of enabled instrumentations.
/// </summary>
public IList<LogInstrumentation> EnabledInstrumentations { get; private set; } = new List<LogInstrumentation>();
public IReadOnlyList<LogInstrumentation> EnabledInstrumentations { get; private set; } = new List<LogInstrumentation>();

protected override void OnLoad(Configuration configuration)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal class MetricSettings : Settings
/// <summary>
/// Gets the list of enabled meters.
/// </summary>
public IList<MetricInstrumentation> EnabledInstrumentations { get; private set; } = new List<MetricInstrumentation>();
public IReadOnlyList<MetricInstrumentation> EnabledInstrumentations { get; private set; } = new List<MetricInstrumentation>();

/// <summary>
/// Gets the list of meters to be added to the MeterProvider at the startup.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

using System.Runtime.CompilerServices;
using OpenTelemetry.Extensions.Docker.Resources;
using OpenTelemetry.Resources;

namespace OpenTelemetry.AutoInstrumentation.Configurations;
Expand All @@ -22,13 +24,21 @@ internal static class ResourceConfigurator
{
internal const string ServiceNameAttribute = "service.name";

public static ResourceBuilder CreateResourceBuilder()
public static ResourceBuilder CreateResourceBuilder(IReadOnlyList<ResourceDetector> enabledResourceDetectors)
{
var resourceBuilder = ResourceBuilder
.CreateEmpty() // Don't use CreateDefault because it puts service name unknown by default.
.AddEnvironmentVariableDetector()
.AddTelemetrySdk()
.AddAttributes(new KeyValuePair<string, object>[] { new(Constants.Tracer.AutoInstrumentationVersionName, Constants.Tracer.Version) });
.CreateEmpty(); // Don't use CreateDefault because it puts service name unknown by default.

foreach (var enabledResourceDetector in enabledResourceDetectors)
{
resourceBuilder = enabledResourceDetector switch
{
ResourceDetector.EnvironmentalVariables => resourceBuilder.AddEnvironmentVariableDetector(),
ResourceDetector.TelemetrySdk => resourceBuilder.AddTelemetrySdk().AddAttributes(new KeyValuePair<string, object>[] { new(Constants.Tracer.AutoInstrumentationVersionName, Constants.Tracer.Version) }),
ResourceDetector.Container => Wrappers.AddContainerResourceDetector(resourceBuilder),
_ => resourceBuilder,
};
}

var pluginManager = Instrumentation.PluginManager;
if (pluginManager != null)
Expand All @@ -45,4 +55,13 @@ public static ResourceBuilder CreateResourceBuilder()

return resourceBuilder;
}

private static class Wrappers
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static ResourceBuilder AddContainerResourceDetector(ResourceBuilder resourceBuilder)
{
return resourceBuilder.AddDetector(new DockerResourceDetector());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// <copyright file="ResourceDetector.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>

namespace OpenTelemetry.AutoInstrumentation.Configurations;

/// <summary>
/// Enum representing supported resource detectors.
/// </summary>
internal enum ResourceDetector
{
/// <summary>
/// Environmental variables resource detector.
/// </summary>
EnvironmentalVariables = 0,

/// <summary>
/// TelemetrySdk resource detector.
/// </summary>
TelemetrySdk = 1,

/// <summary>
/// Container resource detector.
/// </summary>
Container = 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal class TracerSettings : Settings
/// <summary>
/// Gets the list of enabled instrumentations.
/// </summary>
public IList<TracerInstrumentation> EnabledInstrumentations { get; private set; } = new List<TracerInstrumentation>();
public IReadOnlyList<TracerInstrumentation> EnabledInstrumentations { get; private set; } = new List<TracerInstrumentation>();

/// <summary>
/// Gets the list of activity configurations to be added to the tracer at the startup.
Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry.AutoInstrumentation/Instrumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static void Initialize()
{
var builder = Sdk
.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder())
.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder(GeneralSettings.Value.EnabledResourceDetectors))
.UseEnvironmentVariables(LazyInstrumentationLoader, TracerSettings.Value, _pluginManager)
.InvokePlugins(_pluginManager);

Expand All @@ -154,7 +154,7 @@ public static void Initialize()
{
var builder = Sdk
.CreateMeterProviderBuilder()
.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder())
.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder(GeneralSettings.Value.EnabledResourceDetectors))
.UseEnvironmentVariables(LazyInstrumentationLoader, MetricSettings.Value, _pluginManager)
.InvokePlugins(_pluginManager);

Expand All @@ -181,7 +181,7 @@ public static void Initialize()
}
}

private static void AddLazilyLoadedMetricInstrumentations(LazyInstrumentationLoader lazyInstrumentationLoader, IList<MetricInstrumentation> enabledInstrumentations)
private static void AddLazilyLoadedMetricInstrumentations(LazyInstrumentationLoader lazyInstrumentationLoader, IReadOnlyList<MetricInstrumentation> enabledInstrumentations)
{
foreach (var instrumentation in enabledInstrumentations)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ private static void AddLazilyLoadedMetricInstrumentations(LazyInstrumentationLoa
}
}

private static void AddLazilyLoadedTraceInstrumentations(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager, IList<TracerInstrumentation> enabledInstrumentations)
private static void AddLazilyLoadedTraceInstrumentations(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager, IReadOnlyList<TracerInstrumentation> enabledInstrumentations)
{
foreach (var instrumentation in enabledInstrumentations)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static ILoggingBuilder AddOpenTelemetryLogs(this ILoggingBuilder builder)

builder.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder());
options.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder(Instrumentation.GeneralSettings.Value.EnabledResourceDetectors));
options.IncludeFormattedMessage = settings.IncludeFormattedMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" />
<PackageReference Include="OpenTelemetry.Instrumentation.Wcf" />
<PackageReference Include="OpenTelemetry.Shims.OpenTracing" />
<PackageReference Include="OpenTelemetry.Extensions.Docker" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
Expand Down
Loading

0 comments on commit 826715c

Please sign in to comment.