Skip to content

Commit

Permalink
Fix issue of multiple instances of OpenTelemetry-Sdk EventSource bein…
Browse files Browse the repository at this point in the history
…g created (#4586)
  • Loading branch information
utpilla committed Jun 13, 2023
1 parent b3ffb46 commit 542379e
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ public abstract class ConsoleExporter<T> : BaseExporter<T>
protected ConsoleExporter(ConsoleExporterOptions options)
{
this.options = options ?? new ConsoleExporterOptions();
ConsoleTagTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
this.WriteLine($"Unsupported attribute type {tagValueType} for {tagKey}.");
};
}

protected void WriteLine(string message)
{
if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Console))
{
System.Console.WriteLine(message);
Console.WriteLine(message);
}

if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Debug))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\TagTransformer.cs" Link="Includes\TagTransformer.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\UnconditionalSuppressMessageAttribute.cs" Link="Includes\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,17 @@ public void FailedExport(string exception)
{
this.WriteEvent(1, exception);
}

[Event(2, Message = "Unsupported attribute type '{0}' for '{1}'. Attribute will not be exported.", Level = EventLevel.Warning)]
public void UnsupportedAttributeType(string type, string key)
{
this.WriteEvent(2, type.ToString(), key);
}

[Event(3, Message = "{0} environment variable has an invalid value: '{1}'", Level = EventLevel.Warning)]
public void InvalidEnvironmentVariable(string key, string value)
{
this.WriteEvent(3, key, value);
}
}
}
10 changes: 10 additions & 0 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ internal JaegerExporter(JaegerExporterOptions options, TProtocolFactory protocol
throw new NotSupportedException();
}

JaegerTagTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
JaegerExporterEventSource.Log.UnsupportedAttributeType(tagValueType, tagKey);
};

ConfigurationExtensions.LogInvalidEnvironmentVariable = (string key, string value) =>
{
JaegerExporterEventSource.Log.InvalidEnvironmentVariable(key, value);
};

this.client = client;
this.batchWriter = protocolFactory.GetProtocol(this.maxPayloadSizeInBytes * 2);
this.spanWriter = protocolFactory.GetProtocol(this.maxPayloadSizeInBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,17 @@ public void CouldNotTranslateLogRecord(string exceptionMessage)
{
this.WriteEvent(9, exceptionMessage);
}

[Event(10, Message = "Unsupported attribute type '{0}' for '{1}'. Attribute will not be exported.", Level = EventLevel.Warning)]
public void UnsupportedAttributeType(string type, string key)
{
this.WriteEvent(10, type.ToString(), key);
}

[Event(11, Message = "{0} environment variable has an invalid value: '{1}'", Level = EventLevel.Warning)]
public void InvalidEnvironmentVariable(string key, string value)
{
this.WriteEvent(11, key, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Diagnostics;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1;
using OtlpResource = OpenTelemetry.Proto.Resource.V1;
Expand Down Expand Up @@ -59,6 +60,18 @@ public OtlpLogExporter(OtlpExporterOptions options)

this.sdkLimitOptions = sdkLimitOptions;

// Each of the Otlp exporters: Traces, Metrics, and Logs set the same value for `OtlpKeyValueTransformer.LogUnsupportedAttributeType`
// and `ConfigurationExtensions.LogInvalidEnvironmentVariable` so it should be fine even if these exporters are used together.
OtlpKeyValueTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
OpenTelemetryProtocolExporterEventSource.Log.UnsupportedAttributeType(tagValueType, tagKey);
};

ConfigurationExtensions.LogInvalidEnvironmentVariable = (string key, string value) =>
{
OpenTelemetryProtocolExporterEventSource.Log.InvalidEnvironmentVariable(key, value);
};

if (exportClient != null)
{
this.exportClient = exportClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.Internal;
using OpenTelemetry.Metrics;
using OtlpCollector = OpenTelemetry.Proto.Collector.Metrics.V1;
using OtlpResource = OpenTelemetry.Proto.Resource.V1;
Expand Down Expand Up @@ -48,6 +49,18 @@ public OtlpMetricExporter(OtlpExporterOptions options)
/// <param name="exportClient">Client used for sending export request.</param>
internal OtlpMetricExporter(OtlpExporterOptions options, IExportClient<OtlpCollector.ExportMetricsServiceRequest> exportClient = null)
{
// Each of the Otlp exporters: Traces, Metrics, and Logs set the same value for `OtlpKeyValueTransformer.LogUnsupportedAttributeType`
// and `ConfigurationExtensions.LogInvalidEnvironmentVariable` so it should be fine even if these exporters are used together.
OtlpKeyValueTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
OpenTelemetryProtocolExporterEventSource.Log.UnsupportedAttributeType(tagValueType, tagKey);
};

ConfigurationExtensions.LogInvalidEnvironmentVariable = (string key, string value) =>
{
OpenTelemetryProtocolExporterEventSource.Log.InvalidEnvironmentVariable(key, value);
};

if (exportClient != null)
{
this.exportClient = exportClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Diagnostics;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.Internal;
using OtlpCollector = OpenTelemetry.Proto.Collector.Trace.V1;
using OtlpResource = OpenTelemetry.Proto.Resource.V1;

Expand Down Expand Up @@ -58,6 +59,16 @@ public OtlpTraceExporter(OtlpExporterOptions options)

this.sdkLimitOptions = sdkLimitOptions;

OtlpKeyValueTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
OpenTelemetryProtocolExporterEventSource.Log.UnsupportedAttributeType(tagValueType, tagKey);
};

ConfigurationExtensions.LogInvalidEnvironmentVariable = (string key, string value) =>
{
OpenTelemetryProtocolExporterEventSource.Log.InvalidEnvironmentVariable(key, value);
};

if (exportClient != null)
{
this.exportClient = exportClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,17 @@ public void FailedExport(string exception)
{
this.WriteEvent(1, exception);
}

[Event(2, Message = "Unsupported attribute type '{0}' for '{1}'. Attribute will not be exported.", Level = EventLevel.Warning)]
public void UnsupportedAttributeType(string type, string key)
{
this.WriteEvent(2, type.ToString(), key);
}

[Event(3, Message = "{0} environment variable has an invalid value: '{1}'", Level = EventLevel.Warning)]
public void InvalidEnvironmentVariable(string key, string value)
{
this.WriteEvent(3, key, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
Expand Down
10 changes: 10 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public ZipkinExporter(ZipkinExporterOptions options, HttpClient client = null)
this.options = options;
this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0) ? ZipkinExporterOptions.DefaultMaxPayloadSizeInBytes : options.MaxPayloadSizeInBytes.Value;
this.httpClient = client ?? options.HttpClientFactory?.Invoke() ?? throw new InvalidOperationException("ZipkinExporter was missing HttpClientFactory or it returned null.");

ZipkinTagTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
ZipkinExporterEventSource.Log.UnsupportedAttributeType(tagValueType, tagKey);
};

ConfigurationExtensions.LogInvalidEnvironmentVariable = (string key, string value) =>
{
ZipkinExporterEventSource.Log.InvalidEnvironmentVariable(key, value);
};
}

internal ZipkinEndpoint LocalEndpoint { get; private set; }
Expand Down
5 changes: 5 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
provided during Logger creation.
([#4433](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4433))

* Fix the issue of potentially running into the `ArgumentException`: `An
instance of EventSource with Guid af2d5796-946b-50cb-5f76-166a609afcbb already
exists.` when using any of the following exporters: `ConsoleExporter`,
`OtlpExporter`, `ZipkinExporter`, `JaegerExporter`.

## 1.5.0

Released 2023-Jun-05
Expand Down
6 changes: 0 additions & 6 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,6 @@ public void MetricViewIgnored(string instrumentName, string meterName, string re
this.WriteEvent(41, instrumentName, meterName, reason, fix);
}

[Event(42, Message = "Unsupported attribute type '{0}' for '{1}'. Attribute will not be exported.", Level = EventLevel.Warning)]
public void UnsupportedAttributeType(string type, string key)
{
this.WriteEvent(42, type.ToString(), key);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
[Event(43, Message = "ForceFlush invoked for processor type '{0}' returned result '{1}'.", Level = EventLevel.Verbose)]
public void ProcessorForceFlushInvoked(string processorType, bool result)
Expand Down
8 changes: 5 additions & 3 deletions src/OpenTelemetry/Internal/Options/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace OpenTelemetry.Internal;

internal static class ConfigurationExtensions
{
public static Action<string, string>? LogInvalidEnvironmentVariable = null;

public delegate bool TryParseFunc<T>(
string value,
#if !NETFRAMEWORK && !NETSTANDARD2_0
Expand Down Expand Up @@ -66,7 +68,7 @@ internal static class ConfigurationExtensions

if (!Uri.TryCreate(stringValue, UriKind.Absolute, out value))
{
OpenTelemetrySdkEventSource.Log.InvalidEnvironmentVariable(key, stringValue);
LogInvalidEnvironmentVariable?.Invoke(key, stringValue!);
return false;
}

Expand All @@ -86,7 +88,7 @@ internal static class ConfigurationExtensions

if (!int.TryParse(stringValue, NumberStyles.None, CultureInfo.InvariantCulture, out value))
{
OpenTelemetrySdkEventSource.Log.InvalidEnvironmentVariable(key, stringValue);
LogInvalidEnvironmentVariable?.Invoke(key, stringValue!);
return false;
}

Expand All @@ -110,7 +112,7 @@ internal static class ConfigurationExtensions

if (!tryParseFunc(stringValue!, out value))
{
OpenTelemetrySdkEventSource.Log.InvalidEnvironmentVariable(key, stringValue);
LogInvalidEnvironmentVariable?.Invoke(key, stringValue!);
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions src/OpenTelemetry/Internal/TagTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace OpenTelemetry.Internal;

internal abstract class TagTransformer<T>
{
public static Action<string, string> LogUnsupportedAttributeType = null;

public bool TryTransformTag(KeyValuePair<string, object> tag, out T result, int? maxLength = null)
{
if (tag.Value == null)
Expand Down Expand Up @@ -58,7 +60,7 @@ public bool TryTransformTag(KeyValuePair<string, object> tag, out T result, int?
// If an exception is thrown when calling ToString
// on any element of the array, then the entire array value
// is ignored.
OpenTelemetrySdkEventSource.Log.UnsupportedAttributeType(tag.Value.GetType().ToString(), tag.Key);
LogUnsupportedAttributeType?.Invoke(tag.Value.GetType().ToString(), tag.Key);
result = default;
return false;
}
Expand All @@ -79,7 +81,7 @@ public bool TryTransformTag(KeyValuePair<string, object> tag, out T result, int?
catch
{
// If ToString throws an exception then the tag is ignored.
OpenTelemetrySdkEventSource.Log.UnsupportedAttributeType(tag.Value.GetType().ToString(), tag.Key);
LogUnsupportedAttributeType?.Invoke(tag.Value.GetType().ToString(), tag.Key);
result = default;
return false;
}
Expand Down

0 comments on commit 542379e

Please sign in to comment.