diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpExporterOptionsConfigurationType.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpExporterOptionsConfigurationType.cs
new file mode 100644
index 00000000000..d3cedd6915c
--- /dev/null
+++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpExporterOptionsConfigurationType.cs
@@ -0,0 +1,17 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+#nullable enable
+
+namespace OpenTelemetry.Exporter;
+
+[Flags]
+internal enum OtlpExporterOptionsConfigurationType
+{
+#pragma warning disable SA1602 // Enumeration items should be documented
+ Default,
+ Logs,
+ Metrics,
+ Traces,
+#pragma warning restore SA1602 // Enumeration items should be documented
+}
diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpExporterSpecEnvVarKeyDefinitions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpExporterSpecEnvVarKeyDefinitions.cs
deleted file mode 100644
index c7e4dad9181..00000000000
--- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpExporterSpecEnvVarKeyDefinitions.cs
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright The OpenTelemetry Authors
-// SPDX-License-Identifier: Apache-2.0
-
-namespace OpenTelemetry.Exporter;
-
-///
-/// Contains spec environment variable key definitions for OpenTelemetry Protocol (OTLP) exporter.
-///
-///
-/// Specification: .
-///
-internal static class OtlpExporterSpecEnvVarKeyDefinitions
-{
- public const string DefaultEndpointEnvVarName = "OTEL_EXPORTER_OTLP_ENDPOINT";
- public const string DefaultHeadersEnvVarName = "OTEL_EXPORTER_OTLP_HEADERS";
- public const string DefaultTimeoutEnvVarName = "OTEL_EXPORTER_OTLP_TIMEOUT";
- public const string DefaultProtocolEnvVarName = "OTEL_EXPORTER_OTLP_PROTOCOL";
-
- public const string MetricsTemporalityPreferenceEnvVarName = "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE";
-}
diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpSpecConfigDefinitions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpSpecConfigDefinitions.cs
new file mode 100644
index 00000000000..3bc62218b3f
--- /dev/null
+++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpSpecConfigDefinitions.cs
@@ -0,0 +1,34 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+namespace OpenTelemetry.Exporter;
+
+///
+/// Contains spec environment variable key definitions for OpenTelemetry Protocol (OTLP) exporter.
+///
+///
+/// Specification: .
+///
+internal static class OtlpSpecConfigDefinitions
+{
+ public const string DefaultEndpointEnvVarName = "OTEL_EXPORTER_OTLP_ENDPOINT";
+ public const string DefaultHeadersEnvVarName = "OTEL_EXPORTER_OTLP_HEADERS";
+ public const string DefaultTimeoutEnvVarName = "OTEL_EXPORTER_OTLP_TIMEOUT";
+ public const string DefaultProtocolEnvVarName = "OTEL_EXPORTER_OTLP_PROTOCOL";
+
+ public const string LogsEndpointEnvVarName = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT";
+ public const string LogsHeadersEnvVarName = "OTEL_EXPORTER_OTLP_LOGS_HEADERS";
+ public const string LogsTimeoutEnvVarName = "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT";
+ public const string LogsProtocolEnvVarName = "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL";
+
+ public const string MetricsEndpointEnvVarName = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";
+ public const string MetricsHeadersEnvVarName = "OTEL_EXPORTER_OTLP_METRICS_HEADERS";
+ public const string MetricsTimeoutEnvVarName = "OTEL_EXPORTER_OTLP_METRICS_TIMEOUT";
+ public const string MetricsProtocolEnvVarName = "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL";
+ public const string MetricsTemporalityPreferenceEnvVarName = "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE";
+
+ public const string TracesEndpointEnvVarName = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
+ public const string TracesHeadersEnvVarName = "OTEL_EXPORTER_OTLP_TRACES_HEADERS";
+ public const string TracesTimeoutEnvVarName = "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT";
+ public const string TracesProtocolEnvVarName = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL";
+}
diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs
index 05382efd6ce..0cffd50806c 100644
--- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs
+++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs
@@ -17,11 +17,18 @@ namespace OpenTelemetry.Exporter;
///
/// OpenTelemetry Protocol (OTLP) exporter options.
-/// OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_PROTOCOL
-/// environment variables are parsed during object construction.
///
+///
+/// Note: OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS,
+/// OTEL_EXPORTER_OTLP_TIMEOUT, and OTEL_EXPORTER_OTLP_PROTOCOL environment
+/// variables are parsed during object construction.
+///
public class OtlpExporterOptions
{
+ internal const string DefaultGrpcEndpoint = "http://localhost:4317";
+ internal const string DefaultHttpEndpoint = "http://localhost:4318";
+ internal const OtlpExportProtocol DefaultOtlpExportProtocol = OtlpExportProtocol.Grpc;
+
internal static readonly KeyValuePair[] StandardHeaders = new KeyValuePair[]
{
new KeyValuePair("User-Agent", GetUserAgentString()),
@@ -29,9 +36,6 @@ public class OtlpExporterOptions
internal readonly Func DefaultHttpClientFactory;
- private const string DefaultGrpcEndpoint = "http://localhost:4317";
- private const string DefaultHttpEndpoint = "http://localhost:4318";
- private const OtlpExportProtocol DefaultOtlpExportProtocol = OtlpExportProtocol.Grpc;
private const string UserAgentProduct = "OTel-OTLP-Exporter-Dotnet";
private Uri endpoint;
@@ -40,39 +44,27 @@ public class OtlpExporterOptions
/// Initializes a new instance of the class.
///
public OtlpExporterOptions()
- : this(new ConfigurationBuilder().AddEnvironmentVariables().Build(), new())
+ : this(OtlpExporterOptionsConfigurationType.Default)
+ {
+ }
+
+ internal OtlpExporterOptions(
+ OtlpExporterOptionsConfigurationType configurationType)
+ : this(
+ configuration: new ConfigurationBuilder().AddEnvironmentVariables().Build(),
+ configurationType,
+ defaultBatchOptions: new())
{
}
internal OtlpExporterOptions(
IConfiguration configuration,
+ OtlpExporterOptionsConfigurationType configurationType,
BatchExportActivityProcessorOptions defaultBatchOptions)
{
- Debug.Assert(configuration != null, "configuration was null");
Debug.Assert(defaultBatchOptions != null, "defaultBatchOptions was null");
- if (configuration.TryGetUriValue(OtlpExporterSpecEnvVarKeyDefinitions.DefaultEndpointEnvVarName, out var endpoint))
- {
- this.endpoint = endpoint;
- }
-
- if (configuration.TryGetStringValue(OtlpExporterSpecEnvVarKeyDefinitions.DefaultHeadersEnvVarName, out var headers))
- {
- this.Headers = headers;
- }
-
- if (configuration.TryGetIntValue(OtlpExporterSpecEnvVarKeyDefinitions.DefaultTimeoutEnvVarName, out var timeout))
- {
- this.TimeoutMilliseconds = timeout;
- }
-
- if (configuration.TryGetValue(
- OtlpExporterSpecEnvVarKeyDefinitions.DefaultProtocolEnvVarName,
- OtlpExportProtocolParser.TryParse,
- out var protocol))
- {
- this.Protocol = protocol;
- }
+ this.ApplyConfiguration(configuration, configurationType);
this.HttpClientFactory = this.DefaultHttpClientFactory = () =>
{
@@ -98,7 +90,7 @@ public Uri Endpoint
{
if (this.endpoint == null)
{
- this.endpoint = this.Protocol == OtlpExportProtocol.Grpc
+ return this.Protocol == OtlpExportProtocol.Grpc
? new Uri(DefaultGrpcEndpoint)
: new Uri(DefaultHttpEndpoint);
}
@@ -195,8 +187,42 @@ internal static OtlpExporterOptions CreateOtlpExporterOptions(
string name)
=> new(
configuration,
+ OtlpExporterOptionsConfigurationType.Default,
serviceProvider.GetRequiredService>().Get(name));
+ internal void ApplyConfigurationUsingSpecificationEnvVars(
+ IConfiguration configuration,
+ string endpointEnvVarKey,
+ bool appendSignalPathToEndpoint,
+ string protocolEnvVarKey,
+ string headersEnvVarKey,
+ string timeoutEnvVarKey)
+ {
+ if (configuration.TryGetUriValue(endpointEnvVarKey, out var endpoint))
+ {
+ this.endpoint = endpoint;
+ this.AppendSignalPathToEndpoint = appendSignalPathToEndpoint;
+ }
+
+ if (configuration.TryGetValue(
+ protocolEnvVarKey,
+ OtlpExportProtocolParser.TryParse,
+ out var protocol))
+ {
+ this.Protocol = protocol;
+ }
+
+ if (configuration.TryGetStringValue(headersEnvVarKey, out var headers))
+ {
+ this.Headers = headers;
+ }
+
+ if (configuration.TryGetIntValue(timeoutEnvVarKey, out var timeout))
+ {
+ this.TimeoutMilliseconds = timeout;
+ }
+ }
+
private static string GetUserAgentString()
{
try
@@ -210,4 +236,60 @@ private static string GetUserAgentString()
return UserAgentProduct;
}
}
+
+ private void ApplyConfiguration(
+ IConfiguration configuration,
+ OtlpExporterOptionsConfigurationType configurationType)
+ {
+ Debug.Assert(configuration != null, "configuration was null");
+
+ // Note: When using the "AddOtlpExporter" extensions configurationType
+ // never has a value other than "Default" because OtlpExporterOptions is
+ // shared by all signals and there is no way to differentiate which
+ // signal is being constructed.
+ if (configurationType == OtlpExporterOptionsConfigurationType.Default)
+ {
+ this.ApplyConfigurationUsingSpecificationEnvVars(
+ configuration!,
+ OtlpSpecConfigDefinitions.DefaultEndpointEnvVarName,
+ appendSignalPathToEndpoint: true,
+ OtlpSpecConfigDefinitions.DefaultProtocolEnvVarName,
+ OtlpSpecConfigDefinitions.DefaultHeadersEnvVarName,
+ OtlpSpecConfigDefinitions.DefaultTimeoutEnvVarName);
+ }
+ else if (configurationType == OtlpExporterOptionsConfigurationType.Logs)
+ {
+ this.ApplyConfigurationUsingSpecificationEnvVars(
+ configuration!,
+ OtlpSpecConfigDefinitions.LogsEndpointEnvVarName,
+ appendSignalPathToEndpoint: false,
+ OtlpSpecConfigDefinitions.LogsProtocolEnvVarName,
+ OtlpSpecConfigDefinitions.LogsHeadersEnvVarName,
+ OtlpSpecConfigDefinitions.LogsTimeoutEnvVarName);
+ }
+ else if (configurationType == OtlpExporterOptionsConfigurationType.Metrics)
+ {
+ this.ApplyConfigurationUsingSpecificationEnvVars(
+ configuration!,
+ OtlpSpecConfigDefinitions.MetricsEndpointEnvVarName,
+ appendSignalPathToEndpoint: false,
+ OtlpSpecConfigDefinitions.MetricsProtocolEnvVarName,
+ OtlpSpecConfigDefinitions.MetricsHeadersEnvVarName,
+ OtlpSpecConfigDefinitions.MetricsTimeoutEnvVarName);
+ }
+ else if (configurationType == OtlpExporterOptionsConfigurationType.Traces)
+ {
+ this.ApplyConfigurationUsingSpecificationEnvVars(
+ configuration!,
+ OtlpSpecConfigDefinitions.TracesEndpointEnvVarName,
+ appendSignalPathToEndpoint: false,
+ OtlpSpecConfigDefinitions.TracesProtocolEnvVarName,
+ OtlpSpecConfigDefinitions.TracesHeadersEnvVarName,
+ OtlpSpecConfigDefinitions.TracesTimeoutEnvVarName);
+ }
+ else
+ {
+ throw new NotSupportedException($"OtlpExporterOptionsConfigurationType '{configurationType}' is not supported.");
+ }
+ }
}
diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs
index 747cf46f949..37a66dc2f10 100644
--- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs
+++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs
@@ -63,7 +63,7 @@ public static MeterProviderBuilder AddOtlpExporter(
services.AddOptions(finalOptionsName).Configure(
(readerOptions, config) =>
{
- var otlpTemporalityPreference = config[OtlpExporterSpecEnvVarKeyDefinitions.MetricsTemporalityPreferenceEnvVarName];
+ var otlpTemporalityPreference = config[OtlpSpecConfigDefinitions.MetricsTemporalityPreferenceEnvVarName];
if (!string.IsNullOrWhiteSpace(otlpTemporalityPreference)
&& Enum.TryParse(otlpTemporalityPreference, ignoreCase: true, out var enumValue))
{
@@ -140,7 +140,7 @@ public static MeterProviderBuilder AddOtlpExporter(
services.AddOptions(finalOptionsName).Configure(
(readerOptions, config) =>
{
- var otlpTemporalityPreference = config[OtlpExporterSpecEnvVarKeyDefinitions.MetricsTemporalityPreferenceEnvVarName];
+ var otlpTemporalityPreference = config[OtlpSpecConfigDefinitions.MetricsTemporalityPreferenceEnvVarName];
if (!string.IsNullOrWhiteSpace(otlpTemporalityPreference)
&& Enum.TryParse(otlpTemporalityPreference, ignoreCase: true, out var enumValue))
{
diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/BaseOtlpHttpExportClientTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/BaseOtlpHttpExportClientTests.cs
index 4a419463b6d..a846af33a4e 100644
--- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/BaseOtlpHttpExportClientTests.cs
+++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/BaseOtlpHttpExportClientTests.cs
@@ -21,7 +21,7 @@ public void ValidateOtlpHttpExportClientEndpoint(string optionEndpoint, string e
{
try
{
- Environment.SetEnvironmentVariable(OtlpExporterSpecEnvVarKeyDefinitions.DefaultEndpointEnvVarName, endpointEnvVar);
+ Environment.SetEnvironmentVariable(OtlpSpecConfigDefinitions.DefaultEndpointEnvVarName, endpointEnvVar);
OtlpExporterOptions options = new() { Protocol = OtlpExportProtocol.HttpProtobuf };
@@ -35,7 +35,7 @@ public void ValidateOtlpHttpExportClientEndpoint(string optionEndpoint, string e
}
finally
{
- Environment.SetEnvironmentVariable(OtlpExporterSpecEnvVarKeyDefinitions.DefaultEndpointEnvVarName, null);
+ Environment.SetEnvironmentVariable(OtlpSpecConfigDefinitions.DefaultEndpointEnvVarName, null);
}
}
diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsTests.cs
index f567ed118e1..4ba49ec760a 100644
--- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsTests.cs
+++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsTests.cs
@@ -13,6 +13,49 @@ public OtlpExporterOptionsTests()
ClearEnvVars();
}
+ public static IEnumerable