Skip to content

Commit

Permalink
[Propagators] Nullable annotations (#5767)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Aug 28, 2024
1 parent 1b3f189 commit ba8a0e4
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 84 deletions.
36 changes: 18 additions & 18 deletions src/OpenTelemetry.Api/.publicApi/Stable/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#nullable enable
~abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string, System.Collections.Generic.IEnumerable<string>> getter) -> OpenTelemetry.Context.Propagation.PropagationContext
~abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Fields.get -> System.Collections.Generic.ISet<string>
~abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string, string> setter) -> void
~OpenTelemetry.Baggage.GetBaggage() -> System.Collections.Generic.IReadOnlyDictionary<string, string>
~OpenTelemetry.Baggage.GetBaggage(string name) -> string
~OpenTelemetry.Baggage.GetEnumerator() -> System.Collections.Generic.Dictionary<string, string>.Enumerator
Expand All @@ -14,34 +11,19 @@
~OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Value.set -> void
~OpenTelemetry.Context.IRuntimeContextSlotValueAccessor.Value.get -> object
~OpenTelemetry.Context.IRuntimeContextSlotValueAccessor.Value.set -> void
~OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.CompositeTextMapPropagator(System.Collections.Generic.IEnumerable<OpenTelemetry.Context.Propagation.TextMapPropagator> propagators) -> void
~OpenTelemetry.Context.RuntimeContextSlot<T>.Name.get -> string
~OpenTelemetry.Context.RuntimeContextSlot<T>.RuntimeContextSlot(string name) -> void
~OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.ThreadLocalRuntimeContextSlot(string name) -> void
~OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Value.get -> object
~OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Value.set -> void
~override OpenTelemetry.Baggage.Equals(object obj) -> bool
~override OpenTelemetry.Context.Propagation.B3Propagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string, System.Collections.Generic.IEnumerable<string>> getter) -> OpenTelemetry.Context.Propagation.PropagationContext
~override OpenTelemetry.Context.Propagation.B3Propagator.Fields.get -> System.Collections.Generic.ISet<string>
~override OpenTelemetry.Context.Propagation.B3Propagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string, string> setter) -> void
~override OpenTelemetry.Context.Propagation.BaggagePropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string, System.Collections.Generic.IEnumerable<string>> getter) -> OpenTelemetry.Context.Propagation.PropagationContext
~override OpenTelemetry.Context.Propagation.BaggagePropagator.Fields.get -> System.Collections.Generic.ISet<string>
~override OpenTelemetry.Context.Propagation.BaggagePropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string, string> setter) -> void
~override OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string, System.Collections.Generic.IEnumerable<string>> getter) -> OpenTelemetry.Context.Propagation.PropagationContext
~override OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.Fields.get -> System.Collections.Generic.ISet<string>
~override OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string, string> setter) -> void
~override OpenTelemetry.Context.Propagation.PropagationContext.Equals(object obj) -> bool
~override OpenTelemetry.Context.Propagation.TraceContextPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string, System.Collections.Generic.IEnumerable<string>> getter) -> OpenTelemetry.Context.Propagation.PropagationContext
~override OpenTelemetry.Context.Propagation.TraceContextPropagator.Fields.get -> System.Collections.Generic.ISet<string>
~override OpenTelemetry.Context.Propagation.TraceContextPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string, string> setter) -> void
~static OpenTelemetry.Baggage.Create(System.Collections.Generic.Dictionary<string, string> baggageItems = null) -> OpenTelemetry.Baggage
~static OpenTelemetry.Baggage.GetBaggage(OpenTelemetry.Baggage baggage = default(OpenTelemetry.Baggage)) -> System.Collections.Generic.IReadOnlyDictionary<string, string>
~static OpenTelemetry.Baggage.GetBaggage(string name, OpenTelemetry.Baggage baggage = default(OpenTelemetry.Baggage)) -> string
~static OpenTelemetry.Baggage.GetEnumerator(OpenTelemetry.Baggage baggage = default(OpenTelemetry.Baggage)) -> System.Collections.Generic.Dictionary<string, string>.Enumerator
~static OpenTelemetry.Baggage.RemoveBaggage(string name, OpenTelemetry.Baggage baggage = default(OpenTelemetry.Baggage)) -> OpenTelemetry.Baggage
~static OpenTelemetry.Baggage.SetBaggage(string name, string value, OpenTelemetry.Baggage baggage = default(OpenTelemetry.Baggage)) -> OpenTelemetry.Baggage
~static OpenTelemetry.Baggage.SetBaggage(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> baggageItems, OpenTelemetry.Baggage baggage = default(OpenTelemetry.Baggage)) -> OpenTelemetry.Baggage
~static OpenTelemetry.Context.Propagation.Propagators.DefaultTextMapPropagator.get -> OpenTelemetry.Context.Propagation.TextMapPropagator
~static OpenTelemetry.Context.RuntimeContext.ContextSlotType.get -> System.Type
~static OpenTelemetry.Context.RuntimeContext.ContextSlotType.set -> void
~static OpenTelemetry.Context.RuntimeContext.GetSlot<T>(string slotName) -> OpenTelemetry.Context.RuntimeContextSlot<T>
Expand All @@ -50,6 +32,9 @@
~static OpenTelemetry.Context.RuntimeContext.RegisterSlot<T>(string slotName) -> OpenTelemetry.Context.RuntimeContextSlot<T>
~static OpenTelemetry.Context.RuntimeContext.SetValue(string slotName, object value) -> void
~static OpenTelemetry.Context.RuntimeContext.SetValue<T>(string slotName, T value) -> void
abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Fields.get -> System.Collections.Generic.ISet<string!>?
abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
abstract OpenTelemetry.Context.RuntimeContextSlot<T>.Get() -> T
abstract OpenTelemetry.Context.RuntimeContextSlot<T>.Set(T value) -> void
abstract OpenTelemetry.Logs.LoggerProviderBuilder.AddInstrumentation<TInstrumentation>(System.Func<TInstrumentation>! instrumentationFactory) -> OpenTelemetry.Logs.LoggerProviderBuilder!
Expand All @@ -73,6 +58,7 @@ OpenTelemetry.Context.IRuntimeContextSlotValueAccessor
OpenTelemetry.Context.Propagation.B3Propagator
OpenTelemetry.Context.Propagation.B3Propagator.B3Propagator() -> void
OpenTelemetry.Context.Propagation.B3Propagator.B3Propagator(bool singleHeader) -> void
OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.CompositeTextMapPropagator(System.Collections.Generic.IEnumerable<OpenTelemetry.Context.Propagation.TextMapPropagator!>! propagators) -> void
OpenTelemetry.Context.Propagation.BaggagePropagator
OpenTelemetry.Context.Propagation.BaggagePropagator.BaggagePropagator() -> void
OpenTelemetry.Context.Propagation.CompositeTextMapPropagator
Expand Down Expand Up @@ -188,7 +174,20 @@ OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void
override OpenTelemetry.Baggage.GetHashCode() -> int
override OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Get() -> T
override OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Set(T value) -> void
override OpenTelemetry.Context.Propagation.B3Propagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
override OpenTelemetry.Context.Propagation.B3Propagator.Fields.get -> System.Collections.Generic.ISet<string!>!
override OpenTelemetry.Context.Propagation.B3Propagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
override OpenTelemetry.Context.Propagation.BaggagePropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
override OpenTelemetry.Context.Propagation.BaggagePropagator.Fields.get -> System.Collections.Generic.ISet<string!>!
override OpenTelemetry.Context.Propagation.BaggagePropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
override OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
override OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.Fields.get -> System.Collections.Generic.ISet<string!>!
override OpenTelemetry.Context.Propagation.CompositeTextMapPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
override OpenTelemetry.Context.Propagation.PropagationContext.GetHashCode() -> int
override OpenTelemetry.Context.Propagation.PropagationContext.Equals(object? obj) -> bool
override OpenTelemetry.Context.Propagation.TraceContextPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
override OpenTelemetry.Context.Propagation.TraceContextPropagator.Fields.get -> System.Collections.Generic.ISet<string!>!
override OpenTelemetry.Context.Propagation.TraceContextPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
override OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Dispose(bool disposing) -> void
override OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Get() -> T
override OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Set(T value) -> void
Expand All @@ -208,6 +207,7 @@ static OpenTelemetry.Baggage.operator !=(OpenTelemetry.Baggage left, OpenTelemet
static OpenTelemetry.Baggage.operator ==(OpenTelemetry.Baggage left, OpenTelemetry.Baggage right) -> bool
static OpenTelemetry.Context.Propagation.PropagationContext.operator !=(OpenTelemetry.Context.Propagation.PropagationContext left, OpenTelemetry.Context.Propagation.PropagationContext right) -> bool
static OpenTelemetry.Context.Propagation.PropagationContext.operator ==(OpenTelemetry.Context.Propagation.PropagationContext left, OpenTelemetry.Context.Propagation.PropagationContext right) -> bool
static OpenTelemetry.Context.Propagation.Propagators.DefaultTextMapPropagator.get -> OpenTelemetry.Context.Propagation.TextMapPropagator!
static OpenTelemetry.Trace.ActivityExtensions.GetStatus(this System.Diagnostics.Activity? activity) -> OpenTelemetry.Trace.Status
static OpenTelemetry.Trace.ActivityExtensions.RecordException(this System.Diagnostics.Activity? activity, System.Exception? ex, in System.Diagnostics.TagList tags) -> void
static OpenTelemetry.Trace.ActivityExtensions.RecordException(this System.Diagnostics.Activity? activity, System.Exception? ex) -> void
Expand Down
13 changes: 8 additions & 5 deletions src/OpenTelemetry.Api/Context/Propagation/B3Propagator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

using System.Diagnostics;
using System.Text;
using OpenTelemetry.Internal;
Expand Down Expand Up @@ -66,7 +68,7 @@ public B3Propagator(bool singleHeader)
/// <inheritdoc/>
[Obsolete("Use B3Propagator class from OpenTelemetry.Extensions.Propagators namespace, shipped as part of OpenTelemetry.Extensions.Propagators package.")]
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
{
if (context.ActivityContext.IsValid())
Expand Down Expand Up @@ -146,7 +148,7 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
}
}

