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

[Discussion] Add deployment.environment support #106

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion .cspell/company-terms.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
OTEL
OTEL
SIGNALFX
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h
* Add `SPLUNK_ACCESS_TOKEN` configuration key to authorize direct ingest.
* Add `SPLUNK_TRACE_RESPONSE_HEADER_ENABLED` configuration key
to support Splunk RUM.
* Add `SIGNALFX_ENV` configuration key to specify environment name.

### Changed

Expand Down
1 change: 1 addition & 0 deletions docs/advanced-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Note: .NET Framework apps can read settings also from `Web.config` and `App.conf

| Environment variable | Default | Description |
|----------------------------------------|---------|--------------------------------------------|
| `SIGNALFX_ENV` | | Specifies environment name. |
| `SPLUNK_REALM` | `none` | Specifies direct OTLP ingest realm. [1] |
| `SPLUNK_ACCESS_TOKEN` | | Specifies direct OTLP ingest access token. |
| `SPLUNK_TRACE_RESPONSE_HEADER_ENABLED` | `true` | Enables Splunk RUM integration. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ namespace Splunk.OpenTelemetry.AutoInstrumentation;

internal static class ConfigurationKeys
{
public static class SignalFx
{
/// <summary>
/// Configuration key for current environment name.
/// </summary>
public const string Environment = "SIGNALFX_ENV";
}

public static class Splunk
{
/// <summary>
Expand Down
8 changes: 7 additions & 1 deletion src/Splunk.OpenTelemetry.AutoInstrumentation/Logs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ namespace Splunk.OpenTelemetry.AutoInstrumentation;
internal class Logs
{
private readonly ILogger _log = new Logger();
private readonly PluginSettings _settings;

public Logs(PluginSettings settings)
{
_settings = settings;
}

public void ConfigureLogsOptions(OpenTelemetryLoggerOptions options)
{
ServiceNameWarning.Instance.SendOnMissingServiceName(_log);
options.ConfigureResource(ResourceConfigurator.Configure);
options.ConfigureResource(b => ResourceConfigurator.Configure(b, _settings));
}
}
2 changes: 1 addition & 1 deletion src/Splunk.OpenTelemetry.AutoInstrumentation/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal Metrics(PluginSettings settings)
public MeterProviderBuilder ConfigureMeterProvider(MeterProviderBuilder builder)
{
ServiceNameWarning.Instance.SendOnMissingServiceName(_log);
return builder.ConfigureResource(ResourceConfigurator.Configure);
return builder.ConfigureResource(b => ResourceConfigurator.Configure(b, _settings));
}

public void ConfigureMetricsOptions(OtlpExporterOptions options)
Expand Down
2 changes: 1 addition & 1 deletion src/Splunk.OpenTelemetry.AutoInstrumentation/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Plugin

private readonly Metrics _metrics = new(Settings);
private readonly Traces _traces = new(Settings);
private readonly Logs _logs = new();
private readonly Logs _logs = new(Settings);

/// <summary>
/// Configures Metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal PluginSettings(IConfigurationSource source)

Realm = source.GetString(ConfigurationKeys.Splunk.Realm);
AccessToken = source.GetString(ConfigurationKeys.Splunk.AccessToken);
EnvironmentName = source.GetString(ConfigurationKeys.SignalFx.Environment);
TraceResponseHeaderEnabled = source.GetBool(ConfigurationKeys.Splunk.TraceResponseHeaderEnabled) ?? true;
IsOtlpEndpointSet = !string.IsNullOrEmpty(source.GetString(ConfigurationKeys.OpenTelemetry.OtlpEndpoint));
}
Expand All @@ -43,6 +44,8 @@ internal PluginSettings(IConfigurationSource source)

public string? AccessToken { get; }

public string? EnvironmentName { get; }

public bool TraceResponseHeaderEnabled { get; }

public bool IsOtlpEndpointSet { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Splunk.OpenTelemetry.AutoInstrumentation;
internal static class ResourceConfigurator
{
private const string SplunkDistroVersionName = "splunk.distro.version";
private const string DeploymentEnvironment = "deployment.environment";
private static readonly string Version;

static ResourceConfigurator()
Expand All @@ -32,9 +33,18 @@ static ResourceConfigurator()
Version = version.FileVersion ?? "unknown";
}

public static void Configure(ResourceBuilder resourceBuilder)
public static void Configure(ResourceBuilder resourceBuilder, PluginSettings settings)
{
resourceBuilder
.AddAttributes(new KeyValuePair<string, object>[] { new(SplunkDistroVersionName, Version) });
var attributes = new List<KeyValuePair<string, object>>
{
new(SplunkDistroVersionName, Version)
};

if (!string.IsNullOrEmpty(settings.EnvironmentName))
{
attributes.Add(new(DeploymentEnvironment, settings.EnvironmentName!));
}

resourceBuilder.AddAttributes(attributes);
}
}
2 changes: 1 addition & 1 deletion src/Splunk.OpenTelemetry.AutoInstrumentation/Traces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal Traces(PluginSettings settings)
public TracerProviderBuilder ConfigureTracerProvider(TracerProviderBuilder builder)
{
ServiceNameWarning.Instance.SendOnMissingServiceName(_log);
return builder.ConfigureResource(ResourceConfigurator.Configure);
return builder.ConfigureResource(b => ResourceConfigurator.Configure(b, _settings));
}

public void ConfigureTracesOptions(OtlpExporterOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,43 @@
// limitations under the License.
// </copyright>

using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using FluentAssertions.Execution;
using OpenTelemetry.Resources;
using Splunk.OpenTelemetry.AutoInstrumentation.Configuration;

namespace Splunk.OpenTelemetry.AutoInstrumentation.Tests;

public class ResourceConfiguratorTests
{
[Fact]
public void ConfigureSplunkDistributionVersion()
public void ResourceConfiguratorConfigure()
{
var configuration = new NameValueCollection
{
{ "SIGNALFX_ENV", "test" },
};

var settings = new PluginSettings(new NameValueConfigurationSource(configuration));
var resourceBuilder = ResourceBuilder.CreateEmpty();

ResourceConfigurator.Configure(resourceBuilder);
ResourceConfigurator.Configure(resourceBuilder, settings);

var resource = resourceBuilder.Build();

using (new AssertionScope())
{
resource.Attributes.Count().Should().Be(1);
var attribute = resource.Attributes.First();
attribute.Key.Should().Be("splunk.distro.version");
(attribute.Value as string).Should().Be(typeof(Plugin).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version);
resource.Attributes.Count().Should().Be(2);

var versionAttribute = resource.Attributes.First();
versionAttribute.Key.Should().Be("splunk.distro.version");
(versionAttribute.Value as string).Should().Be(typeof(Plugin).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version);

var envAttribute = resource.Attributes.Last();
envAttribute.Key.Should().Be("deployment.environment");
(envAttribute.Value as string).Should().Be("test");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ internal void TracerSettings_DefaultValues()
{
settings.Realm.Should().BeNull();
settings.AccessToken.Should().BeNull();
settings.EnvironmentName.Should().BeNull();
settings.TraceResponseHeaderEnabled.Should().BeTrue();
}
}

private static void ClearEnvVars()
{
Environment.SetEnvironmentVariable(ConfigurationKeys.SignalFx.Environment, null);
Environment.SetEnvironmentVariable(ConfigurationKeys.Splunk.Realm, null);
Environment.SetEnvironmentVariable(ConfigurationKeys.Splunk.AccessToken, null);
Environment.SetEnvironmentVariable(ConfigurationKeys.Splunk.TraceResponseHeaderEnabled, null);
Expand Down