Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
aa1aeb0
[fix][broker] Ensure prometheus metrics are grouped by type (#8407)
marksilcox May 11, 2022
0dfa415
[fix][functions-worker] Ensure prometheus metrics are grouped by type…
marksilcox May 12, 2022
56259be
[fix][broker] remove extra SimpleTextOutputStream and use prometheus …
marksilcox May 13, 2022
8b5e247
[fix][broker] ensure new line added before appending new keys from en…
marksilcox May 14, 2022
2cb3923
[fix][broker] Use `Collector.doubleToGoString`
marksilcox Jun 15, 2022
447582b
[fix][broker] Update to ensure topic values are still rounded
marksilcox Jun 16, 2022
a865a00
[fix][broker] Fix licence header (again)
marksilcox Jun 16, 2022
357d4fa
[fix][broker] Update writing to use existing data structures
marksilcox Jun 23, 2022
8182afe
Update pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/Pro…
marksilcox Jun 28, 2022
818de83
[fix][broker] Update writing to use ByteBuf
marksilcox Jul 1, 2022
d84f48d
[fix][broker] Updated stream output
marksilcox Jul 4, 2022
d6c7af8
[fix][broker] PR Comments
marksilcox Jul 7, 2022
93461d6
[fix][broker] Fix failing test after merge
marksilcox Jul 19, 2022
7b30fb3
[fix][broker] Updates from PR comments and new test
marksilcox Aug 3, 2022
a31ebfb
[fix][broker] Updates from PR comments
marksilcox Aug 8, 2022
5f9d66d
formatting
marksilcox Aug 21, 2022
ed0e97f
[fix][broker] fixing cherry-pick conflicts
marksilcox Dec 14, 2022
30446c2
[fix][broker] fixing cherry-pick conflicts
marksilcox Dec 14, 2022
21ec9c0
Update license header
marksilcox Dec 15, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void updateStats(TopicStats stats) {

stats.replicationStats.forEach((n, as) -> {
AggregatedReplicationStats replStats =
replicationStats.computeIfAbsent(n, k -> new AggregatedReplicationStats());
replicationStats.computeIfAbsent(n, k -> new AggregatedReplicationStats());
replStats.msgRateIn += as.msgRateIn;
replStats.msgRateOut += as.msgRateOut;
replStats.msgThroughputIn += as.msgThroughputIn;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* 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 java.util.HashMap;
import java.util.Map;
import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
import org.apache.pulsar.common.util.SimpleTextOutputStream;

/**
* Helper class to ensure that metrics of the same name are grouped together under the same TYPE header when written.
* Those are the requirements of the
* <a href="https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md#grouping-and-sorting">Prometheus Exposition Format</a>.
*/
public class PrometheusMetricStreams {
private final Map<String, SimpleTextOutputStream> metricStreamMap = new HashMap<>();

/**
* Write the given metric and sample value to the stream. Will write #TYPE header if metric not seen before.
* @param metricName name of the metric.
* @param value value of the sample
* @param labelsAndValuesArray varargs of label and label value
*/
void writeSample(String metricName, Number value, String... labelsAndValuesArray) {
SimpleTextOutputStream stream = initGaugeType(metricName);
stream.write(metricName).write('{');
for (int i = 0; i < labelsAndValuesArray.length; i += 2) {
stream.write(labelsAndValuesArray[i]).write("=\"").write(labelsAndValuesArray[i + 1]).write('\"');
if (i + 2 != labelsAndValuesArray.length) {
stream.write(',');
}
}
stream.write("} ").write(value).write(' ').write(System.currentTimeMillis()).write('\n');
}

/**
* Flush all the stored metrics to the supplied stream.
* @param stream the stream to write to.
*/
void flushAllToStream(SimpleTextOutputStream stream) {
metricStreamMap.values().forEach(s -> stream.write(s.getBuffer()));
}

/**
* Release all the streams to clean up resources.
*/
void releaseAll() {
metricStreamMap.values().forEach(s -> s.getBuffer().release());
metricStreamMap.clear();
}

private SimpleTextOutputStream initGaugeType(String metricName) {
return metricStreamMap.computeIfAbsent(metricName, s -> {
SimpleTextOutputStream stream = new SimpleTextOutputStream(PulsarByteBufAllocator.DEFAULT.directBuffer());
stream.write("# TYPE ").write(metricName).write(" gauge\n");
return stream;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
/**
* Generate metrics aggregated at the namespace level and optionally at a topic level and formats them out
* in a text format suitable to be consumed by Prometheus.
* Format specification can be found at {@link https://prometheus.io/docs/instrumenting/exposition_formats/}
* Format specification can be found at <a
* href="https://prometheus.io/docs/instrumenting/exposition_formats/">Exposition Formats</a>
*/
public class PrometheusMetricsGenerator {

Expand Down Expand Up @@ -85,38 +86,43 @@ public double get() {
}

public static void generate(PulsarService pulsar, boolean includeTopicMetrics, boolean includeConsumerMetrics,
boolean includeProducerMetrics, OutputStream out) throws IOException {
boolean includeProducerMetrics, OutputStream out) throws IOException {
generate(pulsar, includeTopicMetrics, includeConsumerMetrics, includeProducerMetrics, false, out, null);
}

public static void generate(PulsarService pulsar, boolean includeTopicMetrics, boolean includeConsumerMetrics,
boolean includeProducerMetrics, boolean splitTopicAndPartitionIndexLabel,
OutputStream out) throws IOException {
boolean includeProducerMetrics, boolean splitTopicAndPartitionIndexLabel,
OutputStream out) throws IOException {
generate(pulsar, includeTopicMetrics, includeConsumerMetrics, includeProducerMetrics,
splitTopicAndPartitionIndexLabel, out, null);
}

public static void generate(PulsarService pulsar, boolean includeTopicMetrics, boolean includeConsumerMetrics,
boolean includeProducerMetrics, boolean splitTopicAndPartitionIndexLabel, OutputStream out,
List<PrometheusRawMetricsProvider> metricsProviders)
throws IOException {
boolean includeProducerMetrics, boolean splitTopicAndPartitionIndexLabel,
OutputStream out,
List<PrometheusRawMetricsProvider> metricsProviders)
throws IOException {
ByteBuf buf = PulsarByteBufAllocator.DEFAULT.heapBuffer();
//Used in namespace/topic and transaction aggregators as share metric names
PrometheusMetricStreams metricStreams = new PrometheusMetricStreams();
try {
SimpleTextOutputStream stream = new SimpleTextOutputStream(buf);

generateSystemMetrics(stream, pulsar.getConfiguration().getClusterName());

NamespaceStatsAggregator.generate(pulsar, includeTopicMetrics, includeConsumerMetrics,
includeProducerMetrics, splitTopicAndPartitionIndexLabel, stream);
includeProducerMetrics, splitTopicAndPartitionIndexLabel, metricStreams);

if (pulsar.getWorkerServiceOpt().isPresent()) {
pulsar.getWorkerService().generateFunctionsStats(stream);
}

if (pulsar.getConfiguration().isTransactionCoordinatorEnabled()) {
TransactionAggregator.generate(pulsar, stream, includeTopicMetrics);
TransactionAggregator.generate(pulsar, metricStreams, includeTopicMetrics);
}

metricStreams.flushAllToStream(stream);

generateBrokerBasicMetrics(pulsar, stream);

generateManagedLedgerBookieClientMetrics(pulsar, stream);
Expand All @@ -128,6 +134,8 @@ public static void generate(PulsarService pulsar, boolean includeTopicMetrics, b
}
out.write(buf.array(), buf.arrayOffset(), buf.readableBytes());
} finally {
//release all the metrics buffers
metricStreams.releaseAll();
buf.release();
}
}
Expand All @@ -141,17 +149,17 @@ private static void generateBrokerBasicMetrics(PulsarService pulsar, SimpleTextO
if (pulsar.getConfiguration().isExposeManagedLedgerMetricsInPrometheus()) {
// generate managedLedger metrics
parseMetricsToPrometheusMetrics(new ManagedLedgerMetrics(pulsar).generate(),
clusterName, Collector.Type.GAUGE, stream);
clusterName, Collector.Type.GAUGE, stream);
}

if (pulsar.getConfiguration().isExposeManagedCursorMetricsInPrometheus()) {
// generate managedCursor metrics
parseMetricsToPrometheusMetrics(new ManagedCursorMetrics(pulsar).generate(),
clusterName, Collector.Type.GAUGE, stream);
clusterName, Collector.Type.GAUGE, stream);
}

parseMetricsToPrometheusMetrics(Collections.singletonList(pulsar.getBrokerService()
.getPulsarStats().getBrokerOperabilityMetrics().generateConnectionMetrics()),
.getPulsarStats().getBrokerOperabilityMetrics().generateConnectionMetrics()),
clusterName, Collector.Type.GAUGE, stream);

// generate loadBalance metrics
Expand Down
Loading