com.aol.micro.server.event.metrics.MetricsCatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micro-event-metrics Show documentation
Show all versions of micro-event-metrics Show documentation
Opinionated rest microservices
package com.aol.micro.server.event.metrics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.aol.micro.server.errors.ErrorCode;
import com.aol.micro.server.events.JobCompleteEvent;
import com.aol.micro.server.events.JobStartEvent;
import com.aol.micro.server.events.RequestTypes.AddQuery;
import com.aol.micro.server.events.RequestTypes.RemoveQuery;
import com.aol.micro.server.events.RequestTypes.RequestData;
import com.aol.micro.server.events.SystemData;
import com.aol.micro.server.health.ErrorEvent;
import com.codahale.metrics.MetricRegistry;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@Component
public class MetricsCatcher {
private final MetricRegistry registry;
private final TimerManager queries;
private final TimerManager jobs;
private final Configuration configuration;
private final String prefix;
@Autowired
public MetricsCatcher(MetricRegistry registry, EventBus bus, Configuration configuration) {
this.prefix = configuration.getPrefix();
this.registry = registry;
bus.register(this);
queries = new TimerManager(
configuration.getNumQueries(), configuration.getHoldQueriesForMinutes());
jobs = new TimerManager(
configuration.getNumJobs(), configuration.getHoldJobsForMinutes());
this.configuration = configuration;
}
@Subscribe
public void requestStart(AddQuery data) {
registry.meter(prefix + ".requests-started")
.mark();
registry.counter(prefix + ".requests-started-count")
.inc();
if (this.configuration.isQueriesByType()) {
RequestData rd = data.getData();
registry.meter(queryStartName(rd) + "-meter")
.mark();
queries.start(rd.getCorrelationId(), registry.timer(queryEndName(rd) + "-timer")
.time());
registry.counter(prefix + ".requests-active-" + rd.getType() + "-count")
.inc();
}
}
private String queryStartName(RequestData rd) {
return prefix + ".request-start-" + rd.getType();
}
private String queryEndName(RequestData rd) {
return prefix + ".request-end-" + rd.getType();
}
@Subscribe
public void requestComplete(RemoveQuery data) {
registry.meter(prefix + ".requests-completed")
.mark();
registry.counter(prefix + ".requests-completed-count")
.inc();
if (this.configuration.isQueriesByType()) {
RequestData rd = data.getData();
registry.meter(queryEndName(rd))
.mark();
queries.complete(rd.getCorrelationId());
registry.counter(prefix + ".requests-active-" + rd.getType() + "-count")
.dec();
}
}
@Subscribe
public void finished(SystemData data) {
registry.meter(prefix + ".jobs-completed")
.mark();
registry.counter(prefix + ".jobs-completed-count")
.inc();
}
@Subscribe
public void jobStarted(JobStartEvent data) {
if (this.configuration.isJobsByType()) {
registry.meter(prefix + ".job-meter-" + data.getType())
.mark();
jobs.start(data.getCorrelationId(), registry.timer(prefix + ".job-timer-" + data.getType())
.time());
registry.counter(prefix + ".jobs-active-" + data.getType() + "-count")
.inc();
}
}
@Subscribe
public void jobComplete(JobCompleteEvent data) {
if (this.configuration.isJobsByType()) {
jobs.complete(data.getCorrelationId());
registry.counter(prefix + ".jobs-active-" + data.getType() + "-count")
.dec();
registry.counter(prefix + ".jobs-processed-" + data.getType() + "-count-data")
.inc(data.getDataSize());
registry.meter(prefix + ".jobs-processed-" + data.getType() + "-meter-data")
.mark(data.getDataSize());
registry.counter(prefix + ".jobs-errors-" + data.getType() + "-count-data")
.inc(data.getErrors());
registry.meter(prefix + ".jobs-errors-" + data.getType() + "-meter-data")
.mark(data.getErrors());
if (data.getErrors() > 0l) {
registry.counter(prefix + ".jobs-succeeded-" + data.getType() + "-count")
.inc();
registry.meter(prefix + ".jobs-succeeded-" + data.getType() + "-meter")
.mark();
} else {
registry.counter(prefix + ".jobs-failed-" + data.getType() + "-count")
.inc();
registry.meter(prefix + ".jobs-failed-" + data.getType() + "-meter")
.mark();
}
}
}
@Subscribe
public void error(ErrorCode c) {
registry.meter(prefix + ".errors")
.mark();
registry.counter(prefix + ".errors-count")
.inc();
if (this.configuration.isErrorsByCode()) {
registry.meter(name(c))
.mark();
registry.counter(name(c) + "-count")
.inc();
}
if (this.configuration.isErrorsByType()) {
registry.meter(prefix + ".error-severity-" + c.getSeverity()
.name())
.mark();
registry.counter(prefix + ".error-severity-" + c.getSeverity()
.name()
+ "-count")
.inc();
}
}
@Subscribe
public void error(ErrorEvent event) {
ErrorCode c = event.getCode();
error(c);
}
private String name(ErrorCode c) {
return prefix + ".error-" + c.getSeverity() + "-" + c.getErrorId();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy