alexliesenfeld/health

How to include "dynamic" parameters in WithInfo()

Closed this issue · 7 comments

func health.WithInfo(values map[string]interface{}) health.CheckerOption
WithInfo sets values that will be available in every health check result. For example, you can use this option if you want to set information about your system that will be returned in every health check result, such as version number, Git SHA, build date, etc. These values will be available in CheckerResult.Info. If you use the default HTTP handler of this library (see NewHandler) or convert the CheckerResult to JSON on your own, these values will be available in the "info" field.

The below code is not reading uptime always. Here, uptime_since() method called only once.

health.WithInfo(map[string]any{
			"version":      "v0.0.1",
			"uptime_since": uptime_since(),
		}),

Hi. Thanks. Yes, you call uptime_since only once (when the map is constructed). Hence, it will never change. It sounds like you would like to include values that change over time?

If yes, I think we could do the following changes.

Possible Solution

We could allow configuring a function that will construct a map on demand, where values can change:

  1. Include new option health.WithInfoFunc. It could take a function as an argument, maybe func WithInfoFunc(f func () map[string]any)). The same way how values from WithInfo are used (see example code below), we would need to
    • evaluate the function provided in WithInfoFunc to get the value map
    • merge the two maps (results from WithInfo and WithInfoFunc) and
    • set the final map as the info field (see relevant code below).

A PR would be very welcome. I can give support on that.

Relevant code

This is how WithInfo is configured. We would need a new option WithInfoFunc:

health/config.go

Lines 192 to 201 in a13c015

// WithInfo sets values that will be available in every health check result. For example, you can use this option
// if you want to set information about your system that will be returned in every health check result, such as
// version number, Git SHA, build date, etc. These values will be available in CheckerResult.Info. If you use the
// default HTTP handler of this library (see NewHandler) or convert the CheckerResult to JSON on your own,
// these values will be available in the "info" field.
func WithInfo(values map[string]interface{}) CheckerOption {
return func(cfg *checkerConfig) {
cfg.info = values
}
}

This is where the result of both, WithInfo and WithInfoFunc would need to be set (Info: ck.cfg.info):

health/check.go

Line 412 in a13c015

return CheckerResult{Status: status, Details: checkResults, Info: ck.cfg.info}

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.