All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.helixservice.feature.metrics.MetricsController Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
/*
 *  Copyright (c) 2016 Les Novell
 *  ------------------------------------------------------
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *
 *      The Apache License v2.0 is available at
 *      http://www.opensource.org/licenses/apache2.0.php
 *
 */

/*
 * @author Les Novell
 *
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *
 *      The Apache License v2.0 is available at
 *      http://www.opensource.org/licenses/apache2.0.php
 *
 */

package io.helixservice.feature.metrics;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import io.helixservice.feature.restservice.controller.HttpMethod;
import io.helixservice.feature.restservice.controller.Request;
import io.helixservice.feature.restservice.controller.Response;
import io.helixservice.feature.restservice.controller.annotation.Controller;
import io.helixservice.feature.restservice.controller.annotation.Endpoint;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

/**
 * Supports HTTP request for metrics snapshot
 */
@Controller
public class MetricsController {
    private MetricRegistry metricRegistry;

    public MetricsController(MetricRegistry metricRegistry) {
        this.metricRegistry = metricRegistry;
    }

    @Endpoint(value = "/metrics", methods = HttpMethod.GET)
    public Response getConsoleMetrics(Request request) {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(byteArrayOutputStream);
        ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).outputTo(printStream).build();
        consoleReporter.report();

        return Response.successResponse(byteArrayOutputStream.toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy