Skip to content

Commit 1a50965

Browse files
[OTLP] Log HTTP response body
Log the HTTP response body, if available. Resolves #6454.
1 parent ee87dc2 commit 1a50965

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ public bool Shutdown(int timeoutMilliseconds)
6565
return true;
6666
}
6767

68+
protected static string? TryGetResponseBody(HttpResponseMessage? httpResponse, CancellationToken cancellationToken)
69+
{
70+
if (httpResponse?.Content == null)
71+
{
72+
return null;
73+
}
74+
75+
try
76+
{
77+
#if NET
78+
var stream = httpResponse.Content.ReadAsStream(cancellationToken);
79+
using var reader = new StreamReader(stream);
80+
return reader.ReadToEnd();
81+
#else
82+
return httpResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();
83+
#endif
84+
}
85+
catch (Exception)
86+
{
87+
return null;
88+
}
89+
}
90+
6891
protected HttpRequestMessage CreateHttpRequest(byte[] buffer, int contentLength)
6992
{
7093
var request = new HttpRequestMessage(HttpMethod.Post, this.Endpoint);

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public OtlpGrpcExportClient(OtlpExporterOptions options, HttpClient httpClient,
3636
/// <inheritdoc/>
3737
public override ExportClientResponse SendExportRequest(byte[] buffer, int contentLength, DateTime deadlineUtc, CancellationToken cancellationToken = default)
3838
{
39+
HttpResponseMessage? httpResponse = null;
40+
3941
try
4042
{
4143
using var httpRequest = this.CreateHttpRequest(buffer, contentLength);
@@ -44,7 +46,7 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
4446
// A missing TE header results in servers aborting the gRPC call.
4547
httpRequest.Headers.TryAddWithoutValidation("TE", "trailers");
4648

47-
using var httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);
49+
httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);
4850

4951
httpResponse.EnsureSuccessStatusCode();
5052

@@ -121,7 +123,8 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
121123
catch (HttpRequestException ex)
122124
{
123125
// Handle non-retryable HTTP errors.
124-
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, ex);
126+
var response = TryGetResponseBody(httpResponse, cancellationToken);
127+
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, response, ex);
125128
return new ExportClientGrpcResponse(
126129
success: false,
127130
deadlineUtc: deadlineUtc,
@@ -156,6 +159,10 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
156159
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
157160
return DefaultExceptionExportClientGrpcResponse;
158161
}
162+
finally
163+
{
164+
httpResponse?.Dispose();
165+
}
159166
}
160167

161168
private static bool IsTransientNetworkError(HttpRequestException ex)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
3535
}
3636
catch (HttpRequestException ex)
3737
{
38-
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, ex);
38+
var response = TryGetResponseBody(httpResponse, cancellationToken);
39+
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, response, ex);
3940
return new ExportClientHttpResponse(success: false, deadlineUtc: deadlineUtc, response: httpResponse, ex);
4041
}
4142

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public void TransientHttpError(Uri endpoint, Exception ex)
6060
}
6161

6262
[NonEvent]
63-
public void HttpRequestFailed(Uri endpoint, Exception ex)
63+
public void HttpRequestFailed(Uri endpoint, string? response, Exception ex)
6464
{
6565
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
6666
{
67-
this.HttpRequestFailed(endpoint.ToString(), ex.ToInvariantString());
67+
this.HttpRequestFailed(endpoint.ToString(), response, ex.ToInvariantString());
6868
}
6969
}
7070

@@ -200,10 +200,10 @@ public void TransientHttpError(string endpoint, string exceptionMessage)
200200
this.WriteEvent(16, endpoint, exceptionMessage);
201201
}
202202

203-
[Event(17, Message = "HTTP request to {0} failed. Exception: {1}", Level = EventLevel.Error)]
204-
public void HttpRequestFailed(string endpoint, string exceptionMessage)
203+
[Event(17, Message = "HTTP request to {0} failed. Response: {1}. Exception: {2}", Level = EventLevel.Error)]
204+
public void HttpRequestFailed(string endpoint, string? response, string exceptionMessage)
205205
{
206-
this.WriteEvent(17, endpoint, exceptionMessage);
206+
this.WriteEvent(17, endpoint, response, exceptionMessage);
207207
}
208208

209209
[Event(18, Message = "Operation unexpectedly canceled for endpoint {0}. Exception: {1}", Level = EventLevel.Warning)]

0 commit comments

Comments
 (0)