Skip to content

Commit c977f73

Browse files
[Shims.OpenTracing] enable code analysis (#6160)
Co-authored-by: Rajkumar Rangaraj <[email protected]>
1 parent 8943733 commit c977f73

15 files changed

+61
-74
lines changed

src/OpenTelemetry.Shims.OpenTracing/OpenTelemetry.Shims.OpenTracing.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<Description>OpenTracing shim for OpenTelemetry .NET</Description>
55
<PackageTags>$(PackageTags);distributed-tracing;OpenTracing</PackageTags>
66
<MinVerTagPrefix>coreunstable-</MinVerTagPrefix>
7+
<AnalysisLevel>latest-all</AnalysisLevel>
78
</PropertyGroup>
89

910
<ItemGroup>

src/OpenTelemetry.Shims.OpenTracing/SpanBuilderShim.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal sealed class SpanBuilderShim : ISpanBuilder
2727
/// <summary>
2828
/// The OpenTelemetry links. These correspond loosely to OpenTracing references.
2929
/// </summary>
30-
private readonly List<Link> links = new();
30+
private readonly List<Link> links = [];
3131

3232
/// <summary>
3333
/// The OpenTelemetry attributes. These correspond to OpenTracing Tags.
@@ -65,7 +65,7 @@ public SpanBuilderShim(Tracer tracer, string spanName)
6565
this.ScopeManager = new ScopeManagerShim();
6666
}
6767

68-
private IScopeManager ScopeManager { get; }
68+
private ScopeManagerShim ScopeManager { get; }
6969

7070
private bool ParentSet => this.parentSpan != null || this.parentSpanContext.IsValid;
7171

@@ -148,10 +148,7 @@ public ISpan Start()
148148
span = this.tracer.StartSpan(this.spanName, this.spanKind, this.parentSpanContext, this.attributes, this.links, this.explicitStartTime ?? default);
149149
}
150150

151-
if (span == null)
152-
{
153-
span = this.tracer.StartSpan(this.spanName, this.spanKind, default(SpanContext), this.attributes, null, this.explicitStartTime ?? default);
154-
}
151+
span ??= this.tracer.StartSpan(this.spanName, this.spanKind, default(SpanContext), this.attributes, null, this.explicitStartTime ?? default);
155152

