Search in sources :

Example 1 with Histogram

use of io.dropwizard.metrics5.Histogram 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)

Aggregations

Counter (io.dropwizard.metrics5.Counter)1 Histogram (io.dropwizard.metrics5.Histogram)1 Meter (io.dropwizard.metrics5.Meter)1 MetricName (io.dropwizard.metrics5.MetricName)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1