private static PropagationContext ExtractFromMultipleHeaders<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
private static PropagationContext ExtractFromMultipleHeaders<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
{
try
{
Expand Down Expand Up @@ -179,7 +181,8 @@ private static PropagationContext ExtractFromMultipleHeaders<T>(PropagationConte
}

var traceOptions = ActivityTraceFlags.None;
if (SampledValues.Contains(getter(carrier, XB3Sampled)?.FirstOrDefault())
var xb3Sampled = getter(carrier, XB3Sampled)?.FirstOrDefault();
if ((xb3Sampled != null && SampledValues.Contains(xb3Sampled))
|| FlagsValue.Equals(getter(carrier, XB3Flags)?.FirstOrDefault(), StringComparison.Ordinal))
{
traceOptions |= ActivityTraceFlags.Recorded;
Expand All @@ -196,7 +199,7 @@ private static PropagationContext ExtractFromMultipleHeaders<T>(PropagationConte
}
}

private static PropagationContext ExtractFromSingleHeader<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
private static PropagationContext ExtractFromSingleHeader<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
{
try
{
Expand All @@ -206,7 +209,7 @@ private static PropagationContext ExtractFromSingleHeader<T>(PropagationContext
return context;
}

var parts = header.Split(XB3CombinedDelimiter);
var parts = header!.Split(XB3CombinedDelimiter);
if (parts.Length < 2 || parts.Length > 4)
{
return context;
Expand Down
26 changes: 18 additions & 8 deletions src/OpenTelemetry.Api/Context/Propagation/BaggagePropagator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using System.Net;
using System.Text;
using OpenTelemetry.Internal;
Expand All @@ -24,7 +29,7 @@ public class BaggagePropagator : TextMapPropagator
public override ISet<string> Fields => new HashSet<string> { BaggageHeaderName };

/// <inheritdoc/>
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
{
if (context.Baggage != default)
{
Expand All @@ -46,16 +51,16 @@ public override PropagationContext Extract<T>(PropagationContext context, T carr

try
{
Dictionary<string, string> baggage = null;
var baggageCollection = getter(carrier, BaggageHeaderName);
if (baggageCollection?.Any() ?? false)
{
TryExtractBaggage(baggageCollection.ToArray(), out baggage);
if (TryExtractBaggage(baggageCollection.ToArray(), out var baggage))
{
return new PropagationContext(context.ActivityContext, new Baggage(baggage));
}
}

return new PropagationContext(
context.ActivityContext,
baggage == null ? context.Baggage : new Baggage(baggage));
return new PropagationContext(context.ActivityContext, context.Baggage);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -102,11 +107,16 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
}
}

internal static bool TryExtractBaggage(string[] baggageCollection, out Dictionary<string, string> baggage)
internal static bool TryExtractBaggage(
string[] baggageCollection,
#if NET
[NotNullWhen(true)]
#endif
out Dictionary<string, string>? baggage)
{
int baggageLength = -1;
bool done = false;
Dictionary<string, string> baggageDictionary = null;
Dictionary<string, string>? baggageDictionary = null;

foreach (var item in baggageCollection)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

using OpenTelemetry.Internal;

namespace OpenTelemetry.Context.Propagation;
Expand Down Expand Up @@ -43,7 +45,7 @@ public CompositeTextMapPropagator(IEnumerable<TextMapPropagator> propagators)
}
else
{
ISet<string> fields = this.propagators[0].Fields;
ISet<string>? fields = this.propagators[0].Fields;

var output = fields is not null
? new HashSet<string>(fields)
Expand All @@ -66,7 +68,7 @@ public CompositeTextMapPropagator(IEnumerable<TextMapPropagator> propagators)
public override ISet<string> Fields => this.allFields;

/// <inheritdoc/>
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
{
for (int i = 0; i < this.propagators.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

namespace OpenTelemetry.Context.Propagation;

internal sealed class NoopTextMapPropagator : TextMapPropagator
{
private static readonly PropagationContext DefaultPropagationContext = default;

public override ISet<string> Fields => null;
public override ISet<string>? Fields => null;

public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
{
return DefaultPropagationContext;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

using System.Diagnostics;

namespace OpenTelemetry.Context.Propagation;
Expand Down Expand Up @@ -53,7 +55,7 @@ public bool Equals(PropagationContext value)
}

/// <inheritdoc/>
public override bool Equals(object obj) => (obj is PropagationContext context) && this.Equals(context);
public override bool Equals(object? obj) => (obj is PropagationContext context) && this.Equals(context);

/// <inheritdoc/>
public override int GetHashCode()
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Api/Context/Propagation/Propagators.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

namespace OpenTelemetry.Context.Propagation;

/// <summary>
Expand Down
Loading

0 comments on commit ba8a0e4

Please sign in to comment.