use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class DerivedLongGaugeImplTest 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);
DerivedLongGaugeImpl derivedLongGauge2 = new DerivedLongGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
derivedLongGauge2.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_INT64, allKeys);
List<LabelValue> allValues = new ArrayList<>(labelValues);
allValues.add(constantValue);
TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(2), TEST_TIME), null);
Metric metric = derivedLongGauge2.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
derivedLongGauge2.removeTimeSeries(labelValues);
Metric metric2 = derivedLongGauge2.getMetric(testClock);
assertThat(metric2).isNull();
}
use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class DoubleGaugeImplTest 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);
DoubleGaugeImpl doubleGauge = new DoubleGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
DoublePoint doublePoint = doubleGauge.getOrCreateTimeSeries(labelValues);
doublePoint.add(1);
doublePoint.add(2);
DoublePoint defaultPoint = doubleGauge.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_DOUBLE, 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.doubleValue(100), TEST_TIME), null);
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(3), TEST_TIME), null));
expectedTimeSeriesList.add(defaultTimeSeries);
Metric metric = doubleGauge.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
doubleGauge.removeTimeSeries(labelValues);
Metric metric2 = doubleGauge.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 LongCumulativeImplTest 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);
LongCumulativeImpl longCumulative = new LongCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
LongPoint longPoint = longCumulative.getOrCreateTimeSeries(labelValues);
longPoint.add(1);
longPoint.add(2);
LongPoint defaultPoint = longCumulative.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_INT64, 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.longValue(100), endTime), START_TIME);
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), endTime), START_TIME));
expectedTimeSeriesList.add(defaultTimeSeries);
Metric metric = longCumulative.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
longCumulative.removeTimeSeries(labelValues);
Metric metric2 = longCumulative.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 PrometheusExportUtils method createMetricFamilySamples.
// Converts a Metric to a Prometheus MetricFamilySamples.
static MetricFamilySamples createMetricFamilySamples(Metric metric, String namespace) {
MetricDescriptor metricDescriptor = metric.getMetricDescriptor();
String name = getNamespacedName(metricDescriptor.getName(), namespace);
Type type = getType(metricDescriptor.getType());
List<String> labelNames = convertToLabelNames(metricDescriptor.getLabelKeys());
List<Sample> samples = Lists.newArrayList();
for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) {
for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) {
samples.addAll(getSamples(name, labelNames, timeSeries.getLabelValues(), point.getValue()));
}
}
return new MetricFamilySamples(name, type, metricDescriptor.getDescription(), samples);
}
use of io.opencensus.metrics.export.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class MetricUtilsTest method viewToMetricDescriptor_NoIntervalViews.
@Test
public void viewToMetricDescriptor_NoIntervalViews() {
MetricDescriptor metricDescriptor = MetricUtils.viewToMetricDescriptor(VIEW_2);
assertThat(metricDescriptor).isNull();
}
Aggregations