How to configure a JSON HealthCheckResultRenderer

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How to configure a JSON HealthCheckResultRenderer

Jhoare
Hi, could anyone give me a steer on how to configure the HealthCheckHandler to return a JSON response?

Here is how I have registered the Module

private void registerModules(BindingsSpec bindingsSpec) {

        Map dataSourceProperties = ImmutableMap.of("URL", "jdbc:h2:mem:dev");
        List<Module> modules = ImmutableList.of((new CodaHaleMetricsModule().healthChecks().jmx().jvmMetrics().metrics()),
                new CustomerModule(), new JacksonModule(), new H2Module(), new HikariModule(dataSourceProperties, "org.h2.jdbcx.JdbcDataSource"), new HandlebarsModule());
        bindingsSpec.add(modules);


    }


And here is my Handler code.

  handler("health-check/:name?", context -> context.render(json(new HealthCheckHandler())));

Thanks

James
Reply | Threaded
Open this post in threaded view
|

Re: How to configure a JSON HealthCheckResultRenderer

Luke Daley
Administrator
You bind a Renderer<HealthCheckResults> and Renderer<HealthCheck.Result>. 

See: https://github.com/ratpack/ratpack/blob/master/ratpack-codahale-metrics/src/test/groovy/ratpack/codahale/metrics/HealthchecksSpec.groovy#L122-122 

We don’t include this because it would couple the metrics module to the Jackson module.
rus
Reply | Threaded
Open this post in threaded view
|

Re: How to configure a JSON HealthCheckResultRenderer

rus
In reply to this post by Jhoare
You would need to add your own renderers for HealthCheckResults and HealthCheck and just use the handler like normal.  I think they would override the default one's provdied by Ratpack.

I don't think there's any reason the default one's shouldn't be producing json in the first place.  Maybe we need a ticket for this.

If you need any more help doing this let me know and I'll set up an example.

rus
Reply | Threaded
Open this post in threaded view
|

Re: How to configure a JSON HealthCheckResultRenderer

rus
Sorry, must have been writing the reply same time as Luke :)

Luke - the metrics module is already dependant on Jackson (not our module) for rendering the metrics in Json.  So we should be able to do the same for the health checks
Reply | Threaded
Open this post in threaded view
|

Re: How to configure a JSON HealthCheckResultRenderer

Jhoare
In reply to this post by Luke Daley
Thanks, so bind:

private void registerModules(BindingsSpec bindingsSpec) {

        Map dataSourceProperties = ImmutableMap.of("URL", "jdbc:h2:mem:dev");
        List<Module> modules = ImmutableList.of((new CodaHaleMetricsModule()
                        .healthChecks().jmx().jvmMetrics().metrics()),
                new CustomerModule(), new JacksonModule(), new H2Module(), new HikariModule(dataSourceProperties, "org.h2.jdbcx.JdbcDataSource"), new HandlebarsModule());
        bindingsSpec.bind(new HealthCheckJsonRenderer());
        bindingsSpec.bind(new HealthChecksJsonRenderer());


        bindingsSpec.add(modules);


    }

Declare

static class HealthChecksJsonRenderer extends RendererSupport<HealthCheckResults> {
        @Override
        public void render(Context context, HealthCheckResults object) throws Exception {
            context.render(json(object.getResults()));
        }
    }

    static class HealthCheckJsonRenderer extends RendererSupport<HealthCheck.Result> {
        @Override
        public void render(Context context, HealthCheck.Result object) throws Exception {
            context.render(json(object));
        }
    }

And

handler("health-check/:name?", new HealthCheckHandler());


Can HealthCheckHandling run Async?

Cheers

James
rus
Reply | Threaded
Open this post in threaded view
|

Re: How to configure a JSON HealthCheckResultRenderer

rus
Jhoare wrote
Can HealthCheckHandling run Async?
 
What has been suggested before is that we run health checks on a non request thread to allow them to block.  I'll create a ticket for this.
Reply | Threaded
Open this post in threaded view
|

Re: How to configure a JSON HealthCheckResultRenderer

Luke Daley
Administrator
The Metrics API doesn’t support asynchronous metrics. 

We are still working out what to do here. Most drastic scenario is that we have to create our own async friendly API on top of the metrics that the coda hale library provides
rus
Reply | Threaded
Open this post in threaded view
|

Re: How to configure a JSON HealthCheckResultRenderer

rus
In reply to this post by Jhoare
This is now available in 0.9.11