Adding method to EvictingQueue, updating SpanSdk to update if key exists (#557)
* Adding method to EvictingQueue, updating SpanSdk to update if key exists * Adding Replacing test to EvictingQueue; Updating SpanTest; Renaming from Update to Replace * Adding wrapper to AddOrUpdateAttribute, adding validation to index in Replace method * Adding NoReplacing Test when element doesn't exist Co-authored-by: Sergey Kanzhelev <S.Kanzhelev@live.com>
This commit is contained in:
parent
a6d5c511dc
commit
79828a209e
|
|
@ -59,6 +59,21 @@ namespace OpenTelemetry.Trace.Internal
|
|||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
internal void Replace(T item, T newItem)
|
||||
{
|
||||
Debug.Assert(item != null, "Item must not be null");
|
||||
Debug.Assert(newItem != null, "Item must not be null");
|
||||
|
||||
var index = Array.IndexOf(this.items, item);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.items[index] = newItem;
|
||||
}
|
||||
|
||||
internal void Add(T item)
|
||||
{
|
||||
Debug.Assert(item != null, "Item must not be null");
|
||||
|
|
|
|||
|
|
@ -333,7 +333,7 @@ namespace OpenTelemetry.Trace
|
|||
new EvictingQueue<KeyValuePair<string, object>>(this.tracerConfiguration.MaxNumberOfAttributes);
|
||||
}
|
||||
|
||||
this.attributes.Add(new KeyValuePair<string, object>(key ?? string.Empty, sanitizedValue));
|
||||
this.AddOrReplaceAttribute(key, sanitizedValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -359,7 +359,7 @@ namespace OpenTelemetry.Trace
|
|||
new EvictingQueue<KeyValuePair<string, object>>(this.tracerConfiguration.MaxNumberOfAttributes);
|
||||
}
|
||||
|
||||
this.attributes.Add(new KeyValuePair<string, object>(key ?? string.Empty, value));
|
||||
this.AddOrReplaceAttribute(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -385,7 +385,7 @@ namespace OpenTelemetry.Trace
|
|||
new EvictingQueue<KeyValuePair<string, object>>(this.tracerConfiguration.MaxNumberOfAttributes);
|
||||
}
|
||||
|
||||
this.attributes.Add(new KeyValuePair<string, object>(key ?? string.Empty, value));
|
||||
this.AddOrReplaceAttribute(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -411,7 +411,7 @@ namespace OpenTelemetry.Trace
|
|||
new EvictingQueue<KeyValuePair<string, object>>(this.tracerConfiguration.MaxNumberOfAttributes);
|
||||
}
|
||||
|
||||
this.attributes.Add(new KeyValuePair<string, object>(key ?? string.Empty, value));
|
||||
this.AddOrReplaceAttribute(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -878,6 +878,20 @@ namespace OpenTelemetry.Trace
|
|||
|| attributeValue is decimal;
|
||||
}
|
||||
|
||||
private void AddOrReplaceAttribute(string key, object value)
|
||||
{
|
||||
var attribute = this.attributes.FirstOrDefault(a => a.Key == (key ?? string.Empty));
|
||||
var newAttribute = new KeyValuePair<string, object>(key ?? string.Empty, value);
|
||||
if (attribute.Equals(default(KeyValuePair<string, object>)))
|
||||
{
|
||||
this.attributes.Add(newAttribute);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.attributes.Replace(attribute, newAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly struct ActivityAndTracestate
|
||||
{
|
||||
public readonly Activity Activity;
|
||||
|
|
|
|||
|
|
@ -86,5 +86,27 @@ namespace OpenTelemetry.Tests
|
|||
|
||||
Assert.Empty(eq);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Replacing()
|
||||
{
|
||||
var eq = new EvictingQueue<int>(1) { 0 };
|
||||
eq.Replace(0, 1);
|
||||
|
||||
var items = eq.ToArray();
|
||||
|
||||
Assert.Equal(1, items[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoReplacing()
|
||||
{
|
||||
var eq = new EvictingQueue<int>(1) { 0 };
|
||||
eq.Replace(1, 1);
|
||||
|
||||
var items = eq.ToArray();
|
||||
|
||||
Assert.Equal(0, items[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -978,10 +978,7 @@ namespace OpenTelemetry.Trace.Test
|
|||
span.SetAttribute(null, 0.1d);
|
||||
span.SetAttribute(null, false);
|
||||
|
||||
Assert.Equal(4, span.Attributes.Count());
|
||||
Assert.Single(span.Attributes.Where(kvp => kvp.Key == string.Empty && kvp.Value is long lv && lv == 1L));
|
||||
Assert.Single(span.Attributes.Where(kvp => kvp.Key == string.Empty && kvp.Value is double dv && dv == 0.1d));
|
||||
Assert.Single(span.Attributes.Where(kvp => kvp.Key == string.Empty && kvp.Value is string sv && sv == "foo"));
|
||||
Assert.Single(span.Attributes);
|
||||
Assert.Single(span.Attributes.Where(kvp => kvp.Key == string.Empty && kvp.Value is bool bv && bv == false));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue