Skip to content

Commit 8de6e69

Browse files
authored
Update project OpenTelemetry.Exporter.OpenTelemetryProtocol to use fi… (#4685)
1 parent 7341a47 commit 8de6e69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+5074
-5115
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs

Lines changed: 291 additions & 292 deletions
Large diffs are not rendered by default.

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

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,55 @@
2020
using Grpc.Net.Client;
2121
#endif
2222

23-
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient
23+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
24+
25+
/// <summary>Base class for sending OTLP export request over gRPC.</summary>
26+
/// <typeparam name="TRequest">Type of export request.</typeparam>
27+
internal abstract class BaseOtlpGrpcExportClient<TRequest> : IExportClient<TRequest>
2428
{
25-
/// <summary>Base class for sending OTLP export request over gRPC.</summary>
26-
/// <typeparam name="TRequest">Type of export request.</typeparam>
27-
internal abstract class BaseOtlpGrpcExportClient<TRequest> : IExportClient<TRequest>
29+
protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
2830
{
29-
protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
30-
{
31-
Guard.ThrowIfNull(options);
32-
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);
31+
Guard.ThrowIfNull(options);
32+
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);
3333

34-
ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options);
34+
ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options);
3535

36-
this.Endpoint = new UriBuilder(options.Endpoint).Uri;
37-
this.Headers = options.GetMetadataFromHeaders();
38-
this.TimeoutMilliseconds = options.TimeoutMilliseconds;
39-
}
36+
this.Endpoint = new UriBuilder(options.Endpoint).Uri;
37+
this.Headers = options.GetMetadataFromHeaders();
38+
this.TimeoutMilliseconds = options.TimeoutMilliseconds;
39+
}
4040

4141
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
42-
internal GrpcChannel Channel { get; set; }
42+
internal GrpcChannel Channel { get; set; }
4343
#else
44-
internal Channel Channel { get; set; }
44+
internal Channel Channel { get; set; }
4545
#endif
4646

47-
internal Uri Endpoint { get; }
47+
internal Uri Endpoint { get; }
4848

49-
internal Metadata Headers { get; }
49+
internal Metadata Headers { get; }
5050

51-
internal int TimeoutMilliseconds { get; }
51+
internal int TimeoutMilliseconds { get; }
5252

53-
/// <inheritdoc/>
54-
public abstract bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
53+
/// <inheritdoc/>
54+
public abstract bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
5555

56-
/// <inheritdoc/>
57-
public virtual bool Shutdown(int timeoutMilliseconds)
56+
/// <inheritdoc/>
57+
public virtual bool Shutdown(int timeoutMilliseconds)
58+
{
59+
if (this.Channel == null)
5860
{
59-
if (this.Channel == null)
60-
{
61-
return true;
62-
}
61+
return true;
62+
}
6363

64-
if (timeoutMilliseconds == -1)
65-
{
66-
this.Channel.ShutdownAsync().Wait();
67-
return true;
68-
}
69-
else
70-
{
71-
return Task.WaitAny(new Task[] { this.Channel.ShutdownAsync(), Task.Delay(timeoutMilliseconds) }) == 0;
72-
}
64+
if (timeoutMilliseconds == -1)
65+
{
66+
this.Channel.ShutdownAsync().Wait();
67+
return true;
68+
}
69+
else
70+
{
71+
return Task.WaitAny(new Task[] { this.Channel.ShutdownAsync(), Task.Delay(timeoutMilliseconds) }) == 0;
7372
}
7473
}
7574
}

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

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,83 +19,82 @@
1919
#endif
2020
using OpenTelemetry.Internal;
2121

