-
Notifications
You must be signed in to change notification settings - Fork 801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add health check for SendGrid #504
Conversation
src/HealthChecks.SendGrid/DependencyInjection/SendGridHealthCheckExtensions.cs
Outdated
Show resolved
Hide resolved
Let me a couple of days to review this! Thanks for contribute this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @manne
Looks good, can you address the feedback?
builder.Services.AddHttpClient(registrationName); | ||
|
||
return builder.Add(new HealthCheckRegistration( | ||
registrationName, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use name ?? NAME instead creating registrationName variable on the others healthchecks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @unaizorrilla I do not understand you.
variable on the others healthchecks
do you mean?
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
Then I have to do this builder.Services.AddHttpClient(name ?? NAME);
. Or do this builder.Services.AddHttpClient();
?
Or did I miss something else? Did not understand the
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeap, don't see this variable is used on both places! is ok!
private readonly string _apiKey; | ||
private readonly Func<HttpClient> _httpClientFactory; | ||
|
||
public SendGridHealthCheck(string apiKey, Func<HttpClient> httpClientFactory) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know others healthcheck's like ( uris ) using this pattern, but probably inject Ihttpclientfactory is more natural here!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How should I invoke _httpClientFactory.CreateClient()
A. with a name or
B. without a name
If A. which value?
I) The name of the healthcheck passed in with AddSendGrid(...)
II) A name specific for the SendGridHealthCheck
class (as in
AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.UI/Core/Notifications/WebHookFailureNotifier.cs
Line 35 in 19bb7ed
_httpClient = httpClientFactory.CreateClient(Keys.HEALTH_CHECK_WEBHOOK_HTTP_CLIENT_NAME); |
var response = await client.SendEmailAsync(msg, cancellationToken); | ||
|
||
HealthCheckResult result; | ||
var isHealthy = response.StatusCode == HttpStatusCode.OK; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is more natural, if ( response.IsSuccessStatusCode ) instead this comparasion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you reduce if/else
if (!response.IsSuccessStatusCode)
{
return new HealthCheckResult( ..... )
}
return HealthCheckResult.Healty()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this can not be done, because await client.SendEmailAsync(msg, cancellationToken)
returns the type Task<Response>
and Response
is a SendGrip C# specific type.
Also the SendGrid documentation explicitly states that a 200
is returned.
Whereas IsSuccessStatusCode
is
public bool IsSuccessStatusCode
{
get { return ((int)_statusCode >= 200) && ((int)_statusCode <= 299); }
}
Reference dotnet/runtime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsSuccessStatusCode include 200 and all 200-299 are Success response, I think is more natural!
I will merge this weekend! Looks good ! |
Merged with minor changes on healthcheck!! When release build finished a new package 3.1.1-preview1 will be available for sendgrid. If package is working fine we can move version to release! |
What this PR does / why we need it:
Adds an health check for SendGrid
Which issue(s) this PR fixes:
none
Special notes for your reviewer:
The health check utilizes the sandbox mode with a valid email.
The email is actually not sent to the recipient. The email request is sent to a SendGrid server, there the email is being validated.
An excerpt from the linked sandbox documentation.
The validation also implies that the API key is correct and email can generally being delivered (e.g there is at least one sender identity). And of course SendGrid services are available.
Does this PR introduce a user-facing change?:
no
Please make sure you've completed the relevant tasks for this PR, out of the following list: