forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose compaction metrics to Prometheus (apache#11739)
- Loading branch information
1 parent
c0ef593
commit adcd231
Showing
5 changed files
with
221 additions
and
19 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
122 changes: 122 additions & 0 deletions
122
pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/metrics/CompactionMetrics.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,122 @@ | ||
/** | ||
* 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.metrics; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Maps; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.broker.PulsarServerException; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.common.naming.TopicName; | ||
import org.apache.pulsar.common.stats.Metrics; | ||
import org.apache.pulsar.compaction.Compactor; | ||
import org.apache.pulsar.compaction.CompactorMXBean; | ||
|
||
@Slf4j | ||
public class CompactionMetrics extends AbstractMetrics { | ||
|
||
private List<Metrics> metricsCollection; | ||
private Map<String, Double> tempAggregatedMetricsMap; | ||
private Map<Metrics, List<String>> topicsByDimensionMap; | ||
private Optional<CompactorMXBean> stats; | ||
|
||
public CompactionMetrics(PulsarService pulsar) { | ||
super(pulsar); | ||
this.metricsCollection = Lists.newArrayList(); | ||
this.topicsByDimensionMap = Maps.newHashMap(); | ||
this.tempAggregatedMetricsMap = Maps.newHashMap(); | ||
this.stats = getCompactorMXBean(); | ||
} | ||
|
||
@Override | ||
public synchronized List<Metrics> generate() { | ||
return aggregate(groupTopicsByDimension()); | ||
} | ||
|
||
|
||
/** | ||
* Aggregation by namespace. | ||
* | ||
* @return List<Metrics> | ||
*/ | ||
private List<Metrics> aggregate(Map<Metrics, List<String>> topicsByDimension) { | ||
metricsCollection.clear(); | ||
if (stats.isPresent()) { | ||
CompactorMXBean compactorMXBean = stats.get(); | ||
for (Map.Entry<Metrics, List<String>> entry : topicsByDimension.entrySet()) { | ||
Metrics metrics = entry.getKey(); | ||
List<String> topics = entry.getValue(); | ||
tempAggregatedMetricsMap.clear(); | ||
for (String topic : topics) { | ||
populateAggregationMapWithSum(tempAggregatedMetricsMap, "brk_cp_totalCompactionCount", | ||
compactorMXBean.getTotalCompactionCount(topic)); | ||
|
||
populateAggregationMapWithSum(tempAggregatedMetricsMap, "brk_cp_totalCompactionRemovedEventCount", | ||
compactorMXBean.getTotalCompactionRemovedEventCount(topic)); | ||
|
||
populateAggregationMapWithSum(tempAggregatedMetricsMap, "brk_cp_totalCompactionSucceedCount", | ||
compactorMXBean.getTotalCompactionSucceedCount(topic)); | ||
|
||
populateAggregationMapWithSum(tempAggregatedMetricsMap, "brk_cp_totalCompactionFailedCount", | ||
compactorMXBean.getTotalCompactionFailedCount(topic)); | ||
} | ||
for (Map.Entry<String, Double> ma : tempAggregatedMetricsMap.entrySet()) { | ||
metrics.put(ma.getKey(), ma.getValue()); | ||
} | ||
metricsCollection.add(metrics); | ||
} | ||
} | ||
|
||
return metricsCollection; | ||
} | ||
|
||
private Map<Metrics, List<String>> groupTopicsByDimension() { | ||
topicsByDimensionMap.clear(); | ||
if (stats.isPresent()) { | ||
tempAggregatedMetricsMap.clear(); | ||
for (String topic : stats.get().getTopics()) { | ||
String namespace = TopicName.get(topic).getNamespace(); | ||
Metrics metrics = super.createMetricsByDimension(namespace); | ||
populateDimensionMap(topicsByDimensionMap, metrics, topic); | ||
} | ||
} | ||
return topicsByDimensionMap; | ||
} | ||
|
||
private Optional<CompactorMXBean> getCompactorMXBean() { | ||
Compactor compactor = null; | ||
try { | ||
compactor = pulsar.getCompactor(false); | ||
} catch (PulsarServerException e) { | ||
log.error("get compactor error", e); | ||
} | ||
return Optional.ofNullable(compactor).map(c -> c.getStats()); | ||
} | ||
|
||
private void populateDimensionMap(Map<Metrics, List<String>> topicsByDimensionMap, Metrics metrics, String topic) { | ||
if (!topicsByDimensionMap.containsKey(metrics)) { | ||
topicsByDimensionMap.put(metrics, Lists.newArrayList(topic)); | ||
} else { | ||
topicsByDimensionMap.get(metrics).add(topic); | ||
} | ||
} | ||
} |
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
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
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