Skip to content
Permalink
Browse files
thanks spec default methods creating call loops....fixing it + a addi…
…ng simpletimer first representation in json and openmetrics endpoints
  • Loading branch information
rmannibucau committed Apr 11, 2020
1 parent e72d8f1 commit 55d1cb28620acb2c1a83aa982d2ce10f35ed53c2
Showing 7 changed files with 230 additions and 155 deletions.
@@ -166,6 +166,11 @@ public Gauge<?> gauge(final MetricID metricID, final Gauge<?> gauge) {
return gauge(metricID.getName(), gauge, metricID.getTagsAsArray());
}

@Override
public Gauge<?> gauge(final String name, final Gauge<?> gauge, final Tag...tags) {
return register(Metadata.builder().reusable().withName(name).build(), gauge, tags);
}

@Override
public Histogram histogram(final Metadata metadata) {
return histogram(metadata, NO_TAG);
@@ -26,10 +26,12 @@
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

public class TimerImpl implements Timer {
private final Histogram histogram;
private final Meter meter;
private final LongAdder elapsed = new LongAdder();

public TimerImpl(final String unit) {
this.histogram = new HistogramImpl(unit);
@@ -43,6 +45,7 @@ public void update(final Duration duration) {
}
histogram.update(duration.toNanos());
meter.mark();
elapsed.add(duration.toNanos());
}

@Override
@@ -73,7 +76,7 @@ public Context time() {

@Override
public Duration getElapsedTime() {
return null;
return Duration.ofNanos(elapsed.longValue());
}

@Override
@@ -23,14 +23,17 @@
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;

import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import javax.json.JsonValue;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.Path;
@@ -54,6 +57,7 @@
import org.eclipse.microprofile.metrics.Metric;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.SimpleTimer;
import org.eclipse.microprofile.metrics.Snapshot;
import org.eclipse.microprofile.metrics.Timer;

@@ -255,24 +259,38 @@ private String getKey(final Map.Entry<MetricID, Metric> e) {
private Object toJson(final Object metric, final String nameSuffix) {
if (Timer.class.isInstance(metric)) {
final Timer meter = Timer.class.cast(metric);
final Map<Object, Object> map = new HashMap<>(15);
final Map<Object, Object> map = new TreeMap<>();
map.putAll(snapshot(meter.getSnapshot(), nameSuffix));
map.putAll(meter(meter, nameSuffix));
final Duration elapsedTime = meter.getElapsedTime();
map.put("elapsedTime" + nameSuffix, elapsedTime == null ? 0 : elapsedTime.toNanos());
return map;
}
if (SimpleTimer.class.isInstance(metric)) {
final SimpleTimer simpleTimer = SimpleTimer.class.cast(metric);
final Map<Object, Object> map = new TreeMap<>();
map.put("count" + nameSuffix, simpleTimer.getCount());
final Duration elapsedTime = simpleTimer.getElapsedTime();
map.put("elapsedTime" + nameSuffix, elapsedTime == null ? 0 : elapsedTime.toNanos());
final Duration minTimeDuration = simpleTimer.getMinTimeDuration();
map.put("minTimeDuration" + nameSuffix, minTimeDuration == null ? JsonValue.NULL : minTimeDuration.toNanos());
final Duration maxTimeDuration = simpleTimer.getMaxTimeDuration();
map.put("maxTimeDuration" + nameSuffix, maxTimeDuration == null ? JsonValue.NULL : maxTimeDuration.toNanos());
return map;
}
if (Meter.class.isInstance(metric)) {
return meter(Meter.class.cast(metric), nameSuffix);
}
if (Histogram.class.isInstance(metric)) {
final Histogram histogram = Histogram.class.cast(metric);
final Map<Object, Object> map = new HashMap<>(11);
final Map<Object, Object> map = new TreeMap<>();
map.putAll(snapshot(histogram.getSnapshot(), nameSuffix));
map.put("count" + nameSuffix, histogram.getCount());
return map;
}
if (ConcurrentGauge.class.isInstance(metric)) {
final ConcurrentGauge concurrentGauge = ConcurrentGauge.class.cast(metric);
final Map<Object, Object> map = new HashMap<>(3);
final Map<Object, Object> map = new TreeMap<>();
map.put("min" + nameSuffix, concurrentGauge.getMin());
map.put("current" + nameSuffix, concurrentGauge.getCount());
map.put("max" + nameSuffix, concurrentGauge.getMax());
@@ -283,7 +301,7 @@ private Object toJson(final Object metric, final String nameSuffix) {
}

private Map<String, Object> meter(final Metered metered, final String nameSuffix) {
final Map<String, Object> map = new HashMap<>(5);
final Map<String, Object> map = new TreeMap<>();
map.put("count" + nameSuffix, metered.getCount());
map.put("meanRate" + nameSuffix, metered.getMeanRate());
map.put("oneMinRate" + nameSuffix, metered.getOneMinuteRate());
@@ -293,7 +311,7 @@ private Map<String, Object> meter(final Metered metered, final String nameSuffix
}

private Map<String, Object> snapshot(final Snapshot snapshot, final String nameSuffix) {
final Map<String, Object> map = new HashMap<>(10);
final Map<String, Object> map = new TreeMap<>();
map.put("p50" + nameSuffix, snapshot.getMedian());
map.put("p75" + nameSuffix, snapshot.get75thPercentile());
map.put("p95" + nameSuffix, snapshot.get95thPercentile());
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.microprofile.metrics.common.prometheus;

// just to expose it "correctly" named
public class OpenMetricsFormatter extends PrometheusFormatter {
}

0 comments on commit 55d1cb2

Please sign in to comment.