@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { SimpleTimer respTimer = new SimpleTimer(); 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"); List<String> labels = new ArrayList<>(tags.keySet()); List<String> labelValues = new ArrayList<>(tags.values()); summary(RERSPONSE_TIME_SECOND, labels).labels(labelValues.stream().toArray(String[]::new)).observe(respTimer.elapsedSeconds()); incCounterForStatusCode(exchange1.getStatusCode(), labels, labelValues); } nextListener.proceed(); }); Handler.next(exchange, next); }
@Override public List<MetricFamilySamples> collect() { List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(); for(Map.Entry<List<String>, Child> c: children.entrySet()) { Child.Value v = c.getValue().get(); List<String> labelNamesWithQuantile = new ArrayList<String>(labelNames); labelNamesWithQuantile.add("quantile"); for(Map.Entry<Double, Double> q : v.quantiles.entrySet()) { List<String> labelValuesWithQuantile = new ArrayList<String>(c.getKey()); labelValuesWithQuantile.add(doubleToGoString(q.getKey())); samples.add(new MetricFamilySamples.Sample(fullname, labelNamesWithQuantile, labelValuesWithQuantile, q.getValue())); } samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count)); samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum)); } return familySamplesList(Type.SUMMARY, samples); }
/** * Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run. * * @param timeable Code that is being timed * @return Result returned by callable. */ public <E> E time(Callable<E> timeable) { Timer timer = startTimer(); try { return timeable.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { timer.observeDuration(); } }
samplerCollector.labels(samplerLabelValues).observe(event.getResult().getTime()); samplerElaspedTimeCollector.labels(samplerLabelValues).observe(event.getResult().getTime() / 1000.0); samplerLatencyCollector.labels(samplerLabelValues).observe(event.getResult().getLatency() / 1000.0); samplerIdleTimeCollector.labels(samplerLabelValues).observe(event.getResult().getIdleTime() / 1000.0); samplerConnectTimeCollector.labels(samplerLabelValues).observe(event.getResult().getConnectTime() / 1000.0); ((Summary) assertionsCollector).labels(assertionsLabelValues).observe(event.getResult().getTime()); else if (assertionsCollector instanceof Counter) ((Counter) assertionsCollector).labels(assertionsLabelValues).inc();
@Override public List<MetricFamilySamples> collect() { List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(); for(Map.Entry<List<String>, Child> c: children.entrySet()) { Child.Value v = c.getValue().get(); List<String> labelNamesWithQuantile = new ArrayList<String>(labelNames); labelNamesWithQuantile.add("quantile"); for(Map.Entry<Double, Double> q : v.quantiles.entrySet()) { List<String> labelValuesWithQuantile = new ArrayList<String>(c.getKey()); labelValuesWithQuantile.add(doubleToGoString(q.getKey())); samples.add(new MetricFamilySamples.Sample(fullname, labelNamesWithQuantile, labelValuesWithQuantile, q.getValue())); } samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count)); samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum)); } return familySamplesList(Type.SUMMARY, samples); }
/** * Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run. * * @param timeable Code that is being timed * @return Result returned by callable. */ public <E> E time(Callable<E> timeable) { Timer timer = startTimer(); try { return timeable.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { timer.observeDuration(); } }
/** * Executes runnable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run. * * @param timeable Code that is being timed * @return Measured duration in seconds for timeable to complete. */ public double time(Runnable timeable) { Timer timer = startTimer(); double elapsed; try { timeable.run(); } finally { elapsed = timer.observeDuration(); } return elapsed; }
public void startTimer(String beanName, String attributeName) { labels(beanName, attributeName).startTimer(); }
public void time(String beanName, String attributeName, Runnable runnable) { labels(beanName, attributeName).time(runnable); }
@Override public void observe(long metricValue) { child.observe(metricValue); }
public void observe(String beanName, String attributeName, double amount) { labels(beanName, attributeName).observe(amount); }
@Override public long count() { return (long) child.get().count; } }
protected void doRequestDuration(RequestMetric metric) { requestDuration.labels(nullToEmpty(metric.getMethod(), Integer.toString(metric.getResponseCode()), metric.getApiId(), metric.getApiVersion(), metric.getClientId())).observe(metric.getRequestDuration()); }
@Override public void recordConnectionAcquiredNanos(long elapsedAcquiredNanos) { elapsedAcquiredSummary.observe(elapsedAcquiredNanos); }
@Around("processRequest()") public void timedProcessRequest(ProceedingJoinPoint joinPoint) throws Throwable { joinPoint.proceed(); Request request = (Request) joinPoint.getArgs()[0]; String type = requestTypeMap.getOrDefault(request.type, "unknown"); requests.labels(type).inc(); long latencyMs = System.currentTimeMillis() - request.createTime; String latencyLabel = isWriteRequest(request.type) ? "write" : "read"; requestsLatency.labels(latencyLabel).observe(latencyMs); }
@Around("processRequest()") public void timedProcessRequest(ProceedingJoinPoint joinPoint) throws Throwable { joinPoint.proceed(); Request request = (Request) joinPoint.getArgs()[0]; String type = requestTypeMap.getOrDefault(request.type, "unknown"); requests.labels(type).inc(); long latencyMs = Time.currentElapsedTime() - request.createTime; String latencyLabel = isWriteRequest(request.type) ? "write" : "read"; requestsLatency.labels(latencyLabel).observe(latencyMs); }
/** * Executes callable code (e.g. a Java 8 Lambda) and observes a duration of how long it took to run. * * @param timeable Code that is being timed * @return Result returned by callable. */ public <E> E time(Callable<E> timeable){ return noLabelsChild.time(timeable); }
/** * Start a timer to track a duration on the summary with no labels. * <p> * Call {@link Timer#observeDuration} at the end of what you want to measure the duration of. */ public Timer startTimer() { return noLabelsChild.startTimer(); }
/** * Observe the given amount on the summary with no labels. */ public void observe(double amt) { noLabelsChild.observe(amt); } /**
/** * Observe the amount of time in seconds since {@link Child#startTimer} was called. * @return Measured duration in seconds since {@link Child#startTimer} was called. */ public double observeDuration() { double elapsed = SimpleTimer.elapsedSecondsFromNanos(start, SimpleTimer.defaultTimeProvider.nanoTime()); child.observe(elapsed); return elapsed; }