Skip to content

Commit

Permalink
[Exporter.Zipkin] Nullable (#5792)
Browse files Browse the repository at this point in the history
Co-authored-by: Mikel Blanchard <[email protected]>
  • Loading branch information
ysolomchenko and CodeBlanch authored Sep 11, 2024
1 parent 37535a5 commit dd74056
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#nullable enable
OpenTelemetry.Exporter.ZipkinExporter
OpenTelemetry.Exporter.ZipkinExporter.ZipkinExporter(OpenTelemetry.Exporter.ZipkinExporterOptions options, System.Net.Http.HttpClient client = null) -> void
OpenTelemetry.Exporter.ZipkinExporter.ZipkinExporter(OpenTelemetry.Exporter.ZipkinExporterOptions! options, System.Net.Http.HttpClient? client = null) -> void
OpenTelemetry.Exporter.ZipkinExporterOptions
OpenTelemetry.Exporter.ZipkinExporterOptions.BatchExportProcessorOptions.get -> OpenTelemetry.BatchExportProcessorOptions<System.Diagnostics.Activity>
OpenTelemetry.Exporter.ZipkinExporterOptions.BatchExportProcessorOptions.set -> void
OpenTelemetry.Exporter.ZipkinExporterOptions.Endpoint.get -> System.Uri
OpenTelemetry.Exporter.ZipkinExporterOptions.BatchExportProcessorOptions.get -> OpenTelemetry.BatchExportProcessorOptions<System.Diagnostics.Activity!>!
OpenTelemetry.Exporter.ZipkinExporterOptions.HttpClientFactory.get -> System.Func<System.Net.Http.HttpClient!>!
OpenTelemetry.Exporter.ZipkinExporterOptions.Endpoint.get -> System.Uri!
OpenTelemetry.Exporter.ZipkinExporterOptions.Endpoint.set -> void
OpenTelemetry.Exporter.ZipkinExporterOptions.ExportProcessorType.get -> OpenTelemetry.ExportProcessorType
OpenTelemetry.Exporter.ZipkinExporterOptions.ExportProcessorType.set -> void
OpenTelemetry.Exporter.ZipkinExporterOptions.HttpClientFactory.get -> System.Func<System.Net.Http.HttpClient>
OpenTelemetry.Exporter.ZipkinExporterOptions.HttpClientFactory.set -> void
OpenTelemetry.Exporter.ZipkinExporterOptions.MaxPayloadSizeInBytes.get -> int?
OpenTelemetry.Exporter.ZipkinExporterOptions.MaxPayloadSizeInBytes.set -> void
OpenTelemetry.Exporter.ZipkinExporterOptions.UseShortTraceIds.get -> bool
OpenTelemetry.Exporter.ZipkinExporterOptions.UseShortTraceIds.set -> void
OpenTelemetry.Exporter.ZipkinExporterOptions.ZipkinExporterOptions() -> void
OpenTelemetry.Trace.ZipkinExporterHelperExtensions
override OpenTelemetry.Exporter.ZipkinExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Trace.ZipkinExporterHelperExtensions.AddZipkinExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.ZipkinExporterOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.ZipkinExporterHelperExtensions.AddZipkinExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Exporter.ZipkinExporterOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.ZipkinExporterHelperExtensions.AddZipkinExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
override OpenTelemetry.Exporter.ZipkinExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity!> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Trace.ZipkinExporterHelperExtensions.AddZipkinExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.ZipkinExporterHelperExtensions.AddZipkinExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string? name, System.Action<OpenTelemetry.Exporter.ZipkinExporterOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.ZipkinExporterHelperExtensions.AddZipkinExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Exporter.ZipkinExporterOptions!>! configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ internal static ZipkinSpan ToZipkinSpan(this Activity activity, ZipkinEndpoint l
{
var context = activity.Context;

string parentId = activity.ParentSpanId == default ?
string? parentId = activity.ParentSpanId == default ?
null
: EncodeSpanId(activity.ParentSpanId);

var tagState = new TagEnumerationState
{
Tags = PooledList<KeyValuePair<string, object>>.Create(),
Tags = PooledList<KeyValuePair<string, object?>>.Create(),
};

tagState.EnumerateTags(activity);
Expand All @@ -38,26 +38,26 @@ internal static ZipkinSpan ToZipkinSpan(this Activity activity, ZipkinEndpoint l
{
if (activity.Status == ActivityStatusCode.Ok)
{
PooledList<KeyValuePair<string, object>>.Add(
PooledList<KeyValuePair<string, object?>>.Add(
ref tagState.Tags,
new KeyValuePair<string, object>(
new KeyValuePair<string, object?>(
SpanAttributeConstants.StatusCodeKey,
"OK"));
}

// activity.Status is Error
else
{
PooledList<KeyValuePair<string, object>>.Add(
PooledList<KeyValuePair<string, object?>>.Add(
ref tagState.Tags,
new KeyValuePair<string, object>(
new KeyValuePair<string, object?>(
SpanAttributeConstants.StatusCodeKey,
"ERROR"));

// Error flag rule from https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/zipkin.md#status
PooledList<KeyValuePair<string, object>>.Add(
PooledList<KeyValuePair<string, object?>>.Add(
ref tagState.Tags,
new KeyValuePair<string, object>(
new KeyValuePair<string, object?>(
ZipkinErrorFlagTagName,
activity.StatusDescription ?? string.Empty));
}
Expand All @@ -67,18 +67,18 @@ internal static ZipkinSpan ToZipkinSpan(this Activity activity, ZipkinEndpoint l
// activity status takes precedence over status tag.
else if (tagState.StatusCode.HasValue && tagState.StatusCode != StatusCode.Unset)
{
PooledList<KeyValuePair<string, object>>.Add(
PooledList<KeyValuePair<string, object?>>.Add(
ref tagState.Tags,
new KeyValuePair<string, object>(
new KeyValuePair<string, object?>(
SpanAttributeConstants.StatusCodeKey,
StatusHelper.GetTagValueForStatusCode(tagState.StatusCode.Value)));

// Error flag rule from https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/zipkin.md#status
if (tagState.StatusCode == StatusCode.Error)
{
PooledList<KeyValuePair<string, object>>.Add(
PooledList<KeyValuePair<string, object?>>.Add(
ref tagState.Tags,
new KeyValuePair<string, object>(
new KeyValuePair<string, object?>(
ZipkinErrorFlagTagName,
tagState.StatusDescription ?? string.Empty));
}
Expand All @@ -87,30 +87,30 @@ internal static ZipkinSpan ToZipkinSpan(this Activity activity, ZipkinEndpoint l
var activitySource = activity.Source;
if (!string.IsNullOrEmpty(activitySource.Name))
{
PooledList<KeyValuePair<string, object>>.Add(ref tagState.Tags, new KeyValuePair<string, object>("otel.scope.name", activitySource.Name));
PooledList<KeyValuePair<string, object?>>.Add(ref tagState.Tags, new KeyValuePair<string, object?>("otel.scope.name", activitySource.Name));

// otel.library.name is deprecated, but has to be propagated according to https://github.com/open-telemetry/opentelemetry-specification/blob/v1.31.0/specification/common/mapping-to-non-otlp.md#instrumentationscope
PooledList<KeyValuePair<string, object>>.Add(ref tagState.Tags, new KeyValuePair<string, object>("otel.library.name", activitySource.Name));
PooledList<KeyValuePair<string, object?>>.Add(ref tagState.Tags, new KeyValuePair<string, object?>("otel.library.name", activitySource.Name));
if (!string.IsNullOrEmpty(activitySource.Version))
{
PooledList<KeyValuePair<string, object>>.Add(ref tagState.Tags, new KeyValuePair<string, object>("otel.scope.version", activitySource.Version));
PooledList<KeyValuePair<string, object?>>.Add(ref tagState.Tags, new KeyValuePair<string, object?>("otel.scope.version", activitySource.Version));

// otel.library.version is deprecated, but has to be propagated according to https://github.com/open-telemetry/opentelemetry-specification/blob/v1.31.0/specification/common/mapping-to-non-otlp.md#instrumentationscope
PooledList<KeyValuePair<string, object>>.Add(ref tagState.Tags, new KeyValuePair<string, object>("otel.library.version", activitySource.Version));
PooledList<KeyValuePair<string, object?>>.Add(ref tagState.Tags, new KeyValuePair<string, object?>("otel.library.version", activitySource.Version));
}
}

ZipkinEndpoint remoteEndpoint = null;
ZipkinEndpoint? remoteEndpoint = null;
if (activity.Kind == ActivityKind.Client || activity.Kind == ActivityKind.Producer)
{
PeerServiceResolver.Resolve(ref tagState, out string peerServiceName, out bool addAsTag);
PeerServiceResolver.Resolve(ref tagState, out string? peerServiceName, out bool addAsTag);

if (peerServiceName != null)
{
remoteEndpoint = RemoteEndpointCache.GetOrAdd((peerServiceName, default), ZipkinEndpoint.Create);
if (addAsTag)
{
PooledList<KeyValuePair<string, object>>.Add(ref tagState.Tags, new KeyValuePair<string, object>(SemanticConventions.AttributePeerService, peerServiceName));
PooledList<KeyValuePair<string, object?>>.Add(ref tagState.Tags, new KeyValuePair<string, object?>(SemanticConventions.AttributePeerService, peerServiceName));
}
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ private static string EncodeTraceId(ActivityTraceId traceId, bool useShortTraceI
return id;
}

private static string ToActivityKind(Activity activity)
private static string? ToActivityKind(Activity activity)
{
return activity.Kind switch
{
Expand All @@ -186,15 +186,15 @@ private static string ToActivityKind(Activity activity)

internal struct TagEnumerationState : PeerServiceResolver.IPeerServiceState
{
public PooledList<KeyValuePair<string, object>> Tags;
public PooledList<KeyValuePair<string, object?>> Tags;

public string PeerService { get; set; }
public string? PeerService { get; set; }

public int? PeerServicePriority { get; set; }

public string HostName { get; set; }
public string? HostName { get; set; }

public string IpAddress { get; set; }
public string? IpAddress { get; set; }

public long Port { get; set; }

Expand Down Expand Up @@ -239,7 +239,7 @@ public void EnumerateTags(Activity activity)
PeerServiceResolver.InspectTag(ref this, key, intVal);
}

PooledList<KeyValuePair<string, object>>.Add(ref this.Tags, tag);
PooledList<KeyValuePair<string, object?>>.Add(ref this.Tags, tag);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public ZipkinEndpoint(string serviceName)
}

public ZipkinEndpoint(
string serviceName,
string ipv4,
string ipv6,
string? serviceName,
string? ipv4,
string? ipv6,
int? port,
Dictionary<string, object> tags)
Dictionary<string, object>? tags)
{
this.ServiceName = serviceName;
this.Ipv4 = ipv4;
Expand All @@ -26,15 +26,15 @@ public ZipkinEndpoint(
this.Tags = tags;
}

public string ServiceName { get; }
public string? ServiceName { get; }

public string Ipv4 { get; }
public string? Ipv4 { get; }

public string Ipv6 { get; }
public string? Ipv6 { get; }

public int? Port { get; }

public Dictionary<string, object> Tags { get; }
public Dictionary<string, object>? Tags { get; }

public static ZipkinEndpoint Create(string serviceName)
{
Expand Down
20 changes: 10 additions & 10 deletions src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ internal readonly struct ZipkinSpan
{
public ZipkinSpan(
string traceId,
string parentId,
string? parentId,
string id,
string kind,
string? kind,
string name,
long? timestamp,
long? duration,
ZipkinEndpoint localEndpoint,
ZipkinEndpoint remoteEndpoint,
ZipkinEndpoint? remoteEndpoint,
in PooledList<ZipkinAnnotation> annotations,
in PooledList<KeyValuePair<string, object>> tags,
in PooledList<KeyValuePair<string, object?>> tags,
bool? debug,
bool? shared)
{
Expand All @@ -44,11 +44,11 @@ public ZipkinSpan(

public string TraceId { get; }

public string ParentId { get; }
public string? ParentId { get; }

public string Id { get; }

public string Kind { get; }
public string? Kind { get; }

public string Name { get; }

Expand All @@ -58,11 +58,11 @@ public ZipkinSpan(

public ZipkinEndpoint LocalEndpoint { get; }

public ZipkinEndpoint RemoteEndpoint { get; }
public ZipkinEndpoint? RemoteEndpoint { get; }

public PooledList<ZipkinAnnotation> Annotations { get; }

public PooledList<KeyValuePair<string, object>> Tags { get; }
public PooledList<KeyValuePair<string, object?>> Tags { get; }

public bool? Debug { get; }

Expand Down Expand Up @@ -148,7 +148,7 @@ public void Write(Utf8JsonWriter writer)
writer.WriteEndArray();
}

if (!this.Tags.IsEmpty || this.LocalEndpoint.Tags != null)
if (!this.Tags.IsEmpty || this.LocalEndpoint!.Tags != null)
{
writer.WritePropertyName(ZipkinSpanJsonHelper.TagsPropertyName);
writer.WriteStartObject();
Expand All @@ -161,7 +161,7 @@ public void Write(Utf8JsonWriter writer)

try
{
foreach (var tag in this.LocalEndpoint.Tags ?? Enumerable.Empty<KeyValuePair<string, object>>())
foreach (var tag in this.LocalEndpoint!.Tags! ?? Enumerable.Empty<KeyValuePair<string, object?>>())
{
ZipkinTagWriter.Instance.TryWriteTag(ref writer, tag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<Description>Zipkin exporter for OpenTelemetry .NET</Description>
<PackageTags>$(PackageTags);Zipkin;distributed-tracing</PackageTags>
<MinVerTagPrefix>core-</MinVerTagPrefix>

<!-- this is temporary. will remove in future PR. -->
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
30 changes: 15 additions & 15 deletions src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ZipkinExporter : BaseExporter<Activity>
/// </summary>
/// <param name="options">Configuration options.</param>
/// <param name="client">Http client to use to upload telemetry.</param>
public ZipkinExporter(ZipkinExporterOptions options, HttpClient client = null)
public ZipkinExporter(ZipkinExporterOptions options, HttpClient? client = null)
{
Guard.ThrowIfNull(options);

Expand All @@ -39,7 +39,7 @@ public ZipkinExporter(ZipkinExporterOptions options, HttpClient client = null)
this.httpClient = client ?? options.HttpClientFactory?.Invoke() ?? throw new InvalidOperationException("ZipkinExporter was missing HttpClientFactory or it returned null.");
}

internal ZipkinEndpoint LocalEndpoint { get; private set; }
internal ZipkinEndpoint? LocalEndpoint { get; private set; }

/// <inheritdoc/>
public override ExportResult Export(in Batch<Activity> batch)
Expand Down Expand Up @@ -83,15 +83,15 @@ internal void SetLocalEndpointFromResource(Resource resource)
{
var hostName = ResolveHostName();

string ipv4 = null;
string ipv6 = null;
string? ipv4 = null;
string? ipv6 = null;
if (!string.IsNullOrEmpty(hostName))
{
ipv4 = ResolveHostAddress(hostName, AddressFamily.InterNetwork);
ipv6 = ResolveHostAddress(hostName, AddressFamily.InterNetworkV6);
ipv4 = ResolveHostAddress(hostName!, AddressFamily.InterNetwork);
ipv6 = ResolveHostAddress(hostName!, AddressFamily.InterNetworkV6);
}

string serviceName = null;
string? serviceName = null;
foreach (var label in resource.Attributes)
{
if (label.Key == ResourceSemanticConventions.AttributeServiceName)
Expand All @@ -115,9 +115,9 @@ internal void SetLocalEndpointFromResource(Resource resource)
tags: null);
}

private static string ResolveHostAddress(string hostName, AddressFamily family)
private static string? ResolveHostAddress(string hostName, AddressFamily family)
{
string result = null;
string? result = null;

try
{
Expand Down Expand Up @@ -145,9 +145,9 @@ private static string ResolveHostAddress(string hostName, AddressFamily family)
return result;
}

private static string ResolveHostName()
private static string? ResolveHostName()
{
string result = null;
string? result = null;

try
{
Expand Down Expand Up @@ -180,7 +180,7 @@ private sealed class JsonContent : HttpContent

private readonly ZipkinExporter exporter;
private readonly Batch<Activity> batch;
private Utf8JsonWriter writer;
private Utf8JsonWriter? writer;

public JsonContent(ZipkinExporter exporter, in Batch<Activity> batch)
{
Expand All @@ -191,13 +191,13 @@ public JsonContent(ZipkinExporter exporter, in Batch<Activity> batch)
}

#if NET
protected override void SerializeToStream(Stream stream, TransportContext context, CancellationToken cancellationToken)
protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken)
{
this.SerializeToStreamInternal(stream);
}
#endif

protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
this.SerializeToStreamInternal(stream);
return Task.CompletedTask;
Expand Down Expand Up @@ -226,7 +226,7 @@ private void SerializeToStreamInternal(Stream stream)

foreach (var activity in this.batch)
{
var zipkinSpan = activity.ToZipkinSpan(this.exporter.LocalEndpoint, this.exporter.options.UseShortTraceIds);
var zipkinSpan = activity.ToZipkinSpan(this.exporter.LocalEndpoint!, this.exporter.options.UseShortTraceIds);

zipkinSpan.Write(this.writer);

Expand Down
Loading

0 comments on commit dd74056

Please sign in to comment.