Skip to content

Commit 6aa97df

Browse files
authored
Enable Roslyn analysis - main project - part I (#4685)
* CA1813 * CA1859 * CA1813 * CA2263 * CA2201 * CA1305 * CA1508 * CA1307 * CA1309 * CA1805 * CA1806 * CA1310 * CA1019 * CA1308
1 parent d95fa7e commit 6aa97df

37 files changed

+109
-313
lines changed

src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationVersion.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ private static string GetAssemblyInformationalVersion()
2626
// The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
2727
// for example: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
2828

29+
#if NET
30+
var indexOfPlusSign = informationalVersion.IndexOf('+', StringComparison.Ordinal);
31+
#else
2932
var indexOfPlusSign = informationalVersion.IndexOf('+');
33+
#endif
3034
return indexOfPlusSign > 0 ? informationalVersion.Substring(0, indexOfPlusSign) : informationalVersion;
3135
}
3236
catch (Exception)

src/OpenTelemetry.AutoInstrumentation/CallTarget/Handlers/IntegrationOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static class IntegrationOptions<TIntegration, TTarget>
1212
{
1313
private static readonly IOtelLogger Log = OtelLogging.GetLogger();
1414

15-
private static volatile bool _disableIntegration = false;
15+
private static volatile bool _disableIntegration;
1616

1717
internal static bool IsIntegrationEnabled => !_disableIntegration;
1818

@@ -36,7 +36,7 @@ internal static void LogException(Exception exception, string? message = null)
3636
}
3737
else if (exception is FileLoadException fileLoadException)
3838
{
39-
if (fileLoadException.FileName != null && (fileLoadException.FileName.StartsWith("System.Diagnostics.DiagnosticSource") || fileLoadException.FileName.StartsWith("System.Runtime.CompilerServices.Unsafe")))
39+
if (fileLoadException.FileName != null && (fileLoadException.FileName.StartsWith("System.Diagnostics.DiagnosticSource", StringComparison.Ordinal) || fileLoadException.FileName.StartsWith("System.Runtime.CompilerServices.Unsafe", StringComparison.Ordinal)))
4040
{
4141
Log.Warning($"FileLoadException for '{fileLoadException.FileName}' has been detected, the integration <{typeof(TIntegration)}, {typeof(TTarget)}> will be disabled.");
4242
_disableIntegration = true;

src/OpenTelemetry.AutoInstrumentation/CallTarget/PreserveContextAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace OpenTelemetry.AutoInstrumentation.CallTarget;
88
/// should execute under the current synchronization context/task scheduler.
99
/// </summary>
1010
[AttributeUsage(AttributeTargets.Method)]
11-
internal class PreserveContextAttribute : Attribute
11+
internal sealed class PreserveContextAttribute : Attribute
1212
{
1313
}

src/OpenTelemetry.AutoInstrumentation/Configurations/ConfigurationExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ internal static class ConfigurationExtensions
1010
public static IReadOnlyList<TEnum> ParseEnabledEnumList<TEnum>(this Configuration source, bool enabledByDefault, string enabledConfigurationTemplate)
1111
where TEnum : struct, Enum, IConvertible
1212
{
13+
#if NET
14+
var allConfigurations = Enum.GetValues<TEnum>();
15+
#else
1316
var allConfigurations = Enum.GetValues(typeof(TEnum)).Cast<TEnum>().ToArray();
17+
#endif
1418
var enabledConfigurations = new List<TEnum>(allConfigurations.Length);
1519

1620
foreach (var configuration in allConfigurations)

src/OpenTelemetry.AutoInstrumentation/Configurations/DelayedInitialization.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal static class Traces
1616
[MethodImpl(MethodImplOptions.NoInlining)]
1717
public static void AddAspNet(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager, TracerSettings tracerSettings)
1818
{
19-
new AspNetInitializer(lazyInstrumentationLoader, pluginManager, tracerSettings);
19+
_ = new AspNetInitializer(lazyInstrumentationLoader, pluginManager, tracerSettings);
2020
}
2121
#endif
2222

@@ -31,7 +31,7 @@ public static void AddAspNetCore(LazyInstrumentationLoader lazyInstrumentationLo
3131
[MethodImpl(MethodImplOptions.NoInlining)]
3232
public static void AddHttpClient(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager, TracerSettings tracerSettings)
3333
{
34-
new HttpClientInitializer(lazyInstrumentationLoader, pluginManager, tracerSettings);
34+
_ = new HttpClientInitializer(lazyInstrumentationLoader, pluginManager, tracerSettings);
3535
}
3636

3737
[MethodImpl(MethodImplOptions.NoInlining)]
@@ -43,7 +43,7 @@ public static void AddGrpcClient(LazyInstrumentationLoader lazyInstrumentationLo
4343
[MethodImpl(MethodImplOptions.NoInlining)]
4444
public static void AddSqlClient(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager)
4545
{
46-
new SqlClientTracerInitializer(lazyInstrumentationLoader, pluginManager);
46+
_ = new SqlClientTracerInitializer(lazyInstrumentationLoader, pluginManager);
4747
}
4848

4949
#if NET
@@ -80,20 +80,20 @@ internal static class Metrics
8080
[MethodImpl(MethodImplOptions.NoInlining)]
8181
public static void AddAspNet(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager)
8282
{
83-
new AspNetMetricsInitializer(lazyInstrumentationLoader, pluginManager);
83+
_ = new AspNetMetricsInitializer(lazyInstrumentationLoader, pluginManager);
8484
}
8585
#endif
8686

8787
[MethodImpl(MethodImplOptions.NoInlining)]
8888
public static void AddHttpClient(LazyInstrumentationLoader lazyInstrumentationLoader)
8989
{
90-
new HttpClientMetricsInitializer(lazyInstrumentationLoader);
90+
_ = new HttpClientMetricsInitializer(lazyInstrumentationLoader);
9191
}
9292

9393
[MethodImpl(MethodImplOptions.NoInlining)]
9494
public static void AddSqlClient(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager)
9595
{
96-
new SqlClientMetricsInitializer(lazyInstrumentationLoader, pluginManager);
96+
_ = new SqlClientMetricsInitializer(lazyInstrumentationLoader, pluginManager);
9797
}
9898
}
9999
}

src/OpenTelemetry.AutoInstrumentation/Configurations/EnvironmentInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void Initialize(NameValueCollection nameValueCollection)
2222
continue;
2323
}
2424

25-
if (!setting.StartsWith(VariablePrefix))
25+
if (!setting.StartsWith(VariablePrefix, StringComparison.Ordinal))
2626
{
2727
// not OTEL_ setting
2828
continue;

src/OpenTelemetry.AutoInstrumentation/Configurations/FileBasedConfiguration/Parser/ConditionalDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public bool Deserialize(
2323
out object? value,
2424
ObjectDeserializer rootDeserializer)
2525
{
26-
var isEmptyObjectOnEmptyYamlAttribute = expectedType.CustomAttributes.Any(a => string.Equals(a.AttributeType.Name, "EmptyObjectOnEmptyYamlAttribute", StringComparison.InvariantCulture));
26+
var isEmptyObjectOnEmptyYamlAttribute = expectedType.CustomAttributes.Any(a => string.Equals(a.AttributeType.Name, "EmptyObjectOnEmptyYamlAttribute", StringComparison.Ordinal));
2727

2828
if (!isEmptyObjectOnEmptyYamlAttribute)
2929
{

src/OpenTelemetry.AutoInstrumentation/Configurations/FileBasedConfiguration/Parser/EnvVarTypeConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public bool Accepts(Type type)
5555
return type switch
5656
{
5757
Type t when t == typeof(string) => replacedValue,
58-
Type t when t == typeof(int) => int.Parse(replacedValue),
59-
Type t when t == typeof(uint) => uint.Parse(replacedValue),
60-
Type t when t == typeof(long) => long.Parse(replacedValue),
58+
Type t when t == typeof(int) => int.Parse(replacedValue, CultureInfo.InvariantCulture),
59+
Type t when t == typeof(uint) => uint.Parse(replacedValue, CultureInfo.InvariantCulture),
60+
Type t when t == typeof(long) => long.Parse(replacedValue, CultureInfo.InvariantCulture),
6161
Type t when t == typeof(float) => float.Parse(replacedValue, CultureInfo.InvariantCulture),
6262
Type t when t == typeof(double) => double.Parse(replacedValue, CultureInfo.InvariantCulture),
6363
Type t when t == typeof(bool) => bool.Parse(replacedValue),
64-
Type t when t == typeof(int?) => int.Parse(replacedValue),
65-
Type t when t == typeof(uint?) => uint.Parse(replacedValue),
64+
Type t when t == typeof(int?) => int.Parse(replacedValue, CultureInfo.InvariantCulture),
65+
Type t when t == typeof(uint?) => uint.Parse(replacedValue, CultureInfo.InvariantCulture),
6666
Type t when t == typeof(object) => replacedValue,
6767
_ => throw new NotSupportedException($"Type {type.FullName} is not supported by the converter")
6868
};

src/OpenTelemetry.AutoInstrumentation/Configurations/FileBasedConfiguration/SamplerFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ internal static class SamplerFactory
9494
return configuredSampler;
9595
}
9696

97-
private static Sampler? CreateTraceIdRatioSampler(TraceIdRatioSamplerConfig config, bool failFast, string path)
97+
private static TraceIdRatioBasedSampler? CreateTraceIdRatioSampler(TraceIdRatioSamplerConfig config, bool failFast, string path)
9898
{
9999
if (!config.Ratio.HasValue)
100100
{
@@ -126,7 +126,7 @@ internal static class SamplerFactory
126126
return new TraceIdRatioBasedSampler(ratio);
127127
}
128128

129-
private static Sampler CreateParentBasedSampler(ParentBasedSamplerConfig config, bool failFast, string path)
129+
private static ParentBasedSampler CreateParentBasedSampler(ParentBasedSamplerConfig config, bool failFast, string path)
130130
{
131131
var rootSampler = GetSamplerOrDefault(config.Root, new AlwaysOnSampler(), failFast, path + ".root", "always_on");
132132
var remoteParentSampled = GetSamplerOrDefault(config.RemoteParentSampled, new AlwaysOnSampler(), failFast, path + ".remote_parent_sampled", "always_on");

src/OpenTelemetry.AutoInstrumentation/Configurations/IntegrationRegistry.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ internal static class IntegrationRegistry
1111

1212
static IntegrationRegistry()
1313
{
14+
#if NET
15+
var values = Enum.GetValues<TracerInstrumentation>();
16+
#else
1417
var values = Enum.GetValues(typeof(TracerInstrumentation));
18+
#endif
1519
var ids = new Dictionary<string, int>(values.Length);
1620

1721
Names = new string[values.Cast<int>().Max() + 1];

0 commit comments

Comments
 (0)