Skip to content

Commit 98b3986

Browse files
Kielekalanwest
andauthored
[OpenTelemetry.Api] enable analysis (#6206)
Co-authored-by: Alan West <[email protected]>
1 parent bb8abee commit 98b3986

17 files changed

+116
-51
lines changed

src/OpenTelemetry.Api/Baggage.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace OpenTelemetry;
1616
public readonly struct Baggage : IEquatable<Baggage>
1717
{
1818
private static readonly RuntimeContextSlot<BaggageHolder> RuntimeContextSlot = RuntimeContext.RegisterSlot<BaggageHolder>("otel.baggage");
19-
private static readonly Dictionary<string, string> EmptyBaggage = new();
19+
private static readonly Dictionary<string, string> EmptyBaggage = [];
2020

2121
private readonly Dictionary<string, string> baggage;
2222

@@ -305,7 +305,9 @@ public Baggage RemoveBaggage(string name)
305305
/// Returns a new <see cref="Baggage"/> with all the key/value pairs removed.
306306
/// </summary>
307307
/// <returns>New <see cref="Baggage"/> with all the key/value pairs removed.</returns>
308+
#pragma warning disable CA1822 // Mark members as static
308309
public Baggage ClearBaggage()
310+
#pragma warning restore CA1822 // Mark members as static
309311
=> default;
310312

311313
/// <summary>
@@ -343,7 +345,11 @@ public override int GetHashCode()
343345
unchecked
344346
{
345347
hash = (hash * 23) + baggage.Comparer.GetHashCode(item.Key);
348+
#if NET
349+
hash = (hash * 23) + item.Value.GetHashCode(StringComparison.Ordinal);
350+
#else
346351
hash = (hash * 23) + item.Value.GetHashCode();
352+
#endif
347353
}
348354
}
349355

src/OpenTelemetry.Api/Context/Propagation/B3Propagator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public sealed class B3Propagator : TextMapPropagator
2727
internal const string UpperTraceId = "0000000000000000";
2828

2929
// Sampled values via the X_B3_SAMPLED header.
30+
internal const char SampledValueChar = '1';
3031
internal const string SampledValue = "1";
3132

3233
// Some old zipkin implementations may send true/false for the sampled header. Only use this for checking incoming values.
@@ -130,7 +131,7 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
130131
if ((context.ActivityContext.TraceFlags & ActivityTraceFlags.Recorded) != 0)
131132
{
132133
sb.Append(XB3CombinedDelimiter);
133-
sb.Append(SampledValue);
134+
sb.Append(SampledValueChar);
134135
}
135136

136137
setter(carrier, XB3Combined, sb.ToString());

src/OpenTelemetry.Api/Context/Propagation/BaggagePropagator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override PropagationContext Extract<T>(PropagationContext context, T carr
5252
var baggageCollection = getter(carrier, BaggageHeaderName);
5353
if (baggageCollection?.Any() ?? false)
5454
{
55-
if (TryExtractBaggage(baggageCollection.ToArray(), out var baggage))
55+
if (TryExtractBaggage([.. baggageCollection], out var baggage))
5656
{
5757
return new PropagationContext(context.ActivityContext, new Baggage(baggage!));
5858
}
@@ -138,7 +138,11 @@ internal static bool TryExtractBaggage(
138138
break;
139139
}
140140

141+
#if NET
142+
if (pair.IndexOf('=', StringComparison.Ordinal) < 0)
143+
#else
141144
if (pair.IndexOf('=') < 0)
145+
#endif
142146
{
143147
continue;
144148
}
@@ -157,10 +161,7 @@ internal static bool TryExtractBaggage(
157161
continue;
158162
}
159163

160-
if (baggageDictionary == null)
161-
{
162-
baggageDictionary = new Dictionary<string, string>();
163-
}
164+
baggageDictionary ??= [];
164165

165166
baggageDictionary[key] = value;
166167
}

src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace OpenTelemetry.Context.Propagation;
1111
/// </summary>
1212
public class CompositeTextMapPropagator : TextMapPropagator
1313
{
14-
private readonly IReadOnlyList<TextMapPropagator> propagators;
14+
private readonly List<TextMapPropagator> propagators;
1515
private readonly ISet<string> allFields;
1616

1717
/// <summary>

src/OpenTelemetry.Api/Context/Propagation/NoopTextMapPropagator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ namespace OpenTelemetry.Context.Propagation;
55

66
internal sealed class NoopTextMapPropagator : TextMapPropagator
77
{
8+
#pragma warning disable CA1805 // Do not initialize unnecessarily
89
private static readonly PropagationContext DefaultPropagationContext = default;
10+
#pragma warning restore CA1805 // Do not initialize unnecessarily
911

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

src/OpenTelemetry.Api/Context/Propagation/TraceContextPropagator.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public class TraceContextPropagator : TextMapPropagator
1616
private const string TraceParent = "traceparent";
1717
private const string TraceState = "tracestate";
1818

19+
// The following length limits are from Trace Context v1 https://www.w3.org/TR/trace-context-1/#key
20+
private const int TraceStateKeyMaxLength = 256;
21+
private const int TraceStateKeyTenantMaxLength = 241;
22+
private const int TraceStateKeyVendorMaxLength = 14;
23+
private const int TraceStateValueMaxLength = 256;
24+
1925
private static readonly int VersionPrefixIdLength = "00-".Length;
2026
private static readonly int TraceIdLength = "0af7651916cd43dd8448eb211c80319c".Length;
2127
private static readonly int VersionAndTraceIdLength = "00-0af7651916cd43dd8448eb211c80319c-".Length;
@@ -24,12 +30,6 @@ public class TraceContextPropagator : TextMapPropagator
2430
private static readonly int OptionsLength = "00".Length;
2531
private static readonly int TraceparentLengthV0 = "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-00".Length;
2632

27-
// The following length limits are from Trace Context v1 https://www.w3.org/TR/trace-context-1/#key
28-
private static readonly int TraceStateKeyMaxLength = 256;
29-
private static readonly int TraceStateKeyTenantMaxLength = 241;
30-
private static readonly int TraceStateKeyVendorMaxLength = 14;
31-
private static readonly int TraceStateValueMaxLength = 256;
32-
3333
/// <inheritdoc/>
3434
public override ISet<string> Fields => new HashSet<string> { TraceState, TraceParent };
3535

@@ -76,7 +76,7 @@ public override PropagationContext Extract<T>(PropagationContext context, T carr
7676
var tracestateCollection = getter(carrier, TraceState);
7777
if (tracestateCollection?.Any() ?? false)
7878
{
79-
TryExtractTracestate(tracestateCollection.ToArray(), out tracestate);
79+
TryExtractTracestate([.. tracestateCollection], out tracestate);
8080
}
8181

8282
return new PropagationContext(
@@ -297,7 +297,11 @@ internal static bool TryExtractTracestate(string[] tracestateCollection, out str
297297
result.Append(',');
298298
}
299299

300+
#if NET
301+
result.Append(listMember);
302+
#else
300303
result.Append(listMember.ToString());
304+
#endif
301305
}
302306
}
303307

src/OpenTelemetry.Api/Context/Propagation/TraceStateUtilsNew.cs renamed to src/OpenTelemetry.Api/Context/Propagation/TraceStateUtils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OpenTelemetry.Context.Propagation;
1010
/// <summary>
1111
/// Extension methods to extract TraceState from string.
1212
/// </summary>
13-
internal static class TraceStateUtilsNew
13+
internal static class TraceStateUtils
1414
{
1515
private const int KeyMaxSize = 256;
1616
private const int ValueMaxSize = 256;
@@ -98,6 +98,7 @@ internal static bool AppendTraceState(string traceStateString, List<KeyValuePair
9898

9999
internal static string GetString(IEnumerable<KeyValuePair<string, string>>? traceState)
100100
{
101+
#pragma warning disable CA1851 // Possible multiple enumerations of 'IEnumerable' collection
101102
if (traceState == null || !traceState.Any())
102103
{
103104
return string.Empty;
@@ -125,6 +126,7 @@ internal static string GetString(IEnumerable<KeyValuePair<string, string>>? trac
125126
.Append(',');
126127
}
127128
}
129+
#pragma warning restore CA1851 // Possible multiple enumerations of 'IEnumerable' collection
128130

129131
return sb.Remove(sb.Length - 1, 1).ToString();
130132
}
@@ -153,7 +155,7 @@ private static bool TryParseKeyValue(ReadOnlySpan<char> pair, out ReadOnlySpan<c
153155
return false;
154156
}
155157

156-
value = pair.Slice(valueStartIdx, pair.Length - valueStartIdx).Trim();
158+
value = pair.Slice(valueStartIdx).Trim();
157159
if (!ValidateValue(value))
158160
{
159161
OpenTelemetryApiEventSource.Log.TracestateValueIsInvalid(value);

src/OpenTelemetry.Api/Context/RuntimeContextSlot.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ protected RuntimeContextSlot(string name)
3131
/// Get the value from the context slot.
3232
/// </summary>
3333
/// <returns>The value retrieved from the context slot.</returns>
34+
#pragma warning disable CA1716 // Identifiers should not match keywords
3435
public abstract T? Get();
36+
#pragma warning restore CA1716 // Identifiers should not match keywords
3537

3638
/// <summary>
3739
/// Set the value to the context slot.
3840
/// </summary>
3941
/// <param name="value">The value to be set.</param>
42+
#pragma warning disable CA1716 // Identifiers should not match keywords
4043
public abstract void Set(T value);
44+
#pragma warning restore CA1716 // Identifiers should not match keywords
4145

4246
/// <inheritdoc/>
4347
public void Dispose()

src/OpenTelemetry.Api/Logs/LogRecordAttributeList.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ namespace OpenTelemetry.Logs;
2727
/// </summary>
2828
internal
2929
#endif
30+
#pragma warning disable CA1815 // Override equals and operator equals on value types
3031
struct LogRecordAttributeList : IReadOnlyList<KeyValuePair<string, object?>>
32+
#pragma warning restore CA1815 // Override equals and operator equals on value types
3133
{
3234
internal const int OverflowMaxCount = 8;
3335
internal const int OverflowAdditionalCapacity = 16;
3436
internal List<KeyValuePair<string, object?>>? OverflowAttributes;
35-
private static readonly IReadOnlyList<KeyValuePair<string, object?>> Empty = Array.Empty<KeyValuePair<string, object?>>();
37+
private static readonly IReadOnlyList<KeyValuePair<string, object?>> Empty = [];
3638
private KeyValuePair<string, object?> attribute1;
3739
private KeyValuePair<string, object?> attribute2;
3840
private KeyValuePair<string, object?> attribute3;
@@ -113,7 +115,9 @@ readonly get
113115
/// <param name="key">Attribute name.</param>
114116
/// <returns>Attribute value.</returns>
115117
[EditorBrowsable(EditorBrowsableState.Never)]
118+
#pragma warning disable CA1044 // Properties should not be write only
116119
public object? this[string key]
120+
#pragma warning restore CA1044 // Properties should not be write only
117121
{
118122
// Note: This only exists to enable collection initializer syntax
119123
// like { ["key"] = value }.
@@ -130,7 +134,7 @@ public static LogRecordAttributeList CreateFromEnumerable(IEnumerable<KeyValuePa
130134
Guard.ThrowIfNull(attributes);
131135

132136
LogRecordAttributeList logRecordAttributes = default;
133-
logRecordAttributes.OverflowAttributes = new(attributes);
137+
logRecordAttributes.OverflowAttributes = [.. attributes];
134138
logRecordAttributes.count = logRecordAttributes.OverflowAttributes.Count;
135139
return logRecordAttributes;
136140
}
@@ -210,8 +214,8 @@ public readonly Enumerator GetEnumerator()
210214

211215
internal readonly IReadOnlyList<KeyValuePair<string, object?>> Export(ref List<KeyValuePair<string, object?>>? attributeStorage)
212216
{
213-
int count = this.count;
214-
if (count <= 0)
217+
int readonlyCount = this.count;
218+
if (readonlyCount <= 0)
215219
{
216220
return Empty;
217221
}
@@ -223,49 +227,49 @@ public readonly Enumerator GetEnumerator()
223227
return overflowAttributes;
224228
}
225229

226-
Debug.Assert(count <= 8, "Invalid size detected.");
230+
Debug.Assert(readonlyCount <= 8, "Invalid size detected.");
227231

228232
attributeStorage ??= new List<KeyValuePair<string, object?>>(OverflowAdditionalCapacity);
229233

230234
// TODO: Perf test this, adjust as needed.
231235
attributeStorage.Add(this.attribute1);
232-
if (count == 1)
236+
if (readonlyCount == 1)
233237
{
234238
return attributeStorage;
235239
}
236240

237241
attributeStorage.Add(this.attribute2);
238-
if (count == 2)
242+
if (readonlyCount == 2)
239243
{
240244
return attributeStorage;
241245
}
242246

243247
attributeStorage.Add(this.attribute3);
244-
if (count == 3)
248+
if (readonlyCount == 3)
245249
{
246250
return attributeStorage;
247251
}
248252

249253
attributeStorage.Add(this.attribute4);
250-
if (count == 4)
254+
if (readonlyCount == 4)
251255
{
252256
return attributeStorage;
253257
}
254258

255259
attributeStorage.Add(this.attribute5);
256-
if (count == 5)
260+
if (readonlyCount == 5)
257261
{
258262
return attributeStorage;
259263
}
260264

261265
attributeStorage.Add(this.attribute6);
262-
if (count == 6)
266+
if (readonlyCount == 6)
263267
{
264268
return attributeStorage;
265269
}
266270

267271
attributeStorage.Add(this.attribute7);
268-
if (count == 7)
272+
if (readonlyCount == 7)
269273
{
270274
return attributeStorage;
271275
}

src/OpenTelemetry.Api/Logs/LogRecordData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ namespace OpenTelemetry.Logs;
2424
/// </summary>
2525
internal
2626
#endif
27+
#pragma warning disable CA1815 // Override equals and operator equals on value types
2728
struct LogRecordData
29+
#pragma warning restore CA1815 // Override equals and operator equals on value types
2830
{
2931
internal DateTime TimestampBacking = DateTime.UtcNow;
3032

0 commit comments

Comments
 (0)