use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class StackdriverExportUtils method createMetric.
// Create a Metric using the LabelKeys and LabelValues.
@VisibleForTesting
static Metric createMetric(io.opencensus.metrics.export.MetricDescriptor metricDescriptor, List<LabelValue> labelValues, String domain, Map<LabelKey, LabelValue> constantLabels) {
Metric.Builder builder = Metric.newBuilder();
builder.setType(generateType(metricDescriptor.getName(), domain));
Map<String, String> stringTagMap = Maps.newHashMap();
List<LabelKey> labelKeys = metricDescriptor.getLabelKeys();
for (int i = 0; i < labelValues.size(); i++) {
String value = labelValues.get(i).getValue();
if (value == null) {
continue;
}
stringTagMap.put(labelKeys.get(i).getKey(), value);
}
for (Map.Entry<LabelKey, LabelValue> constantLabel : constantLabels.entrySet()) {
String constantLabelKey = constantLabel.getKey().getKey();
String constantLabelValue = constantLabel.getValue().getValue();
constantLabelValue = constantLabelValue == null ? "" : constantLabelValue;
stringTagMap.put(constantLabelKey, constantLabelValue);
}
builder.putAllLabels(stringTagMap);
return builder.build();
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class StackdriverExportUtilsTest method convertSummaryMetric.
@Test
public void convertSummaryMetric() {
io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor1 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SUMMARY_SUFFIX_COUNT, METRIC_DESCRIPTION, METRIC_UNIT_2, Type.CUMULATIVE_INT64, LABEL_KEY);
io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor2 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SUMMARY_SUFFIX_SUM, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_DOUBLE, LABEL_KEY);
List<LabelKey> labelKeys = new ArrayList<>(LABEL_KEY);
labelKeys.add(PERCENTILE_LABEL_KEY);
io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor3 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SNAPSHOT_SUFFIX_PERCENTILE, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_DOUBLE, labelKeys);
List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries1 = Collections.singletonList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(LABEL_VALUE, Point.create(Value.longValue(22), TIMESTAMP), null));
List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries2 = Collections.singletonList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(LABEL_VALUE, Point.create(Value.doubleValue(74.8), TIMESTAMP), null));
LabelValue existingLabelValues = LABEL_VALUE.get(0);
List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries3 = Arrays.asList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("50.0")), Point.create(Value.doubleValue(6), TIMESTAMP), null), io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("75.0")), Point.create(Value.doubleValue(10.2), TIMESTAMP), null), io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("98.0")), Point.create(Value.doubleValue(4.6), TIMESTAMP), null), io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("99.0")), Point.create(Value.doubleValue(1.2), TIMESTAMP), null));
List<io.opencensus.metrics.export.Metric> metrics = StackdriverExportUtils.convertSummaryMetric(SUMMARY_METRIC);
assertThat(metrics).isNotEmpty();
assertThat(metrics.size()).isEqualTo(3);
assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor1);
assertThat(metrics.get(0).getTimeSeriesList()).isNotEmpty();
assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
assertThat(metrics.get(0).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries1);
assertThat(metrics.get(1).getTimeSeriesList().size()).isEqualTo(1);
assertThat(metrics.get(1).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries2);
assertThat(metrics.get(1).getTimeSeriesList()).isNotEmpty();
assertThat(metrics.get(1).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor2);
assertThat(metrics.get(2).getTimeSeriesList()).isNotEmpty();
assertThat(metrics.get(2).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor3);
assertThat(metrics.get(2).getTimeSeriesList().size()).isEqualTo(4);
assertThat(metrics.get(2).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries3);
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class LongGaugeImplTest 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);
LongGaugeImpl longGauge = new LongGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
LongPoint longPoint = longGauge.getOrCreateTimeSeries(labelValues);
longPoint.add(1);
longPoint.add(2);
LongPoint defaultPoint = longGauge.getDefaultTimeSeries();
defaultPoint.set(100);
List<LabelKey> allKeys = new ArrayList<>(labelKeys);
allKeys.add(constantKey);
MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_INT64, allKeys);
List<LabelValue> allValues = new ArrayList<>(labelValues);
allValues.add(constantValue);
List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
TimeSeries defaultTimeSeries = TimeSeries.createWithOnePoint(Arrays.asList(UNSET_VALUE, UNSET_VALUE, constantValue), Point.create(Value.longValue(100), TEST_TIME), null);
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), TEST_TIME), null));
expectedTimeSeriesList.add(defaultTimeSeries);
Metric metric = longGauge.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
longGauge.removeTimeSeries(labelValues);
Metric metric2 = longGauge.getMetric(testClock);
assertThat(metric2).isNotNull();
assertThat(metric2.getTimeSeriesList()).containsExactly(defaultTimeSeries);
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class DropWizardMetricsTest method collect_Histogram.
@Test
public void collect_Histogram() {
Histogram resultCounts = metricRegistry.histogram("result");
resultCounts.update(200);
ArrayList<Metric> metrics = new ArrayList<>(dropWizardMetrics.getMetrics());
assertThat(metrics.size()).isEqualTo(1);
assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("codahale_result_histogram", "Collected from codahale (metric=result, type=com.codahale.metrics.Histogram)", DEFAULT_UNIT, Type.SUMMARY, Collections.<LabelKey>emptyList()));
assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(0);
assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
assertThat(metrics.get(0).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(0).getTimeSeriesList().get(0).getStartTimestamp()).isNotNull();
}
use of io.opencensus.metrics.LabelKey in project instrumentation-java by census-instrumentation.
the class DropWizardMetricsTest method collect_Counter.
@Test
public void collect_Counter() {
// create dropwizard metrics
Counter evictions = metricRegistry.counter("cache_evictions");
evictions.inc();
evictions.inc(3);
evictions.dec();
evictions.dec(2);
ArrayList<Metric> metrics = new ArrayList<>(dropWizardMetrics.getMetrics());
assertThat(metrics.size()).isEqualTo(1);
assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("codahale_cache_evictions_counter", "Collected from codahale (metric=cache_evictions, " + "type=com.codahale.metrics.Counter)", DEFAULT_UNIT, Type.GAUGE_INT64, Collections.<LabelKey>emptyList()));
assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(0);
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();
}
Aggregations