22-
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient
22+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
23+
24+
/// <summary>Base class for sending OTLP export request over HTTP.</summary>
25+
/// <typeparam name="TRequest">Type of export request.</typeparam>
26+
internal abstract class BaseOtlpHttpExportClient<TRequest> : IExportClient<TRequest>
2327
{
24-
/// <summary>Base class for sending OTLP export request over HTTP.</summary>
25-
/// <typeparam name="TRequest">Type of export request.</typeparam>
26-
internal abstract class BaseOtlpHttpExportClient<TRequest> : IExportClient<TRequest>
28+
protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath)
2729
{
28-
protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath)
29-
{
30-
Guard.ThrowIfNull(options);
31-
Guard.ThrowIfNull(httpClient);
32-
Guard.ThrowIfNull(signalPath);
33-
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);
34-
35-
Uri exporterEndpoint = !options.ProgrammaticallyModifiedEndpoint
36-
? options.Endpoint.AppendPathIfNotPresent(signalPath)
37-
: options.Endpoint;
38-
this.Endpoint = new UriBuilder(exporterEndpoint).Uri;
39-
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
40-
this.HttpClient = httpClient;
41-
}
30+
Guard.ThrowIfNull(options);
31+
Guard.ThrowIfNull(httpClient);
32+
Guard.ThrowIfNull(signalPath);
33+
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);
34+
35+
Uri exporterEndpoint = !options.ProgrammaticallyModifiedEndpoint
36+
? options.Endpoint.AppendPathIfNotPresent(signalPath)
37+
: options.Endpoint;
38+
this.Endpoint = new UriBuilder(exporterEndpoint).Uri;
39+
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
40+
this.HttpClient = httpClient;
41+
}
4242

43-
internal HttpClient HttpClient { get; }
43+
internal HttpClient HttpClient { get; }
4444

45-
internal Uri Endpoint { get; set; }
45+
internal Uri Endpoint { get; set; }
4646

47-
internal IReadOnlyDictionary<string, string> Headers { get; }
47+
internal IReadOnlyDictionary<string, string> Headers { get; }
4848

49-
/// <inheritdoc/>
50-
public bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default)
49+
/// <inheritdoc/>
50+
public bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default)
51+
{
52+
try
5153
{
52-
try
53-
{
54-
using var httpRequest = this.CreateHttpRequest(request);
55-
56-
using var httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);
54+
using var httpRequest = this.CreateHttpRequest(request);
5755

58-
httpResponse?.EnsureSuccessStatusCode();
59-
}
60-
catch (HttpRequestException ex)
61-
{
62-
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
56+
using var httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);
6357

64-
return false;
65-
}
66-
67-
return true;
58+
httpResponse?.EnsureSuccessStatusCode();
6859
}
69-
70-
/// <inheritdoc/>
71-
public bool Shutdown(int timeoutMilliseconds)
60+
catch (HttpRequestException ex)
7261
{
73-
this.HttpClient.CancelPendingRequests();
74-
return true;
62+
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
63+
64+
return false;
7565
}
7666

77-
protected abstract HttpContent CreateHttpContent(TRequest exportRequest);
67+
return true;
68+
}
7869

79-
protected HttpRequestMessage CreateHttpRequest(TRequest exportRequest)
80-
{
81-
var request = new HttpRequestMessage(HttpMethod.Post, this.Endpoint);
82-
foreach (var header in this.Headers)
83-
{
84-
request.Headers.Add(header.Key, header.Value);
85-
}
70+
/// <inheritdoc/>
71+
public bool Shutdown(int timeoutMilliseconds)
72+
{
73+
this.HttpClient.CancelPendingRequests();
74+
return true;
75+
}
8676

87-
request.Content = this.CreateHttpContent(exportRequest);
77+
protected abstract HttpContent CreateHttpContent(TRequest exportRequest);
8878

89-
return request;
79+
protected HttpRequestMessage CreateHttpRequest(TRequest exportRequest)
80+
{
81+
var request = new HttpRequestMessage(HttpMethod.Post, this.Endpoint);
82+
foreach (var header in this.Headers)
83+
{
84+
request.Headers.Add(header.Key, header.Value);
9085
}
9186

92-
protected HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken)
93-
{
87+
request.Content = this.CreateHttpContent(exportRequest);
88+
89+
return request;
90+
}
91+
92+
protected HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken)
93+
{
9494
#if NET6_0_OR_GREATER
95-
return this.HttpClient.Send(request, cancellationToken);
95+
return this.HttpClient.Send(request, cancellationToken);
9696
#else
97-
return this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
97+
return this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
9898
#endif
99-
}
10099
}
101100
}

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,28 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17-
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient
17+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
18+
19+
internal static class ExporterClientValidation
1820
{
19-
internal static class ExporterClientValidation
21+
internal static void EnsureUnencryptedSupportIsEnabled(OtlpExporterOptions options)
2022
{
21-
internal static void EnsureUnencryptedSupportIsEnabled(OtlpExporterOptions options)
22-
{
23-
var version = Environment.Version;
23+
var version = Environment.Version;
2424

25-
// This verification is only required for .NET Core 3.x
26-
if (version.Major != 3)
27-
{
28-
return;
29-
}
25+
// This verification is only required for .NET Core 3.x
26+
if (version.Major != 3)
27+
{
28+
return;
29+
}
3030

31-
if (options.Endpoint.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
31+
if (options.Endpoint.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
32+
{
33+
if (AppContext.TryGetSwitch(
34+
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", out var unencryptedIsSupported) == false
35+
|| unencryptedIsSupported == false)
3236
{
33-
if (AppContext.TryGetSwitch(
34-
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", out var unencryptedIsSupported) == false
35-
|| unencryptedIsSupported == false)
36-
{
37-
throw new InvalidOperationException(
38-
"Calling insecure gRPC services on .NET Core 3.x requires enabling the 'System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport' switch. See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client");
39-
}
37+
throw new InvalidOperationException(
38+
"Calling insecure gRPC services on .NET Core 3.x requires enabling the 'System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport' switch. See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client");
4039
}
4140
}
4241
}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,29 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17-
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient
17+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
18+
19+
/// <summary>Export client interface.</summary>
20+
/// <typeparam name="TRequest">Type of export request.</typeparam>
21+
internal interface IExportClient<in TRequest>
1822
{
19-
/// <summary>Export client interface.</summary>
20-
/// <typeparam name="TRequest">Type of export request.</typeparam>
21-
internal interface IExportClient<in TRequest>
22-
{
23-
/// <summary>
24-
/// Method for sending export request to the server.
25-
/// </summary>
26-
/// <param name="request">The request to send to the server.</param>
27-
/// <param name="cancellationToken">An optional token for canceling the call.</param>
28-
/// <returns>True if the request has been sent successfully, otherwise false.</returns>
29-
bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
23+
/// <summary>
24+
/// Method for sending export request to the server.
25+
/// </summary>
26+
/// <param name="request">The request to send to the server.</param>
27+
/// <param name="cancellationToken">An optional token for canceling the call.</param>
28+
/// <returns>True if the request has been sent successfully, otherwise false.</returns>
29+
bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
3030

31-
/// <summary>
32-
/// Method for shutting down the export client.
33-
/// </summary>
34-
/// <param name="timeoutMilliseconds">
35-
/// The number of milliseconds to wait, or <c>Timeout.Infinite</c> to
36-
/// wait indefinitely.
37-
/// </param>
38-
/// <returns>
39-
/// Returns <c>true</c> if shutdown succeeded; otherwise, <c>false</c>.
40-
/// </returns>
41-
bool Shutdown(int timeoutMilliseconds);
42-
}
31+
/// <summary>
32+
/// Method for shutting down the export client.
33+
/// </summary>
34+
/// <param name="timeoutMilliseconds">
35+
/// The number of milliseconds to wait, or <c>Timeout.Infinite</c> to
36+
/// wait indefinitely.
37+
/// </param>
38+
/// <returns>
39+
/// Returns <c>true</c> if shutdown succeeded; otherwise, <c>false</c>.
40+
/// </returns>
41+
bool Shutdown(int timeoutMilliseconds);
4342
}

0 commit comments

Comments
 (0)