Search in sources :

Example 1 with Context

use of io.dropwizard.metrics5.Timer.Context in project instrumentation-java by census-instrumentation.

the class DropWizardMetricsTest method collect.

@Test
public void collect() throws InterruptedException {
    // create dropwizard metrics
    Map<String, String> tags = new HashMap<>();
    tags.put("tag1", "value1");
    tags.put("tag2", "value2");
    List<LabelKey> labelKeys = new ArrayList<>();
    List<LabelValue> labelValues = new ArrayList<>();
    for (Map.Entry<String, String> e : tags.entrySet()) {
        labelKeys.add(LabelKey.create(e.getKey(), e.getKey()));
        labelValues.add(LabelValue.create(e.getValue()));
    }
    Counter evictions = metricRegistry.counter(new MetricName("cache_evictions", tags));
    evictions.inc();
    evictions.inc(3);
    evictions.dec();
    evictions.dec(2);
    metricRegistry.gauge(new MetricName("boolean_gauge", tags), BooleanGauge::new);
    metricRegistry.gauge(new MetricName("double_gauge", tags), DoubleGauge::new);
    metricRegistry.gauge(new MetricName("float_gauge", tags), FloatGauge::new);
    metricRegistry.gauge(new MetricName("integer_gauge", tags), IntegerGauge::new);
    metricRegistry.gauge(new MetricName("long_gauge", tags), LongGauge::new);
    metricRegistry.gauge(new MetricName("notags_boolean_gauge", Collections.emptyMap()), BooleanGauge::new);
    Meter getRequests = metricRegistry.meter(new MetricName("get_requests", tags));
    getRequests.mark();
    getRequests.mark();
    Histogram resultCounts = metricRegistry.histogram(new MetricName("result", tags));
    resultCounts.update(200);
    Timer timer = metricRegistry.timer(new MetricName("requests", tags));
    Timer.Context context = timer.time();
    Thread.sleep(1L);
    context.stop();
    ArrayList<Metric> metrics = new ArrayList<>(dropWizardMetrics.getMetrics());
    assertThat(metrics.size()).isEqualTo(10);
    assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_cache_evictions_counter", "Collected from dropwizard5 (metric=cache_evictions, " + "type=io.dropwizard.metrics5.Counter)", DEFAULT_UNIT, Type.GAUGE_INT64, labelKeys));
    assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(1));
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    // boolean gauge with tags
    assertThat(metrics.get(1).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_boolean_gauge_gauge", "Collected from dropwizard5 (metric=boolean_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$BooleanGauge)", DEFAULT_UNIT, Type.GAUGE_INT64, labelKeys));
    assertThat(metrics.get(1).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(1));
    assertThat(metrics.get(1).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(2).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_double_gauge_gauge", "Collected from dropwizard5 (metric=double_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$DoubleGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(2).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(1.234));
    assertThat(metrics.get(2).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(3).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_float_gauge_gauge", "Collected from dropwizard5 (metric=float_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$FloatGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(3).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(0.1234000027179718));
    assertThat(metrics.get(3).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(4).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_integer_gauge_gauge", "Collected from dropwizard5 (metric=integer_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$IntegerGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(4).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(1234.0));
    assertThat(metrics.get(4).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(5).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_long_gauge_gauge", "Collected from dropwizard5 (metric=long_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$LongGauge)", DEFAULT_UNIT, Type.GAUGE_DOUBLE, labelKeys));
    assertThat(metrics.get(5).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.doubleValue(1234.0));
    assertThat(metrics.get(5).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    // boolean gauge with tags
    assertThat(metrics.get(6).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_notags_boolean_gauge_gauge", "Collected from dropwizard5 (metric=notags_boolean_gauge, " + "type=io.opencensus.contrib.dropwizard5.DropWizardMetricsTest$BooleanGauge)", DEFAULT_UNIT, Type.GAUGE_INT64, Collections.emptyList()));
    assertThat(metrics.get(6).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(0);
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(1));
    assertThat(metrics.get(6).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
    assertThat(metrics.get(7).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_get_requests_meter", "Collected from dropwizard5 (metric=get_requests, " + "type=io.dropwizard.metrics5.Meter)", DEFAULT_UNIT, Type.CUMULATIVE_INT64, labelKeys));
    assertThat(metrics.get(7).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(2));
    assertThat(metrics.get(7).getTimeSeriesList().get(0).getStartTimestamp()).isNotNull();
    assertThat(metrics.get(8).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_result_histogram", "Collected from dropwizard5 (metric=result, " + "type=io.dropwizard.metrics5.Histogram)", DEFAULT_UNIT, Type.SUMMARY, labelKeys));
    assertThat(metrics.get(8).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getLabelValues()).isEqualTo(labelValues);
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.summaryValue(Summary.create(1L, 0.0, Snapshot.create(1L, 0.0, Arrays.asList(ValueAtPercentile.create(50.0, 200.0), ValueAtPercentile.create(75.0, 200.0), ValueAtPercentile.create(98.0, 200.0), ValueAtPercentile.create(99.0, 200.0), ValueAtPercentile.create(99.9, 200.0))))));
    assertThat(metrics.get(8).getTimeSeriesList().get(0).getStartTimestamp()).isInstanceOf(Timestamp.class);
    assertThat(metrics.get(9).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("dropwizard5_requests_timer", "Collected from dropwizard5 (metric=requests, " + "type=io.dropwizard.metrics5.Timer)", NS_UNIT, Type.SUMMARY, labelKeys));
    assertThat(metrics.get(9).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(tags.size());
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.summaryValue(Summary.create(1L, 0.0, Snapshot.create(1L, 0.0, Arrays.asList(ValueAtPercentile.create(50.0, timer.getSnapshot().getMedian()), ValueAtPercentile.create(75.0, timer.getSnapshot().get75thPercentile()), ValueAtPercentile.create(98.0, timer.getSnapshot().get98thPercentile()), ValueAtPercentile.create(99.0, timer.getSnapshot().get99thPercentile()), ValueAtPercentile.create(99.9, timer.getSnapshot().get999thPercentile()))))));
    assertThat(metrics.get(9).getTimeSeriesList().get(0).getStartTimestamp()).isInstanceOf(Timestamp.class);
}
Also used : Histogram(io.dropwizard.metrics5.Histogram) LabelValue(io.opencensus.metrics.LabelValue) HashMap(java.util.HashMap) Meter(io.dropwizard.metrics5.Meter) ArrayList(java.util.ArrayList) MetricName(io.dropwizard.metrics5.MetricName) Counter(io.dropwizard.metrics5.Counter) Timer(io.dropwizard.metrics5.Timer) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Context

use of io.dropwizard.metrics5.Timer.Context in project feign by OpenFeign.

the class MeteredDecoder method decode.

@Override
public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
    final RequestTemplate template = response.request().requestTemplate();
    final MeteredBody body = response.body() == null ? null : new MeteredBody(response.body());
    response = response.toBuilder().body(body).build();
    final Object decoded;
    try (final Context classTimer = metricRegistry.timer(metricName.metricName(template.methodMetadata(), template.feignTarget()).tagged("uri", template.methodMetadata().template().path()), metricSuppliers.timers()).time()) {
        decoded = decoder.decode(response, type);
    } catch (IOException | RuntimeException e) {
        metricRegistry.meter(metricName.metricName(template.methodMetadata(), template.feignTarget(), "error_count").tagged("exception_name", e.getClass().getSimpleName()).tagged("uri", template.methodMetadata().template().path()), metricSuppliers.meters()).mark();
        throw e;
    } catch (Exception e) {
        metricRegistry.meter(metricName.metricName(template.methodMetadata(), template.feignTarget(), "error_count").tagged("exception_name", e.getClass().getSimpleName()).tagged("uri", template.methodMetadata().template().path()), metricSuppliers.meters()).mark();
        throw new IOException(e);
    }
    if (body != null) {
        metricRegistry.histogram(metricName.metricName(template.methodMetadata(), template.feignTarget(), "response_size").tagged("uri", template.methodMetadata().template().path()), metricSuppliers.histograms()).update(body.count());
    }
    return decoded;
}
Also used : Context(io.dropwizard.metrics5.Timer.Context) RequestTemplate(feign.RequestTemplate) IOException(java.io.IOException) DecodeException(feign.codec.DecodeException) IOException(java.io.IOException) FeignException(feign.FeignException)

Example 3 with Context

use of io.dropwizard.metrics5.Timer.Context in project feign by OpenFeign.

the class MeteredInvocationHandleFactory method create.

@Override
public InvocationHandler create(Target target, Map<Method, MethodHandler> dispatch) {
    final Class clientClass = target.type();
    final InvocationHandler invocationHandle = invocationHandler.create(target, dispatch);
    return (proxy, method, args) -> {
        if (JAVA_OBJECT_METHODS.contains(method.getName()) || Util.isDefault(method)) {
            LOG.trace("Skipping metrics for method={}", method);
            return invocationHandle.invoke(proxy, method, args);
        }
        try (final Context classTimer = metricRegistry.timer(metricName.metricName(clientClass, method, target.url()), metricSuppliers.timers()).time()) {
            return invocationHandle.invoke(proxy, method, args);
        } catch (final FeignException e) {
            metricRegistry.meter(metricName.metricName(clientClass, method, target.url()).resolve("http_error").tagged("http_status", String.valueOf(e.status())).tagged("error_group", e.status() / 100 + "xx"), metricSuppliers.meters()).mark();
            throw e;
        } catch (final Throwable e) {
            metricRegistry.meter(metricName.metricName(clientClass, method, target.url()).resolve("exception").tagged("exception_name", e.getClass().getSimpleName()), metricSuppliers.meters()).mark();
            throw e;
        }
    };
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Logger(org.slf4j.Logger) Context(io.dropwizard.metrics5.Timer.Context) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) feign(feign) MetricRegistry(io.dropwizard.metrics5.MetricRegistry) Context(io.dropwizard.metrics5.Timer.Context) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 4 with Context

use of io.dropwizard.metrics5.Timer.Context in project feign by OpenFeign.

the class MeteredClient method execute.

@Override
public Response execute(Request request, Options options) throws IOException {
    final RequestTemplate template = request.requestTemplate();
    try (final Context classTimer = metricRegistry.timer(metricName.metricName(template.methodMetadata(), template.feignTarget()).tagged("uri", template.methodMetadata().template().path()), metricSuppliers.timers()).time()) {
        Response response = client.execute(request, options);
        metricRegistry.counter(metricName.metricName(template.methodMetadata(), template.feignTarget(), "http_response_code").tagged("http_status", String.valueOf(response.status())).tagged("status_group", response.status() / 100 + "xx").tagged("uri", template.methodMetadata().template().path())).inc();
        return response;
    } catch (FeignException e) {
        metricRegistry.counter(metricName.metricName(template.methodMetadata(), template.feignTarget(), "http_response_code").tagged("http_status", String.valueOf(e.status())).tagged("status_group", e.status() / 100 + "xx").tagged("uri", template.methodMetadata().template().path())).inc();
        throw e;
    } catch (IOException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : Context(io.dropwizard.metrics5.Timer.Context) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

Context (io.dropwizard.metrics5.Timer.Context)3 IOException (java.io.IOException)2 Map (java.util.Map)2 feign (feign)1 FeignException (feign.FeignException)1 RequestTemplate (feign.RequestTemplate)1 DecodeException (feign.codec.DecodeException)1 Counter (io.dropwizard.metrics5.Counter)1 Histogram (io.dropwizard.metrics5.Histogram)1 Meter (io.dropwizard.metrics5.Meter)1 MetricName (io.dropwizard.metrics5.MetricName)1 MetricRegistry (io.dropwizard.metrics5.MetricRegistry)1 Timer (io.dropwizard.metrics5.Timer)1 LabelKey (io.opencensus.metrics.LabelKey)1 LabelValue (io.opencensus.metrics.LabelValue)1 Metric (io.opencensus.metrics.export.Metric)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1