Skip to content

Commit 13ed63a

Browse files
authored
[Exporter.OpenTelemetryProtocol] enable analysis (#6251)
1 parent 0252f6c commit 13ed63a

23 files changed

+62
-22
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Builder/OpenTelemetryBuilderOtlpExporterExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public static class OpenTelemetryBuilderOtlpExporterExtensions
3636
/// <returns>Supplied <see cref="IOpenTelemetryBuilder"/> for chaining calls.</returns>
3737
public static IOpenTelemetryBuilder UseOtlpExporter(
3838
this IOpenTelemetryBuilder builder)
39+
#pragma warning disable CA1062 // Validate arguments of public methods
3940
=> UseOtlpExporter(builder, name: null, configuration: null, configure: null);
41+
#pragma warning restore CA1062 // Validate arguments of public methods
4042

4143
/// <summary><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)"/></summary>
4244
/// <remarks><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/remarks"/></remarks>
@@ -56,7 +58,9 @@ public static IOpenTelemetryBuilder UseOtlpExporter(
5658
{
5759
Guard.ThrowIfNull(baseUrl);
5860

61+
#pragma warning disable CA1062 // Validate arguments of public methods
5962
return UseOtlpExporter(builder, name: null, configuration: null, configure: otlpBuilder =>
63+
#pragma warning restore CA1062 // Validate arguments of public methods
6064
{
6165
otlpBuilder.ConfigureDefaultExporterOptions(o =>
6266
{
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.cs]
2+
generated_code = true

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/Grpc/GrpcProtocolHelpers.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
// Copyright 2019 The gRPC Authors
57
//
68
// Licensed under the Apache License, Version 2.0 (the "License");

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/Grpc/GrpcStatusDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
using System.Text;
57

68
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient.Grpc;

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/Grpc/Status.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
// Copyright 2015 gRPC authors.
57
//
68
// Licensed under the Apache License, Version 2.0 (the "License");

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/Grpc/StatusCode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
// Copyright 2015 gRPC authors.
57
//
68
// Licensed under the Apache License, Version 2.0 (the "License");

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/Grpc/TrailingHeadersHelpers.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
// Copyright 2019 The gRPC Authors
57
//
68
// Licensed under the Apache License, Version 2.0 (the "License");

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/OtlpExportClient.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,12 @@ internal abstract class OtlpExportClient : IExportClient
1414
private static readonly Version Http2RequestVersion = new(2, 0);
1515

1616
#if NET
17-
private static readonly bool SynchronousSendSupportedByCurrentPlatform;
18-
19-
static OtlpExportClient()
20-
{
21-
#if NET
22-
// See: https://github.com/dotnet/runtime/blob/280f2a0c60ce0378b8db49adc0eecc463d00fe5d/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L767
23-
SynchronousSendSupportedByCurrentPlatform = !OperatingSystem.IsAndroid()
17+
// See: https://github.com/dotnet/runtime/blob/280f2a0c60ce0378b8db49adc0eecc463d00fe5d/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L767
18+
private static readonly bool SynchronousSendSupportedByCurrentPlatform = !OperatingSystem.IsAndroid()
2419
&& !OperatingSystem.IsIOS()
2520
&& !OperatingSystem.IsTvOS()
2621
&& !OperatingSystem.IsBrowser();
2722
#endif
28-
}
29-
#endif
3023

3124
protected OtlpExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath)
3225
{

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/OtlpGrpcExportClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
4646
var trailingHeaders = httpResponse.TrailingHeaders();
4747
Status status = GrpcProtocolHelpers.GetResponseStatus(httpResponse, trailingHeaders);
4848

49-
if (status.Detail.Equals(Status.NoReplyDetailMessage))
49+
if (status.Detail.Equals(Status.NoReplyDetailMessage, StringComparison.Ordinal))
5050
{
5151
#if NET
5252
using var responseStream = httpResponse.Content.ReadAsStream(cancellationToken);

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/OtlpRetry.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
using System.Net;
55
using System.Net.Http.Headers;
6+
#if NET
7+
using System.Security.Cryptography;
8+
#endif
69
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient.Grpc;
710

811
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
@@ -40,13 +43,12 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClie
4043
/// </summary>
4144
internal static class OtlpRetry
4245
{
43-
public const string GrpcStatusDetailsHeader = "grpc-status-details-bin";
4446
public const int InitialBackoffMilliseconds = 1000;
4547
private const int MaxBackoffMilliseconds = 5000;
4648
private const double BackoffMultiplier = 1.5;
4749

4850
#if !NET
49-
private static readonly Random Random = new Random();
51+
private static readonly Random Random = new();
5052
#endif
5153

5254
public static bool TryGetHttpRetryResult(ExportClientHttpResponse response, int retryDelayInMilliSeconds, out RetryResult retryResult)
@@ -156,9 +158,7 @@ private static bool TryGetRetryResult<TStatusCode, TCarrier>(TStatusCode statusC
156158
return false;
157159
}
158160

159-
var delayDuration = throttleDelay.HasValue
160-
? throttleDelay.Value
161-
: TimeSpan.FromMilliseconds(GetRandomNumber(0, nextRetryDelayMilliseconds));
161+
var delayDuration = throttleDelay ?? TimeSpan.FromMilliseconds(GetRandomNumber(0, nextRetryDelayMilliseconds));
162162

163163
if (deadline.HasValue && IsDeadlineExceeded(deadline + delayDuration))
164164
{
@@ -246,13 +246,15 @@ private static bool IsHttpStatusCodeRetryable(HttpStatusCode statusCode, bool ha
246246
private static int GetRandomNumber(int min, int max)
247247
{
248248
#if NET
249-
return Random.Shared.Next(min, max);
249+
return RandomNumberGenerator.GetInt32(min, max);
250250
#else
251251
// TODO: Implement this better to minimize lock contention.
252252
// Consider pulling in Random.Shared implementation.
253253
lock (Random)
254254
{
255+
#pragma warning disable CA5394 // Do not use insecure randomness
255256
return Random.Next(min, max);
257+
#pragma warning restore CA5394 // Do not use insecure randomness
256258
}
257259
#endif
258260
}

0 commit comments

Comments
 (0)