Skip to content

Commit

Permalink
add make value nullable in IDictionary<string, string?>
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspimentel committed Nov 11, 2024
1 parent a0667c9 commit ee6da69
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions tracer/src/Datadog.Trace.Manual/Baggage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ namespace Datadog.Trace;
public static class Baggage
{
// only used when IL-rewriting is not available
private static IDictionary<string, string>? _current;
private static IDictionary<string, string?>? _current;

/// <summary>
/// Gets or sets the baggage collection for the current execution context.
/// </summary>
[Instrumented]
public static IDictionary<string, string> Current
public static IDictionary<string, string?> Current
{
get
{
// auto-instrumentation will return Trace.Baggage.Current instead
return _current ??= new Dictionary<string, string>();
return _current ??= new Dictionary<string, string?>();
}

set
Expand Down
48 changes: 24 additions & 24 deletions tracer/src/Datadog.Trace/Baggage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace Datadog.Trace;
/// <summary>
/// Baggage is a collection of name-value pairs that are propagated to downstream services.
/// </summary>
internal sealed class Baggage : IDictionary<string, string>
internal sealed class Baggage : IDictionary<string, string?>
{
private static readonly AsyncLocal<Baggage> AsyncStorage = new();

private static readonly List<KeyValuePair<string, string>> EmptyList = [];
private static readonly List<KeyValuePair<string, string?>> EmptyList = [];

private List<KeyValuePair<string, string>>? _items;
private List<KeyValuePair<string, string?>>? _items;

/// <summary>
/// Initializes a new instance of the <see cref="Baggage"/> class.
Expand All @@ -36,7 +36,7 @@ public Baggage()
/// Initializes a new instance of the <see cref="Baggage"/> class using the specified items.
/// </summary>
/// <param name="items">The baggage items.</param>
public Baggage(IEnumerable<KeyValuePair<string, string>>? items)
public Baggage(IEnumerable<KeyValuePair<string, string?>>? items)
{
if (items != null!)
{
Expand All @@ -58,9 +58,9 @@ public static Baggage Current
/// </summary>
public int Count => _items?.Count ?? 0;

bool ICollection<KeyValuePair<string, string>>.IsReadOnly => false;
bool ICollection<KeyValuePair<string, string?>>.IsReadOnly => false;

ICollection<string> IDictionary<string, string>.Keys
ICollection<string> IDictionary<string, string?>.Keys
{
get
{
Expand All @@ -86,7 +86,7 @@ ICollection<string> IDictionary<string, string>.Keys
}
}

ICollection<string> IDictionary<string, string>.Values
ICollection<string?> IDictionary<string, string?>.Values
{
get
{
Expand All @@ -96,7 +96,7 @@ ICollection<string> IDictionary<string, string>.Values
{
if (items.Count > 0)
{
var values = new string[items.Count];
var values = new string?[items.Count];

for (int i = 0; i < items.Count; i++)
{
Expand All @@ -112,7 +112,7 @@ ICollection<string> IDictionary<string, string>.Values
}
}

public string this[string key]
public string? this[string key]
{
get
{
Expand All @@ -131,7 +131,7 @@ public string this[string key]
}
}

private List<KeyValuePair<string, string>> EnsureListInitialized()
private List<KeyValuePair<string, string?>> EnsureListInitialized()
{
if (_items == null)
{
Expand All @@ -146,7 +146,7 @@ private List<KeyValuePair<string, string>> EnsureListInitialized()
/// </summary>
/// <param name="key">The baggage item name.</param>
/// <param name="value">The baggage item value.</param>
public void AddOrReplace(string key, string value)
public void AddOrReplace(string key, string? value)
{
var items = EnsureListInitialized();

Expand All @@ -156,20 +156,20 @@ public void AddOrReplace(string key, string value)
}
}

private static void AddOrReplaceInternal(List<KeyValuePair<string, string>> items, string key, string value)
private static void AddOrReplaceInternal(List<KeyValuePair<string, string?>> items, string key, string? value)
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].Key == key)
{
// key found, replace with new value
items[i] = new KeyValuePair<string, string>(key, value);
items[i] = new KeyValuePair<string, string?>(key, value);
return;
}
}

// key not found, add new entry
items.Add(new KeyValuePair<string, string>(key, value));
items.Add(new KeyValuePair<string, string?>(key, value));
}

public bool TryGetValue(string key, out string value)
Expand All @@ -193,7 +193,7 @@ public bool TryGetValue(string key, out string value)
return false;
}

bool IDictionary<string, string>.ContainsKey(string key)
bool IDictionary<string, string?>.ContainsKey(string key)
{
if (_items is { } items)
{
Expand All @@ -212,7 +212,7 @@ bool IDictionary<string, string>.ContainsKey(string key)
return false;
}

bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item)
bool ICollection<KeyValuePair<string, string?>>.Contains(KeyValuePair<string, string?> item)
{
if (_items is { } items)
{
Expand All @@ -231,7 +231,7 @@ bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, str
return false;
}

public void Add(string key, string value)
public void Add(string key, string? value)
{
var items = EnsureListInitialized();

Expand All @@ -245,11 +245,11 @@ public void Add(string key, string value)
}
}

items.Add(new KeyValuePair<string, string>(key, value));
items.Add(new KeyValuePair<string, string?>(key, value));
}
}

void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item)
void ICollection<KeyValuePair<string, string?>>.Add(KeyValuePair<string, string?> item)
{
Add(item.Key, item.Value);
}
Expand Down Expand Up @@ -284,7 +284,7 @@ public bool Remove(string name)
return false;
}

bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item)
bool ICollection<KeyValuePair<string, string?>>.Remove(KeyValuePair<string, string?> item)
{
if (_items is { } items)
{
Expand All @@ -304,7 +304,7 @@ bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, strin
return false;
}

void ICollection<KeyValuePair<string, string>>.CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
void ICollection<KeyValuePair<string, string?>>.CopyTo(KeyValuePair<string, string?>[] array, int arrayIndex)
{
if (array == null!)
{
Expand Down Expand Up @@ -419,7 +419,7 @@ public void MergeInto(Baggage destination)
}

public void Enumerate<T>(T processor)
where T : struct, ICancellableObserver<KeyValuePair<string, string>>
where T : struct, ICancellableObserver<KeyValuePair<string, string?>>
{
if (_items is { } list)
{
Expand All @@ -440,9 +440,9 @@ public void Enumerate<T>(T processor)
}
}

private List<KeyValuePair<string, string>>.Enumerator GetEnumerator() => _items?.GetEnumerator() ?? EmptyList.GetEnumerator();
private List<KeyValuePair<string, string?>>.Enumerator GetEnumerator() => _items?.GetEnumerator() ?? EmptyList.GetEnumerator();

IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator() => GetEnumerator();
IEnumerator<KeyValuePair<string, string?>> IEnumerable<KeyValuePair<string, string?>>.GetEnumerator() => GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation.Ba
[EditorBrowsable(EditorBrowsableState.Never)]
public class Baggage_GetCurrent_Integration
{
internal static CallTargetReturn<IDictionary<string, string>?> OnMethodEnd<TTarget>(
internal static CallTargetReturn<IDictionary<string, string?>?> OnMethodEnd<TTarget>(
IDictionary<string, string>? returnValue,
Exception? exception,
in CallTargetState state)
{
return new CallTargetReturn<IDictionary<string, string>?>(Trace.Baggage.Current);
return new CallTargetReturn<IDictionary<string, string?>?>(Trace.Baggage.Current);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation.Ba
[EditorBrowsable(EditorBrowsableState.Never)]
public class Baggage_SetCurrent_Integration
{
internal static CallTargetState OnMethodBegin<TTarget>(ref IDictionary<string, string>? value)
internal static CallTargetState OnMethodBegin<TTarget>(ref IDictionary<string, string?>? value)
{
Trace.Baggage.Current = value switch
{
Expand Down
6 changes: 3 additions & 3 deletions tracer/src/Datadog.Trace/Propagators/W3CBaggagePropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ internal static string CreateHeader(Baggage baggage, int maxBaggageItems, int ma
return baggage;
}

private struct W3CBaggageHeaderBuilder : ICancellableObserver<KeyValuePair<string, string>>
private struct W3CBaggageHeaderBuilder : ICancellableObserver<KeyValuePair<string, string?>>
{
private readonly int _maxBaggageItems;
private readonly int _maxBaggageLength;
Expand All @@ -296,7 +296,7 @@ public W3CBaggageHeaderBuilder(int maxBaggageItems, int maxBaggageLength, String

public bool CancellationRequested { get; private set; }

public void OnNext(KeyValuePair<string, string> item)
public void OnNext(KeyValuePair<string, string?> item)
{
if (string.IsNullOrWhiteSpace(item.Key) || string.IsNullOrEmpty(item.Value))
{
Expand All @@ -316,7 +316,7 @@ public void OnNext(KeyValuePair<string, string> item)
}

var key = Encode(item.Key, KeyCharsToEncode);
var value = Encode(item.Value, ValueCharsToEncode);
var value = Encode(item.Value!, ValueCharsToEncode);

var keyValuePairString = _sb.Length > 0 ?
$"{PairSeparator}{key}{KeyAndValueSeparator}{value}" :
Expand Down

0 comments on commit ee6da69

Please sign in to comment.