Skip to content

Commit c9475f9

Browse files
committed
switched to use a static variable. test tweaks.
1 parent 1930110 commit c9475f9

File tree

7 files changed

+17
-13
lines changed

7 files changed

+17
-13
lines changed

simpleclient/src/main/java/io/prometheus/client/Collector.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -149,27 +149,27 @@ public MetricFamilySamples filter(Predicate<String> sampleNameFilter) {
149149
* {@code # HELP}), and as this name <a href="https://github.com/prometheus/common/issues/319">must be unique</a>
150150
* we include the name without suffix here as well.
151151
*/
152-
public String[] getNames() {
152+
public List<String> getNames() {
153153
List<String> names = new ArrayList<String>();
154154
switch (type) {
155155
case COUNTER:
156156
names.add(name + "_total");
157-
if (getUseCreated()) {
157+
if (USE_CREATED) {
158158
names.add(name + "_created");
159159
}
160160
break;
161161
case SUMMARY:
162162
names.add(name + "_count");
163163
names.add(name + "_sum");
164-
if (getUseCreated()) {
164+
if (USE_CREATED) {
165165
names.add(name + "_created");
166166
}
167167
break;
168168
case HISTOGRAM:
169169
names.add(name + "_count");
170170
names.add(name + "_sum");
171171
names.add(name + "_bucket");
172-
if (getUseCreated()) {
172+
if (USE_CREATED) {
173173
names.add(name + "_created");
174174
}
175175
break;
@@ -185,7 +185,7 @@ public String[] getNames() {
185185
// NOP - `name` is added to all
186186
}
187187
names.add(name);
188-
return names.toArray(new String[0]);
188+
return names;
189189
}
190190

191191

@@ -398,6 +398,8 @@ public static String doubleToGoString(double d) {
398398
protected static final String DISABLE_CREATED_SERIES = "PROMETHEUS_DISABLE_CREATED_SERIES";
399399
private static final List<String> TRUTHS = Arrays.asList("true", "1", "t");
400400

401+
protected static boolean USE_CREATED = getUseCreated();
402+
401403
protected static boolean getUseCreated() {
402404
String disable_series = System.getenv(DISABLE_CREATED_SERIES);
403405
if (disable_series != null) {

simpleclient/src/main/java/io/prometheus/client/CollectorRegistry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private List<String> collectorNames(Collector m) {
117117

118118
List<String> names = new ArrayList<String>();
119119
for (Collector.MetricFamilySamples family : mfs) {
120-
names.addAll(Arrays.asList(family.getNames()));
120+
names.addAll(family.getNames());
121121
}
122122
return names;
123123
}

simpleclient/src/main/java/io/prometheus/client/Counter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public List<MetricFamilySamples> collect() {
355355
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
356356
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
357357
samples.add(new MetricFamilySamples.Sample(fullname + "_total", labelNames, c.getKey(), c.getValue().get(), c.getValue().getExemplar()));
358-
if (getUseCreated()) {
358+
if (USE_CREATED) {
359359
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), c.getValue().created() / 1000.0));
360360
}
361361
}

simpleclient/src/main/java/io/prometheus/client/Histogram.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ public List<MetricFamilySamples> collect() {
574574
}
575575
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.buckets[buckets.length-1]));
576576
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
577-
if (getUseCreated()) {
577+
if (USE_CREATED) {
578578
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
579579
}
580580
}

simpleclient/src/main/java/io/prometheus/client/Summary.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public List<MetricFamilySamples> collect() {
404404
}
405405
samples.add(new MetricFamilySamples.Sample(fullname + "_count", labelNames, c.getKey(), v.count));
406406
samples.add(new MetricFamilySamples.Sample(fullname + "_sum", labelNames, c.getKey(), v.sum));
407-
if (getUseCreated()) {
407+
if (USE_CREATED) {
408408
samples.add(new MetricFamilySamples.Sample(fullname + "_created", labelNames, c.getKey(), v.created / 1000.0));
409409
}
410410
}

simpleclient/src/test/java/io/prometheus/client/DisableCreatedTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void setUp() {
1818
}
1919

2020
@Test
21-
public void testDisableCreatedAll() {
21+
public void testDisableCreatedAll() throws Exception {
2222
EnvironmentVariables env = new EnvironmentVariables(Collector.DISABLE_CREATED_SERIES, "true");
2323
Counter counter1 = Counter.build()
2424
.name("counter1")
@@ -47,12 +47,14 @@ public void testDisableCreatedAll() {
4747
List<Collector.MetricFamilySamples> mfsList;
4848
try {
4949
env.setup();
50+
Collector.USE_CREATED = Collector.getUseCreated();
5051
mfsList = Collections.list(registry.metricFamilySamples());
5152
String extracted = System.getenv(Collector.DISABLE_CREATED_SERIES);
5253
Assert.assertEquals("Env isn't set", "true", extracted);
54+
}
55+
finally {
5356
env.teardown();
54-
} catch (Exception e) {
55-
throw new RuntimeException(e);
57+
Collector.USE_CREATED = Collector.getUseCreated();
5658
}
5759
assertSamplesInclude(mfsList, "counter1_total", 2);
5860
assertSamplesInclude(mfsList, "counter1_created", 0);

simpleclient/src/test/java/io/prometheus/client/HistogramTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private void assertExemplar(Histogram histogram, double value, String... labels)
299299
}
300300
if (lowerBound < value && value <= upperBound) {
301301
Assert.assertNotNull("No exemplar found in bucket [" + lowerBound + ", " + upperBound + "]", bucket.exemplar);
302-
Assert.assertEquals(value, bucket.exemplar.getValue(), 0.001);
302+
Assert.assertEquals(value, bucket.exemplar.getValue(), 0.01);
303303
Assert.assertEquals(labels.length/2, bucket.exemplar.getNumberOfLabels());
304304
for (int i=0; i<labels.length; i+=2) {
305305
Assert.assertEquals(labels[i], bucket.exemplar.getLabelName(i/2));

0 commit comments

Comments
 (0)