-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[branch-2.9] Group prometheus metrics. #17852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
codelipenghui
merged 1 commit into
apache:branch-2.9
from
tjiuming:dev/group_prometheus_metrics
Sep 29, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
352 changes: 185 additions & 167 deletions
352
...ker/src/main/java/org/apache/pulsar/broker/stats/prometheus/NamespaceStatsAggregator.java
Large diffs are not rendered by default.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
...oker/src/main/java/org/apache/pulsar/broker/stats/prometheus/PrometheusMetricStreams.java
This file contains hidden or 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,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; | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tjiuming I noticed you had removed the
exceptionHappens, if we don't have the check, will we release thebufmultiple times?From the master branch:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codelipenghui No, this check is for #14453, the feature didn't check-pick to 2.9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tjiuming will the
bufcan get a chance to be released multiple times? Since themetricStreams.releaseAll()will also get a chance to release thebuf?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codelipenghui
bufandmetricStreamsonly released in thefinallyscope, andmetricStreams.releaseAll()will not releasebufUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for missing out on this bug. I wasn't aware that
PrometheusMetricsGeneratordoesn't look the same in master and in apache-2.9, specifically the generate0, and the performance improvement was backported, so it confused me as well.The fix looks solid, as this buffer is not returned as it did in
generate0so it should always be released.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tjiuming Oh, I see. They are different buffers.