forked from dotnet/aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAspireRabbitMQExtensions.cs
261 lines (230 loc) · 11.7 KB
/
AspireRabbitMQExtensions.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Net.Sockets;
using Aspire;
using Aspire.RabbitMQ.Client;
using HealthChecks.RabbitMQ;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;
namespace Microsoft.Extensions.Hosting;
/// <summary>
/// Extension methods for connecting to a RabbitMQ message broker.
/// </summary>
public static class AspireRabbitMQExtensions
{
private const string ActivitySourceName = "Aspire.RabbitMQ.Client";
private static readonly ActivitySource s_activitySource = new ActivitySource(ActivitySourceName);
private const string DefaultConfigSectionName = "Aspire:RabbitMQ:Client";
/// <summary>
/// Registers <see cref="IConnection"/> as a singleton in the services provided by the <paramref name="builder"/>.
/// Enables retries, corresponding health check, logging, and telemetry.
/// </summary>
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
/// <param name="connectionName">A name used to retrieve the connection string from the ConnectionStrings configuration section.</param>
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="RabbitMQClientSettings"/>. It's invoked after the settings are read from the configuration.</param>
/// <param name="configureConnectionFactory">An optional method that can be used for customizing the <see cref="ConnectionFactory"/>. It's invoked after the options are read from the configuration.</param>
/// <remarks>Reads the configuration from "Aspire:RabbitMQ:Client" section.</remarks>
public static void AddRabbitMQClient(
this IHostApplicationBuilder builder,
string connectionName,
Action<RabbitMQClientSettings>? configureSettings = null,
Action<ConnectionFactory>? configureConnectionFactory = null)
=> AddRabbitMQClient(builder, configureSettings, configureConnectionFactory, connectionName, serviceKey: null);
/// <summary>
/// Registers <see cref="IConnection"/> as a keyed singleton for the given <paramref name="name"/> in the services provided by the <paramref name="builder"/>.
/// Enables retries, corresponding health check, logging, and telemetry.
/// </summary>
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
/// <param name="name">The name of the component, which is used as the <see cref="ServiceDescriptor.ServiceKey"/> of the service and also to retrieve the connection string from the ConnectionStrings configuration section.</param>
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="RabbitMQClientSettings"/>. It's invoked after the settings are read from the configuration.</param>
/// <param name="configureConnectionFactory">An optional method that can be used for customizing the <see cref="ConnectionFactory"/>. It's invoked after the options are read from the configuration.</param>
/// <remarks>Reads the configuration from "Aspire:RabbitMQ:Client:{name}" section.</remarks>
public static void AddKeyedRabbitMQClient(
this IHostApplicationBuilder builder,
string name,
Action<RabbitMQClientSettings>? configureSettings = null,
Action<ConnectionFactory>? configureConnectionFactory = null)
{
ArgumentException.ThrowIfNullOrEmpty(name);
AddRabbitMQClient(builder, configureSettings, configureConnectionFactory, connectionName: name, serviceKey: name);
}
private static void AddRabbitMQClient(
IHostApplicationBuilder builder,
Action<RabbitMQClientSettings>? configureSettings,
Action<ConnectionFactory>? configureConnectionFactory,
string connectionName,
object? serviceKey)
{
ArgumentNullException.ThrowIfNull(builder);
var configSection = builder.Configuration.GetSection(DefaultConfigSectionName);
var namedConfigSection = configSection.GetSection(connectionName);
var settings = new RabbitMQClientSettings();
configSection.Bind(settings);
namedConfigSection.Bind(settings);
if (builder.Configuration.GetConnectionString(connectionName) is string connectionString)
{
settings.ConnectionString = connectionString;
}
configureSettings?.Invoke(settings);
IConnectionFactory CreateConnectionFactory(IServiceProvider sp)
{
// ensure the log forwarder is initialized
sp.GetRequiredService<RabbitMQEventSourceLogForwarder>().Start();
var factory = new ConnectionFactory();
var configurationOptionsSection = configSection.GetSection("ConnectionFactory");
var namedConfigurationOptionsSection = namedConfigSection.GetSection("ConnectionFactory");
configurationOptionsSection.Bind(factory);
namedConfigurationOptionsSection.Bind(factory);
// the connection string from settings should win over the one from the ConnectionFactory section
var connectionString = settings.ConnectionString;
if (!string.IsNullOrEmpty(connectionString))
{
factory.Uri = new(connectionString);
}
configureConnectionFactory?.Invoke(factory);
return factory;
}
if (serviceKey is null)
{
builder.Services.AddSingleton<IConnectionFactory>(CreateConnectionFactory);
builder.Services.AddSingleton<IConnection>(sp => CreateConnection(sp.GetRequiredService<IConnectionFactory>(), settings.MaxConnectRetryCount));
}
else
{
builder.Services.AddKeyedSingleton<IConnectionFactory>(serviceKey, (sp, _) => CreateConnectionFactory(sp));
builder.Services.AddKeyedSingleton<IConnection>(serviceKey, (sp, key) => CreateConnection(sp.GetRequiredKeyedService<IConnectionFactory>(key), settings.MaxConnectRetryCount));
}
builder.Services.AddSingleton<RabbitMQEventSourceLogForwarder>();
if (!settings.DisableTracing)
{
builder.Services.AddOpenTelemetry()
.WithTracing(traceBuilder =>
traceBuilder
.AddSource(ActivitySourceName)
#if RABBITMQ_V6
// Note that RabbitMQ.Client v6.x doesn't have built-in support for tracing. See https://github.com/rabbitmq/rabbitmq-dotnet-client/pull/1261
#else
.AddSource("RabbitMQ.Client.*")
#endif
);
}
if (!settings.DisableHealthChecks)
{
builder.TryAddHealthCheck(new HealthCheckRegistration(
serviceKey is null ? "RabbitMQ.Client" : $"RabbitMQ.Client_{connectionName}",
sp =>
{
try
{
// if the IConnection can't be resolved, make a health check that will fail
var connection = serviceKey is null ? sp.GetRequiredService<IConnection>() : sp.GetRequiredKeyedService<IConnection>(serviceKey);
#if RABBITMQ_V6
var options = new RabbitMQHealthCheckOptions();
options.Connection = connection;
return new RabbitMQHealthCheck(options);
#else
return new RabbitMQHealthCheck(connection);
#endif
}
catch (Exception ex)
{
return new FailedHealthCheck(ex);
}
},
failureStatus: default,
tags: default));
}
}
private sealed class FailedHealthCheck(Exception ex) : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
}
}
private static IConnection CreateConnection(IConnectionFactory factory, int retryCount)
{
var resiliencePipelineBuilder = new ResiliencePipelineBuilder();
if (retryCount > 0)
{
resiliencePipelineBuilder.AddRetry(new RetryStrategyOptions
{
ShouldHandle = static args => args.Outcome is { Exception: SocketException or BrokerUnreachableException }
? PredicateResult.True()
: PredicateResult.False(),
BackoffType = DelayBackoffType.Exponential,
MaxRetryAttempts = retryCount,
Delay = TimeSpan.FromSeconds(1),
});
}
var resiliencePipeline = resiliencePipelineBuilder.Build();
using var activity = s_activitySource.StartActivity("rabbitmq connect", ActivityKind.Client);
AddRabbitMQTags(activity, factory.Uri);
#if RABBITMQ_V6
return resiliencePipeline.Execute(static factory =>
{
using var connectAttemptActivity = s_activitySource.StartActivity("rabbitmq connect attempt", ActivityKind.Client);
AddRabbitMQTags(connectAttemptActivity, factory.Uri, "connect");
try
{
return factory.CreateConnection();
}
catch (Exception ex)
{
AddRabbitMQExceptionTags(connectAttemptActivity, ex);
throw;
}
}, factory);
#else
return resiliencePipeline.ExecuteAsync(static async (factory, cancellationToken) =>
{
using var connectAttemptActivity = s_activitySource.StartActivity("rabbitmq connect attempt", ActivityKind.Client);
AddRabbitMQTags(connectAttemptActivity, factory.Uri, "connect");
try
{
return await factory.CreateConnectionAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
AddRabbitMQExceptionTags(connectAttemptActivity, ex);
throw;
}
}, factory).AsTask().GetAwaiter().GetResult(); // see https://github.com/dotnet/aspire/issues/565
#endif
}
private static void AddRabbitMQTags(Activity? activity, Uri address, string? operation = null)
{
if (activity is null)
{
return;
}
activity.AddTag("server.address", address.Host);
activity.AddTag("server.port", address.Port);
activity.AddTag("messaging.system", "rabbitmq");
if (operation is not null)
{
activity.AddTag("messaging.operation", operation);
}
}
private static void AddRabbitMQExceptionTags(Activity? connectAttemptActivity, Exception ex)
{
if (connectAttemptActivity is null)
{
return;
}
connectAttemptActivity.AddTag("exception.message", ex.Message);
// Note that "exception.stacktrace" is the full exception detail, not just the StackTrace property.
// See https://opentelemetry.io/docs/specs/semconv/attributes-registry/exception/
// and https://github.com/open-telemetry/opentelemetry-specification/pull/697#discussion_r453662519
connectAttemptActivity.AddTag("exception.stacktrace", ex.ToString());
connectAttemptActivity.AddTag("exception.type", ex.GetType().FullName);
connectAttemptActivity.SetStatus(ActivityStatusCode.Error);
}
}