Skip to content
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

Merged
merged 5 commits into from
Apr 30, 2020
Merged

Add health check for SendGrid #504

merged 5 commits into from
Apr 30, 2020

Conversation

manne
Copy link
Contributor

@manne manne commented Apr 25, 2020

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.

Sandbox mode is an optional parameter within mail_settings. Enabling sandbox mode allows you to send a test email to ensure that your request body is formatted correctly without delivering the email to any of your recipients.
When making a request with sandbox mode enabled, we will validate the form, type, and shape of your request. In other words, sandbox mode will validate each parameter you include and the structure of your JSON payload. [...]

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:

  • Code compiles correctly
  • Created/updated tests
  • Unit tests passing
  • End-to-end tests passing
    • not all on my machine.
  • Extended the documentation
    • I could not find a place where to add the documentation. The unit tests should be sufficient ;)
  • Provided sample for the feature
    • There are two unit tests

@unaizorrilla
Copy link
Collaborator

Let me a couple of days to review this!

Thanks for contribute this

Copy link
Collaborator

@unaizorrilla unaizorrilla left a 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,
Copy link
Collaborator

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

Copy link
Contributor Author

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

Copy link
Collaborator

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)
Copy link
Collaborator

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!!

Copy link
Contributor Author

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

_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;
Copy link
Collaborator

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

Copy link
Collaborator

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()

Copy link
Contributor Author

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

Copy link
Collaborator

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!

@unaizorrilla
Copy link
Collaborator

I will merge this weekend! Looks good !

@unaizorrilla unaizorrilla merged commit bcb62ea into Xabaril:master Apr 30, 2020
@unaizorrilla
Copy link
Collaborator

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!

@manne manne deleted the sendgrid branch April 30, 2020 17:29
@sungam3r sungam3r mentioned this pull request Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants