Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
19 changes: 19 additions & 0 deletions docs/file-based-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ For more details, see: [OpenTelemetry YAML File Format Specification](https://gi

## Configuration Examples

## General Configuration

``` yaml
# The file format version.
# The yaml format is documented at
# <https://github.com/open-telemetry/opentelemetry-configuration/tree/main/schema>
# By default, the latest version is used.
file_format: "1.0-rc.1"
# Configure if the SDK is disabled or not.
# If omitted or null, false is used
disabled: false
# Configure if the Fail Fast is enabled or not.
# If omitted or null, false is used
fail_fast: false
# Configure if the Flush On Unhandled Exception is enabled or not.
# If omitted or null, false is used.
flush_on_unhandled_exception: false
```

### Resource Configuration

You can configure resource attributes directly in YAML or via the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.AutoInstrumentation.Configurations.FileBasedConfiguration;

namespace OpenTelemetry.AutoInstrumentation.Configurations;

internal class FailFastSettings : Settings
Expand All @@ -11,4 +13,9 @@ protected override void OnLoadEnvVar(Configuration configuration)
{
FailFast = configuration.GetBool(ConfigurationKeys.FailFast) ?? false;
}

protected override void OnLoadFile(YamlConfiguration configuration)
{
FailFast = configuration.FailFast;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,25 @@ internal class YamlConfiguration
/// </summary>
[YamlMember(Alias = "resource")]
public ResourceConfiguration? Resource { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the SDK is disabled.
/// If omitted or null, false is used.
/// </summary>
[YamlMember(Alias = "disabled")]
public bool Disabled { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether the Fail Fast is enabled.
/// If omitted or null, false is used.
/// </summary>
[YamlMember(Alias = "fail_fast")]
public bool FailFast { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether the Flush On Unhandled Exception is enabled.
/// If omitted or null, false is used.
/// </summary>
[YamlMember(Alias = "flush_on_unhandled_exception")]
public bool FlushOnUnhandledException { get; set; } = false;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.AutoInstrumentation.Configurations.FileBasedConfiguration;

namespace OpenTelemetry.AutoInstrumentation.Configurations;

internal class GeneralSettings : Settings
{
/// <summary>
/// Gets the list of plugins represented by <see cref="Type.AssemblyQualifiedName"/>.
/// </summary>
public IList<string> Plugins { get; } = new List<string>();
public IList<string> Plugins { get; } = [];

/// <summary>
/// Gets a value indicating whether the <see cref="AppDomain.UnhandledException"/> event should trigger
Expand Down Expand Up @@ -43,4 +45,10 @@ protected override void OnLoadEnvVar(Configuration configuration)

ProfilerEnabled = configuration.GetString(ConfigurationKeys.ProfilingEnabled) == "1";
}

protected override void OnLoadFile(YamlConfiguration configuration)
{
SetupSdk = !configuration.Disabled;
FlushOnUnhandledException = configuration.FlushOnUnhandledException;
}
Copy link
Member

Choose a reason for hiding this comment

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

Mandatory for this PR: You need to set up here also ProfilerEnabled (by fallback to envs).

Plugins are alsoe required, but it can be configured also from the file. If you want to make a follow up PR, you need to ensure that it is extracted to separate setting, and readed from EvnVars.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Plugins will be added in separate PR.

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ internal abstract class Settings
private static readonly bool IsYamlConfigEnabled = Environment.GetEnvironmentVariable(ConfigurationKeys.FileBasedConfiguration.Enabled) == "true";
private static readonly Lazy<YamlConfiguration> YamlConfiguration = new(ReadYamlConfiguration);

private bool FailFast { get; set; }

public static T FromDefaultSources<T>(bool failFast)
where T : Settings, new()
{
if (IsYamlConfigEnabled)
{
var settings = new T { FailFast = failFast };
var settings = new T();
settings.LoadFile(YamlConfiguration.Value);
return settings;
}
else
{
var configuration = new Configuration(failFast, new EnvironmentConfigurationSource(failFast));
var settings = new T { FailFast = failFast };
var settings = new T();
settings.LoadEnvVar(configuration);
return settings;
}
Expand Down Expand Up @@ -60,7 +58,7 @@ protected virtual void OnLoadFile(YamlConfiguration configuration)
{
// TODO temporary fallback to env var configuration until we support all settings in yaml
// TODO make the method abstract when all settings are supported in yaml
var envVarConfiguration = new Configuration(FailFast, new EnvironmentConfigurationSource(FailFast));
var envVarConfiguration = new Configuration(configuration.FailFast, new EnvironmentConfigurationSource(configuration.FailFast));
OnLoadEnvVar(envVarConfiguration);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.AutoInstrumentation.Configurations;
using OpenTelemetry.AutoInstrumentation.Configurations.FileBasedConfiguration;
using Xunit;

namespace OpenTelemetry.AutoInstrumentation.Tests.Configurations.FileBased;

public class FilebasedGeneralSettingsTests
{
[Fact]
public void LoadFile_GeneralSettings()
{
var conf = new YamlConfiguration
{
Disabled = false,
FlushOnUnhandledException = true,
};

var settings = new GeneralSettings();

settings.LoadFile(conf);

Assert.True(settings.SetupSdk);
Assert.True(settings.FlushOnUnhandledException);
}

[Fact]
public void LoadFile_FailFastSettings()
{
var conf = new YamlConfiguration
{
FailFast = true,
};

var settings = new FailFastSettings();

settings.LoadFile(conf);

Assert.True(settings.FailFast);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
file_format: "1.0-rc.1"
disabled: false
fail_fast: false
flush_on_unhandled_exception: false

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
file_format: "1.0-rc.1"
disabled: ${OTEL_SDK_DISABLED}
fail_fast: ${OTEL_DOTNET_AUTO_FAIL_FAST_ENABLED}
flush_on_unhandled_exception: ${OTEL_DOTNET_AUTO_FLUSH_ON_UNHANDLEDEXCEPTION}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using Xunit;
using YamlParser = OpenTelemetry.AutoInstrumentation.Configurations.FileBasedConfiguration.Parser.Parser;

namespace OpenTelemetry.AutoInstrumentation.Tests.Configurations.FileBased.Parser;

[Collection("Non-Parallel Collection")]
public class ParserGeneralTests
{
[Fact]
public void Parse_FullConfigYaml_ShouldPopulateModelCorrectly()
{
var config = YamlParser.ParseYaml("Configurations/FileBased/Files/TestGeneralFile.yaml");

Assert.NotNull(config);

Assert.Equal("1.0-rc.1", config.FileFormat);
Assert.False(config.Disabled);
Assert.False(config.FailFast);
Assert.False(config.FlushOnUnhandledException);
}

[Fact]
public void Parse_EnvVarYaml_ShouldPopulateModelCompletely()
{
Environment.SetEnvironmentVariable("OTEL_SDK_DISABLED", "true");
Environment.SetEnvironmentVariable("OTEL_DOTNET_AUTO_FAIL_FAST_ENABLED", "true");
Environment.SetEnvironmentVariable("OTEL_DOTNET_AUTO_FLUSH_ON_UNHANDLEDEXCEPTION", "false");

var config = YamlParser.ParseYaml("Configurations/FileBased/Files/TestGeneralFileEnvVars.yaml");

Assert.NotNull(config);

Assert.Equal("1.0-rc.1", config.FileFormat);
Assert.True(config.Disabled);
Assert.True(config.FailFast);
Assert.False(config.FlushOnUnhandledException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
</ItemGroup>

<ItemGroup>
<None Update="Configurations\FileBased\Files\TestGeneralFile.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Configurations\FileBased\Files\TestResourceFile.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Configurations\FileBased\Files\TestGeneralFileEnvVars.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Configurations\FileBased\Files\TestResourceFileEnvVars.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Loading