use of io.dropwizard.metrics.MetricName in project light-4j by networknt.
the class MetricRegistryTest method registersMultipleMetricsWithAPrefix.
@Test
public void registersMultipleMetricsWithAPrefix() throws Exception {
final MetricName myCounter = MetricName.build("my.counter");
final MetricName myGauge = MetricName.build("my.gauge");
final MetricSet metrics = () -> {
final Map<MetricName, Metric> metrics1 = new HashMap<>();
metrics1.put(GAUGE, gauge);
metrics1.put(COUNTER, counter);
return metrics1;
};
registry.register("my", metrics);
assertThat(registry.getNames()).containsOnly(myGauge, myCounter);
}
use of io.dropwizard.metrics.MetricName in project light-4j by networknt.
the class InfluxDbReporter method report.
@Override
public void report(final SortedMap<MetricName, Gauge> gauges, final SortedMap<MetricName, Counter> counters, final SortedMap<MetricName, Histogram> histograms, final SortedMap<MetricName, Meter> meters, final SortedMap<MetricName, Timer> timers) {
final long now = System.currentTimeMillis();
if (logger.isDebugEnabled())
logger.debug("InfluxDbReporter report is called with counter size " + counters.size());
try {
influxDb.flush();
for (Map.Entry<MetricName, Gauge> entry : gauges.entrySet()) {
reportGauge(entry.getKey(), entry.getValue(), now);
}
for (Map.Entry<MetricName, Counter> entry : counters.entrySet()) {
reportCounter(entry.getKey(), entry.getValue(), now);
}
for (Map.Entry<MetricName, Histogram> entry : histograms.entrySet()) {
reportHistogram(entry.getKey(), entry.getValue(), now);
}
for (Map.Entry<MetricName, Meter> entry : meters.entrySet()) {
reportMeter(entry.getKey(), entry.getValue(), now);
}
for (Map.Entry<MetricName, Timer> entry : timers.entrySet()) {
reportTimer(entry.getKey(), entry.getValue(), now);
}
if (influxDb.hasSeriesData()) {
influxDb.writeData();
}
// reset counters
for (Map.Entry<MetricName, Counter> entry : counters.entrySet()) {
Counter counter = entry.getValue();
long count = counter.getCount();
counter.dec(count);
}
} catch (Exception e) {
logger.error("Unable to report to InfluxDB. Discarding data.", e);
}
}
use of io.dropwizard.metrics.MetricName in project light-4j by networknt.
the class MetricsHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
long startTime = Clock.defaultClock().getTick();
exchange.addExchangeCompleteListener((exchange1, nextListener) -> {
Map<String, Object> auditInfo = exchange1.getAttachment(AuditHandler.AUDIT_INFO);
if (auditInfo != null) {
Map<String, String> tags = new HashMap<>();
tags.put("endpoint", (String) auditInfo.get(Constants.ENDPOINT_STRING));
tags.put("clientId", auditInfo.get(Constants.CLIENT_ID_STRING) != null ? (String) auditInfo.get(Constants.CLIENT_ID_STRING) : "unknown");
long time = Clock.defaultClock().getTick() - startTime;
MetricName metricName = new MetricName("response_time");
metricName = metricName.tagged(commonTags);
metricName = metricName.tagged(tags);
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.TIMERS).update(time, TimeUnit.NANOSECONDS);
incCounterForStatusCode(exchange1.getStatusCode(), commonTags, tags);
}
nextListener.proceed();
});
next.handleRequest(exchange);
}
use of io.dropwizard.metrics.MetricName in project light-4j by networknt.
the class MetricsHandler method incCounterForStatusCode.
private void incCounterForStatusCode(int statusCode, Map<String, String> commonTags, Map<String, String> tags) {
MetricName metricName = new MetricName("request").tagged(commonTags).tagged(tags);
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.COUNTERS).inc();
if (statusCode >= 200 && statusCode < 400) {
metricName = new MetricName("success").tagged(commonTags).tagged(tags);
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.COUNTERS).inc();
} else if (statusCode == 401 || statusCode == 403) {
metricName = new MetricName("auth_error").tagged(commonTags).tagged(tags);
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.COUNTERS).inc();
} else if (statusCode >= 400 && statusCode < 500) {
metricName = new MetricName("request_error").tagged(commonTags).tagged(tags);
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.COUNTERS).inc();
} else if (statusCode >= 500) {
metricName = new MetricName("server_error").tagged(commonTags).tagged(tags);
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.COUNTERS).inc();
}
}
use of io.dropwizard.metrics.MetricName in project light-4j by networknt.
the class MetricRegistryTest method removesMetricsMatchingAFilter.
@Test
public void removesMetricsMatchingAFilter() throws Exception {
final MetricName timer1 = MetricName.build("timer-1");
final MetricName timer2 = MetricName.build("timer-2");
final MetricName histogram1 = MetricName.build("histogram-1");
registry.timer(timer1);
registry.timer(timer2);
registry.histogram(histogram1);
assertThat(registry.getNames()).contains(timer1, timer2, histogram1);
registry.removeMatching((name, metric) -> name.getKey().endsWith("1"));
assertThat(registry.getNames()).doesNotContain(timer1, histogram1);
assertThat(registry.getNames()).contains(timer2);
verify(listener).onTimerRemoved(timer1);
verify(listener).onHistogramRemoved(histogram1);
}
Aggregations