use of io.opencensus.metrics.LabelKey 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);
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class DerivedDoubleCumulativeImplTest method withConstantLabels.
@Test
public void withConstantLabels() {
List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
LabelKey constantKey = LabelKey.create("constant_key", "desc");
LabelValue constantValue = LabelValue.create("constant_value");
Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
DerivedDoubleCumulativeImpl derivedDoubleCumulative2 = new DerivedDoubleCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
derivedDoubleCumulative2.createTimeSeries(labelValues, new QueueManager(), queueManagerFunction);
testClock.advanceTime(ONE_MINUTE);
Timestamp endTime = testClock.now();
List<LabelKey> allKeys = new ArrayList<>(labelKeys);
allKeys.add(constantKey);
MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_DOUBLE, allKeys);
List<LabelValue> allValues = new ArrayList<>(labelValues);
allValues.add(constantValue);
TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(2.5), endTime), START_TIME);
Metric metric = derivedDoubleCumulative2.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
derivedDoubleCumulative2.removeTimeSeries(labelValues);
Metric metric2 = derivedDoubleCumulative2.getMetric(testClock);
assertThat(metric2).isNull();
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class DoubleCumulativeImplTest method testEquals.
@Test
public void testEquals() {
List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
DoubleCumulativeImpl doubleCumulative = new DoubleCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, EMPTY_CONSTANT_LABELS, START_TIME);
DoublePoint defaultPoint1 = doubleCumulative.getDefaultTimeSeries();
DoublePoint defaultPoint2 = doubleCumulative.getDefaultTimeSeries();
DoublePoint doublePoint1 = doubleCumulative.getOrCreateTimeSeries(labelValues);
DoublePoint doublePoint2 = doubleCumulative.getOrCreateTimeSeries(labelValues);
new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(doublePoint1, doublePoint2).testEquals();
doubleCumulative.clear();
DoublePoint newDefaultPointAfterClear = doubleCumulative.getDefaultTimeSeries();
DoublePoint newDoublePointAfterClear = doubleCumulative.getOrCreateTimeSeries(labelValues);
doubleCumulative.removeTimeSeries(labelValues);
DoublePoint newDoublePointAfterRemove = doubleCumulative.getOrCreateTimeSeries(labelValues);
new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(doublePoint1, doublePoint2).addEqualityGroup(newDefaultPointAfterClear).addEqualityGroup(newDoublePointAfterClear).addEqualityGroup(newDoublePointAfterRemove).testEquals();
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class DerivedDoubleGaugeImplTest method withConstantLabels.
@Test
public void withConstantLabels() {
List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
LabelKey constantKey = LabelKey.create("constant_key", "desc");
LabelValue constantValue = LabelValue.create("constant_value");
Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
DerivedDoubleGaugeImpl derivedDoubleGauge2 = new DerivedDoubleGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
derivedDoubleGauge2.createTimeSeries(labelValues, new QueueManager(), queueManagerFunction);
List<LabelKey> allKeys = new ArrayList<>(labelKeys);
allKeys.add(constantKey);
MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_DOUBLE, allKeys);
List<LabelValue> allValues = new ArrayList<>(labelValues);
allValues.add(constantValue);
TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(2.5), TEST_TIME), null);
Metric metric = derivedDoubleGauge2.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
derivedDoubleGauge2.removeTimeSeries(labelValues);
Metric metric2 = derivedDoubleGauge2.getMetric(testClock);
assertThat(metric2).isNull();
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class MetricUtils method viewToMetricDescriptor.
@javax.annotation.Nullable
static MetricDescriptor viewToMetricDescriptor(View view) {
if (view.getWindow() instanceof View.AggregationWindow.Interval) {
// Only creates Metric for cumulative stats.
return null;
}
List<LabelKey> labelKeys = new ArrayList<LabelKey>();
for (TagKey tagKey : view.getColumns()) {
// TODO: add description
labelKeys.add(LabelKey.create(tagKey.getName(), ""));
}
Measure measure = view.getMeasure();
Aggregation aggregation = view.getAggregation();
return MetricDescriptor.create(view.getName().asString(), view.getDescription(), getUnit(measure, aggregation), getType(measure, aggregation), labelKeys);
}
Aggregations