Forked from RicardoRFaria/gist:9ae90d4d3ffa1edfa000be6348edc55e
Created
January 10, 2018 17:59
-
-
Save trustlix/82402b85d0a17603b3d6254bd9b1899b to your computer and use it in GitHub Desktop.
Hystrix publisher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.oobj.me.hystrix; | |
import io.prometheus.client.exporter.MetricsServlet; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.log4j.Logger; | |
import org.dom4j.Element; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.servlet.ServletContextHandler; | |
import org.eclipse.jetty.servlet.ServletHolder; | |
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; | |
import com.soundcloud.prometheus.hystrix.HystrixPrometheusMetricsPublisher; | |
/** | |
* Servidor de publicacao de metricas coletadas pelo hystrix | |
* | |
* @author Ricardo Faria | |
* | |
*/ | |
public class HystrixPublishServer { | |
private final static Logger LOG = Logger.getLogger(HystrixPublishServer.class); | |
private static final String NOME_STREAM = "/hystrix.stream"; | |
private static final String PORTA_PUBLICACAO = "porta"; | |
private static final String PROMETHEUS = "dashboard"; | |
private static final int PORTA_PADRAO = 7777; | |
/** | |
* Element utilizado para definir a inicializacao do hystrix | |
* | |
* @param element | |
*/ | |
public HystrixPublishServer(Element element) { | |
try { | |
int porta = getPorta(element); | |
boolean prometheusAtivado = isPrometheusAtivado(element); | |
if(prometheusAtivado) { | |
publicarPrometheus(); | |
} | |
inicializarServer(porta, prometheusAtivado); | |
LOG.info("Publicador de metricas do hystrix iniciado na porta " | |
+ porta + "."); | |
} catch (Exception e) { | |
LOG.error("Falha ao publicar servidor de metricas do hystrix.", e); | |
} | |
} | |
private int getPorta(Element element) { | |
int porta = PORTA_PADRAO; | |
String portaString = element.attributeValue(PORTA_PUBLICACAO); | |
if (StringUtils.isNumeric(portaString)) { | |
porta = Integer.valueOf(portaString); | |
} | |
return porta; | |
} | |
private boolean isPrometheusAtivado(Element element) { | |
boolean ativado = false; | |
String ativadoString = element.attributeValue(PROMETHEUS); | |
ativado = Boolean.parseBoolean(ativadoString); | |
return ativado; | |
} | |
private void publicarPrometheus(){ | |
HystrixPrometheusMetricsPublisher.register("bureau"); | |
} | |
private void inicializarServer(int porta, boolean prometheusAtivado) throws Exception { | |
Server server = new Server(porta); | |
ServletContextHandler context = new ServletContextHandler( | |
ServletContextHandler.NO_SESSIONS); | |
server.setHandler(context); | |
final HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet(); | |
final ServletHolder holder = new ServletHolder(servlet); | |
context.addServlet(holder, NOME_STREAM); | |
if(prometheusAtivado) { | |
context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics"); | |
} | |
server.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment