rcrowley / go-tigertonic

A Go framework for building JSON web services inspired by Dropwizard

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Healthchecks

bigkevmcd opened this issue · comments

It'd be nice to have a HealthCheck registry, and some sort of "healthy" or "unhealthy" output with a Handler that can be linked up...

metrics.Registry implements json.Marshaler so you can do this:

func metricsJSON(*url.URL, http.Header, interface{}) (int, http.Header, metrics.Registry, error) {
    return http.StatusOK, nil, metrics.DefaultRegistry, nil
}

Does that get you what you want?

Not quite, say we have an upstream service, Swift (OpenStack S3-alike) for example, or a database server (Mongo) or RabbitMQ, I'd register something that checked they were operational (as DropWizard does), then register the Handler at say, "/healthcheck", and when that URL was hit, I'd like to see...

Swift: OK
MongoDB: OK
RabbitMQ: Problem contacting server

Try something like this on for size:

metrics.Register(
    "MongoDB",
    metrics.NewHealthcheck(func(h metrics.Healthcheck) {
        h.Unhealthy(errors.New("MongoDB broke down"))
    }),
)
metrics.Register(
    "RabbitMQ",
    metrics.NewHealthcheck(func(h metrics.Healthcheck) {
        h.Healthy()
    }),
)
metrics.Register(
    "Swift",
    metrics.NewHealthcheck(func(h metrics.Healthcheck) {
        h.Healthy()
    }),
)
mux.Handle(
    "GET",
    "/metrics.json",
    tigertonic.Marshaled(func(*url.URL, http.Header, interface{}) (int, http.Header, metrics.Registry, error) {
        return http.StatusOK, nil, metrics.DefaultRegistry, nil
    }),
)

That will produce JSON along these lines:

{
    "MongoDB": {
        "error": "MongoDB broke down"
    },
    "RabbitMQ": {
        "error": null
    },
    "Swift": {
        "error": null
    }
}

And should you need to separate your healthchecks from other metrics, register them with your own metrics.Registry instead of with metrics.DefaultRegistry.

Does that suit or is it just that you're looking for something that looks like what you pasted?

Will do...looks good to me.

Hadn't made the connection to metrics before.

Thanks for the response :-)