Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ public bool Shutdown(int timeoutMilliseconds)
return true;
}

protected static string? TryGetResponseBody(HttpResponseMessage? httpResponse, CancellationToken cancellationToken)
{
if (httpResponse?.Content == null)
{
return null;
}

try
{
#if NET
var stream = httpResponse.Content.ReadAsStream(cancellationToken);
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
#else
return httpResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();
#endif
}
catch (Exception)
{
return null;
}
}

protected HttpRequestMessage CreateHttpRequest(byte[] buffer, int contentLength)
{
var request = new HttpRequestMessage(HttpMethod.Post, this.Endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public OtlpGrpcExportClient(OtlpExporterOptions options, HttpClient httpClient,
/// <inheritdoc/>
public override ExportClientResponse SendExportRequest(byte[] buffer, int contentLength, DateTime deadlineUtc, CancellationToken cancellationToken = default)
{
HttpResponseMessage? httpResponse = null;

try
{
using var httpRequest = this.CreateHttpRequest(buffer, contentLength);
Expand All @@ -44,7 +46,7 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
// A missing TE header results in servers aborting the gRPC call.
httpRequest.Headers.TryAddWithoutValidation("TE", "trailers");

using var httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);
httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);

httpResponse.EnsureSuccessStatusCode();

Expand Down Expand Up @@ -121,7 +123,8 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
catch (HttpRequestException ex)
{
// Handle non-retryable HTTP errors.
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, ex);
var response = TryGetResponseBody(httpResponse, cancellationToken);
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, response, ex);
return new ExportClientGrpcResponse(
success: false,
deadlineUtc: deadlineUtc,
Expand Down Expand Up @@ -156,6 +159,10 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
return DefaultExceptionExportClientGrpcResponse;
}
finally
{
httpResponse?.Dispose();
}
}

private static bool IsTransientNetworkError(HttpRequestException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public override ExportClientResponse SendExportRequest(byte[] buffer, int conten
}
catch (HttpRequestException ex)
{
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, ex);
var response = TryGetResponseBody(httpResponse, cancellationToken);
OpenTelemetryProtocolExporterEventSource.Log.HttpRequestFailed(this.Endpoint, response, ex);
return new ExportClientHttpResponse(success: false, deadlineUtc: deadlineUtc, response: httpResponse, ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public void TransientHttpError(Uri endpoint, Exception ex)
}

[NonEvent]
public void HttpRequestFailed(Uri endpoint, Exception ex)
public void HttpRequestFailed(Uri endpoint, string? response, Exception ex)
{
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
{
this.HttpRequestFailed(endpoint.ToString(), ex.ToInvariantString());
this.HttpRequestFailed(endpoint.ToString(), response, ex.ToInvariantString());
}
}

Expand Down Expand Up @@ -200,10 +200,10 @@ public void TransientHttpError(string endpoint, string exceptionMessage)
this.WriteEvent(16, endpoint, exceptionMessage);
}

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

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