Skip to content

Commit

Permalink
Add IntakeResponse deserialization for detailed error logging
Browse files Browse the repository at this point in the history
Introduced the `IntakeResponse` class to better handle and log errors from APM server responses. Updated `PayloadSenderV2` to use this class for extracting error messages and logging them in a more informative way. Added a deserialization method in `PayloadItemSerializer` for converting response streams.
  • Loading branch information
Mpdreamz committed Oct 7, 2024
1 parent af6d159 commit efac464
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Elastic.Apm/Report/IntakeResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to Elasticsearch B.V under
// one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Collections.Generic;
using Elastic.Apm.Libraries.Newtonsoft.Json;

namespace Elastic.Apm.Report;

internal class IntakeResponse
{
[JsonProperty("accepted")]
public int Accepted { get; set; }

[JsonProperty("errors")]
public IReadOnlyCollection<IntakeError> Errors { get; set; }
}

internal class IntakeError
{

[JsonProperty("message")]
public string Message { get; set; }
}
17 changes: 15 additions & 2 deletions src/Elastic.Apm/Report/PayloadSenderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
Expand Down Expand Up @@ -420,14 +421,26 @@ private void ProcessQueueItems(object[] queueItems)
// ReSharper disable ConditionIsAlwaysTrueOrFalse
if (response is null || !response.IsSuccessStatusCode)
{
var message = "Unknown 400 Bad Request";
if (response?.Content != null)
{
#if NET6_0_OR_GREATER
var intakeResponse = _payloadItemSerializer.Deserialize<IntakeResponse>(response.Content.ReadAsStream());
#else
var intakeResponse = _payloadItemSerializer.Deserialize<IntakeResponse>(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult());
#endif
if (intakeResponse.Errors.Count > 0)
message = string.Join(", ", intakeResponse.Errors.Select(e => e.Message).Distinct());
}
_logger?.Error()
?.Log("Failed sending event."
+ " Events intake API absolute URL: {EventsIntakeAbsoluteUrl}."
+ " APM Server response: status code: {ApmServerResponseStatusCode}"
+ ", content: \n{ApmServerResponseContent}"
+ ", reasons: {ApmServerResponseContent}"
, _intakeV2EventsAbsoluteUrl.Sanitize()
, response?.StatusCode,
response?.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult());
message
);
}
// ReSharper enable ConditionIsAlwaysTrueOrFalse
else
Expand Down
8 changes: 8 additions & 0 deletions src/Elastic.Apm/Report/Serialization/PayloadItemSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ internal T Deserialize<T>(string json)
return val ?? default;
}

internal T Deserialize<T>(Stream stream)
{
using var sr = new StreamReader(stream);
using var jsonTextReader = new JsonTextReader(sr);
var val = _serializer.Deserialize<T>(jsonTextReader);
return val;
}

/// <summary>
/// Serializes the item to JSON
/// </summary>
Expand Down

0 comments on commit efac464

Please sign in to comment.