Search in sources :

Example 21 with TaggedMetricName

use of com.wavefront.common.TaggedMetricName in project java by wavefrontHQ.

the class HealthCheckManagerImpl method getHealthCheckResponse.

@Override
public HttpResponse getHealthCheckResponse(ChannelHandlerContext ctx, @Nonnull FullHttpRequest request) throws URISyntaxException {
    int port = ((InetSocketAddress) ctx.channel().localAddress()).getPort();
    if (!enabledPorts.contains(port))
        return null;
    URI uri = new URI(request.uri());
    if (!(this.path == null || this.path.equals(uri.getPath())))
        return null;
    // it is a health check URL, now we need to determine current status and respond accordingly
    final boolean ok = isHealthy(port);
    Metrics.newGauge(new TaggedMetricName("listeners", "healthcheck.status", "port", String.valueOf(port)), new Gauge<Integer>() {

        @Override
        public Integer value() {
            return isHealthy(port) ? 1 : 0;
        }
    });
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(ok ? passStatusCode : failStatusCode), Unpooled.copiedBuffer(ok ? passResponseBody : failResponseBody, CharsetUtil.UTF_8));
    if (contentType != null) {
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
    }
    if (HttpUtil.isKeepAlive(request)) {
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    Metrics.newCounter(new TaggedMetricName("listeners", "healthcheck.httpstatus." + (ok ? passStatusCode : failStatusCode) + ".count", "port", String.valueOf(port))).inc();
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) InetSocketAddress(java.net.InetSocketAddress) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) URI(java.net.URI) TaggedMetricName(com.wavefront.common.TaggedMetricName)

Aggregations

TaggedMetricName (com.wavefront.common.TaggedMetricName)21 IOException (java.io.IOException)6 Test (org.junit.Test)5 Counter (com.yammer.metrics.core.Counter)4 MetricName (com.yammer.metrics.core.MetricName)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Histogram (com.yammer.metrics.core.Histogram)2 WavefrontHistogram (com.yammer.metrics.core.WavefrontHistogram)2 URI (java.net.URI)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ImmutableList (com.google.common.collect.ImmutableList)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 CachingHostnameLookupResolver (com.wavefront.agent.channel.CachingHostnameLookupResolver)1 ChannelUtils.writeHttpResponse (com.wavefront.agent.channel.ChannelUtils.writeHttpResponse)1 ConnectionTrackingHandler (com.wavefront.agent.channel.ConnectionTrackingHandler)1 HealthCheckManagerImpl (com.wavefront.agent.channel.HealthCheckManagerImpl)1