use of io.prometheus.client.Collector.MetricFamilySamples.Sample 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.prometheus.client.Collector.MetricFamilySamples.Sample in project instrumentation-java by census-instrumentation.
the class PrometheusExportUtils method getSamples.
// Converts a point value in Metric to a list of Prometheus Samples.
@VisibleForTesting
static List<Sample> getSamples(final String name, final List<String> labelNames, List<LabelValue> labelValuesList, Value value) {
Preconditions.checkArgument(labelNames.size() == labelValuesList.size(), "Keys and Values don't have same size.");
final List<Sample> samples = Lists.newArrayList();
final List<String> labelValues = new ArrayList<String>(labelValuesList.size());
for (LabelValue labelValue : labelValuesList) {
String val = labelValue == null ? "" : labelValue.getValue();
labelValues.add(val == null ? "" : val);
}
return value.match(new Function<Double, List<Sample>>() {
@Override
public List<Sample> apply(Double arg) {
samples.add(new Sample(name, labelNames, labelValues, arg));
return samples;
}
}, new Function<Long, List<Sample>>() {
@Override
public List<Sample> apply(Long arg) {
samples.add(new Sample(name, labelNames, labelValues, arg));
return samples;
}
}, new Function<Distribution, List<Sample>>() {
@Override
public List<Sample> apply(final Distribution arg) {
BucketOptions bucketOptions = arg.getBucketOptions();
List<Double> boundaries = new ArrayList<>();
if (bucketOptions != null) {
boundaries = bucketOptions.match(new Function<ExplicitOptions, List<Double>>() {
@Override
public List<Double> apply(ExplicitOptions arg) {
return arg.getBucketBoundaries();
}
}, Functions.<List<Double>>throwIllegalArgumentException());
}
List<String> labelNamesWithLe = new ArrayList<String>(labelNames);
labelNamesWithLe.add(LABEL_NAME_BUCKET_BOUND);
long cumulativeCount = 0;
for (int i = 0; i < arg.getBuckets().size(); i++) {
List<String> labelValuesWithLe = new ArrayList<String>(labelValues);
// The label value of "le" is the upper inclusive bound.
// For the last bucket, it should be "+Inf".
String bucketBoundary = doubleToGoString(i < boundaries.size() ? boundaries.get(i) : Double.POSITIVE_INFINITY);
labelValuesWithLe.add(bucketBoundary);
cumulativeCount += arg.getBuckets().get(i).getCount();
samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_BUCKET, labelNamesWithLe, labelValuesWithLe, cumulativeCount));
}
samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_COUNT, labelNames, labelValues, arg.getCount()));
samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_SUM, labelNames, labelValues, arg.getSum()));
return samples;
}
}, new Function<Summary, List<Sample>>() {
@Override
public List<Sample> apply(Summary arg) {
Long count = arg.getCount();
if (count != null) {
samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_COUNT, labelNames, labelValues, count));
}
Double sum = arg.getSum();
if (sum != null) {
samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_SUM, labelNames, labelValues, sum));
}
List<ValueAtPercentile> valueAtPercentiles = arg.getSnapshot().getValueAtPercentiles();
List<String> labelNamesWithQuantile = new ArrayList<String>(labelNames);
labelNamesWithQuantile.add(LABEL_NAME_QUANTILE);
for (ValueAtPercentile valueAtPercentile : valueAtPercentiles) {
List<String> labelValuesWithQuantile = new ArrayList<String>(labelValues);
labelValuesWithQuantile.add(doubleToGoString(valueAtPercentile.getPercentile() / 100));
samples.add(new MetricFamilySamples.Sample(name, labelNamesWithQuantile, labelValuesWithQuantile, valueAtPercentile.getValue()));
}
return samples;
}
}, Functions.<List<Sample>>throwIllegalArgumentException());
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project instrumentation-java by census-instrumentation.
the class PrometheusExportUtilsTest method getSamples.
@Test
public void getSamples() {
assertThat(PrometheusExportUtils.getSamples(METRIC_NAME, convertToLabelNames(LABEL_KEY), LABEL_VALUE, DOUBLE_VALUE)).containsExactly(new Sample(METRIC_NAME, Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), -5.5));
assertThat(PrometheusExportUtils.getSamples(METRIC_NAME, convertToLabelNames(Collections.singletonList(K3_LABEL_KEY)), Collections.singletonList(V3_LABEL_VALUE), LONG_VALUE)).containsExactly(new Sample(METRIC_NAME, Collections.singletonList("k_3"), Collections.singletonList("v-3"), 123456789));
assertThat(PrometheusExportUtils.getSamples(METRIC_NAME, convertToLabelNames(Arrays.asList(K1_LABEL_KEY, K3_LABEL_KEY)), Arrays.asList(V1_LABEL_VALUE, null), LONG_VALUE)).containsExactly(new Sample(METRIC_NAME, Arrays.asList("k1", "k_3"), Arrays.asList("v1", ""), 123456789));
assertThat(PrometheusExportUtils.getSamples(METRIC_NAME, convertToLabelNames(Collections.singletonList(K3_LABEL_KEY)), Collections.singletonList(V3_LABEL_VALUE), SUMMARY_VALUE_2)).containsExactly(new Sample(METRIC_NAME + "_count", Collections.singletonList("k_3"), Collections.singletonList("v-3"), 22), new Sample(METRIC_NAME + "_sum", Collections.singletonList("k_3"), Collections.singletonList("v-3"), 74.8), new Sample(METRIC_NAME, Arrays.asList("k_3", LABEL_NAME_QUANTILE), Arrays.asList("v-3", "0.995"), 8.2), new Sample(METRIC_NAME, Arrays.asList("k_3", LABEL_NAME_QUANTILE), Arrays.asList("v-3", "0.99"), 10.2)).inOrder();
assertThat(PrometheusExportUtils.getSamples(METRIC_NAME, convertToLabelNames(Collections.singletonList(K1_LABEL_KEY)), Collections.singletonList(V1_LABEL_VALUE), DISTRIBUTION_VALUE)).containsExactly(new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "1.0"), 0), new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "2.0"), 2), new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "5.0"), 4), new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "+Inf"), 5), new Sample(METRIC_NAME + "_count", Collections.singletonList("k1"), Collections.singletonList("v1"), 5), new Sample(METRIC_NAME + "_sum", Collections.singletonList("k1"), Collections.singletonList("v1"), 22.0)).inOrder();
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project instrumentation-java by census-instrumentation.
the class PrometheusStatsCollectorTest method testCollect.
@Test
public void testCollect() {
PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager, "");
assertThat(collector.collect()).containsExactly(new MetricFamilySamples(METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Arrays.asList(new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "k2", "le"), Arrays.asList("v1", "v2", "1.0"), 0), new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "k2", "le"), Arrays.asList("v1", "v2", "2.0"), 2), new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "k2", "le"), Arrays.asList("v1", "v2", "5.0"), 4), new Sample(METRIC_NAME + "_bucket", Arrays.asList("k1", "k2", "le"), Arrays.asList("v1", "v2", "+Inf"), 5), new Sample(METRIC_NAME + "_count", Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 5), new Sample(METRIC_NAME + "_sum", Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 22.0))));
}
Aggregations