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 unit the metric descriptor unit.
* @param codahaleSnapshot the snapshot object to collect
* @param count the value or count
* @return a {@code Metric}.
*/
private Metric collectSnapshotAndCount(String metricName, String metricDescription, String unit, com.codahale.metrics.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, Collections.<LabelKey>emptyList());
TimeSeries timeSeries = TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), point, cumulativeStartTimestamp);
return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class DropWizardMetrics method collectGauge.
/**
* Returns a {@code Metric} collected from {@link Gauge}.
*
* @param dropwizardMetric the metric name.
* @param gauge the gauge object to collect.
* @return a {@code Metric}.
*/
@SuppressWarnings("rawtypes")
@Nullable
private Metric collectGauge(MetricName dropwizardMetric, Gauge gauge) {
// TODO cache dropwizard MetricName -> OC MetricDescriptor, Labels conversion
String metricName = DropWizardUtils.generateFullMetricName(dropwizardMetric.getKey(), "gauge");
String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardMetric.getKey(), gauge);
AbstractMap.SimpleImmutableEntry<List<LabelKey>, List<LabelValue>> labels = DropWizardUtils.generateLabels(dropwizardMetric);
// Figure out which gauge instance and call the right method to get value
Type type;
Value value;
Object obj = gauge.getValue();
if (obj instanceof Number) {
type = Type.GAUGE_DOUBLE;
value = Value.doubleValue(((Number) obj).doubleValue());
} else if (obj instanceof Boolean) {
type = Type.GAUGE_INT64;
value = Value.longValue(((Boolean) obj) ? 1 : 0);
} else {
// Ignoring Gauge (gauge.getKey()) with unhandled type.
return null;
}
MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, type, labels.getKey());
TimeSeries timeSeries = TimeSeries.createWithOnePoint(labels.getValue(), Point.create(value, 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 io.dropwizard.metrics5.Meter}.
*
* @param dropwizardMetric the metric name.
* @param meter the meter object to collect
* @return a {@code Metric}.
*/
private Metric collectMeter(MetricName dropwizardMetric, Meter meter) {
String metricName = DropWizardUtils.generateFullMetricName(dropwizardMetric.getKey(), "meter");
String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardMetric.getKey(), meter);
final AbstractMap.SimpleImmutableEntry<List<LabelKey>, List<LabelValue>> labels = DropWizardUtils.generateLabels(dropwizardMetric);
MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, Type.CUMULATIVE_INT64, labels.getKey());
TimeSeries timeSeries = TimeSeries.createWithOnePoint(labels.getValue(), 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 DerivedLongCumulativeImplTest 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);
DerivedLongCumulativeImpl derivedLongCumulative2 = new DerivedLongCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
derivedLongCumulative2.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_INT64, allKeys);
List<LabelValue> allValues = new ArrayList<>(labelValues);
allValues.add(constantValue);
TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), endTime), START_TIME);
Metric metric = derivedLongCumulative2.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
derivedLongCumulative2.removeTimeSeries(labelValues);
Metric metric2 = derivedLongCumulative2.getMetric(testClock);
assertThat(metric2).isNull();
}
use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class DoubleCumulativeImplTest 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);
DoubleCumulativeImpl doubleCumulative = new DoubleCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
DoublePoint doublePoint = doubleCumulative.getOrCreateTimeSeries(labelValues);
doublePoint.add(1);
doublePoint.add(2);
DoublePoint defaultPoint = doubleCumulative.getDefaultTimeSeries();
defaultPoint.add(100);
List<LabelKey> allKeys = new ArrayList<>(labelKeys);
allKeys.add(constantKey);
MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_DOUBLE, allKeys);
testClock.advanceTime(ONE_MINUTE);
Timestamp endTime = testClock.now();
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.doubleValue(100), endTime), START_TIME);
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(3), endTime), START_TIME));
expectedTimeSeriesList.add(defaultTimeSeries);
Metric metric = doubleCumulative.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
doubleCumulative.removeTimeSeries(labelValues);
Metric metric2 = doubleCumulative.getMetric(testClock);
assertThat(metric2).isNotNull();
assertThat(metric2.getTimeSeriesList()).containsExactly(defaultTimeSeries);
}
Aggregations