Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit d62a2e7

Browse files
authored
Exporter/Prometheus: Allow to set custom namespace. (#1850)
* Exporter/Prometheus: Allow to set custom namespace. * Fix wording.
1 parent 1a08c36 commit d62a2e7

6 files changed

Lines changed: 133 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- Add HTTP text format serializer to Tag propagation component.
33
- Support constant labels in Gauge APIs.
44
- Add an option to allow users to override the default "opencensus_task" metric label in Stackdriver Stats Exporter.
5+
- Allow setting custom namespace in Prometheus exporter.
56

67
## 0.20.0 - 2019-03-28
78
- Add OpenCensus Java OC-Agent Trace Exporter.

exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtils.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
* <p>Each OpenCensus {@link Metric} will be converted to a Prometheus {@link MetricFamilySamples},
5656
* and each {@code Point} of the {@link Metric} will be converted to Prometheus {@link Sample}s.
5757
*
58-
* <p>{@link io.opencensus.metrics.export.Value.ValueDouble}, {@link
58+
* <p>{@code io.opencensus.metrics.export.Value.ValueDouble}, {@code
5959
* io.opencensus.metrics.export.Value.ValueLong} will be converted to a single {@link Sample}.
60-
* {@link io.opencensus.metrics.export.Value.ValueSummary} will be converted to two {@link Sample}s
61-
* sum and count. {@link io.opencensus.metrics.export.Value.ValueDistribution} will be converted to
60+
* {@code io.opencensus.metrics.export.Value.ValueSummary} will be converted to two {@code Sample}s
61+
* sum and count. {@code io.opencensus.metrics.export.Value.ValueDistribution} will be converted to
6262
* a list of {@link Sample}s that have the sum, count and histogram buckets.
6363
*
6464
* <p>{@link LabelKey} and {@link LabelValue} will be converted to Prometheus {@code LabelName} and
@@ -76,9 +76,9 @@ final class PrometheusExportUtils {
7676
@VisibleForTesting static final String LABEL_NAME_QUANTILE = "quantile";
7777

7878
// Converts a Metric to a Prometheus MetricFamilySamples.
79-
static MetricFamilySamples createMetricFamilySamples(Metric metric) {
79+
static MetricFamilySamples createMetricFamilySamples(Metric metric, String namespace) {
8080
MetricDescriptor metricDescriptor = metric.getMetricDescriptor();
81-
String name = Collector.sanitizeMetricName(metricDescriptor.getName());
81+
String name = getNamespacedName(metricDescriptor.getName(), namespace);
8282
Type type = getType(metricDescriptor.getType());
8383
List<String> labelNames = convertToLabelNames(metricDescriptor.getLabelKeys());
8484
List<Sample> samples = Lists.newArrayList();
@@ -94,8 +94,8 @@ static MetricFamilySamples createMetricFamilySamples(Metric metric) {
9494
// Converts a MetricDescriptor to a Prometheus MetricFamilySamples.
9595
// Used only for Prometheus metric registry, should not contain any actual samples.
9696
static MetricFamilySamples createDescribableMetricFamilySamples(
97-
MetricDescriptor metricDescriptor) {
98-
String name = Collector.sanitizeMetricName(metricDescriptor.getName());
97+
MetricDescriptor metricDescriptor, String namespace) {
98+
String name = getNamespacedName(metricDescriptor.getName(), namespace);
9999
Type type = getType(metricDescriptor.getType());
100100
List<String> labelNames = convertToLabelNames(metricDescriptor.getLabelKeys());
101101

@@ -116,6 +116,16 @@ static MetricFamilySamples createDescribableMetricFamilySamples(
116116
name, type, metricDescriptor.getDescription(), Collections.<Sample>emptyList());
117117
}
118118

119+
private static String getNamespacedName(String metricName, String namespace) {
120+
if (!namespace.isEmpty()) {
121+
if (!namespace.endsWith("/") && !namespace.endsWith("_")) {
122+
namespace += '_';
123+
}
124+
metricName = namespace + metricName;
125+
}
126+
return Collector.sanitizeMetricName(metricName);
127+
}
128+
119129
@VisibleForTesting
120130
static Type getType(MetricDescriptor.Type type) {
121131
if (type == MetricDescriptor.Type.CUMULATIVE_INT64

exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollector.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public final class PrometheusStatsCollector extends Collector implements Collect
5252
private static final String EXPORT_METRICS_TO_PROMETHEUS = "ExportMetricsToPrometheus";
5353
private final MetricReader collectMetricReader;
5454
private final MetricReader describeMetricReader;
55+
private final String namespace;
5556

5657
/**
5758
* Creates a {@link PrometheusStatsCollector} and registers it to Prometheus {@link
@@ -68,7 +69,7 @@ public final class PrometheusStatsCollector extends Collector implements Collect
6869
* @since 0.12
6970
*/
7071
public static void createAndRegister() {
71-
new PrometheusStatsCollector(Metrics.getExportComponent().getMetricProducerManager())
72+
new PrometheusStatsCollector(Metrics.getExportComponent().getMetricProducerManager(), "")
7273
.register();
7374
}
7475

@@ -88,12 +89,18 @@ public static void createAndRegister(PrometheusStatsConfiguration configuration)
8889
if (registry == null) {
8990
registry = CollectorRegistry.defaultRegistry;
9091
}
91-
new PrometheusStatsCollector(Metrics.getExportComponent().getMetricProducerManager())
92+
new PrometheusStatsCollector(
93+
Metrics.getExportComponent().getMetricProducerManager(), configuration.getNamespace())
9294
.register(registry);
9395
}
9496

9597
private static final class ExportMetricExporter extends MetricExporter {
9698
private final ArrayList<MetricFamilySamples> samples = new ArrayList<>();
99+
private final String namespace;
100+
101+
private ExportMetricExporter(String namespace) {
102+
this.namespace = namespace;
103+
}
97104

98105
@Override
99106
public void export(Collection<Metric> metrics) {
@@ -111,7 +118,7 @@ public void export(Collection<Metric> metrics) {
111118
continue;
112119
}
113120
try {
114-
samples.add(PrometheusExportUtils.createMetricFamilySamples(metric));
121+
samples.add(PrometheusExportUtils.createMetricFamilySamples(metric, namespace));
115122
} catch (Throwable e) {
116123
logger.log(Level.WARNING, "Exception thrown when collecting metric samples.", e);
117124
tracer
@@ -127,13 +134,18 @@ public void export(Collection<Metric> metrics) {
127134

128135
@Override
129136
public List<MetricFamilySamples> collect() {
130-
ExportMetricExporter exportMetricExporter = new ExportMetricExporter();
137+
ExportMetricExporter exportMetricExporter = new ExportMetricExporter(namespace);
131138
collectMetricReader.readAndExport(exportMetricExporter);
132139
return exportMetricExporter.samples;
133140
}
134141

135142
private static final class DescribeMetricExporter extends MetricExporter {
136143
private final ArrayList<MetricFamilySamples> samples = new ArrayList<>();
144+
private final String namespace;
145+
146+
private DescribeMetricExporter(String namespace) {
147+
this.namespace = namespace;
148+
}
137149

138150
@Override
139151
public void export(Collection<Metric> metrics) {
@@ -142,7 +154,7 @@ public void export(Collection<Metric> metrics) {
142154
try {
143155
samples.add(
144156
PrometheusExportUtils.createDescribableMetricFamilySamples(
145-
metric.getMetricDescriptor()));
157+
metric.getMetricDescriptor(), namespace));
146158
} catch (Throwable e) {
147159
logger.log(Level.WARNING, "Exception thrown when describing metrics.", e);
148160
tracer
@@ -158,13 +170,13 @@ public void export(Collection<Metric> metrics) {
158170

159171
@Override
160172
public List<MetricFamilySamples> describe() {
161-
DescribeMetricExporter describeMetricExporter = new DescribeMetricExporter();
173+
DescribeMetricExporter describeMetricExporter = new DescribeMetricExporter(namespace);
162174
describeMetricReader.readAndExport(describeMetricExporter);
163175
return describeMetricExporter.samples;
164176
}
165177

166178
@VisibleForTesting
167-
PrometheusStatsCollector(MetricProducerManager metricProducerManager) {
179+
PrometheusStatsCollector(MetricProducerManager metricProducerManager, String namespace) {
168180
this.collectMetricReader =
169181
MetricReader.create(
170182
MetricReader.Options.builder()
@@ -177,6 +189,7 @@ public List<MetricFamilySamples> describe() {
177189
.setMetricProducerManager(metricProducerManager)
178190
.setSpanName(DESCRIBE_METRICS_FOR_PROMETHEUS)
179191
.build());
192+
this.namespace = namespace;
180193
}
181194

182195
private static String exceptionMessage(Throwable e) {

exporters/stats/prometheus/src/main/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsConfiguration.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.google.auto.value.AutoValue;
2020
import io.prometheus.client.CollectorRegistry;
21-
import javax.annotation.Nullable;
2221
import javax.annotation.concurrent.Immutable;
2322

2423
/**
@@ -38,17 +37,26 @@ public abstract class PrometheusStatsConfiguration {
3837
* @return the Prometheus {@code CollectorRegistry}.
3938
* @since 0.13
4039
*/
41-
@Nullable
4240
public abstract CollectorRegistry getRegistry();
4341

42+
/**
43+
* Returns the namespace used for Prometheus metrics.
44+
*
45+
* @return the namespace.
46+
* @since 0.21
47+
*/
48+
public abstract String getNamespace();
49+
4450
/**
4551
* Returns a new {@link Builder}.
4652
*
4753
* @return a {@code Builder}.
4854
* @since 0.13
4955
*/
5056
public static Builder builder() {
51-
return new AutoValue_PrometheusStatsConfiguration.Builder();
57+
return new AutoValue_PrometheusStatsConfiguration.Builder()
58+
.setRegistry(CollectorRegistry.defaultRegistry)
59+
.setNamespace("");
5260
}
5361

5462
/**
@@ -70,6 +78,15 @@ public abstract static class Builder {
7078
*/
7179
public abstract Builder setRegistry(CollectorRegistry registry);
7280

81+
/**
82+
* Sets the namespace used for Prometheus metrics.
83+
*
84+
* @param namespace the namespace.
85+
* @return this.
86+
* @since 0.21
87+
*/
88+
public abstract Builder setNamespace(String namespace);
89+
7390
/**
7491
* Builds a new {@link PrometheusStatsConfiguration} with current settings.
7592
*

exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusExportUtilsTest.java

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,49 @@ public void getType() {
188188
public void createDescribableMetricFamilySamples() {
189189
assertThat(
190190
PrometheusExportUtils.createDescribableMetricFamilySamples(
191-
CUMULATIVE_METRIC_DESCRIPTOR))
191+
CUMULATIVE_METRIC_DESCRIPTOR, ""))
192192
.isEqualTo(
193193
new MetricFamilySamples(
194194
METRIC_NAME, Type.COUNTER, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
195195
assertThat(
196-
PrometheusExportUtils.createDescribableMetricFamilySamples(SUMMARY_METRIC_DESCRIPTOR))
196+
PrometheusExportUtils.createDescribableMetricFamilySamples(
197+
SUMMARY_METRIC_DESCRIPTOR, ""))
197198
.isEqualTo(
198199
new MetricFamilySamples(
199200
METRIC_NAME2, Type.SUMMARY, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
200201
assertThat(
201-
PrometheusExportUtils.createDescribableMetricFamilySamples(HISTOGRAM_METRIC_DESCRIPTOR))
202+
PrometheusExportUtils.createDescribableMetricFamilySamples(
203+
HISTOGRAM_METRIC_DESCRIPTOR, ""))
202204
.isEqualTo(
203205
new MetricFamilySamples(
204206
METRIC_NAME3, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
205207
}
206208

209+
@Test
210+
public void createDescribableMetricFamilySamples_WithNamespace() {
211+
String namespace1 = "myorg";
212+
assertThat(
213+
PrometheusExportUtils.createDescribableMetricFamilySamples(
214+
CUMULATIVE_METRIC_DESCRIPTOR, namespace1))
215+
.isEqualTo(
216+
new MetricFamilySamples(
217+
namespace1 + '_' + METRIC_NAME,
218+
Type.COUNTER,
219+
METRIC_DESCRIPTION,
220+
Collections.<Sample>emptyList()));
221+
222+
String namespace2 = "opencensus/";
223+
assertThat(
224+
PrometheusExportUtils.createDescribableMetricFamilySamples(
225+
CUMULATIVE_METRIC_DESCRIPTOR, namespace2))
226+
.isEqualTo(
227+
new MetricFamilySamples(
228+
"opencensus_" + METRIC_NAME,
229+
Type.COUNTER,
230+
METRIC_DESCRIPTION,
231+
Collections.<Sample>emptyList()));
232+
}
233+
207234
@Test
208235
public void getSamples() {
209236
assertThat(
@@ -306,7 +333,7 @@ public void createDescribableMetricFamilySamples_Histogram_DisallowLeLabelName()
306333
"Prometheus Histogram cannot have a label named 'le', "
307334
+ "because it is a reserved label for bucket boundaries. "
308335
+ "Please remove this key from your view.");
309-
PrometheusExportUtils.createDescribableMetricFamilySamples(LE_LABEL_METRIC_DESCRIPTOR);
336+
PrometheusExportUtils.createDescribableMetricFamilySamples(LE_LABEL_METRIC_DESCRIPTOR, "");
310337
}
311338

312339
@Test
@@ -315,12 +342,13 @@ public void createDescribableMetricFamilySamples_Summary_DisallowQuantileLabelNa
315342
thrown.expectMessage(
316343
"Prometheus Summary cannot have a label named 'quantile', "
317344
+ "because it is a reserved label. Please remove this key from your view.");
318-
PrometheusExportUtils.createDescribableMetricFamilySamples(QUANTILE_LABEL_METRIC_DESCRIPTOR);
345+
PrometheusExportUtils.createDescribableMetricFamilySamples(
346+
QUANTILE_LABEL_METRIC_DESCRIPTOR, "");
319347
}
320348

321349
@Test
322350
public void createMetricFamilySamples() {
323-
assertThat(PrometheusExportUtils.createMetricFamilySamples(LONG_METRIC))
351+
assertThat(PrometheusExportUtils.createMetricFamilySamples(LONG_METRIC, ""))
324352
.isEqualTo(
325353
new MetricFamilySamples(
326354
METRIC_NAME,
@@ -332,7 +360,7 @@ public void createMetricFamilySamples() {
332360
Arrays.asList("k1", "k2"),
333361
Arrays.asList("v1", "v2"),
334362
123456789))));
335-
assertThat(PrometheusExportUtils.createMetricFamilySamples(SUMMARY_METRIC))
363+
assertThat(PrometheusExportUtils.createMetricFamilySamples(SUMMARY_METRIC, ""))
336364
.isEqualTo(
337365
new MetricFamilySamples(
338366
METRIC_NAME2,
@@ -354,7 +382,7 @@ public void createMetricFamilySamples() {
354382
Arrays.asList("k_3", LABEL_NAME_QUANTILE),
355383
Arrays.asList("v1", "0.99"),
356384
10.2))));
357-
assertThat(PrometheusExportUtils.createMetricFamilySamples(DISTRIBUTION_METRIC))
385+
assertThat(PrometheusExportUtils.createMetricFamilySamples(DISTRIBUTION_METRIC, ""))
358386
.isEqualTo(
359387
new MetricFamilySamples(
360388
METRIC_NAME3,
@@ -392,4 +420,21 @@ public void createMetricFamilySamples() {
392420
Collections.singletonList("v-3"),
393421
22.0))));
394422
}
423+
424+
@Test
425+
public void createMetricFamilySamples_WithNamespace() {
426+
String namespace = "opencensus_";
427+
assertThat(PrometheusExportUtils.createMetricFamilySamples(LONG_METRIC, namespace))
428+
.isEqualTo(
429+
new MetricFamilySamples(
430+
namespace + METRIC_NAME,
431+
Type.COUNTER,
432+
METRIC_DESCRIPTION,
433+
Collections.singletonList(
434+
new Sample(
435+
namespace + METRIC_NAME,
436+
Arrays.asList("k1", "k2"),
437+
Arrays.asList("v1", "v2"),
438+
123456789))));
439+
}
395440
}

exporters/stats/prometheus/src/test/java/io/opencensus/exporter/stats/prometheus/PrometheusStatsCollectorTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public void setUp() {
108108

109109
@Test
110110
public void testCollect() {
111-
PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager);
111+
PrometheusStatsCollector collector =
112+
new PrometheusStatsCollector(mockMetricProducerManager, "");
112113
assertThat(collector.collect())
113114
.containsExactly(
114115
new MetricFamilySamples(
@@ -151,32 +152,48 @@ public void testCollect() {
151152
@Test
152153
public void testCollect_SkipDistributionMetricWithLeLabelKey() {
153154
doReturn(Collections.singletonList(LE_LABEL_METRIC)).when(mockMetricProducer).getMetrics();
154-
PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager);
155+
PrometheusStatsCollector collector =
156+
new PrometheusStatsCollector(mockMetricProducerManager, "");
155157
assertThat(collector.collect()).isEmpty();
156158
}
157159

158160
@Test
159161
public void testDescribe() {
160-
PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager);
162+
PrometheusStatsCollector collector =
163+
new PrometheusStatsCollector(mockMetricProducerManager, "");
161164
assertThat(collector.describe())
162165
.containsExactly(
163166
new MetricFamilySamples(
164167
METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
165168
}
166169

170+
@Test
171+
public void testDescribe_WithNamespace() {
172+
String namespace = "myorg";
173+
PrometheusStatsCollector collector =
174+
new PrometheusStatsCollector(mockMetricProducerManager, namespace);
175+
assertThat(collector.describe())
176+
.containsExactly(
177+
new MetricFamilySamples(
178+
namespace + '_' + METRIC_NAME,
179+
Type.HISTOGRAM,
180+
METRIC_DESCRIPTION,
181+
Collections.<Sample>emptyList()));
182+
}
183+
167184
@Test
168185
public void testCollect_WithNoopViewManager() {
169186
PrometheusStatsCollector collector =
170187
new PrometheusStatsCollector(
171-
ExportComponent.newNoopExportComponent().getMetricProducerManager());
188+
ExportComponent.newNoopExportComponent().getMetricProducerManager(), "");
172189
assertThat(collector.collect()).isEmpty();
173190
}
174191

175192
@Test
176193
public void testDescribe_WithNoopViewManager() {
177194
PrometheusStatsCollector collector =
178195
new PrometheusStatsCollector(
179-
ExportComponent.newNoopExportComponent().getMetricProducerManager());
196+
ExportComponent.newNoopExportComponent().getMetricProducerManager(), "");
180197
assertThat(collector.describe()).isEmpty();
181198
}
182199
}

0 commit comments

Comments
 (0)