-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix][metrics]wrong metrics text generated when label_cluster specifi…
…ed (#17704) * [fix][metrics]wrong metrics text generated when label_cluster specified * improve logic branch * mark test group (cherry picked from commit 518cdcd)
- Loading branch information
1 parent
d9594e4
commit 03d8a37
Showing
3 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...t/java/org/apache/pulsar/broker/stats/prometheus/PrometheusMetricsGeneratorUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.stats.prometheus; | ||
|
||
import static org.testng.Assert.*; | ||
import io.prometheus.client.CollectorRegistry; | ||
import io.prometheus.client.Counter; | ||
import java.io.ByteArrayOutputStream; | ||
import java.util.Collections; | ||
import java.util.UUID; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "broker") | ||
public class PrometheusMetricsGeneratorUtilsTest { | ||
|
||
private static final String LABEL_NAME_CLUSTER = "cluster"; | ||
|
||
@Test | ||
public void testGenerateSystemMetricsWithSpecifyCluster() throws Exception { | ||
String defaultClusterValue = "cluster_test"; | ||
String specifyClusterValue = "lb_x"; | ||
String metricsName = "label_contains_cluster" + randomString(); | ||
Counter counter = new Counter.Builder() | ||
.name(metricsName) | ||
.labelNames(LABEL_NAME_CLUSTER) | ||
.help("x") | ||
.register(CollectorRegistry.defaultRegistry); | ||
counter.labels(specifyClusterValue).inc(); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
PrometheusMetricsGeneratorUtils.generate(defaultClusterValue, out, Collections.emptyList()); | ||
assertTrue(out.toString().contains( | ||
String.format("%s_total{cluster=\"%s\"} 1.0", metricsName, specifyClusterValue) | ||
)); | ||
// cleanup | ||
out.close(); | ||
CollectorRegistry.defaultRegistry.unregister(counter); | ||
} | ||
|
||
@Test | ||
public void testGenerateSystemMetricsWithDefaultCluster() throws Exception { | ||
String defaultClusterValue = "cluster_test"; | ||
String labelName = "lb_name"; | ||
String labelValue = "lb_value"; | ||
// default cluster. | ||
String metricsName = "label_use_default_cluster" + randomString(); | ||
Counter counter = new Counter.Builder() | ||
.name(metricsName) | ||
.labelNames(labelName) | ||
.help("x") | ||
.register(CollectorRegistry.defaultRegistry); | ||
counter.labels(labelValue).inc(); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
PrometheusMetricsGeneratorUtils.generate(defaultClusterValue, out, Collections.emptyList()); | ||
assertTrue(out.toString().contains( | ||
String.format("%s_total{cluster=\"%s\",%s=\"%s\"} 1.0", | ||
metricsName, defaultClusterValue, labelName, labelValue) | ||
)); | ||
// cleanup | ||
out.close(); | ||
CollectorRegistry.defaultRegistry.unregister(counter); | ||
} | ||
|
||
@Test | ||
public void testGenerateSystemMetricsWithoutCustomizedLabel() throws Exception { | ||
String defaultClusterValue = "cluster_test"; | ||
// default cluster. | ||
String metricsName = "label_without_customized_label" + randomString(); | ||
Counter counter = new Counter.Builder() | ||
.name(metricsName) | ||
.help("x") | ||
.register(CollectorRegistry.defaultRegistry); | ||
counter.inc(); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
PrometheusMetricsGeneratorUtils.generate(defaultClusterValue, out, Collections.emptyList()); | ||
assertTrue(out.toString().contains( | ||
String.format("%s_total{cluster=\"%s\"} 1.0", metricsName, defaultClusterValue) | ||
)); | ||
// cleanup | ||
out.close(); | ||
CollectorRegistry.defaultRegistry.unregister(counter); | ||
} | ||
|
||
private static String randomString(){ | ||
return UUID.randomUUID().toString().replaceAll("-", ""); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...r-broker-common/src/test/java/org/apache/pulsar/broker/stats/prometheus/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.stats.prometheus; |