-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[otlp] Refactor shared protobuf otlp export client code into a base c…
…lass (#6001)
- Loading branch information
1 parent
88d2ad6
commit 7eeddf5
Showing
3 changed files
with
118 additions
and
127 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...ry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/ProtobufOtlpExportClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#if NETFRAMEWORK | ||
using System.Net.Http; | ||
#endif | ||
using System.Net.Http.Headers; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient; | ||
|
||
internal abstract class ProtobufOtlpExportClient : IProtobufExportClient | ||
{ | ||
private static readonly Version Http2RequestVersion = new(2, 0); | ||
|
||
#if NET | ||
private static readonly bool SynchronousSendSupportedByCurrentPlatform; | ||
|
||
static ProtobufOtlpExportClient() | ||
{ | ||
#if NET | ||
// See: https://github.com/dotnet/runtime/blob/280f2a0c60ce0378b8db49adc0eecc463d00fe5d/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L767 | ||
SynchronousSendSupportedByCurrentPlatform = !OperatingSystem.IsAndroid() | ||
&& !OperatingSystem.IsIOS() | ||
&& !OperatingSystem.IsTvOS() | ||
&& !OperatingSystem.IsBrowser(); | ||
#endif | ||
} | ||
#endif | ||
|
||
protected ProtobufOtlpExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath) | ||
{ | ||
Guard.ThrowIfNull(options); | ||
Guard.ThrowIfNull(httpClient); | ||
Guard.ThrowIfNull(signalPath); | ||
|
||
Uri exporterEndpoint = options.Endpoint.AppendPathIfNotPresent(signalPath); | ||
this.Endpoint = new UriBuilder(exporterEndpoint).Uri; | ||
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v)); | ||
this.HttpClient = httpClient; | ||
} | ||
|
||
internal HttpClient HttpClient { get; } | ||
|
||
internal Uri Endpoint { get; } | ||
|
||
internal IReadOnlyDictionary<string, string> Headers { get; } | ||
|
||
internal abstract MediaTypeHeaderValue MediaTypeHeader { get; } | ||
|
||
internal virtual bool RequireHttp2 => false; | ||
|
||
public abstract ExportClientResponse SendExportRequest(byte[] buffer, int contentLength, DateTime deadlineUtc, CancellationToken cancellationToken = default); | ||
|
||
/// <inheritdoc/> | ||
public bool Shutdown(int timeoutMilliseconds) | ||
{ | ||
this.HttpClient.CancelPendingRequests(); | ||
return true; | ||
} | ||
|
||
protected HttpRequestMessage CreateHttpRequest(byte[] buffer, int contentLength) | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Post, this.Endpoint); | ||
|
||
if (this.RequireHttp2) | ||
{ | ||
request.Version = Http2RequestVersion; | ||
|
||
#if NET6_0_OR_GREATER | ||
request.VersionPolicy = HttpVersionPolicy.RequestVersionExact; | ||
#endif | ||
} | ||
|
||
foreach (var header in this.Headers) | ||
{ | ||
request.Headers.Add(header.Key, header.Value); | ||
} | ||
|
||
// TODO: Support compression. | ||
|
||
request.Content = new ByteArrayContent(buffer, 0, contentLength); | ||
request.Content.Headers.ContentType = this.MediaTypeHeader; | ||
|
||
return request; | ||
} | ||
|
||
protected HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
#if NET | ||
// Note: SendAsync must be used with HTTP/2 because synchronous send is | ||
// not supported. | ||
return this.RequireHttp2 || !SynchronousSendSupportedByCurrentPlatform | ||
? this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult() | ||
: this.HttpClient.Send(request, cancellationToken); | ||
#else | ||
return this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult(); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters