Skip to content

Commit 21aeaa7

Browse files
authored
[shared] Refactor logging for configuration extensions (#5469)
1 parent 7065739 commit 21aeaa7

21 files changed

+109
-66
lines changed

OpenTelemetry.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dedicated-pipeline", "docs\
343343
EndProject
344344
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Configuration", "Configuration", "{87A20A76-D524-4AAC-AF92-8725BFED0415}"
345345
ProjectSection(SolutionItems) = preProject
346+
src\Shared\Configuration\IConfigurationExtensionsLogger.cs = src\Shared\Configuration\IConfigurationExtensionsLogger.cs
346347
src\Shared\Configuration\OpenTelemetryConfigurationExtensions.cs = src\Shared\Configuration\OpenTelemetryConfigurationExtensions.cs
347348
EndProjectSection
348349
EndProject

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExperimentalOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public ExperimentalOptions()
2424

2525
public ExperimentalOptions(IConfiguration configuration)
2626
{
27-
if (configuration.TryGetBoolValue(EmitLogEventEnvVar, out var emitLogEventAttributes))
27+
if (configuration.TryGetBoolValue(OpenTelemetryProtocolExporterEventSource.Log, EmitLogEventEnvVar, out var emitLogEventAttributes))
2828
{
2929
this.EmitLogEventAttributes = emitLogEventAttributes;
3030
}
3131

32-
if (configuration.TryGetBoolValue(EnableInMemoryRetryEnvVar, out var enableInMemoryRetry))
32+
if (configuration.TryGetBoolValue(OpenTelemetryProtocolExporterEventSource.Log, EnableInMemoryRetryEnvVar, out var enableInMemoryRetry))
3333
{
3434
this.EnableInMemoryRetry = enableInMemoryRetry;
3535
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OpenTelemetryProtocolExporterEventSource.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics.Tracing;
5+
using Microsoft.Extensions.Configuration;
56
using OpenTelemetry.Internal;
67

78
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
89

910
[EventSource(Name = "OpenTelemetry-Exporter-OpenTelemetryProtocol")]
10-
internal sealed class OpenTelemetryProtocolExporterEventSource : EventSource
11+
internal sealed class OpenTelemetryProtocolExporterEventSource : EventSource, IConfigurationExtensionsLogger
1112
{
1213
public static readonly OpenTelemetryProtocolExporterEventSource Log = new();
1314

@@ -81,8 +82,8 @@ public void UnsupportedAttributeType(string type, string key)
8182
this.WriteEvent(10, type.ToString(), key);
8283
}
8384

84-
[Event(11, Message = "{0} environment variable has an invalid value: '{1}'", Level = EventLevel.Warning)]
85-
public void InvalidEnvironmentVariable(string key, string value)
85+
[Event(11, Message = "Configuration key '{0}' has an invalid value: '{1}'", Level = EventLevel.Warning)]
86+
public void InvalidConfigurationValue(string key, string value)
8687
{
8788
this.WriteEvent(11, key, value);
8889
}
@@ -92,4 +93,9 @@ public void TrySubmitRequestException(string ex)
9293
{
9394
this.WriteEvent(12, ex);
9495
}
96+
97+
void IConfigurationExtensionsLogger.LogInvalidConfigurationValue(string key, string value)
98+
{
99+
this.InvalidConfigurationValue(key, value);
100+
}
95101
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/SdkLimitOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public int? LogRecordAttributeCountLimit
163163

164164
private static void SetIntConfigValue(IConfiguration configuration, string key, Action<int?> setter, int? defaultValue)
165165
{
166-
if (configuration.TryGetIntValue(key, out var result))
166+
if (configuration.TryGetIntValue(OpenTelemetryProtocolExporterEventSource.Log, key, out var result))
167167
{
168168
setter(result);
169169
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Extensions.Configuration;
1212
using Microsoft.Extensions.DependencyInjection;
1313
using Microsoft.Extensions.Options;
14+
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
1415
using OpenTelemetry.Internal;
1516
using OpenTelemetry.Trace;
1617

@@ -179,13 +180,14 @@ internal void ApplyConfigurationUsingSpecificationEnvVars(
179180
string headersEnvVarKey,
180181
string timeoutEnvVarKey)
181182
{
182-
if (configuration.TryGetUriValue(endpointEnvVarKey, out var endpoint))
183+
if (configuration.TryGetUriValue(OpenTelemetryProtocolExporterEventSource.Log, endpointEnvVarKey, out var endpoint))
183184
{
184185
this.endpoint = endpoint;
185186
this.AppendSignalPathToEndpoint = appendSignalPathToEndpoint;
186187
}
187188

188189
if (configuration.TryGetValue<OtlpExportProtocol>(
190+
OpenTelemetryProtocolExporterEventSource.Log,
189191
protocolEnvVarKey,
190192
OtlpExportProtocolParser.TryParse,
191193
out var protocol))
@@ -198,7 +200,7 @@ internal void ApplyConfigurationUsingSpecificationEnvVars(
198200
this.Headers = headers;
199201
}
200202

201-
if (configuration.TryGetIntValue(timeoutEnvVarKey, out var timeout))
203+
if (configuration.TryGetIntValue(OpenTelemetryProtocolExporterEventSource.Log, timeoutEnvVarKey, out var timeout))
202204
{
203205
this.TimeoutMilliseconds = timeout;
204206
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporter.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#nullable enable
55

66
using System.Diagnostics;
7-
using Microsoft.Extensions.Configuration;
87
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
98
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission;
109
using OpenTelemetry.Logs;
@@ -36,7 +35,7 @@ public OtlpLogExporter(OtlpExporterOptions options)
3635
/// <summary>
3736
/// Initializes a new instance of the <see cref="OtlpLogExporter"/> class.
3837
/// </summary>
39-
/// <param name="exporterOptions">Configuration options for the exporter.</param>
38+
/// <param name="exporterOptions"><see cref="OtlpExporterOptions"/>.</param>
4039
/// <param name="sdkLimitOptions"><see cref="SdkLimitOptions"/>.</param>
4140
/// <param name="experimentalOptions"><see cref="ExperimentalOptions"/>.</param>
4241
/// <param name="transmissionHandler"><see cref="OtlpExporterTransmissionHandler{T}"/>.</param>
@@ -50,12 +49,6 @@ internal OtlpLogExporter(
5049
Debug.Assert(sdkLimitOptions != null, "sdkLimitOptions was null");
5150
Debug.Assert(experimentalOptions != null, "experimentalOptions was null");
5251

53-
// Each of the Otlp exporters: Traces, Metrics, and Logs set the same
54-
// value for
55-
// `OpenTelemetryConfigurationExtensions.LogInvalidEnvironmentVariable`
56-
// so it should be fine even if these exporters are used together.
57-
OpenTelemetryConfigurationExtensions.LogInvalidEnvironmentVariable = OpenTelemetryProtocolExporterEventSource.Log.InvalidEnvironmentVariable;
58-
5952
this.transmissionHandler = transmissionHandler ?? exporterOptions.GetLogsExportTransmissionHandler(experimentalOptions!);
6053

6154
this.otlpLogRecordTransformer = new OtlpLogRecordTransformer(sdkLimitOptions!, experimentalOptions!);

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporter.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using Microsoft.Extensions.Configuration;
4+
using System.Diagnostics;
55
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
66
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission;
77
using OpenTelemetry.Metrics;
@@ -32,21 +32,18 @@ public OtlpMetricExporter(OtlpExporterOptions options)
3232
/// <summary>
3333
/// Initializes a new instance of the <see cref="OtlpMetricExporter"/> class.
3434
/// </summary>
35-
/// <param name="options">Configuration options for the export.</param>
35+
/// <param name="exporterOptions"><see cref="OtlpExporterOptions"/>.</param>
3636
/// <param name="experimentalOptions"><see cref="ExperimentalOptions"/>.</param>
3737
/// <param name="transmissionHandler"><see cref="OtlpExporterTransmissionHandler{T}"/>.</param>
3838
internal OtlpMetricExporter(
39-
OtlpExporterOptions options,
39+
OtlpExporterOptions exporterOptions,
4040
ExperimentalOptions experimentalOptions,
4141
OtlpExporterTransmissionHandler<OtlpCollector.ExportMetricsServiceRequest> transmissionHandler = null)
4242
{
43-
// Each of the Otlp exporters: Traces, Metrics, and Logs set the same
44-
// value for
45-
// `OpenTelemetryConfigurationExtensions.LogInvalidEnvironmentVariable`
46-
// so it should be fine even if these exporters are used together.
47-
OpenTelemetryConfigurationExtensions.LogInvalidEnvironmentVariable = OpenTelemetryProtocolExporterEventSource.Log.InvalidEnvironmentVariable;
43+
Debug.Assert(exporterOptions != null, "exporterOptions was null");
44+
Debug.Assert(experimentalOptions != null, "experimentalOptions was null");
4845

49-
this.transmissionHandler = transmissionHandler ?? options.GetMetricsExportTransmissionHandler(experimentalOptions);
46+
this.transmissionHandler = transmissionHandler ?? exporterOptions.GetMetricsExportTransmissionHandler(experimentalOptions);
5047
}
5148

5249
internal OtlpResource.Resource ProcessResource => this.processResource ??= this.ParentProvider.GetResource().ToOtlpResource();

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporter.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics;
5-
using Microsoft.Extensions.Configuration;
65
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
76
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission;
87
using OtlpCollector = OpenTelemetry.Proto.Collector.Trace.V1;
@@ -48,12 +47,6 @@ internal OtlpTraceExporter(
4847

4948
this.sdkLimitOptions = sdkLimitOptions;
5049

51-
// Each of the Otlp exporters: Traces, Metrics, and Logs set the same
52-
// value for
53-
// `OpenTelemetryConfigurationExtensions.LogInvalidEnvironmentVariable`
54-
// so it should be fine even if these exporters are used together.
55-
OpenTelemetryConfigurationExtensions.LogInvalidEnvironmentVariable = OpenTelemetryProtocolExporterEventSource.Log.InvalidEnvironmentVariable;
56-
5750
this.transmissionHandler = transmissionHandler ?? exporterOptions.GetTraceExportTransmissionHandler(experimentalOptions);
5851
}
5952

src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinExporterEventSource.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics.Tracing;
5+
using Microsoft.Extensions.Configuration;
56
using OpenTelemetry.Internal;
67

78
namespace OpenTelemetry.Exporter.Zipkin.Implementation;
@@ -10,7 +11,7 @@ namespace OpenTelemetry.Exporter.Zipkin.Implementation;
1011
/// EventSource events emitted from the project.
1112
/// </summary>
1213
[EventSource(Name = "OpenTelemetry-Exporter-Zipkin")]
13-
internal sealed class ZipkinExporterEventSource : EventSource
14+
internal sealed class ZipkinExporterEventSource : EventSource, IConfigurationExtensionsLogger
1415
{
1516
public static ZipkinExporterEventSource Log = new();
1617

@@ -35,9 +36,14 @@ public void UnsupportedAttributeType(string type, string key)
3536
this.WriteEvent(2, type.ToString(), key);
3637
}
3738

38-
[Event(3, Message = "{0} environment variable has an invalid value: '{1}'", Level = EventLevel.Warning)]
39-
public void InvalidEnvironmentVariable(string key, string value)
39+
[Event(3, Message = "Configuration key '{0}' has an invalid value: '{1}'", Level = EventLevel.Warning)]
40+
public void InvalidConfigurationValue(string key, string value)
4041
{
4142
this.WriteEvent(3, key, value);
4243
}
44+
45+
void IConfigurationExtensionsLogger.LogInvalidConfigurationValue(string key, string value)
46+
{
47+
this.InvalidConfigurationValue(key, value);
48+
}
4349
}

src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Net.Sockets;
1111
using System.Runtime.CompilerServices;
1212
using System.Text.Json;
13-
using Microsoft.Extensions.Configuration;
1413
using OpenTelemetry.Exporter.Zipkin.Implementation;
1514
using OpenTelemetry.Internal;
1615
using OpenTelemetry.Resources;
@@ -38,8 +37,6 @@ public ZipkinExporter(ZipkinExporterOptions options, HttpClient client = null)
3837
this.options = options;
3938
this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0) ? ZipkinExporterOptions.DefaultMaxPayloadSizeInBytes : options.MaxPayloadSizeInBytes.Value;
4039
this.httpClient = client ?? options.HttpClientFactory?.Invoke() ?? throw new InvalidOperationException("ZipkinExporter was missing HttpClientFactory or it returned null.");
41-
42-
OpenTelemetryConfigurationExtensions.LogInvalidEnvironmentVariable = ZipkinExporterEventSource.Log.InvalidEnvironmentVariable;
4340
}
4441

4542
internal ZipkinEndpoint LocalEndpoint { get; private set; }

0 commit comments

Comments
 (0)