Skip to content

Commit

Permalink
Fix the RabbitMQQueueDetails parsing to account for duplicated elements
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach committed Oct 2, 2024
1 parent 30ffb3f commit 5949e1a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/Query/RabbitMQ/RabbitMQManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,28 @@ public async Task AddAdditionalQueueDetails(List<RabbitMQQueueDetails> queues, C
return (null, false);
}

var queues = items.Select(item => new RabbitMQQueueDetails(item!)).ToArray();

return (queues, pageCount > pageReturned);
return (MaterializeQueueDetails(items), pageCount > pageReturned);
}
// Older versions of RabbitMQ API did not have paging and returned the array of items directly
case JsonArray arr:
{
var queues = arr.Select(item => new RabbitMQQueueDetails(item!)).ToArray();

return (queues, false);
return (MaterializeQueueDetails(arr), false);
}
default:
throw new Exception($"Was not able to get list of queues from RabbitMQ broker. API call succeeded and deserialized but was of unexpected type '{container.GetType().FullName}'.");
}
}
}

static RabbitMQQueueDetails[] MaterializeQueueDetails(JsonArray items)
{
// It is not possible to directly operated on the JsonNode. When the JsonNode is a JObject
// and the indexer is access the internal dictionary is initialized which can cause key not found exceptions
// when the payload contains the same key multiple times (which happened in the past).
var queues = items.Select(item => new RabbitMQQueueDetails(item!.Deserialize<JsonElement>())).ToArray();
return queues;
}

public async Task<RabbitMQDetails> GetRabbitDetails(CancellationToken cancellationToken = default)
{
var overviewUrl = $"{ManagementUri}/api/overview";
Expand Down
12 changes: 6 additions & 6 deletions src/Query/RabbitMQ/RabbitMQQueueDetails.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
namespace Particular.ThroughputQuery.RabbitMQ
{
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Text.Json;

public class RabbitMQQueueDetails
{
public RabbitMQQueueDetails(JsonNode token)
public RabbitMQQueueDetails(JsonElement token)
{
Name = token["name"]!.GetValue<string>();
VHost = token["vhost"]!.GetValue<string>();
if (token!["message_stats"] is JsonObject stats && stats["ack"] is JsonValue val)
Name = token.GetProperty("name").GetString();
VHost = token.GetProperty("vhost").GetString();
if (token.TryGetProperty("message_stats", out var stats) && stats.TryGetProperty("ack", out var val))
{
AckedMessages = val.GetValue<long>();
AckedMessages = val.GetInt64();
}
}

Expand Down

0 comments on commit 5949e1a

Please sign in to comment.