Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,7 @@ flexible messaging model and an intuitive client API.</description>
<exclude>**/*.so</exclude>
<exclude>**/*.so.*</exclude>
<exclude>**/*.dylib</exclude>
<exclude>src/test/resources/*.txt</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.text.NumberFormat;
import java.util.Arrays;
Expand All @@ -49,6 +50,7 @@
import java.util.regex.Pattern;
import javax.crypto.SecretKey;
import javax.naming.AuthenticationException;
import org.apache.commons.io.IOUtils;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
Expand All @@ -73,7 +75,7 @@
@Test(groups = "flaky")
public class PrometheusMetricsTest extends BrokerTestBase {

@BeforeMethod
@BeforeMethod(alwaysRun = true)
@Override
protected void setup() throws Exception {
super.baseSetup();
Expand Down Expand Up @@ -893,9 +895,6 @@ public String getCommandData() {
provider.close();
}

// Investigate issue
// java.lang.AssertionError: line pulsar_broker_publish_latency{cluster="test",quantile="0.0"} +Inf does not match pattern
// ^(\w+)\{([^\}]+)\}\s(-?[\d\w\.-]+)(\s(\d+))?$ expected [true] but found [false]
@Test
public void testExpiringTokenMetrics() throws Exception {
SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
Expand Down Expand Up @@ -957,9 +956,26 @@ public String getCommandData() {
provider.close();
}

// Investigate issue
// java.lang.AssertionError: line pulsar_broker_publish_latency{cluster="test",quantile="0.0"} +Inf does not match pattern
// ^(\w+)\{([^\}]+)\}\s(-?[\d\w\.-]+)(\s(\d+))?$ expected [true] but found [false]
@Test
public void testParsingWithPositiveInfinityValue() {
Multimap<String, Metric> metrics = parseMetrics("pulsar_broker_publish_latency{cluster=\"test\",quantile=\"0.0\"} +Inf");
List<Metric> cm = (List<Metric>) metrics.get("pulsar_broker_publish_latency");
assertEquals(cm.size(), 1);
assertEquals(cm.get(0).tags.get("cluster"), "test");
assertEquals(cm.get(0).tags.get("quantile"), "0.0");
assertEquals(cm.get(0).value, Double.POSITIVE_INFINITY);
}

@Test
public void testParsingWithNegativeInfinityValue() {
Multimap<String, Metric> metrics = parseMetrics("pulsar_broker_publish_latency{cluster=\"test\",quantile=\"0.0\"} -Inf");
List<Metric> cm = (List<Metric>) metrics.get("pulsar_broker_publish_latency");
assertEquals(cm.size(), 1);
assertEquals(cm.get(0).tags.get("cluster"), "test");
assertEquals(cm.get(0).tags.get("quantile"), "0.0");
assertEquals(cm.get(0).value, Double.NEGATIVE_INFINITY);
}

@Test
public void testManagedCursorPersistStats() throws Exception {
final String subName = "my-sub";
Expand Down Expand Up @@ -1083,6 +1099,12 @@ private void compareBrokerConnectionStateCount(List<Metric> cm, double count) {
assertEquals(cm.get(0).value, count);
}

@Test
void testParseMetrics() throws IOException {
String sampleMetrics = IOUtils.toString(getClass().getClassLoader()
.getResourceAsStream("prometheus_metrics_sample.txt"), StandardCharsets.UTF_8);
parseMetrics(sampleMetrics);
}

/**
* Hacky parsing of Prometheus text format. Should be good enough for unit tests
Expand All @@ -1095,7 +1117,7 @@ private static Multimap<String, Metric> parseMetrics(String metrics) {
// or
// pulsar_subscriptions_count{cluster="standalone", namespace="sample/standalone/ns1",
// topic="persistent://sample/standalone/ns1/test-2"} 0.0 1517945780897
Pattern pattern = Pattern.compile("^(\\w+)\\{([^\\}]+)\\}\\s(-?[\\d\\w\\.-]+)(\\s(\\d+))?$");
Pattern pattern = Pattern.compile("^(\\w+)\\{([^\\}]+)\\}\\s([+-]?[\\d\\w\\.-]+)(\\s(\\d+))?$");
Pattern tagsPattern = Pattern.compile("(\\w+)=\"([^\"]+)\"(,\\s?)?");

Splitter.on("\n").split(metrics).forEach(line -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.functions.worker;

import java.nio.charset.StandardCharsets;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -32,6 +33,8 @@
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.testng.annotations.Test;

import static com.google.common.base.Preconditions.checkArgument;

Expand All @@ -51,8 +54,19 @@ public static String getPrometheusMetrics(int metricsPort) throws IOException {
return result.toString();
}

@Test
void testParseMetrics() throws IOException {
String sampleMetrics = IOUtils.toString(getClass().getClassLoader()
.getResourceAsStream("prometheus_metrics_sample.txt"), StandardCharsets.UTF_8);
parseMetrics(sampleMetrics);
}

/**
* Hacky parsing of Prometheus text format. Sould be good enough for unit tests
* Hacky parsing of Prometheus text format. Should be good enough for unit tests
*
* This parser doesn't handle parsing multiple lines for a single metric key.
* There another implementation in {@link org.apache.pulsar.broker.stats.PrometheusMetricsTest}
* that supports parsing multiple lines.
*/
public static Map<String, Metric> parseMetrics(String metrics) {
final Map<String, Metric> parsed = new HashMap<>();
Expand All @@ -61,15 +75,14 @@ public static Map<String, Metric> parseMetrics(String metrics) {
// or
// pulsar_subscriptions_count{cluster="standalone", namespace="sample/standalone/ns1",
// topic="persistent://sample/standalone/ns1/test-2"} 0.0 1517945780897
Pattern pattern = Pattern.compile("^(\\w+)(\\{[^\\}]+\\})?\\s(-?[\\d\\w\\.-]+)(\\s(\\d+))?$");
Pattern pattern = Pattern.compile("^(\\w+)(\\{[^\\}]+\\})?\\s([+-]?[\\d\\w\\.-]+)(\\s(\\d+))?$");
Pattern tagsPattern = Pattern.compile("(\\w+)=\"([^\"]+)\"(,\\s?)?");
Arrays.asList(metrics.split("\n")).forEach(line -> {
if (line.isEmpty() || line.startsWith("#")) {
return;
}
Matcher matcher = pattern.matcher(line);
log.info("line: {}", line);
checkArgument(matcher.matches());
checkArgument(matcher.matches(), "Cannot parse metrics from line: '" + line + "'");
String name = matcher.group(1);
Metric m = new Metric();
String numericValue = matcher.group(3);
Expand Down
Loading