156153
if (this.error)
157154
{

src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ internal sealed class SpanShim : ISpan
1414
/// </summary>
1515
public const string DefaultEventName = "log";
1616

17-
private static readonly IReadOnlyCollection<Type> OpenTelemetrySupportedAttributeValueTypes = new List<Type>
18-
{
17+
private static readonly IReadOnlyCollection<Type> OpenTelemetrySupportedAttributeValueTypes =
18+
[
1919
typeof(string),
2020
typeof(bool),
2121
typeof(byte),
@@ -24,7 +24,7 @@ internal sealed class SpanShim : ISpan
2424
typeof(long),
2525
typeof(float),
2626
typeof(double),
27-
};
27+
];
2828

2929
private readonly SpanContextShim spanContextShim;
3030

src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,7 @@ public TracerShim(Trace.TracerProvider tracerProvider, TextMapPropagator? textFo
4848
/// <inheritdoc/>
4949
public global::OpenTracing.ISpan? ActiveSpan => this.ScopeManager.Active?.Span;
5050

51-
private TextMapPropagator Propagator
52-
{
53-
get
54-
{
55-
return this.definedPropagator ?? Propagators.DefaultTextMapPropagator;
56-
}
57-
}
51+
private TextMapPropagator Propagator => this.definedPropagator ?? Propagators.DefaultTextMapPropagator;
5852

5953
/// <inheritdoc/>
6054
public global::OpenTracing.ISpanBuilder BuildSpan(string operationName)

src/Shared/AssemblyVersionExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ private static string ParsePackageVersion(string informationalVersion)
4646
// The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
4747
// For package version, value of AssemblyInformationalVersionAttribute without commit hash is returned.
4848

49-
var indexOfPlusSign = informationalVersion!.IndexOf('+');
49+
#if NET
50+
var indexOfPlusSign = informationalVersion.IndexOf('+', StringComparison.Ordinal);
51+
#else
52+
var indexOfPlusSign = informationalVersion.IndexOf('+');
53+
#endif
5054
return indexOfPlusSign > 0
5155
? informationalVersion.Substring(0, indexOfPlusSign)
5256
: informationalVersion;

test/OpenTelemetry.Shims.OpenTracing.Tests/IntegrationTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void WithActivities(
5555
b => b.AddSource(ChildActivitySource))
5656
.Build();
5757

58-
ITracer otTracer = new TracerShim(
58+
var otTracer = new TracerShim(
5959
tracerProvider,
6060
Propagators.DefaultTextMapPropagator);
6161

@@ -99,7 +99,7 @@ public void WithActivities(
9999
}
100100
}
101101

102-
private class TestSampler : Sampler
102+
private sealed class TestSampler : Sampler
103103
{
104104
private readonly Func<SamplingParameters, SamplingDecision> shouldSampleDelegate;
105105

Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System.Diagnostics;
54
using Xunit;
65

76
namespace OpenTelemetry.Shims.OpenTracing.Tests;
87

98
[CollectionDefinition(nameof(ListenAndSampleAllActivitySources))]
10-
public sealed class ListenAndSampleAllActivitySources : ICollectionFixture<ListenAndSampleAllActivitySources.Fixture>
11-
{
12-
public sealed class Fixture : IDisposable
13-
{
14-
private readonly ActivityListener listener;
15-
16-
public Fixture()
17-
{
18-
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
19-
Activity.ForceDefaultIdFormat = true;
20-
21-
this.listener = new ActivityListener
22-
{
23-
ShouldListenTo = _ => true,
24-
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllDataAndRecorded,
25-
};
26-
27-
ActivitySource.AddActivityListener(this.listener);
28-
}
29-
30-
public void Dispose()
31-
{
32-
this.listener.Dispose();
33-
}
34-
}
35-
}
9+
#pragma warning disable CA1515
10+
public sealed class ListenAndSampleAllActivitySources : ICollectionFixture<ListenAndSampleAllActivitySourcesFixture>;
11+
#pragma warning restore CA1515
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Diagnostics;
5+
6+
namespace OpenTelemetry.Shims.OpenTracing.Tests;
7+
8+
#pragma warning disable CA1515
9+
public sealed class ListenAndSampleAllActivitySourcesFixture : IDisposable
10+
#pragma warning restore CA1515
11+
{
12+
private readonly ActivityListener listener;
13+
14+
public ListenAndSampleAllActivitySourcesFixture()
15+
{
16+
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
17+
Activity.ForceDefaultIdFormat = true;
18+
19+
this.listener = new ActivityListener
20+
{
21+
ShouldListenTo = _ => true,
22+
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllDataAndRecorded,
23+
};
24+
25+
ActivitySource.AddActivityListener(this.listener);
26+
}
27+
28+
public void Dispose()
29+
{
30+
this.listener.Dispose();
31+
}
32+
}

test/OpenTelemetry.Shims.OpenTracing.Tests/OpenTelemetry.Shims.OpenTracing.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<Description>Unit test project for OpenTelemetry.Shims.OpenTracing</Description>
55
<TargetFrameworks>$(TargetFrameworksForTests)</TargetFrameworks>
66
<DefineConstants Condition="'$(RunningDotNetPack)' != 'true'">$(DefineConstants);BUILDING_USING_PROJECTS</DefineConstants>
7+
<AnalysisLevel>latest-all</AnalysisLevel>
78
</PropertyGroup>
89

910
<ItemGroup>

test/OpenTelemetry.Shims.OpenTracing.Tests/SpanBuilderShimTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public void AsChildOf_WithSpan()
9494
public void Start_ActivityOperationRootSpanChecks()
9595
{
9696
// Create an activity
97-
using var activity = new Activity("foo")
98-
.SetIdFormat(ActivityIdFormat.W3C)
97+
using var activity = new Activity("foo");
98+
activity.SetIdFormat(ActivityIdFormat.W3C)
9999
.Start();
100100

101101
// matching root operation name
@@ -132,7 +132,7 @@ public void AsChildOf_MultipleCallsWithSpan()
132132

133133
Assert.NotNull(spanShim.Span.Activity);
134134
Assert.Equal("foo", spanShim.Span.Activity.OperationName);
135-
Assert.Contains(spanShim.Context.TraceId, spanShim.Span.Activity.TraceId.ToHexString());
135+
Assert.Contains(spanShim.Context.TraceId, spanShim.Span.Activity.TraceId.ToHexString(), StringComparison.Ordinal);
136136

137137
// TODO: Check for multi level parenting
138138
}
@@ -191,7 +191,7 @@ public void AsChildOf_MultipleCallsWithSpanContext()
191191
var spanShim = (SpanShim)shim.Start();
192192
Assert.NotNull(spanShim.Span.Activity);
193193
Assert.Equal("foo", spanShim.Span.Activity.OperationName);
194-
Assert.Contains(spanContext1.TraceId, spanShim.Span.Activity.ParentId);
194+
Assert.Contains(spanContext1.TraceId, spanShim.Span.Activity.ParentId, StringComparison.Ordinal);
195195
Assert.Equal(spanContext2.SpanId, spanShim.Span.Activity.Links.First().Context.SpanId.ToHexString());
196196
}
197197

test/OpenTelemetry.Shims.OpenTracing.Tests/TestFormatTextMap.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
namespace OpenTelemetry.Shims.OpenTracing.Tests;
77

8-
internal class TestFormatTextMap : IFormat<ITextMap>
8+
internal sealed class TestFormatTextMap : IFormat<ITextMap>
99
{
1010
}

test/OpenTelemetry.Shims.OpenTracing.Tests/TestSpan.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace OpenTelemetry.Shims.OpenTracing.Tests;
88

9-
internal class TestSpan : ISpan
9+
internal sealed class TestSpan : ISpan
1010
{
1111
public ISpanContext Context => throw new NotImplementedException();
1212

test/OpenTelemetry.Shims.OpenTracing.Tests/TestSpanContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace OpenTelemetry.Shims.OpenTracing.Tests;
77

8-
internal class TestSpanContext : ISpanContext
8+
internal sealed class TestSpanContext : ISpanContext
99
{
1010
public string TraceId => throw new NotImplementedException();
1111

test/OpenTelemetry.Shims.OpenTracing.Tests/TestTextMap.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace OpenTelemetry.Shims.OpenTracing.Tests;
88

9-
internal class TestTextMap : ITextMap
9+
internal sealed class TestTextMap : ITextMap
1010
{
1111
public bool GetEnumeratorCalled { get; private set; }
1212

test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs

+2-20
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void InjectExtract_TextMap_Ok()
132132
AssertOpenTracerSpanContextEqual(spanContextShim, extractedSpanContext!);
133133
}
134134

135-
private static void AssertOpenTracerSpanContextEqual(ISpanContext source, ISpanContext target)
135+
private static void AssertOpenTracerSpanContextEqual(SpanContextShim source, ISpanContext target)
136136
{
137137
Assert.Equal(source.TraceId, target.TraceId);
138138
Assert.Equal(source.SpanId, target.SpanId);
@@ -144,7 +144,7 @@ private static void AssertOpenTracerSpanContextEqual(ISpanContext source, ISpanC
144144
/// Simple ITextMap implementation used for the inject/extract tests.
145145
/// </summary>
146146
/// <seealso cref="OpenTracing.Propagation.ITextMap" />
147-
private class TextMapCarrier : ITextMap
147+
private sealed class TextMapCarrier : ITextMap
148148
{
149149
private readonly Dictionary<string, string> map = new();
150150

@@ -159,22 +159,4 @@ public void Set(string key, string value)
159159

160160
IEnumerator IEnumerable.GetEnumerator() => this.map.GetEnumerator();
161161
}
162-
163-
/// <summary>
164-
/// Simple IBinary implementation used for the inject/extract tests.
165-
/// </summary>
166-
/// <seealso cref="OpenTracing.Propagation.IBinary" />
167-
private class BinaryCarrier : IBinary
168-
{
169-
private readonly MemoryStream carrierStream = new();
170-
171-
public MemoryStream Get() => this.carrierStream;
172-
173-
public void Set(MemoryStream stream)
174-
{
175-
this.carrierStream.SetLength(stream.Length);
176-
this.carrierStream.Seek(0, SeekOrigin.Begin);
177-
stream.CopyTo(this.carrierStream, (int)this.carrierStream.Length);
178-
}
179-
}
180162
}

0 commit comments

Comments
 (0)