-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathRabbitMQQuery.cs
More file actions
322 lines (280 loc) · 14.1 KB
/
RabbitMQQuery.cs
File metadata and controls
322 lines (280 loc) · 14.1 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#nullable enable
namespace ServiceControl.Transports.RabbitMQ;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
using ServiceControl.Transports.BrokerThroughput;
public class RabbitMQQuery : BrokerThroughputQuery
{
HttpClient? httpClient;
readonly ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()) // Add retry using the default options
.AddTimeout(TimeSpan.FromMinutes(2)) // Add timeout if it keeps failing
.Build();
readonly ILogger<RabbitMQQuery> logger;
readonly TimeProvider timeProvider;
readonly ConnectionConfiguration connectionConfiguration;
public RabbitMQQuery(ILogger<RabbitMQQuery> logger,
TimeProvider timeProvider,
TransportSettings transportSettings) : base(logger, "RabbitMQ")
{
this.logger = logger;
this.timeProvider = timeProvider;
connectionConfiguration = ConnectionConfiguration.Create(transportSettings.ConnectionString, string.Empty);
}
protected override void InitializeCore(ReadOnlyDictionary<string, string> settings)
{
if (!settings.TryGetValue(RabbitMQSettings.UserName, out string? username) ||
string.IsNullOrEmpty(username))
{
logger.LogInformation("Using username from connectionstring");
username = connectionConfiguration.UserName;
Diagnostics.AppendLine(
$"Username not set, defaulted to using \"{username}\" username from the ConnectionString used by instance");
}
else
{
Diagnostics.AppendLine($"Username set to \"{username}\"");
}
if (!settings.TryGetValue(RabbitMQSettings.Password, out string? password) ||
string.IsNullOrEmpty(password))
{
logger.LogInformation("Using password from connectionstring");
password = connectionConfiguration.Password;
Diagnostics.AppendLine(
"Password not set, defaulted to using password from the ConnectionString used by instance");
}
else
{
Diagnostics.AppendLine("Password set");
}
var defaultCredential = new NetworkCredential(username, password);
if (!settings.TryGetValue(RabbitMQSettings.API, out string? apiUrl) ||
string.IsNullOrEmpty(apiUrl))
{
apiUrl =
$"{(connectionConfiguration.UseTls ? $"https://{connectionConfiguration.Host}:15671" : $"http://{connectionConfiguration.Host}:15672")}";
Diagnostics.AppendLine(
$"RabbitMQ API Url not set, defaulted to using \"{apiUrl}\" from the ConnectionString used by instance");
}
else
{
Diagnostics.AppendLine($"RabbitMQ API Url set to \"{apiUrl}\"");
if (!Uri.TryCreate(apiUrl, UriKind.Absolute, out _))
{
InitialiseErrors.Add("API url configured is invalid");
}
}
if (InitialiseErrors.Count == 0)
{
// ideally we would use the HttpClientFactory, but it would be a bit more involved to set that up
// so for now we are using a virtual method that can be overriden in tests
// https://github.com/Particular/ServiceControl/issues/4493
httpClient = CreateHttpClient(defaultCredential, apiUrl);
var authToken = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authToken);
}
}
protected virtual HttpClient CreateHttpClient(NetworkCredential defaultCredential, string apiUrl) =>
new(new SocketsHttpHandler
{
Credentials = defaultCredential,
PooledConnectionLifetime = TimeSpan.FromMinutes(2)
})
{ BaseAddress = new Uri(apiUrl) };
public override async IAsyncEnumerable<QueueThroughput> GetThroughputPerDay(IBrokerQueue brokerQueue,
DateOnly startDate,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var queue = (RabbitMQBrokerQueueDetails)brokerQueue;
var url = $"/api/queues/{HttpUtility.UrlEncode(queue.VHost)}/{HttpUtility.UrlEncode(queue.QueueName)}";
logger.LogDebug($"Querying {url}");
var newReading = await pipeline.ExecuteAsync(async token => new RabbitMQBrokerQueueDetails(await httpClient!.GetFromJsonAsync<JsonElement>(url, token)), cancellationToken);
_ = queue.CalculateThroughputFrom(newReading);
// looping for 24hrs, in 4 increments of 15 minutes
for (var i = 0; i < 24 * 4; i++)
{
await Task.Delay(TimeSpan.FromMinutes(15), timeProvider, cancellationToken);
logger.LogDebug($"Querying {url}");
newReading = await pipeline.ExecuteAsync(async token => new RabbitMQBrokerQueueDetails(await httpClient!.GetFromJsonAsync<JsonElement>(url, token)), cancellationToken);
var newTotalThroughput = queue.CalculateThroughputFrom(newReading);
yield return new QueueThroughput
{
DateUTC = DateOnly.FromDateTime(timeProvider.GetUtcNow().DateTime),
TotalThroughput = newTotalThroughput
};
}
}
async Task<(string rabbitVersion, string managementVersion)> GetRabbitDetails(bool skipResiliencePipeline, CancellationToken cancellationToken)
{
var overviewUrl = "/api/overview";
JsonObject obj;
if (skipResiliencePipeline)
{
obj = (await httpClient!.GetFromJsonAsync<JsonObject>(overviewUrl, cancellationToken))!;
}
else
{
obj = (await pipeline.ExecuteAsync(async token =>
await httpClient!.GetFromJsonAsync<JsonObject>(overviewUrl, token), cancellationToken))!;
}
var statsDisabled = obj["disable_stats"]?.GetValue<bool>() ?? false;
if (statsDisabled)
{
throw new Exception("The RabbitMQ broker is configured with 'management.disable_stats = true' or 'management_agent.disable_metrics_collector = true' and as a result queue statistics cannot be collected using this tool. Consider changing the configuration of the RabbitMQ broker.");
}
var rabbitVersion = obj["rabbitmq_version"] ?? obj["product_version"] ?? obj["lavinmq_version"];
var mgmtVersion = obj["management_version"];
return (rabbitVersion?.GetValue<string>() ?? "Unknown", mgmtVersion?.GetValue<string>() ?? "Unknown");
}
public override async IAsyncEnumerable<IBrokerQueue> GetQueueNames(
[EnumeratorCancellation] CancellationToken cancellationToken)
{
var page = 1;
bool morePages;
var vHosts = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase);
(string rabbitVersion, string managementVersion) = await GetRabbitDetails(false, cancellationToken);
Data["RabbitMQVersion"] = rabbitVersion;
Data["RabbitMQManagementVersionVersion"] = managementVersion;
do
{
(var queues, morePages) = await GetPage(page, cancellationToken);
if (queues != null)
{
foreach (var rabbitMQQueueDetails in queues)
{
if (rabbitMQQueueDetails.QueueName.StartsWith("nsb.delay-level-") ||
rabbitMQQueueDetails.QueueName.StartsWith("nsb.v2.delay-level-") ||
rabbitMQQueueDetails.QueueName.StartsWith("nsb.v2.verify-"))
{
continue;
}
vHosts.Add(rabbitMQQueueDetails.VHost);
await AddAdditionalQueueDetails(rabbitMQQueueDetails, cancellationToken);
yield return rabbitMQQueueDetails;
}
}
page++;
} while (morePages);
ScopeType = vHosts.Count > 1 ? "VirtualHost" : null;
}
async Task AddAdditionalQueueDetails(RabbitMQBrokerQueueDetails brokerQueue, CancellationToken cancellationToken)
{
try
{
var bindingsUrl = $"/api/queues/{HttpUtility.UrlEncode(brokerQueue.VHost)}/{HttpUtility.UrlEncode(brokerQueue.QueueName)}/bindings";
var bindings = await pipeline.ExecuteAsync(async token => await httpClient!.GetFromJsonAsync<JsonArray>(bindingsUrl, token), cancellationToken);
var conventionalBindingFound = bindings?.Any(binding => binding!["source"]?.GetValue<string>() == brokerQueue.QueueName
&& binding["vhost"]?.GetValue<string>() == brokerQueue.VHost
&& binding["destination"]?.GetValue<string>() == brokerQueue.QueueName
&& binding["destination_type"]?.GetValue<string>() == "queue"
&& binding["routing_key"]?.GetValue<string>() == string.Empty
&& binding["properties_key"]?.GetValue<string>() == "~") ?? false;
if (conventionalBindingFound)
{
brokerQueue.EndpointIndicators.Add("ConventionalTopologyBinding");
}
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// Clearly no conventional topology binding here
}
try
{
var exchangeUrl = $"/api/exchanges/{HttpUtility.UrlEncode(brokerQueue.VHost)}/{HttpUtility.UrlEncode(brokerQueue.QueueName)}/bindings/destination";
var bindings = await pipeline.ExecuteAsync(async token => await httpClient!.GetFromJsonAsync<JsonArray>(exchangeUrl, token), cancellationToken);
var delayBindingFound = bindings?.Any(binding =>
{
var source = binding!["source"]?.GetValue<string>();
return source is "nsb.v2.delay-delivery" or "nsb.delay-delivery"
&& binding["vhost"]?.GetValue<string>() == brokerQueue.VHost
&& binding["destination"]?.GetValue<string>() == brokerQueue.QueueName
&& binding["destination_type"]?.GetValue<string>() == "exchange"
&& binding["routing_key"]?.GetValue<string>() == $"#.{brokerQueue.QueueName}";
}) ?? false;
if (delayBindingFound)
{
brokerQueue.EndpointIndicators.Add("DelayBinding");
}
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// Clearly no delay binding here
}
}
public async Task<(RabbitMQBrokerQueueDetails[]?, bool morePages)> GetPage(int page, CancellationToken cancellationToken)
{
var url = $"/api/queues/{HttpUtility.UrlEncode(connectionConfiguration.VirtualHost)}?page={page}&page_size=500&name=&use_regex=false&pagination=true";
var container = await pipeline.ExecuteAsync(async token => await httpClient!.GetFromJsonAsync<JsonNode>(url, token), cancellationToken);
switch (container)
{
case JsonObject obj:
{
var pageCount = obj["page_count"]!.GetValue<int>();
var pageReturned = obj["page"]!.GetValue<int>();
if (obj["items"] is not JsonArray items)
{
return (null, false);
}
return (MaterializeQueueDetails(items), pageCount > pageReturned);
}
// Older versions of RabbitMQ API did not have paging and returned the array of items directly
case JsonArray arr:
{
return (MaterializeQueueDetails(arr), false);
}
default:
throw new Exception("Was not able to get list of queues from RabbitMQ broker.");
}
}
static RabbitMQBrokerQueueDetails[] 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 RabbitMQBrokerQueueDetails(item!.Deserialize<JsonElement>())).ToArray();
return queues;
}
public override KeyDescriptionPair[] Settings =>
[
new KeyDescriptionPair(RabbitMQSettings.API, RabbitMQSettings.APIDescription),
new KeyDescriptionPair(RabbitMQSettings.UserName, RabbitMQSettings.UserNameDescription),
new KeyDescriptionPair(RabbitMQSettings.Password, RabbitMQSettings.PasswordDescription)
];
protected override async Task<(bool Success, List<string> Errors)> TestConnectionCore(
CancellationToken cancellationToken)
{
try
{
await GetRabbitDetails(true, cancellationToken);
}
catch (HttpRequestException e)
{
throw new Exception($"Failed to connect to '{httpClient!.BaseAddress}'", e);
}
return (true, []);
}
public static class RabbitMQSettings
{
public static readonly string API = "RabbitMQ/ApiUrl";
public static readonly string APIDescription = "RabbitMQ management URL";
public static readonly string UserName = "RabbitMQ/UserName";
public static readonly string UserNameDescription = "Username to access the RabbitMQ management interface";
public static readonly string Password = "RabbitMQ/Password";
public static readonly string PasswordDescription = "Password to access the RabbitMQ management interface";
}
}