diff --git a/src/OpenTelemetry.Api/Context/CorrelationContext.cs b/src/OpenTelemetry.Api/Context/CorrelationContext.cs new file mode 100644 index 00000000000..b35b0f1f9c8 --- /dev/null +++ b/src/OpenTelemetry.Api/Context/CorrelationContext.cs @@ -0,0 +1,76 @@ +// +// Copyright 2018, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace OpenTelemetry.Context +{ + /// + /// Correlation context. + /// + public readonly struct CorrelationContext : IEquatable + { + private static readonly List EmptyList = new List(); + private readonly List entries; + + /// + /// Initializes a new instance of the struct. + /// + /// Entries for correlation context. + internal CorrelationContext(List entries) + { + this.entries = entries; + } + + /// + /// Gets empty object of struct. + /// + public static CorrelationContext Empty { get; } = new CorrelationContext(EmptyList); + + /// + /// Gets all the in this . + /// + public IEnumerable Entries => this.entries; + + /// + /// Gets the with the specified name. + /// + /// Name of the to get. + /// The with the specified name. If not found - null. + public string GetEntryValue(string key) => this.entries.LastOrDefault(x => x.Key == key).Value; + + /// + public bool Equals(CorrelationContext other) + { + if (this.entries.Count() != other.entries.Count()) + { + return false; + } + + foreach (CorrelationContextEntry entry in this.entries) + { + if (other.GetEntryValue(entry.Key) != entry.Value) + { + return false; + } + } + + return true; + } + } +} diff --git a/src/OpenTelemetry.Api/DistributedContext/DistributedContextBuilder.cs b/src/OpenTelemetry.Api/Context/CorrelationContextBuilder.cs similarity index 64% rename from src/OpenTelemetry.Api/DistributedContext/DistributedContextBuilder.cs rename to src/OpenTelemetry.Api/Context/CorrelationContextBuilder.cs index b32679cf8cf..ec99eaac199 100644 --- a/src/OpenTelemetry.Api/DistributedContext/DistributedContextBuilder.cs +++ b/src/OpenTelemetry.Api/Context/CorrelationContextBuilder.cs @@ -1,4 +1,4 @@ -// +// // Copyright 2018, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,17 +19,17 @@ namespace OpenTelemetry.Context { /// - /// Distributed context Builder. + /// Correlation context Builder. /// - public struct DistributedContextBuilder + public struct CorrelationContextBuilder { - private List entries; + private List entries; /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the struct. /// /// Flag to allow inheriting the current context entries. - public DistributedContextBuilder(bool inheritCurrentContext) + public CorrelationContextBuilder(bool inheritCurrentContext) { this.entries = null; @@ -40,15 +40,15 @@ public DistributedContextBuilder(bool inheritCurrentContext) if (inheritCurrentContext) { - this.entries = new List(DistributedContext.Current.Entries); + this.entries = new List(DistributedContext.Current.CorrelationContext.Entries); } } /// - /// Initializes a new instance of the struct using some context. + /// Initializes a new instance of the struct using some context. /// /// Initial context. - public DistributedContextBuilder(DistributedContext context) + public CorrelationContextBuilder(CorrelationContext context) { if (DistributedContext.Carrier is NoopDistributedContextCarrier) { @@ -56,40 +56,40 @@ public DistributedContextBuilder(DistributedContext context) return; } - this.entries = new List(context.Entries); + this.entries = new List(context.Entries); } /// - /// Create instance from key and value entry. + /// Create instance from key and value entry. /// /// Entry key. /// Entry value. - /// Instance of . - public static DistributedContext CreateContext(string key, string value) => - new DistributedContextBuilder(inheritCurrentContext: false).Add(key, value).Build(); + /// Instance of . + public static CorrelationContext CreateContext(string key, string value) => + new CorrelationContextBuilder(inheritCurrentContext: false).Add(key, value).Build(); /// - /// Create instance from entry. + /// Create instance from entry. /// /// Entry to add to the context. - /// Instance of . - public static DistributedContext CreateContext(DistributedContextEntry entry) => - new DistributedContextBuilder(inheritCurrentContext: false).Add(entry).Build(); + /// Instance of . + public static CorrelationContext CreateContext(CorrelationContextEntry entry) => + new CorrelationContextBuilder(inheritCurrentContext: false).Add(entry).Build(); /// - /// Create instance from entry. + /// Create instance from entry. /// /// List of entries to add to the context. - /// Instance of . - public static DistributedContext CreateContext(IEnumerable entries) => - new DistributedContextBuilder(inheritCurrentContext: false).Add(entries).Build(); + /// Instance of . + public static CorrelationContext CreateContext(IEnumerable entries) => + new CorrelationContextBuilder(inheritCurrentContext: false).Add(entries).Build(); /// /// Add Distributed Context entry to the builder. /// /// Entry to add to the context. - /// The current instance. - public DistributedContextBuilder Add(DistributedContextEntry entry) + /// The current instance. + public CorrelationContextBuilder Add(CorrelationContextEntry entry) { if (DistributedContext.Carrier is NoopDistributedContextCarrier || entry == default) { @@ -98,7 +98,7 @@ public DistributedContextBuilder Add(DistributedContextEntry entry) if (this.entries == null) { - this.entries = new List(); + this.entries = new List(); } else { @@ -122,10 +122,10 @@ public DistributedContextBuilder Add(DistributedContextEntry entry) /// Entry key. /// Entry value. /// Entry metadata. - /// The current instance. - public DistributedContextBuilder Add(string key, string value, EntryMetadata metadata) + /// The current instance. + public CorrelationContextBuilder Add(string key, string value, EntryMetadata metadata) { - return this.Add(new DistributedContextEntry(key, value, metadata)); + return this.Add(new CorrelationContextEntry(key, value, metadata)); } /// @@ -133,18 +133,18 @@ public DistributedContextBuilder Add(string key, string value, EntryMetadata met /// /// Entry key. /// Entry value. - /// The current instance. - public DistributedContextBuilder Add(string key, string value) + /// The current instance. + public CorrelationContextBuilder Add(string key, string value) { - return this.Add(new DistributedContextEntry(key, value)); + return this.Add(new CorrelationContextEntry(key, value)); } /// /// Add Distributed Context entry to the builder. /// /// List of entries to add to the context. - /// The current instance. - public DistributedContextBuilder Add(IEnumerable entries) + /// The current instance. + public CorrelationContextBuilder Add(IEnumerable entries) { if (DistributedContext.Carrier is NoopDistributedContextCarrier || entries == null) { @@ -163,8 +163,8 @@ public DistributedContextBuilder Add(IEnumerable entrie /// Remove Distributed Context entry from the context. /// /// Entry key. - /// The current instance. - public DistributedContextBuilder Remove(string key) + /// The current instance. + public CorrelationContextBuilder Remove(string key) { if (key == null || DistributedContext.Carrier is NoopDistributedContextCarrier || this.entries == null) { @@ -181,17 +181,17 @@ public DistributedContextBuilder Remove(string key) } /// - /// Build a Distributed Context from current builder. + /// Build a Correlation Context from current builder. /// - /// instance. - public DistributedContext Build() + /// instance. + public CorrelationContext Build() { if (DistributedContext.Carrier is NoopDistributedContextCarrier || this.entries == null) { - return DistributedContext.Empty; + return CorrelationContext.Empty; } - var context = new DistributedContext(this.entries); + var context = new CorrelationContext(this.entries); this.entries = null; // empty current builder entries. return context; } diff --git a/src/OpenTelemetry.Api/DistributedContext/DistributedContextEntry.cs b/src/OpenTelemetry.Api/Context/CorrelationContextEntry.cs similarity index 73% rename from src/OpenTelemetry.Api/DistributedContext/DistributedContextEntry.cs rename to src/OpenTelemetry.Api/Context/CorrelationContextEntry.cs index 230877cd528..8a3af854542 100644 --- a/src/OpenTelemetry.Api/DistributedContext/DistributedContextEntry.cs +++ b/src/OpenTelemetry.Api/Context/CorrelationContextEntry.cs @@ -1,4 +1,4 @@ -// +// // Copyright 2018, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,30 +21,30 @@ namespace OpenTelemetry.Context /// /// Distributed Context entry with the key, value and metadata. /// - public readonly struct DistributedContextEntry + public readonly struct CorrelationContextEntry { /// - /// Initializes a new instance of the struct with the key and value. + /// Initializes a new instance of the struct with the key and value. /// /// Key name for the entry. /// Value associated with the key name. - public DistributedContextEntry(string key, string value) + public CorrelationContextEntry(string key, string value) : this(key, value, EntryMetadata.NoPropagationEntry) { } /// - /// Initializes a new instance of the struct with the key, value, and metadata. + /// Initializes a new instance of the struct with the key, value, and metadata. /// /// Key name for the entry. /// Value associated with the key name. /// Entry metadata. - public DistributedContextEntry(string key, string value, in EntryMetadata metadata) + public CorrelationContextEntry(string key, string value, in EntryMetadata metadata) { if (key == null) { this.Key = string.Empty; - OpenTelemetryApiEventSource.Log.InvalidArgument(nameof(DistributedContextEntry), nameof(key), "is null"); + OpenTelemetryApiEventSource.Log.InvalidArgument(nameof(CorrelationContextEntry), nameof(key), "is null"); } else { @@ -54,7 +54,7 @@ public DistributedContextEntry(string key, string value, in EntryMetadata metada if (value == null) { this.Value = string.Empty; - OpenTelemetryApiEventSource.Log.InvalidArgument(nameof(DistributedContextEntry), nameof(value), "is null"); + OpenTelemetryApiEventSource.Log.InvalidArgument(nameof(CorrelationContextEntry), nameof(value), "is null"); } else { @@ -80,29 +80,29 @@ public DistributedContextEntry(string key, string value, in EntryMetadata metada public EntryMetadata Metadata { get; } /// - /// Compare two entries of for equality. + /// Compare two entries of for equality. /// /// First Entry to compare. /// Second Entry to compare. - public static bool operator ==(DistributedContextEntry entry1, DistributedContextEntry entry2) => entry1.Equals(entry2); + public static bool operator ==(CorrelationContextEntry entry1, CorrelationContextEntry entry2) => entry1.Equals(entry2); /// - /// Compare two entries of for not equality. + /// Compare two entries of for not equality. /// /// First Entry to compare. /// Second Entry to compare. - public static bool operator !=(DistributedContextEntry entry1, DistributedContextEntry entry2) => !entry1.Equals(entry2); + public static bool operator !=(CorrelationContextEntry entry1, CorrelationContextEntry entry2) => !entry1.Equals(entry2); /// public override bool Equals(object o) { - return o is DistributedContextEntry that && (this.Key == that.Key && this.Value == that.Value); + return o is CorrelationContextEntry that && (this.Key == that.Key && this.Value == that.Value); } /// public override string ToString() { - return this.Key is null ? "{}" : $"{nameof(DistributedContextEntry)}{{{nameof(this.Key)}={this.Key}, {nameof(this.Value)}={this.Value}}}"; + return this.Key is null ? "{}" : $"{nameof(CorrelationContextEntry)}{{{nameof(this.Key)}={this.Key}, {nameof(this.Value)}={this.Value}}}"; } /// diff --git a/src/OpenTelemetry.Api/DistributedContext/DistributedContext.cs b/src/OpenTelemetry.Api/Context/DistributedContext.cs similarity index 66% rename from src/OpenTelemetry.Api/DistributedContext/DistributedContext.cs rename to src/OpenTelemetry.Api/Context/DistributedContext.cs index 29ef2d0e0e1..bdb50dfd8df 100644 --- a/src/OpenTelemetry.Api/DistributedContext/DistributedContext.cs +++ b/src/OpenTelemetry.Api/Context/DistributedContext.cs @@ -1,4 +1,4 @@ -// +// // Copyright 2018, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,7 +15,6 @@ // using System; -using System.Collections.Generic; using System.Linq; using OpenTelemetry.Internal; @@ -26,26 +25,25 @@ namespace OpenTelemetry.Context /// public readonly struct DistributedContext : IEquatable { - private static readonly List EmptyList = new List(); private static DistributedContextCarrier carrier = NoopDistributedContextCarrier.Instance; - private readonly List entries; + private readonly CorrelationContext correlationContext; /// /// Initializes a new instance of the struct. /// - /// Entries for distributed context. - internal DistributedContext(List entries) + /// The correlation context. + internal DistributedContext(CorrelationContext correlationContext) { - this.entries = entries; + this.correlationContext = correlationContext; } /// /// Gets empty object of struct. /// - public static DistributedContext Empty { get; } = new DistributedContext(EmptyList); + public static DistributedContext Empty { get; } = new DistributedContext(CorrelationContext.Empty); /// - /// Gets the current . + /// Gets the current . /// public static DistributedContext Current => carrier.Current; @@ -68,9 +66,9 @@ public static DistributedContextCarrier Carrier } /// - /// Gets all the in this . + /// Gets the for the current distributed context. /// - public IEnumerable Entries => this.entries; + public CorrelationContext CorrelationContext => this.correlationContext; /// /// Sets the current . @@ -79,17 +77,10 @@ public static DistributedContextCarrier Carrier /// Scope object. On disposal - original context will be restored. public static IDisposable SetCurrent(in DistributedContext context) => carrier.SetCurrent(context); - /// - /// Gets the with the specified name. - /// - /// Name of the to get. - /// The with the specified name. If not found - null. - public string GetEntryValue(string key) => this.entries.LastOrDefault(x => x.Key == key).Value; - /// public bool Equals(DistributedContext other) { - return this.entries.Count == other.entries.Count && this.entries.All(entry => other.GetEntryValue(entry.Key) == entry.Value); + return this.CorrelationContext.Equals(other.CorrelationContext); } } } diff --git a/src/OpenTelemetry.Api/Context/DistributedContextBuilder.cs b/src/OpenTelemetry.Api/Context/DistributedContextBuilder.cs new file mode 100644 index 00000000000..42bed6164d3 --- /dev/null +++ b/src/OpenTelemetry.Api/Context/DistributedContextBuilder.cs @@ -0,0 +1,90 @@ +// +// Copyright 2018, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; + +namespace OpenTelemetry.Context +{ + /// + /// Distributed context Builder. + /// + public struct DistributedContextBuilder + { + private CorrelationContextBuilder correlationContextBuilder; + + /// + /// Initializes a new instance of the struct. + /// + /// Flag to allow inheriting the current context entries. + public DistributedContextBuilder(bool inheritCurrentContext) + { + this.correlationContextBuilder = new CorrelationContextBuilder(false); + + if (DistributedContext.Carrier is NoopDistributedContextCarrier) + { + return; + } + + if (inheritCurrentContext) + { + this.correlationContextBuilder.Add(DistributedContext.Current.CorrelationContext.Entries); + } + } + + /// + /// Create context. + /// + /// The correlation key. + /// The correlation value. + /// A instance. + public static DistributedContext CreateContext(string key, string value) => + new DistributedContext(new CorrelationContextBuilder(inheritCurrentContext: false).Add(key, value).Build()); + + /// + /// Create context. + /// + /// A list of correlations to create the context with. + /// A instance. + public static DistributedContext CreateContext(IEnumerable entries) => + new DistributedContext(new CorrelationContextBuilder(inheritCurrentContext: false).Add(entries).Build()); + + /// + /// Configures correlations to be used with the context. + /// + /// An used to configure correlations. + /// The current instance. + public DistributedContextBuilder Correlations(Action configureCorrelations) + { + configureCorrelations?.Invoke(this.correlationContextBuilder); + return this; + } + + /// + /// Build a Distributed Context from current builder. + /// + /// instance. + public DistributedContext Build() + { + if (DistributedContext.Carrier is NoopDistributedContextCarrier) + { + return DistributedContext.Empty; + } + + return new DistributedContext(this.correlationContextBuilder.Build()); + } + } +} diff --git a/src/OpenTelemetry.Api/DistributedContext/DistributedContextCarrier.cs b/src/OpenTelemetry.Api/Context/DistributedContextCarrier.cs similarity index 95% rename from src/OpenTelemetry.Api/DistributedContext/DistributedContextCarrier.cs rename to src/OpenTelemetry.Api/Context/DistributedContextCarrier.cs index 1ad6255964a..9ef39b757c2 100644 --- a/src/OpenTelemetry.Api/DistributedContext/DistributedContextCarrier.cs +++ b/src/OpenTelemetry.Api/Context/DistributedContextCarrier.cs @@ -19,7 +19,7 @@ namespace OpenTelemetry.Context { /// - /// Abstraction to the carrier of the DistributedContext current object. + /// Abstraction to the carrier of the DistributedContext.Current object. /// public abstract class DistributedContextCarrier { diff --git a/src/OpenTelemetry.Api/DistributedContext/EntryMetadata.cs b/src/OpenTelemetry.Api/Context/EntryMetadata.cs similarity index 100% rename from src/OpenTelemetry.Api/DistributedContext/EntryMetadata.cs rename to src/OpenTelemetry.Api/Context/EntryMetadata.cs diff --git a/src/OpenTelemetry.Api/DistributedContext/NoopDistributedContextCarrier.cs b/src/OpenTelemetry.Api/Context/NoopDistributedContextCarrier.cs similarity index 100% rename from src/OpenTelemetry.Api/DistributedContext/NoopDistributedContextCarrier.cs rename to src/OpenTelemetry.Api/Context/NoopDistributedContextCarrier.cs diff --git a/src/OpenTelemetry.Api/DistributedContext/Propagation/EntryPropagationFilter.cs b/src/OpenTelemetry.Api/Context/Propagation/EntryPropagationFilter.cs similarity index 87% rename from src/OpenTelemetry.Api/DistributedContext/Propagation/EntryPropagationFilter.cs rename to src/OpenTelemetry.Api/Context/Propagation/EntryPropagationFilter.cs index 83b1ffcca5e..0492ee69cde 100644 --- a/src/OpenTelemetry.Api/DistributedContext/Propagation/EntryPropagationFilter.cs +++ b/src/OpenTelemetry.Api/Context/Propagation/EntryPropagationFilter.cs @@ -19,7 +19,7 @@ namespace OpenTelemetry.Context.Propagation { /// - /// Filter defining propagation rules for . + /// Filter defining propagation rules for . /// public readonly struct EntryPropagationFilter { @@ -29,7 +29,7 @@ public readonly struct EntryPropagationFilter /// Operator to apply. /// String to apply the operator with. /// Action to execute on entry. - public EntryPropagationFilter(FilterMatchOperator op, string matchString, Action action) + public EntryPropagationFilter(FilterMatchOperator op, string matchString, Action action) { this.Operator = op; this.MatchString = matchString; @@ -61,14 +61,14 @@ public enum FilterMatchOperator internal string MatchString { get; } - internal Action Action { get; } + internal Action Action { get; } /// - /// Check whether matches this filter pattern. + /// Check whether matches this filter pattern. /// /// Distributed Context entry to check. - /// True if matches this filter, false - otherwise. - public bool IsMatch(DistributedContextEntry entry) + /// True if matches this filter, false - otherwise. + public bool IsMatch(CorrelationContextEntry entry) { bool result = false; switch (this.Operator) diff --git a/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs b/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs index 76f29e5efdf..d5ac898372f 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs +++ b/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs @@ -139,7 +139,7 @@ public string GetBaggageItem(string key) throw new ArgumentNullException(nameof(key)); } - // TODO Revisit once DistributedContext is finalized + // TODO Revisit once CorrelationContext is finalized throw new NotImplementedException(); } diff --git a/src/OpenTelemetry/DistributedContext/AsyncLocalDistributedContextCarrier.cs b/src/OpenTelemetry/Context/AsyncLocalDistributedContextCarrier.cs similarity index 100% rename from src/OpenTelemetry/DistributedContext/AsyncLocalDistributedContextCarrier.cs rename to src/OpenTelemetry/Context/AsyncLocalDistributedContextCarrier.cs diff --git a/src/OpenTelemetry/DistributedContext/DistributedContextState.cs b/src/OpenTelemetry/Context/DistributedContextState.cs similarity index 100% rename from src/OpenTelemetry/DistributedContext/DistributedContextState.cs rename to src/OpenTelemetry/Context/DistributedContextState.cs diff --git a/src/OpenTelemetry/DistributedContext/NoopDistributedContextBinarySerializer.cs b/src/OpenTelemetry/Context/NoopDistributedContextBinarySerializer.cs similarity index 92% rename from src/OpenTelemetry/DistributedContext/NoopDistributedContextBinarySerializer.cs rename to src/OpenTelemetry/Context/NoopDistributedContextBinarySerializer.cs index ea838a91a52..87fe598c970 100644 --- a/src/OpenTelemetry/DistributedContext/NoopDistributedContextBinarySerializer.cs +++ b/src/OpenTelemetry/Context/NoopDistributedContextBinarySerializer.cs @@ -24,9 +24,9 @@ public class NoopDistributedContextBinarySerializer : DistributedContextBinarySe internal static readonly DistributedContextBinarySerializerBase Instance = new NoopDistributedContextBinarySerializer(); private static readonly byte[] EmptyByteArray = { }; - public override byte[] ToByteArray(DistributedContext tags) + public override byte[] ToByteArray(DistributedContext context) { - if (tags.Entries is null) + if (context.CorrelationContext.Entries is null) { OpenTelemetrySdkEventSource.Log.FailedToInjectContext("entries are null"); } diff --git a/src/OpenTelemetry/DistributedContext/Propagation/DistributedContextBinarySerializer.cs b/src/OpenTelemetry/Context/Propagation/DistributedContextBinarySerializer.cs similarity index 89% rename from src/OpenTelemetry/DistributedContext/Propagation/DistributedContextBinarySerializer.cs rename to src/OpenTelemetry/Context/Propagation/DistributedContextBinarySerializer.cs index 7a2c96b6ae3..d1686ab909a 100644 --- a/src/OpenTelemetry/DistributedContext/Propagation/DistributedContextBinarySerializer.cs +++ b/src/OpenTelemetry/Context/Propagation/DistributedContextBinarySerializer.cs @@ -24,9 +24,9 @@ internal DistributedContextBinarySerializer() { } - public override byte[] ToByteArray(DistributedContext tags) + public override byte[] ToByteArray(DistributedContext context) { - return SerializationUtils.SerializeBinary(tags); + return SerializationUtils.SerializeBinary(context); } public override DistributedContext FromByteArray(byte[] bytes) diff --git a/src/OpenTelemetry/DistributedContext/Propagation/DistributedContextBinarySerializerBase.cs b/src/OpenTelemetry/Context/Propagation/DistributedContextBinarySerializerBase.cs similarity index 100% rename from src/OpenTelemetry/DistributedContext/Propagation/DistributedContextBinarySerializerBase.cs rename to src/OpenTelemetry/Context/Propagation/DistributedContextBinarySerializerBase.cs diff --git a/src/OpenTelemetry/DistributedContext/Propagation/SerializationUtils.cs b/src/OpenTelemetry/Context/Propagation/SerializationUtils.cs similarity index 96% rename from src/OpenTelemetry/DistributedContext/Propagation/SerializationUtils.cs rename to src/OpenTelemetry/Context/Propagation/SerializationUtils.cs index 8eea59bdee8..814341b712d 100644 --- a/src/OpenTelemetry/DistributedContext/Propagation/SerializationUtils.cs +++ b/src/OpenTelemetry/Context/Propagation/SerializationUtils.cs @@ -41,7 +41,7 @@ internal static byte[] SerializeBinary(DistributedContext dc) byteArrayDataOutput.WriteByte(VersionId); var totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars. - foreach (var tag in dc.Entries) + foreach (var tag in dc.CorrelationContext.Entries) { totalChars += tag.Key.Length; totalChars += tag.Value.Length; @@ -97,9 +97,9 @@ internal static DistributedContext DeserializeBinary(byte[] bytes) return DistributedContext.Empty; } - internal static bool TryParseTags(MemoryStream buffer, out List tags) + internal static bool TryParseTags(MemoryStream buffer, out List tags) { - tags = new List(); + tags = new List(); var limit = buffer.Length; var totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars. while (buffer.Position < limit) @@ -111,7 +111,7 @@ internal static bool TryParseTags(MemoryStream buffer, out List +// // Copyright 2018, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,7 @@ namespace OpenTelemetry.Context.Test { - public class DistributedContextBuilderTest + public class CorrelationContextBuilderTest { private const string KEY_1 = "key 1"; private const string KEY_2 = "key 2"; @@ -28,16 +28,16 @@ public class DistributedContextBuilderTest private const string VALUE_1 = "value 1"; private const string VALUE_2 = "value 2"; - private static readonly List List1 = new List(1) - {new DistributedContextEntry(KEY_1, VALUE_1)}; + private static readonly List List1 = new List(1) + {new CorrelationContextEntry(KEY_1, VALUE_1)}; - private static readonly List List2 = new List(2) + private static readonly List List2 = new List(2) { - new DistributedContextEntry(KEY_1, VALUE_1), - new DistributedContextEntry(KEY_2, VALUE_2), + new CorrelationContextEntry(KEY_1, VALUE_1), + new CorrelationContextEntry(KEY_2, VALUE_2), }; - public DistributedContextBuilderTest() + public CorrelationContextBuilderTest() { DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; } @@ -45,57 +45,57 @@ public DistributedContextBuilderTest() [Fact] public void ContextCreation() { - DistributedContext dc = DistributedContextBuilder.CreateContext(null); - Assert.Equal(DistributedContext.Empty, dc); + CorrelationContext dc = CorrelationContextBuilder.CreateContext(null); + Assert.Equal(CorrelationContext.Empty, dc); - dc = DistributedContextBuilder.CreateContext(DistributedContext.Empty.Entries); - Assert.Equal(DistributedContext.Empty, dc); + dc = CorrelationContextBuilder.CreateContext(CorrelationContext.Empty.Entries); + Assert.Equal(CorrelationContext.Empty, dc); - dc = DistributedContextBuilder.CreateContext(KEY_1, VALUE_1); - Assert.Equal(DistributedContextBuilder.CreateContext(List1), dc); + dc = CorrelationContextBuilder.CreateContext(KEY_1, VALUE_1); + Assert.Equal(CorrelationContextBuilder.CreateContext(List1), dc); - Assert.Equal(dc, new DistributedContextBuilder(dc).Build()); + Assert.Equal(dc, new CorrelationContextBuilder(dc).Build()); } [Fact] public void AddEntries() { - Assert.Equal(DistributedContext.Empty, new DistributedContextBuilder(inheritCurrentContext: false).Build()); + Assert.Equal(CorrelationContext.Empty, new CorrelationContextBuilder(inheritCurrentContext: false).Build()); Assert.Equal( - DistributedContextBuilder.CreateContext(List1), new DistributedContextBuilder(inheritCurrentContext: false) + CorrelationContextBuilder.CreateContext(List1), new CorrelationContextBuilder(inheritCurrentContext: false) .Add(KEY_1, VALUE_1) .Build() ); Assert.Equal( - DistributedContextBuilder.CreateContext(List1), new DistributedContextBuilder(inheritCurrentContext: false) - .Add(new DistributedContextEntry(KEY_1, VALUE_1)) + CorrelationContextBuilder.CreateContext(List1), new CorrelationContextBuilder(inheritCurrentContext: false) + .Add(new CorrelationContextEntry(KEY_1, VALUE_1)) .Build() ); Assert.Equal( - DistributedContextBuilder.CreateContext(List2), new DistributedContextBuilder(inheritCurrentContext: false) + CorrelationContextBuilder.CreateContext(List2), new CorrelationContextBuilder(inheritCurrentContext: false) .Add(KEY_1, VALUE_1) .Add(KEY_2, VALUE_2) .Build() ); Assert.Equal( - DistributedContextBuilder.CreateContext(List2), new DistributedContextBuilder(inheritCurrentContext: false) - .Add(new DistributedContextEntry(KEY_1, VALUE_1)) - .Add(new DistributedContextEntry(KEY_2, VALUE_2)) + CorrelationContextBuilder.CreateContext(List2), new CorrelationContextBuilder(inheritCurrentContext: false) + .Add(new CorrelationContextEntry(KEY_1, VALUE_1)) + .Add(new CorrelationContextEntry(KEY_2, VALUE_2)) .Build() ); Assert.Equal( - DistributedContextBuilder.CreateContext(List1), new DistributedContextBuilder(inheritCurrentContext: false) + CorrelationContextBuilder.CreateContext(List1), new CorrelationContextBuilder(inheritCurrentContext: false) .Add(List1) .Build() ); Assert.Equal( - DistributedContextBuilder.CreateContext(List2), new DistributedContextBuilder(inheritCurrentContext: false) + CorrelationContextBuilder.CreateContext(List2), new CorrelationContextBuilder(inheritCurrentContext: false) .Add(List2) .Build() ); @@ -105,14 +105,14 @@ public void AddEntries() public void RemoveEntries() { Assert.Equal( - DistributedContextBuilder.CreateContext(List1), new DistributedContextBuilder(inheritCurrentContext: false) + CorrelationContextBuilder.CreateContext(List1), new CorrelationContextBuilder(inheritCurrentContext: false) .Add(List2) .Remove(KEY_2) .Build() ); Assert.Equal( - DistributedContext.Empty, new DistributedContextBuilder(inheritCurrentContext: false) + CorrelationContext.Empty, new CorrelationContextBuilder(inheritCurrentContext: false) .Add(List2) .Remove(KEY_2) .Remove(KEY_1) @@ -123,19 +123,19 @@ public void RemoveEntries() [Fact] public void EnsureEmptyListAfterBuild() { - var dcb = new DistributedContextBuilder(inheritCurrentContext: false); - Assert.Equal(DistributedContext.Empty, dcb.Build()); + var dcb = new CorrelationContextBuilder(inheritCurrentContext: false); + Assert.Equal(CorrelationContext.Empty, dcb.Build()); dcb.Add(List2); - Assert.Equal(DistributedContextBuilder.CreateContext(List2), dcb.Build()); - Assert.Equal(DistributedContext.Empty, dcb.Build()); + Assert.Equal(CorrelationContextBuilder.CreateContext(List2), dcb.Build()); + Assert.Equal(CorrelationContext.Empty, dcb.Build()); var dc = dcb.Add(List1).Build(); Assert.Equal(dc, dcb.Add(List1).Build()); - dcb = new DistributedContextBuilder(dc); + dcb = new CorrelationContextBuilder(dc); Assert.Equal(dc, dcb.Build()); - Assert.Equal(DistributedContext.Empty, dcb.Build()); + Assert.Equal(CorrelationContext.Empty, dcb.Build()); } } } diff --git a/test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedEntryTest.cs b/test/OpenTelemetry.Tests/Impl/Context/CorrelationContextEntryTest.cs similarity index 71% rename from test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedEntryTest.cs rename to test/OpenTelemetry.Tests/Impl/Context/CorrelationContextEntryTest.cs index 0dd2ade61d3..74112bbdf1e 100644 --- a/test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedEntryTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Context/CorrelationContextEntryTest.cs @@ -1,4 +1,4 @@ -// +// // Copyright 2018, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,21 +19,21 @@ namespace OpenTelemetry.Context.Test { - public class DistributedEntryTest + public class CorrelationContextEntryTest { [Fact] public void TestGetKey() { - Assert.Equal("k", new DistributedContextEntry("k", "v").Key); + Assert.Equal("k", new CorrelationContextEntry("k", "v").Key); } [Fact] public void TestTagEquals() { - var tag1 = new DistributedContextEntry("Key", "foo"); - var tag2 = new DistributedContextEntry("Key", "foo"); - var tag3 = new DistributedContextEntry("Key", "bar"); - var tag4 = new DistributedContextEntry("Key2", "foo"); + var tag1 = new CorrelationContextEntry("Key", "foo"); + var tag2 = new CorrelationContextEntry("Key", "foo"); + var tag3 = new CorrelationContextEntry("Key", "bar"); + var tag4 = new CorrelationContextEntry("Key2", "foo"); Assert.Equal(tag1, tag2); Assert.NotEqual(tag1, tag3); Assert.NotEqual(tag1, tag4); @@ -45,7 +45,7 @@ public void TestTagEquals() [Fact] public void TestNullKeyNullValue() { - var entry = new DistributedContextEntry(null, null); + var entry = new CorrelationContextEntry(null, null); Assert.Empty(entry.Key); Assert.Empty(entry.Value); } @@ -53,7 +53,7 @@ public void TestNullKeyNullValue() [Fact] public void TestNullKey() { - var entry = new DistributedContextEntry(null, "foo"); + var entry = new CorrelationContextEntry(null, "foo"); Assert.Empty(entry.Key); Assert.Equal("foo", entry.Value); } @@ -61,7 +61,7 @@ public void TestNullKey() [Fact] public void TestNullValue() { - var entry = new DistributedContextEntry("foo", null); + var entry = new CorrelationContextEntry("foo", null); Assert.Equal("foo", entry.Key); Assert.Empty(entry.Value); } diff --git a/test/OpenTelemetry.Tests/Impl/Context/CorrelationContextTest.cs b/test/OpenTelemetry.Tests/Impl/Context/CorrelationContextTest.cs new file mode 100644 index 00000000000..f2757baac6e --- /dev/null +++ b/test/OpenTelemetry.Tests/Impl/Context/CorrelationContextTest.cs @@ -0,0 +1,129 @@ +// +// Copyright 2018, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +using System; +using System.Collections.Generic; +using Xunit; + +namespace OpenTelemetry.Context.Test +{ + public class CorrelationContextTest + { + private const string K1 = "k1"; + private const string K2 = "k2"; + + private const string V1 = "v1"; + private const string V2 = "v2"; + + public CorrelationContextTest() + { + DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; + } + + [Fact] + public void EmptyContext() + { + var dc = CorrelationContextBuilder.CreateContext(new List()); + Assert.Empty(dc.Entries); + Assert.Equal(CorrelationContext.Empty, dc); + } + + [Fact] + public void NonEmptyContext() + { + var list = new List(2) { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K2, V2) }; + var dc = CorrelationContextBuilder.CreateContext(list); + Assert.Equal(list, dc.Entries); + } + + [Fact] + public void AddExtraKey() + { + var list = new List(1) { new CorrelationContextEntry(K1, V1)}; + var dc = CorrelationContextBuilder.CreateContext(list); + Assert.Equal(list, dc.Entries); + + list.Add(new CorrelationContextEntry(K2, V2)); + var dc1 = CorrelationContextBuilder.CreateContext(list); + Assert.Equal(list, dc1.Entries); + } + + [Fact] + public void AddExistingKey() + { + var list = new List(2) { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K1, V2) }; + var dc = CorrelationContextBuilder.CreateContext(list); + Assert.Equal(new List(1) { new CorrelationContextEntry(K1, V2) }, dc.Entries); + } + + [Fact] + public void UseDefaultEntry() + { + Assert.Equal(CorrelationContext.Empty, CorrelationContextBuilder.CreateContext(new List(1) { default })); + Assert.Equal(CorrelationContext.Empty, CorrelationContextBuilder.CreateContext(null)); + } + + [Fact] + public void RemoveExistingKey() + { + var list = new List(2) { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K2, V2) }; + var dc = CorrelationContextBuilder.CreateContext(list); + Assert.Equal(list, dc.Entries); + + list.RemoveAt(0); + + dc = CorrelationContextBuilder.CreateContext(list); + Assert.Equal(list, dc.Entries); + + list.Clear(); + dc = CorrelationContextBuilder.CreateContext(list); + Assert.Equal(CorrelationContext.Empty, dc); + } + + [Fact] + public void TestIterator() + { + var list = new List(2) { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K2, V2) }; + var dc = CorrelationContextBuilder.CreateContext(list); + + var i = dc.Entries.GetEnumerator(); + Assert.True(i.MoveNext()); + var tag1 = i.Current; + Assert.True(i.MoveNext()); + var tag2 = i.Current; + Assert.False(i.MoveNext()); + Assert.Equal(new List { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K2, V2)}, new List { tag1, tag2 }); + } + + [Fact] + public void TestEquals() + { + var dc1 = CorrelationContextBuilder.CreateContext(new List(2) { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K2, V2) }); + var dc2 = CorrelationContextBuilder.CreateContext(new List(2) { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K2, V2) }); + var dc3 = CorrelationContextBuilder.CreateContext(new List(2) { new CorrelationContextEntry(K2, V2), new CorrelationContextEntry(K1, V1) }); + var dc4 = CorrelationContextBuilder.CreateContext(new List(2) { new CorrelationContextEntry(K1, V1), new CorrelationContextEntry(K2, V1) }); + var dc5 = CorrelationContextBuilder.CreateContext(new List(2) { new CorrelationContextEntry(K1, V2), new CorrelationContextEntry(K2, V1) }); + + Assert.True(dc1.Equals(dc2)); + Assert.True(dc1.Equals(dc3)); + + Assert.False(dc1.Equals(dc4)); + Assert.False(dc2.Equals(dc4)); + Assert.False(dc3.Equals(dc4)); + Assert.False(dc5.Equals(dc4)); + Assert.False(dc4.Equals(dc5)); + } + } +} diff --git a/test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedContextsScopeTest.cs b/test/OpenTelemetry.Tests/Impl/Context/DistributedContextsScopeTest.cs similarity index 83% rename from test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedContextsScopeTest.cs rename to test/OpenTelemetry.Tests/Impl/Context/DistributedContextsScopeTest.cs index 2c7ba6fa458..9509a1986e8 100644 --- a/test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedContextsScopeTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Context/DistributedContextsScopeTest.cs @@ -31,9 +31,9 @@ public class DistributedContextsScopeTest public void NoopContextCarrier() { DistributedContext.Carrier = NoopDistributedContextCarrier.Instance; - List list = new List(2) + List list = new List(2) { - new DistributedContextEntry(KEY_1, VALUE_1), new DistributedContextEntry(KEY_2, VALUE_2), + new CorrelationContextEntry(KEY_1, VALUE_1), new CorrelationContextEntry(KEY_2, VALUE_2), }; Assert.Equal(DistributedContext.Empty, DistributedContext.Current); @@ -53,10 +53,10 @@ public void NoopContextCarrier() public async void AsyncContextCarrier() { DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; - List list = new List(2) { new DistributedContextEntry(KEY_1, VALUE_1), new DistributedContextEntry(KEY_2, VALUE_2), }; + List list = new List(2) { new CorrelationContextEntry(KEY_1, VALUE_1), new CorrelationContextEntry(KEY_2, VALUE_2), }; - DistributedContext dc1 = DistributedContextBuilder.CreateContext(KEY_1, VALUE_1); - DistributedContext dc2 = DistributedContextBuilder.CreateContext(list); + var dc1 = DistributedContextBuilder.CreateContext(KEY_1, VALUE_1); + var dc2 = DistributedContextBuilder.CreateContext(list); DistributedContext.SetCurrent(DistributedContext.Empty); Assert.Equal(DistributedContext.Empty, DistributedContext.Current); @@ -85,8 +85,8 @@ public async void AsyncContextCarrier() public async void TestContextInheritance() { DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; - var list1 = new List(1) { new DistributedContextEntry(KEY_1, VALUE_1)}; - var list2 = new List(2) { new DistributedContextEntry(KEY_1, VALUE_1), new DistributedContextEntry(KEY_2, VALUE_2), }; + var list1 = new List(1) {new CorrelationContextEntry(KEY_1, VALUE_1)}; + var list2 = new List(2) {new CorrelationContextEntry(KEY_1, VALUE_1), new CorrelationContextEntry(KEY_2, VALUE_2)}; DistributedContext.SetCurrent(DistributedContext.Empty); await Task.Run(() => Assert.Equal(DistributedContext.Empty, DistributedContext.Current)); @@ -99,10 +99,10 @@ public async void TestContextInheritance() { await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list1), DistributedContext.Current)); - using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: true).Add(KEY_2, VALUE_2).Build())) + using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: true).Correlations(b => b.Add(KEY_2, VALUE_2)).Build())) { await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list2), DistributedContext.Current)); - using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: true).Remove(KEY_2).Build())) + using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: true).Correlations(b => b.Remove(KEY_2)).Build())) { await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list1), DistributedContext.Current)); } diff --git a/test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextDeserializationTest.cs b/test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextDeserializationTest.cs similarity index 91% rename from test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextDeserializationTest.cs rename to test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextDeserializationTest.cs index fc73016aa9d..f3f5bcffa8a 100644 --- a/test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextDeserializationTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextDeserializationTest.cs @@ -46,10 +46,10 @@ public void TestConstants() public void TestNoTagsSerialization() { var dc = serializer.FromByteArray(serializer.ToByteArray(DistributedContext.Empty)); - Assert.Empty(dc.Entries); + Assert.Empty(dc.CorrelationContext.Entries); dc = serializer.FromByteArray(new byte[] {SerializationUtils.VersionId}); // One byte that represents Version ID. - Assert.Empty(dc.Entries); + Assert.Empty(dc.CorrelationContext.Entries); } [Fact] @@ -121,7 +121,7 @@ public void TestDeserializeMultipleTags() EncodeTagToOutPut("Key2", "Value2", output); var expected = DistributedContextBuilder.CreateContext( - new List(2) {new DistributedContextEntry("Key1", "Value1"), new DistributedContextEntry("Key2", "Value2")} + new List(2) {new CorrelationContextEntry("Key1", "Value1"), new CorrelationContextEntry("Key2", "Value2")} ); Assert.Equal(expected, serializer.FromByteArray(output.ToArray())); } @@ -150,11 +150,11 @@ public void TestDeserializeNonConsecutiveDuplicateKeys() EncodeTagToOutPut("Key2", "Value5", output); var expected = DistributedContextBuilder.CreateContext( - new List(3) + new List(3) { - new DistributedContextEntry("Key1", "Value4"), - new DistributedContextEntry("Key2", "Value5"), - new DistributedContextEntry("Key3", "Value3"), + new CorrelationContextEntry("Key1", "Value4"), + new CorrelationContextEntry("Key2", "Value5"), + new CorrelationContextEntry("Key3", "Value3"), } ); Assert.Equal(expected, serializer.FromByteArray(output.ToArray())); @@ -184,11 +184,11 @@ public void TestDeserializeNonConsecutiveDuplicateTags() EncodeTagToOutPut("Key2", "Value2", output); var expected = DistributedContextBuilder.CreateContext( - new List(3) + new List(3) { - new DistributedContextEntry("Key1", "Value1"), - new DistributedContextEntry("Key2", "Value2"), - new DistributedContextEntry("Key3", "Value3"), + new CorrelationContextEntry("Key1", "Value1"), + new CorrelationContextEntry("Key2", "Value2"), + new CorrelationContextEntry("Key3", "Value3"), } ); Assert.Equal(expected, serializer.FromByteArray(output.ToArray())); @@ -209,10 +209,10 @@ public void StopParsingAtUnknownField() EncodeTagToOutPut("Key3", "Value3", output); var expected = DistributedContextBuilder.CreateContext( - new List(2) + new List(2) { - new DistributedContextEntry("Key1", "Value1"), - new DistributedContextEntry("Key2", "Value2"), + new CorrelationContextEntry("Key1", "Value1"), + new CorrelationContextEntry("Key2", "Value2"), } ); Assert.Equal(expected, serializer.FromByteArray(output.ToArray())); diff --git a/test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextRoundtripTest.cs b/test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextRoundtripTest.cs similarity index 86% rename from test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextRoundtripTest.cs rename to test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextRoundtripTest.cs index c5f97468179..bce0297fd95 100644 --- a/test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextRoundtripTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextRoundtripTest.cs @@ -45,29 +45,30 @@ public void TestRoundtripSerialization_NormalTagContext() this.TestRoundtripSerialization(DistributedContextBuilder.CreateContext(K1, V1)); var expected = DistributedContextBuilder.CreateContext( - new List(3) + new List(3) { - new DistributedContextEntry(K1, V1), - new DistributedContextEntry(K2, V2), - new DistributedContextEntry(K3, V3), + new CorrelationContextEntry(K1, V1), + new CorrelationContextEntry(K2, V2), + new CorrelationContextEntry(K3, V3), } ); - this.TestRoundtripSerialization(expected); + this.TestRoundtripSerialization(expected); this.TestRoundtripSerialization(DistributedContextBuilder.CreateContext(K1, V_EMPTY)); } [Fact] public void TestRoundtrip_TagContextWithMaximumSize() { - var list = new List(); + var list = new List(); for (var i = 0; i < SerializationUtils.TagContextSerializedSizeLimit / 8; i++) { // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8. // Add 1024 tags, the total size should just be 8192. + var str = i.ToString("0000"); - list.Add(new DistributedContextEntry(str, str)); + list.Add(new CorrelationContextEntry(str, str)); } this.TestRoundtripSerialization(DistributedContextBuilder.CreateContext(list)); @@ -75,7 +76,7 @@ public void TestRoundtrip_TagContextWithMaximumSize() private void TestRoundtripSerialization(DistributedContext expected) { - var bytes= this.serializer.ToByteArray(expected); + var bytes = this.serializer.ToByteArray(expected); var actual = this.serializer.FromByteArray(bytes); Assert.Equal(expected, actual); } diff --git a/test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextSerializationTest.cs b/test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextSerializationTest.cs similarity index 82% rename from test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextSerializationTest.cs rename to test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextSerializationTest.cs index b5e2158951e..edad08b109b 100644 --- a/test/OpenTelemetry.Tests/Impl/DistributedContext/Propagation/DistributedContextSerializationTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Context/Propagation/DistributedContextSerializationTest.cs @@ -37,10 +37,10 @@ public class DistributedContextSerializationTest private const string V3 = "v3"; private const string V4 = "v4"; - private static readonly DistributedContextEntry T1 = new DistributedContextEntry(K1, V1); - private static readonly DistributedContextEntry T2 = new DistributedContextEntry(K2, V2); - private static readonly DistributedContextEntry T3 = new DistributedContextEntry(K3, V3); - private static readonly DistributedContextEntry T4 = new DistributedContextEntry(K4, V4); + private static readonly CorrelationContextEntry T1 = new CorrelationContextEntry(K1, V1); + private static readonly CorrelationContextEntry T2 = new CorrelationContextEntry(K2, V2); + private static readonly CorrelationContextEntry T3 = new CorrelationContextEntry(K3, V3); + private static readonly CorrelationContextEntry T4 = new CorrelationContextEntry(K4, V4); private readonly DistributedContextBinarySerializer serializer; @@ -71,26 +71,26 @@ public void TestSerializeWithMultipleTags() [Fact] public void TestSerializeTooLargeTagContext() { - var list = new List(); + var list = new List(); for (var i = 0; i < SerializationUtils.TagContextSerializedSizeLimit / 8 - 1; i++) { // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8. var str = i.ToString("0000"); - list.Add(new DistributedContextEntry(str, str)); + list.Add(new CorrelationContextEntry(str, str)); } // The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte // more than limit. - list.Add(new DistributedContextEntry("last", "last1")); + list.Add(new CorrelationContextEntry("last", "last1")); var dc = DistributedContextBuilder.CreateContext(list); Assert.Empty(serializer.ToByteArray(dc)); } - private void TestSerialize(params DistributedContextEntry[] tags) + private void TestSerialize(params CorrelationContextEntry[] tags) { - var list = new List(tags); + var list = new List(tags); var actual = serializer.ToByteArray(DistributedContextBuilder.CreateContext(list)); var tagsList = tags.ToList(); @@ -99,7 +99,7 @@ private void TestSerialize(params DistributedContextEntry[] tags) foreach (var distributedContextEntries in tagPermutation) { - var l = (List) distributedContextEntries; + var l = (List) distributedContextEntries; var expected = new MemoryStream(); expected.WriteByte(SerializationUtils.VersionId); @@ -125,14 +125,14 @@ private static void EncodeString(string input, MemoryStream byteArrayOutPutStrea byteArrayOutPutStream.Write(inpBytes, 0, inpBytes.Length); } - private static void RotateRight(IList sequence, int count) + private static void RotateRight(IList sequence, int count) { var tmp = sequence[count - 1]; sequence.RemoveAt(count - 1); sequence.Insert(0, tmp); } - private static IEnumerable> Permutate(IList sequence, int count) + private static IEnumerable> Permutate(IList sequence, int count) { if (count == 0) { diff --git a/test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedContextTest.cs b/test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedContextTest.cs deleted file mode 100644 index 80f677c90de..00000000000 --- a/test/OpenTelemetry.Tests/Impl/DistributedContext/DistributedContextTest.cs +++ /dev/null @@ -1,129 +0,0 @@ -// -// Copyright 2018, OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -using System; -using System.Collections.Generic; -using Xunit; - -namespace OpenTelemetry.Context.Test -{ - public class DistributedContextTest - { - private const string K1 = "k1"; - private const string K2 = "k2"; - - private const string V1 = "v1"; - private const string V2 = "v2"; - - public DistributedContextTest() - { - DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; - } - - [Fact] - public void EmptyContext() - { - var dc = DistributedContextBuilder.CreateContext(new List()); - Assert.Empty(dc.Entries); - Assert.Equal(DistributedContext.Empty, dc); - } - - [Fact] - public void NonEmptyContext() - { - var list = new List(2) { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V2) }; - var dc = DistributedContextBuilder.CreateContext(list); - Assert.Equal(list, dc.Entries); - } - - [Fact] - public void AddExtraKey() - { - var list = new List(1) { new DistributedContextEntry(K1, V1)}; - var dc = DistributedContextBuilder.CreateContext(list); - Assert.Equal(list, dc.Entries); - - list.Add(new DistributedContextEntry(K2, V2)); - var dc1 = DistributedContextBuilder.CreateContext(list); - Assert.Equal(list, dc1.Entries); - } - - [Fact] - public void AddExistingKey() - { - var list = new List(2) { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K1, V2) }; - var dc = DistributedContextBuilder.CreateContext(list); - Assert.Equal(new List(1) { new DistributedContextEntry(K1, V2) }, dc.Entries); - } - - [Fact] - public void UseDefaultEntry() - { - Assert.Equal(DistributedContext.Empty, DistributedContextBuilder.CreateContext(new List(1) { default })); - Assert.Equal(DistributedContext.Empty, DistributedContextBuilder.CreateContext(null)); - } - - [Fact] - public void RemoveExistingKey() - { - var list = new List(2) { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V2) }; - var dc = DistributedContextBuilder.CreateContext(list); - Assert.Equal(list, dc.Entries); - - list.RemoveAt(0); - - dc = DistributedContextBuilder.CreateContext(list); - Assert.Equal(list, dc.Entries); - - list.Clear(); - dc = DistributedContextBuilder.CreateContext(list); - Assert.Equal(DistributedContext.Empty, dc); - } - - [Fact] - public void TestIterator() - { - var list = new List(2) { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V2) }; - var dc = DistributedContextBuilder.CreateContext(list); - - var i = dc.Entries.GetEnumerator(); - Assert.True(i.MoveNext()); - var tag1 = i.Current; - Assert.True(i.MoveNext()); - var tag2 = i.Current; - Assert.False(i.MoveNext()); - Assert.Equal(new List { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V2)}, new List { tag1, tag2 }); - } - - [Fact] - public void TestEquals() - { - var dc1 = DistributedContextBuilder.CreateContext(new List(2) { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V2) }); - var dc2 = DistributedContextBuilder.CreateContext(new List(2) { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V2) }); - var dc3 = DistributedContextBuilder.CreateContext(new List(2) { new DistributedContextEntry(K2, V2), new DistributedContextEntry(K1, V1) }); - var dc4 = DistributedContextBuilder.CreateContext(new List(2) { new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V1) }); - var dc5 = DistributedContextBuilder.CreateContext(new List(2) { new DistributedContextEntry(K1, V2), new DistributedContextEntry(K2, V1) }); - - Assert.True(dc1.Equals(dc2)); - Assert.True(dc1.Equals(dc3)); - - Assert.False(dc1.Equals(dc4)); - Assert.False(dc2.Equals(dc4)); - Assert.False(dc3.Equals(dc4)); - Assert.False(dc5.Equals(dc4)); - Assert.False(dc4.Equals(dc5)); - } - } -}