diff options
author | Yang Song <songy23@users.noreply.github.com> | 2018-05-02 17:04:00 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-02 17:04:00 -0700 |
commit | 3838c4c4b150fad4e10671c9e37f36c894a9c535 (patch) | |
tree | 16638ed5817a09b479e7e860c88f008506e97fc4 /exporters | |
parent | e830beea50125ae280c010b37d7fa77b45f37e9d (diff) | |
download | opencensus-java-3838c4c4b150fad4e10671c9e37f36c894a9c535.tar.gz |
Set bucket bounds as le labels for Prometheus exporter. (#1167)
Closes #1164.
Previously we don't set the bucket bounds for Prometheus Samples,
since bucket bounds is a built-in feature for Prometheus Histogram.
However, as #1164 suggested, we can manually set the bucket bounds
with the same label le that the built-in Histogram uses,
to get the same output as the built-in Prometheus Histogram Samples.
Diffstat (limited to 'exporters')
5 files changed, 214 insertions, 53 deletions
diff --git a/exporters/stats/prometheus/README.md b/exporters/stats/prometheus/README.md index 9a874c29..afe340b4 100644 --- a/exporters/stats/prometheus/README.md +++ b/exporters/stats/prometheus/README.md @@ -78,4 +78,4 @@ and [Bridges](https://github.com/prometheus/client_java#bridges). Java 7 or above is required for using this exporter. -## FAQ
\ No newline at end of file +## FAQ diff --git a/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtils.java b/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtils.java index e745b594..6999b75b 100644 --- a/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtils.java +++ b/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtils.java @@ -16,6 +16,8 @@ package io.opencensus.exporter.stats.prometheus; +import static io.prometheus.client.Collector.doubleToGoString; + import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; @@ -86,6 +88,7 @@ final class PrometheusExportUtils { @VisibleForTesting static final String SAMPLE_SUFFIX_BUCKET = "_bucket"; @VisibleForTesting static final String SAMPLE_SUFFIX_COUNT = "_count"; @VisibleForTesting static final String SAMPLE_SUFFIX_SUM = "_sum"; + @VisibleForTesting static final String LABEL_NAME_BUCKET_BOUND = "le"; // Converts a ViewData to a Prometheus MetricFamilySamples. static MetricFamilySamples createMetricFamilySamples(ViewData viewData) { @@ -93,10 +96,12 @@ final class PrometheusExportUtils { String name = Collector.sanitizeMetricName(OPENCENSUS_NAMESPACE + '_' + view.getName().asString()); Type type = getType(view.getAggregation(), view.getWindow()); + List<String> labelNames = convertToLabelNames(view.getColumns()); List<Sample> samples = Lists.newArrayList(); for (Entry<List</*@Nullable*/ TagValue>, AggregationData> entry : viewData.getAggregationMap().entrySet()) { - samples.addAll(getSamples(name, view.getColumns(), entry.getKey(), entry.getValue())); + samples.addAll( + getSamples(name, labelNames, entry.getKey(), entry.getValue(), view.getAggregation())); } return new MetricFamilySamples( name, type, OPENCENSUS_HELP_MSG + view.getDescription(), samples); @@ -108,6 +113,13 @@ final class PrometheusExportUtils { String name = Collector.sanitizeMetricName(OPENCENSUS_NAMESPACE + '_' + view.getName().asString()); Type type = getType(view.getAggregation(), view.getWindow()); + List<String> labelNames = convertToLabelNames(view.getColumns()); + if (containsDisallowedLeLabelForHistogram(labelNames, type)) { + throw new IllegalStateException( + "Prometheus Histogram cannot have a label named 'le', " + + "because it is a reserved label for bucket boundaries. " + + "Please remove this tag key from your view."); + } return new MetricFamilySamples( name, type, OPENCENSUS_HELP_MSG + view.getDescription(), Collections.<Sample>emptyList()); } @@ -133,20 +145,19 @@ final class PrometheusExportUtils { }); } + // Converts a row in ViewData (a.k.a Entry<List<TagValue>, AggregationData>) to a list of + // Prometheus Samples. @VisibleForTesting static List<Sample> getSamples( final String name, - List<TagKey> tagKeys, + final List<String> labelNames, List</*@Nullable*/ TagValue> tagValues, - AggregationData aggregationData) { + AggregationData aggregationData, + final Aggregation aggregation) { Preconditions.checkArgument( - tagKeys.size() == tagValues.size(), "Tag keys and tag values have different sizes."); + labelNames.size() == tagValues.size(), "Label names and tag values have different sizes."); final List<Sample> samples = Lists.newArrayList(); - final List<String> labelNames = new ArrayList<String>(tagKeys.size()); final List<String> labelValues = new ArrayList<String>(tagValues.size()); - for (TagKey tagKey : tagKeys) { - labelNames.add(Collector.sanitizeMetricName(tagKey.getName())); - } for (TagValue tagValue : tagValues) { String labelValue = tagValue == null ? "" : tagValue.asString(); labelValues.add(labelValue); @@ -177,11 +188,29 @@ final class PrometheusExportUtils { new Function<DistributionData, Void>() { @Override public Void apply(DistributionData arg) { - for (long bucketCount : arg.getBucketCounts()) { + // For histogram buckets, manually add the bucket boundaries as "le" labels. See + // https://github.com/prometheus/client_java/commit/ed184d8e50c82e98bb2706723fff764424840c3a#diff-c505abbde72dd6bf36e89917b3469404R241 + @SuppressWarnings("unchecked") + Distribution distribution = (Distribution) aggregation; + List<Double> boundaries = distribution.getBucketBoundaries().getBoundaries(); + List<String> labelNamesWithLe = new ArrayList<String>(labelNames); + labelNamesWithLe.add(LABEL_NAME_BUCKET_BOUND); + for (int i = 0; i < arg.getBucketCounts().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); samples.add( new MetricFamilySamples.Sample( - name + SAMPLE_SUFFIX_BUCKET, labelNames, labelValues, bucketCount)); + name + SAMPLE_SUFFIX_BUCKET, + labelNamesWithLe, + labelValuesWithLe, + arg.getBucketCounts().get(i))); } + samples.add( new MetricFamilySamples.Sample( name + SAMPLE_SUFFIX_COUNT, labelNames, labelValues, arg.getCount())); @@ -234,5 +263,30 @@ final class PrometheusExportUtils { return samples; } + // Converts the list of tag keys to a list of string label names. Also sanitizes the tag keys. + @VisibleForTesting + static List<String> convertToLabelNames(List<TagKey> tagKeys) { + final List<String> labelNames = new ArrayList<String>(tagKeys.size()); + for (TagKey tagKey : tagKeys) { + labelNames.add(Collector.sanitizeMetricName(tagKey.getName())); + } + return labelNames; + } + + // Returns true if there is an "le" label name in histogram label names, returns false otherwise. + // Similar check to + // https://github.com/prometheus/client_java/commit/ed184d8e50c82e98bb2706723fff764424840c3a#diff-c505abbde72dd6bf36e89917b3469404R78 + static boolean containsDisallowedLeLabelForHistogram(List<String> labelNames, Type type) { + if (!Type.HISTOGRAM.equals(type)) { + return false; + } + for (String label : labelNames) { + if (LABEL_NAME_BUCKET_BOUND.equals(label)) { + return true; + } + } + return false; + } + private PrometheusExportUtils() {} } diff --git a/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollector.java b/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollector.java index 884d3ccb..2f4d189d 100644 --- a/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollector.java +++ b/exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollector.java @@ -16,6 +16,10 @@ package io.opencensus.exporter.stats.prometheus; +import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.containsDisallowedLeLabelForHistogram; +import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.convertToLabelNames; +import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.getType; + import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -39,6 +43,7 @@ import java.util.logging.Logger; * * @since 0.12 */ +@SuppressWarnings("deprecation") public final class PrometheusStatsCollector extends Collector implements Collector.Describable { private static final Logger logger = Logger.getLogger(PrometheusStatsCollector.class.getName()); @@ -90,20 +95,27 @@ public final class PrometheusStatsCollector extends Collector implements Collect span.addAnnotation("Collect Prometheus Metric Samples."); try (Scope scope = tracer.withSpan(span)) { for (View view : viewManager.getAllExportedViews()) { - ViewData viewData = viewManager.getView(view.getName()); - if (viewData == null) { - continue; - } else { - samples.add(PrometheusExportUtils.createMetricFamilySamples(viewData)); + if (containsDisallowedLeLabelForHistogram( + convertToLabelNames(view.getColumns()), + getType(view.getAggregation(), view.getWindow()))) { + continue; // silently skip Distribution views with "le" tag key + } + try { + ViewData viewData = viewManager.getView(view.getName()); + if (viewData == null) { + continue; + } else { + samples.add(PrometheusExportUtils.createMetricFamilySamples(viewData)); + } + } catch (Throwable e) { + logger.log(Level.WARNING, "Exception thrown when collecting metric samples.", e); + span.setStatus( + Status.UNKNOWN.withDescription( + "Exception thrown when collecting Prometheus Metric Samples: " + + exceptionMessage(e))); } } span.addAnnotation("Finish collecting Prometheus Metric Samples."); - } catch (Throwable e) { - logger.log(Level.WARNING, "Exception thrown when collecting metric samples.", e); - span.setStatus( - Status.UNKNOWN.withDescription( - "Exception thrown when collecting Prometheus Metric Samples: " - + exceptionMessage(e))); } finally { span.end(); } @@ -118,14 +130,16 @@ public final class PrometheusStatsCollector extends Collector implements Collect span.addAnnotation("Describe Prometheus Metrics."); try (Scope scope = tracer.withSpan(span)) { for (View view : viewManager.getAllExportedViews()) { - samples.add(PrometheusExportUtils.createDescribableMetricFamilySamples(view)); + try { + samples.add(PrometheusExportUtils.createDescribableMetricFamilySamples(view)); + } catch (Throwable e) { + logger.log(Level.WARNING, "Exception thrown when describing metrics.", e); + span.setStatus( + Status.UNKNOWN.withDescription( + "Exception thrown when describing Prometheus Metrics: " + exceptionMessage(e))); + } } span.addAnnotation("Finish describing Prometheus Metrics."); - } catch (Throwable e) { - logger.log(Level.WARNING, "Exception thrown when describing metrics.", e); - span.setStatus( - Status.UNKNOWN.withDescription( - "Exception thrown when describing Prometheus Metrics: " + exceptionMessage(e))); } finally { span.end(); } diff --git a/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtilsTest.java b/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtilsTest.java index 507fd3b6..1d112a31 100644 --- a/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtilsTest.java +++ b/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtilsTest.java @@ -17,11 +17,13 @@ package io.opencensus.exporter.stats.prometheus; import static com.google.common.truth.Truth.assertThat; +import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.LABEL_NAME_BUCKET_BOUND; import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.OPENCENSUS_HELP_MSG; import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.OPENCENSUS_NAMESPACE; import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.SAMPLE_SUFFIX_BUCKET; import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.SAMPLE_SUFFIX_COUNT; import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.SAMPLE_SUFFIX_SUM; +import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.convertToLabelNames; import com.google.common.collect.ImmutableMap; import io.opencensus.common.Duration; @@ -85,6 +87,7 @@ public class PrometheusExportUtilsTest { private static final TagKey K1 = TagKey.create("k1"); private static final TagKey K2 = TagKey.create("k2"); private static final TagKey K3 = TagKey.create("k-3"); + private static final TagKey TAG_KEY_LE = TagKey.create(LABEL_NAME_BUCKET_BOUND); private static final TagValue V1 = TagValue.create("v1"); private static final TagValue V2 = TagValue.create("v2"); private static final TagValue V3 = TagValue.create("v-3"); @@ -106,6 +109,14 @@ public class PrometheusExportUtilsTest { VIEW_NAME_3, DESCRIPTION, MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(K1), CUMULATIVE); private static final View VIEW4 = View.create(VIEW_NAME_4, DESCRIPTION, MEASURE_DOUBLE, COUNT, Arrays.asList(K1), INTERVAL); + private static final View DISTRIBUTION_VIEW_WITH_LE_KEY = + View.create( + VIEW_NAME_1, + DESCRIPTION, + MEASURE_DOUBLE, + DISTRIBUTION, + Arrays.asList(K1, TAG_KEY_LE), + CUMULATIVE); private static final CumulativeData CUMULATIVE_DATA = CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(2000)); private static final IntervalData INTERVAL_DATA = IntervalData.create(Timestamp.fromMillis(1000)); @@ -118,6 +129,7 @@ public class PrometheusExportUtilsTest { assertThat(SAMPLE_SUFFIX_BUCKET).isEqualTo("_bucket"); assertThat(SAMPLE_SUFFIX_COUNT).isEqualTo("_count"); assertThat(SAMPLE_SUFFIX_SUM).isEqualTo("_sum"); + assertThat(LABEL_NAME_BUCKET_BOUND).isEqualTo("le"); } @Test @@ -166,45 +178,77 @@ public class PrometheusExportUtilsTest { public void getSamples() { assertThat( PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K1, K2), Arrays.asList(V1, V2), SUM_DATA_DOUBLE)) + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K1, K2)), + Arrays.asList(V1, V2), + SUM_DATA_DOUBLE, + SUM)) .containsExactly( new Sample(SAMPLE_NAME, Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), -5.5)); assertThat( PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K3), Arrays.asList(V3), SUM_DATA_LONG)) + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K3)), + Arrays.asList(V3), + SUM_DATA_LONG, + SUM)) .containsExactly( new Sample(SAMPLE_NAME, Arrays.asList("k_3"), Arrays.asList("v-3"), 123456789)); assertThat( PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K1, K3), Arrays.asList(V1, null), COUNT_DATA)) + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K1, K3)), + Arrays.asList(V1, null), + COUNT_DATA, + COUNT)) .containsExactly( new Sample(SAMPLE_NAME, Arrays.asList("k1", "k_3"), Arrays.asList("v1", ""), 12345)); assertThat( PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K3), Arrays.asList(V3), MEAN_DATA)) + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K3)), + Arrays.asList(V3), + MEAN_DATA, + MEAN)) .containsExactly( new Sample(SAMPLE_NAME + "_count", Arrays.asList("k_3"), Arrays.asList("v-3"), 22), new Sample(SAMPLE_NAME + "_sum", Arrays.asList("k_3"), Arrays.asList("v-3"), 74.8)) .inOrder(); assertThat( PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K1), Arrays.asList(V1), DISTRIBUTION_DATA)) + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K1)), + Arrays.asList(V1), + DISTRIBUTION_DATA, + DISTRIBUTION)) .containsExactly( - new Sample(SAMPLE_NAME + "_bucket", Arrays.asList("k1"), Arrays.asList("v1"), 0), - new Sample(SAMPLE_NAME + "_bucket", Arrays.asList("k1"), Arrays.asList("v1"), 2), - new Sample(SAMPLE_NAME + "_bucket", Arrays.asList("k1"), Arrays.asList("v1"), 2), - new Sample(SAMPLE_NAME + "_bucket", Arrays.asList("k1"), Arrays.asList("v1"), 1), + new Sample( + SAMPLE_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "-5.0"), 0), + new Sample( + SAMPLE_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "0.0"), 2), + new Sample( + SAMPLE_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "5.0"), 2), + new Sample( + SAMPLE_NAME + "_bucket", Arrays.asList("k1", "le"), Arrays.asList("v1", "+Inf"), 1), new Sample(SAMPLE_NAME + "_count", Arrays.asList("k1"), Arrays.asList("v1"), 5), new Sample(SAMPLE_NAME + "_sum", Arrays.asList("k1"), Arrays.asList("v1"), 22.0)) .inOrder(); assertThat( PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K1, K2), Arrays.asList(V1, V2), LAST_VALUE_DATA_DOUBLE)) + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K1, K2)), + Arrays.asList(V1, V2), + LAST_VALUE_DATA_DOUBLE, + LAST_VALUE)) .containsExactly( new Sample(SAMPLE_NAME, Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 7.9)); assertThat( PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K3), Arrays.asList(V3), LAST_VALUE_DATA_LONG)) + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K3)), + Arrays.asList(V3), + LAST_VALUE_DATA_LONG, + LAST_VALUE)) .containsExactly( new Sample(SAMPLE_NAME, Arrays.asList("k_3"), Arrays.asList("v-3"), 66666666)); } @@ -212,9 +256,23 @@ public class PrometheusExportUtilsTest { @Test public void getSamples_KeysAndValuesHaveDifferentSizes() { thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Tag keys and tag values have different sizes."); + thrown.expectMessage("Label names and tag values have different sizes."); PrometheusExportUtils.getSamples( - SAMPLE_NAME, Arrays.asList(K1, K2, K3), Arrays.asList(V1, V2), DISTRIBUTION_DATA); + SAMPLE_NAME, + convertToLabelNames(Arrays.asList(K1, K2, K3)), + Arrays.asList(V1, V2), + DISTRIBUTION_DATA, + DISTRIBUTION); + } + + @Test + public void createDescribableMetricFamilySamples_Histogram_DisallowLeLabelName() { + thrown.expect(IllegalStateException.class); + thrown.expectMessage( + "Prometheus Histogram cannot have a label named 'le', " + + "because it is a reserved label for bucket boundaries. " + + "Please remove this tag key from your view."); + PrometheusExportUtils.createDescribableMetricFamilySamples(DISTRIBUTION_VIEW_WITH_LE_KEY); } @Test @@ -266,23 +324,23 @@ public class PrometheusExportUtilsTest { Arrays.asList( new Sample( OPENCENSUS_NAMESPACE + "_view_3_bucket", - Arrays.asList("k1"), - Arrays.asList("v-3"), + Arrays.asList("k1", "le"), + Arrays.asList("v-3", "-5.0"), 0), new Sample( OPENCENSUS_NAMESPACE + "_view_3_bucket", - Arrays.asList("k1"), - Arrays.asList("v-3"), + Arrays.asList("k1", "le"), + Arrays.asList("v-3", "0.0"), 2), new Sample( OPENCENSUS_NAMESPACE + "_view_3_bucket", - Arrays.asList("k1"), - Arrays.asList("v-3"), + Arrays.asList("k1", "le"), + Arrays.asList("v-3", "5.0"), 2), new Sample( OPENCENSUS_NAMESPACE + "_view_3_bucket", - Arrays.asList("k1"), - Arrays.asList("v-3"), + Arrays.asList("k1", "le"), + Arrays.asList("v-3", "+Inf"), 1), new Sample( OPENCENSUS_NAMESPACE + "_view_3_count", diff --git a/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollectorTest.java b/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollectorTest.java index 73f72dba..3f97ac3c 100644 --- a/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollectorTest.java +++ b/exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollectorTest.java @@ -17,6 +17,7 @@ package io.opencensus.exporter.stats.prometheus; import static com.google.common.truth.Truth.assertThat; +import static io.opencensus.exporter.stats.prometheus.PrometheusExportUtils.LABEL_NAME_BUCKET_BOUND; import static org.mockito.Mockito.doReturn; import com.google.common.collect.ImmutableMap; @@ -60,6 +61,7 @@ public class PrometheusStatsCollectorTest { MeasureDouble.create("measure", "description", "1"); private static final TagKey K1 = TagKey.create("k1"); private static final TagKey K2 = TagKey.create("k2"); + private static final TagKey LE_TAG_KEY = TagKey.create(LABEL_NAME_BUCKET_BOUND); private static final TagValue V1 = TagValue.create("v1"); private static final TagValue V2 = TagValue.create("v2"); private static final DistributionData DISTRIBUTION_DATA = @@ -67,11 +69,24 @@ public class PrometheusStatsCollectorTest { private static final View VIEW = View.create( VIEW_NAME, DESCRIPTION, MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(K1, K2), CUMULATIVE); + private static final View VIEW_WITH_LE_TAG_KEY = + View.create( + VIEW_NAME, + DESCRIPTION, + MEASURE_DOUBLE, + DISTRIBUTION, + Arrays.asList(K1, LE_TAG_KEY), + CUMULATIVE); private static final CumulativeData CUMULATIVE_DATA = CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(2000)); private static final ViewData VIEW_DATA = ViewData.create( VIEW, ImmutableMap.of(Arrays.asList(V1, V2), DISTRIBUTION_DATA), CUMULATIVE_DATA); + private static final ViewData VIEW_DATA_WITH_LE_TAG_KEY = + ViewData.create( + VIEW_WITH_LE_TAG_KEY, + ImmutableMap.of(Arrays.asList(V1, V2), DISTRIBUTION_DATA), + CUMULATIVE_DATA); @Mock private ViewManager mockViewManager; @@ -94,13 +109,25 @@ public class PrometheusStatsCollectorTest { "Opencensus Prometheus metrics: View description", Arrays.asList( new Sample( - name + "_bucket", Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 0), + name + "_bucket", + Arrays.asList("k1", "k2", "le"), + Arrays.asList("v1", "v2", "-5.0"), + 0), new Sample( - name + "_bucket", Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 2), + name + "_bucket", + Arrays.asList("k1", "k2", "le"), + Arrays.asList("v1", "v2", "0.0"), + 2), new Sample( - name + "_bucket", Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 2), + name + "_bucket", + Arrays.asList("k1", "k2", "le"), + Arrays.asList("v1", "v2", "5.0"), + 2), new Sample( - name + "_bucket", Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 1), + name + "_bucket", + Arrays.asList("k1", "k2", "le"), + Arrays.asList("v1", "v2", "+Inf"), + 1), new Sample( name + "_count", Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"), 5), new Sample( @@ -111,6 +138,14 @@ public class PrometheusStatsCollectorTest { } @Test + public void testCollect_SkipDistributionViewWithLeTagKey() { + doReturn(ImmutableSet.of(VIEW_WITH_LE_TAG_KEY)).when(mockViewManager).getAllExportedViews(); + doReturn(VIEW_DATA_WITH_LE_TAG_KEY).when(mockViewManager).getView(VIEW_NAME); + PrometheusStatsCollector collector = new PrometheusStatsCollector(mockViewManager); + assertThat(collector.collect()).isEmpty(); + } + + @Test public void testDescribe() { PrometheusStatsCollector collector = new PrometheusStatsCollector(mockViewManager); assertThat(collector.describe()) |