use of io.opencensus.metrics.export.MetricDescriptor 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.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class MetricUtilsTest method viewToMetricDescriptor_Count.
@Test
public void viewToMetricDescriptor_Count() {
MetricDescriptor metricDescriptor = MetricUtils.viewToMetricDescriptor(VIEW_3);
assertThat(metricDescriptor).isNotNull();
assertThat(metricDescriptor.getName()).isEqualTo(VIEW_NAME.asString());
assertThat(metricDescriptor.getUnit()).isEqualTo(MetricUtils.COUNT_UNIT);
assertThat(metricDescriptor.getType()).isEqualTo(Type.CUMULATIVE_INT64);
assertThat(metricDescriptor.getDescription()).isEqualTo(VIEW_DESCRIPTION);
assertThat(metricDescriptor.getLabelKeys()).containsExactly(LabelKey.create(KEY.getName(), ""));
}
use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class DropWizardMetrics method collectCounter.
/**
* Returns a {@code Metric} collected from {@link Counter}.
*
* @param dropwizardName the metric name.
* @param counter the counter object to collect.
* @return a {@code Metric}.
*/
private Metric collectCounter(String dropwizardName, Counter counter) {
String metricName = DropWizardUtils.generateFullMetricName(dropwizardName, "counter");
String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardName, counter);
MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, Type.GAUGE_INT64, Collections.<LabelKey>emptyList());
TimeSeries timeSeries = TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), Point.create(Value.longValue(counter.getCount()), clock.now()), null);
return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class DropWizardMetrics method collectMeter.
/**
* Returns a {@code Metric} collected from {@link Meter}.
*
* @param dropwizardName the metric name.
* @param meter the meter object to collect
* @return a {@code Metric}.
*/
private Metric collectMeter(String dropwizardName, Meter meter) {
String metricName = DropWizardUtils.generateFullMetricName(dropwizardName, "meter");
String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardName, meter);
MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, Type.CUMULATIVE_INT64, Collections.<LabelKey>emptyList());
TimeSeries timeSeries = TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), Point.create(Value.longValue(meter.getCount()), clock.now()), cumulativeStartTimestamp);
return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class DropWizardMetrics method collectSnapshotAndCount.
/**
* Returns a {@code Metric} collected from {@link Snapshot}.
*
* @param metricName the metric name.
* @param metricDescription the metric description.
* @param labelKeys metric label keys
* @param labelValues metric label values
* @param codahaleSnapshot the snapshot object to collect
* @param count the value or count
* @return a {@code Metric}.
*/
private Metric collectSnapshotAndCount(String metricName, String metricDescription, List<LabelKey> labelKeys, List<LabelValue> labelValues, String unit, io.dropwizard.metrics5.Snapshot codahaleSnapshot, long count) {
List<ValueAtPercentile> valueAtPercentiles = Arrays.asList(ValueAtPercentile.create(50.0, codahaleSnapshot.getMedian()), ValueAtPercentile.create(75.0, codahaleSnapshot.get75thPercentile()), ValueAtPercentile.create(98.0, codahaleSnapshot.get98thPercentile()), ValueAtPercentile.create(99.0, codahaleSnapshot.get99thPercentile()), ValueAtPercentile.create(99.9, codahaleSnapshot.get999thPercentile()));
Snapshot snapshot = Snapshot.create((long) codahaleSnapshot.size(), 0.0, valueAtPercentiles);
Point point = Point.create(Value.summaryValue(Summary.create(count, 0.0, snapshot)), clock.now());
// TODO(mayurkale): OPTIMIZATION: Cache the MetricDescriptor objects.
MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, unit, Type.SUMMARY, labelKeys);
TimeSeries timeSeries = TimeSeries.createWithOnePoint(labelValues, point, cumulativeStartTimestamp);
return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Aggregations