Skip to content

Conversation

jbro
Copy link
Contributor

@jbro jbro commented Aug 15, 2024

Changes

Added the field Body to the ActiveHealthChecks struct, which optionally sets the body of the request used for the health check requests.

Also added Caddyfile support for the same option under the name health_request_body.

Rational

After adding the option to setting the HTTP method used for active health checks it turns out it's also helpful to set the body. Eg when using the POST method on a JSON RPC service with an empty body may return a 400 Bad Request, instead of a 200 OK. Being able to set the body to a valid NOOP RPC call enables a "deeper" health check, than just accepting the 400 status.

Comment on lines 403 to 412
// if body is provided, create a reader for it, otherwise nil
var requestBody io.Reader
if h.HealthChecks.Active.Body != "" {
requestBody = strings.NewReader(h.HealthChecks.Active.Body)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to run it through the replacer 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Though in reality it would only let {env.*} and {file.*} work, not much else. There's no request context available here in active health checks because they happen in their own goroutines.

Copy link
Contributor Author

@jbro jbro Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah I did think about doing that, but then I guess I forgot all about it again :-)

I'll give it go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've add a replacer to the body in my latest commit. I've moved the initialization of replacer used for the headers up, and reused that. Is that a good solution ?

@francislavoie francislavoie added the feature ⚙️ New feature or request label Aug 15, 2024
@francislavoie francislavoie changed the title Add option to specify the body used for active health checks reverseproxy: Active health checks request body option Aug 15, 2024
@jbro jbro force-pushed the activehealth-check-request-body branch 2 times, most recently from 992108c to a2fd07a Compare August 19, 2024 13:40
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this!

It feels a little weird to set a body without a Content-Type, but maybe we can see if anyone needs that feature and then add a way to customize Headers. (Honestly it's starting to feel a lot like the respond directive, i.e. the static_response middleware, and I wonder if we would benefit from just using that instead. But that's a matter for another discussion.)

@francislavoie
Copy link
Member

francislavoie commented Aug 19, 2024

@mholt we have health_headers already, actually. Users setting a body can (and likely should) also set Content-Type that way.

@jbro
Copy link
Contributor Author

jbro commented Aug 19, 2024

It feels a little weird to set a body without a Content-Type, but maybe we can see if anyone needs that feature and then add a way to customize Headers. (Honestly it's starting to feel a lot like the respond directive, i.e. the static_response middleware, and I wonder if we would benefit from just using that instead. But that's a matter for another discussion.)

Hmm that's a very good point, and you are probably right that in a weeks time I'll probably find out that I need to set a Content-Type, luckily there is already an option to add headers to the request :-)

I just checked http.Request, and I don't think anything of value can't be set after this PR is merged. I mean the protocol version is not something I usually touch, and we probably don't want to explicitly set the content length either. The only maybe could be trailers?

@jbro jbro force-pushed the activehealth-check-request-body branch from a2fd07a to fdf435f Compare August 19, 2024 16:52
@mholt
Copy link
Member

mholt commented Aug 19, 2024

@francislavoie Ah you're right, I thought that was for headers to check for healthy status, but it seems we are using that to set them.

@mholt mholt merged commit 54a0c8f into caddyserver:master Aug 19, 2024
23 checks passed
@jbro jbro deleted the activehealth-check-request-body branch August 19, 2024 16:59
@ngotchac
Copy link

Hi, just a heads-up, this is not in the current docs, nor is health_method.

@mholt
Copy link
Member

mholt commented May 12, 2025

Ah thanks, @ngotchac -- been a bit busy lately; would either @jbro or you be available to send a quick PR to https://github.com/caddyserver/website?

@jbro
Copy link
Contributor Author

jbro commented May 12, 2025

@mholt sure I can do that. I take it that it's just this file that needs updating ?

@mholt
Copy link
Member

mholt commented May 12, 2025

Yep! Thanks

@jbro
Copy link
Contributor Author

jbro commented May 12, 2025

PR here caddyserver/website#470

@ngotchac
Copy link

Thanks for the prompt reply! You beat me to it @jbro , I was about to open the PR 😛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants