forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOtlpGrpcExportClient.cs
163 lines (143 loc) · 7.18 KB
/
OtlpGrpcExportClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NETFRAMEWORK
using System.Net.Http;
#endif
using System.Net.Http.Headers;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient.Grpc;
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
/// <summary>Base class for sending OTLP export request over gRPC.</summary>
internal sealed class OtlpGrpcExportClient : OtlpExportClient
{
public const string GrpcStatusDetailsHeader = "grpc-status-details-bin";
private static readonly ExportClientHttpResponse SuccessExportResponse = new(success: true, deadlineUtc: default, response: null, exception: null);
private static readonly MediaTypeHeaderValue MediaHeaderValue = new("application/grpc");
private static readonly ExportClientGrpcResponse DefaultExceptionExportClientGrpcResponse
= new(
success: false,
deadlineUtc: default,
exception: null,
status: null,
grpcStatusDetailsHeader: null);
public OtlpGrpcExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath)
: base(options, httpClient, signalPath)
{
}
internal override MediaTypeHeaderValue MediaTypeHeader => MediaHeaderValue;
internal override bool RequireHttp2 => true;
/// <inheritdoc/>
public override ExportClientResponse SendExportRequest(byte[] buffer, int contentLength, DateTime deadlineUtc, CancellationToken cancellationToken = default)
{
try
{
using var httpRequest = this.CreateHttpRequest(buffer, contentLength);
using var httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);
httpResponse.EnsureSuccessStatusCode();
var trailingHeaders = httpResponse.TrailingHeaders();
Status status = GrpcProtocolHelpers.GetResponseStatus(httpResponse, trailingHeaders);
if (status.Detail.Equals(Status.NoReplyDetailMessage))
{
#if NET
using var responseStream = httpResponse.Content.ReadAsStream(cancellationToken);
#else
using var responseStream = httpResponse.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
#endif
int firstByte = responseStream.ReadByte();
if (firstByte == -1)
{
if (status.StatusCode == StatusCode.OK)
{
status = new Status(StatusCode.Internal, "Failed to deserialize response message.");
}
OpenTelemetryProtocolExporterEventSource.Log.ResponseDeserializationFailed(this.Endpoint.ToString());
return new ExportClientGrpcResponse(
success: false,
deadlineUtc: deadlineUtc,
exception: null,
status: status,
grpcStatusDetailsHeader: null);
}
// Note: Trailing headers might not be fully available until the
// response stream is consumed. gRPC often sends critical
// information like error details or final statuses in trailing
// headers which can only be reliably accessed after reading
// the response body.
trailingHeaders = httpResponse.TrailingHeaders();
status = GrpcProtocolHelpers.GetResponseStatus(httpResponse, trailingHeaders);
}
if (status.StatusCode == StatusCode.OK)
{
OpenTelemetryProtocolExporterEventSource.Log.ExportSuccess(this.Endpoint.ToString(), "Export completed successfully.");
return SuccessExportResponse;
}
string? grpcStatusDetailsHeader = null;
if (status.StatusCode == StatusCode.ResourceExhausted || status.StatusCode == StatusCode.Unavailable)
{
grpcStatusDetailsHeader = GrpcProtocolHelpers.GetHeaderValue(trailingHeaders, GrpcStatusDetailsHeader);
}
OpenTelemetryProtocolExporterEventSource.Log.ExportFailure(this.Endpoint, "Export failed due to unexpected status code.", status);
return new ExportClientGrpcResponse(
success: false,
deadlineUtc: deadlineUtc,
exception: null,
status: status,
grpcStatusDetailsHeader: grpcStatusDetailsHeader);
}
catch (HttpRequestException ex) when (ex.InnerException is TimeoutException || IsTransientNetworkError(ex))
{
// Handle transient HTTP errors (retryable)
OpenTelemetryProtocolExporterEventSource.Log.TransientHttpError(this.Endpoint, ex);
return new ExportClientGrpcResponse(
success: false,
deadlineUtc: deadlineUtc,
exception: ex,
status: new Status(StatusCode.Unavailable, "Transient HTTP error - retryable"),
grpcStatusDetailsHeader: null);
}
catch (HttpRequestException ex)
{
// Handle non-retryable HTTP errors.
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, ex);
return new ExportClientGrpcResponse(
success: false,
deadlineUtc: deadlineUtc,
exception: ex,
status: null,
grpcStatusDetailsHeader: null);
}
catch (OperationCanceledException ex) when (!cancellationToken.IsCancellationRequested)
{
// Handle unexpected cancellation.
OpenTelemetryProtocolExporterEventSource.Log.OperationUnexpectedlyCanceled(this.Endpoint, ex);
return new ExportClientGrpcResponse(
success: false,
deadlineUtc: deadlineUtc,
exception: ex,
status: new Status(StatusCode.Cancelled, "Operation was canceled unexpectedly."),
grpcStatusDetailsHeader: null);
}
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
// Handle TaskCanceledException caused by TimeoutException.
OpenTelemetryProtocolExporterEventSource.Log.RequestTimedOut(this.Endpoint, ex);
return new ExportClientGrpcResponse(
success: false,
deadlineUtc: deadlineUtc,
exception: ex,
status: new Status(StatusCode.DeadlineExceeded, "Request timed out."),
grpcStatusDetailsHeader: null);
}
catch (Exception ex)
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
return DefaultExceptionExportClientGrpcResponse;
}
}
private static bool IsTransientNetworkError(HttpRequestException ex)
{
return ex.InnerException is System.Net.Sockets.SocketException socketEx
&& (socketEx.SocketErrorCode == System.Net.Sockets.SocketError.TimedOut
|| socketEx.SocketErrorCode == System.Net.Sockets.SocketError.ConnectionReset
|| socketEx.SocketErrorCode == System.Net.Sockets.SocketError.HostUnreachable);
}
}