Skip to content
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

[BugFix] Add HistogramMetric to support histogram with tags (backport #52782) #52806

Closed
wants to merge 1 commit into from
Closed
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
66 changes: 66 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/metric/HistogramMetric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 com.starrocks.metric;

import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Histogram;
import com.google.api.client.util.Lists;
import com.google.common.base.Joiner;

import java.util.List;
import java.util.stream.Collectors;

/**
* Histogram metric with tags to distinguish different metrics with the same name.
* e.g. mv_refresh_duration{mv_db_name="db1", mv_name="mv1"}
*/
public final class HistogramMetric extends Histogram {
private final List<MetricLabel> labels = Lists.newArrayList();
private final String name;

public HistogramMetric(String name) {
super(new ExponentiallyDecayingReservoir());
this.name = name;
}

public void addLabel(MetricLabel label) {
labels.add(label);
}

public String getName() {
return name;
}

public String getTagName() {
List<String> labelStrings = labels.stream().map(l -> l.getKey() + "=\"" + l.getValue()
+ "\"").collect(Collectors.toList());
return Joiner.on(", ").join(labelStrings);
}

/**
* Get the histogram name with tags in the format of "name_tag1=value1, tag2=value2"
*/
public String getHistogramName() {
String tagName = getTagName();
if (!tagName.isEmpty()) {
return name + "_" + tagName;
} else {
return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public void visit(@SuppressWarnings("rawtypes") Metric metric) {
metric.getValue().toString(), labels);
}

@Override
public void visitHistogram(HistogramMetric histogram) {

}

@Override
public void visitHistogram(String name, Histogram histogram) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,20 @@ protected void initMaterializedViewMetrics() {

// histogram metrics
try {
<<<<<<< HEAD
Database db = GlobalStateMgr.getCurrentState().getDb(mvId.getDbId());
MaterializedView mv = (MaterializedView) db.getTable(mvId.getId());
histRefreshJobDuration = metricRegistry.histogram(MetricRegistry.name("mv_refresh_duration",
db.getFullName(), mv.getName()));
=======
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(mvId.getDbId());
MaterializedView mv = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore()
.getTable(db.getId(), mvId.getId());
HistogramMetric histogram = new HistogramMetric("mv_refresh_duration");
histogram.addLabel(new MetricLabel("db_name", db.getFullName()));
histogram.addLabel(new MetricLabel("mv_name", mv.getName()));
histRefreshJobDuration = metricRegistry.histogram(histogram.getHistogramName(), () -> histogram);
>>>>>>> f744c57c46 ([BugFix] Add HistogramMetric to support histogram with tags (#52782))
} catch (Exception e) {
LOG.warn("Ignore histogram metrics for materialized view: {}", mvId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public MetricVisitor(String prefix) {

public abstract void visitHistogram(String name, Histogram histogram);

public abstract void visitHistogram(HistogramMetric histogram);

public abstract void getNodeInfo();

public abstract String build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Snapshot;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.starrocks.monitor.jvm.JvmStats;
import com.starrocks.monitor.jvm.JvmStats.BufferPool;
import com.starrocks.monitor.jvm.JvmStats.GarbageCollector;
Expand Down Expand Up @@ -186,11 +187,42 @@ public void visit(@SuppressWarnings("rawtypes") Metric metric) {
sb.append(" ").append(metric.getValue().toString()).append("\n");
}

@Override
public void visitHistogram(HistogramMetric histogram) {
final String fullName = prefix + "_" + histogram.getName();

if (!metricNames.contains(fullName)) {
sb.append(HELP).append(fullName).append(" ").append("\n");
sb.append(TYPE).append(fullName).append(" ").append("summary\n");
metricNames.add(fullName);
}

Snapshot snapshot = histogram.getSnapshot();
String tagName = histogram.getTagName();
String delimiter = Strings.isNullOrEmpty(tagName) ? "" : ", ";
sb.append(fullName).append("{quantile=\"0.75\"").append(delimiter).append(tagName).append("} ")
.append(snapshot.get75thPercentile()).append("\n");
sb.append(fullName).append("{quantile=\"0.95\"").append(delimiter).append(tagName).append("} ")
.append(snapshot.get95thPercentile()).append("\n");
sb.append(fullName).append("{quantile=\"0.98\"").append(delimiter).append(tagName).append("} ")
.append(snapshot.get98thPercentile()).append("\n");
sb.append(fullName).append("{quantile=\"0.99\"").append(delimiter).append(tagName).append("} ")
.append(snapshot.get99thPercentile()).append("\n");
sb.append(fullName).append("{quantile=\"0.999\"").append(delimiter).append(tagName).append("} ")
.append(snapshot.get999thPercentile()).append("\n");
sb.append(fullName).append("_sum ").append(histogram.getCount() * snapshot.getMean()).append("\n");
sb.append(fullName).append("_count ").append(histogram.getCount()).append("\n");
}

@Override
public void visitHistogram(String name, Histogram histogram) {
final String fullName = prefix + "_" + name.replaceAll("\\.", "_");
sb.append(HELP).append(fullName).append(" ").append("\n");
sb.append(TYPE).append(fullName).append(" ").append("summary\n");

if (!metricNames.contains(fullName)) {
sb.append(HELP).append(fullName).append(" ").append("\n");
sb.append(TYPE).append(fullName).append(" ").append("summary\n");
metricNames.add(fullName);
}

Snapshot snapshot = histogram.getSnapshot();
sb.append(fullName).append("{quantile=\"0.75\"} ").append(snapshot.get75thPercentile()).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public void visit(Metric metric) {
return;
}

@Override
public void visitHistogram(HistogramMetric histogram) {
}

@Override
public void visitHistogram(String name, Histogram histogram) {
if (!CORE_METRICS.containsKey(name)) {
Expand Down
22 changes: 22 additions & 0 deletions fe/fe-core/src/test/java/com/starrocks/metric/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,26 @@ public void testAddLabel() {
Assert.assertEquals(m.getLabels().get(0).getValue(), "v1");
Assert.assertEquals(m.getLabels().get(1).getValue(), "v2");
}

@Test
public void testPrometheusHistogramMetrics() {
PrometheusMetricVisitor prometheusMetricVisitor = new PrometheusMetricVisitor("sr");
HistogramMetric histogramMetric = new HistogramMetric("duration");
histogramMetric.addLabel(new MetricLabel("k1", "v1"));
histogramMetric.addLabel(new MetricLabel("k2", "v2"));
prometheusMetricVisitor.visitHistogram(histogramMetric);
String output = prometheusMetricVisitor.build();
List<String> metricNames = Arrays.asList(
"sr_duration{quantile=\"0.75\", k1=\"v1\", k2=\"v2\"}",
"sr_duration{quantile=\"0.95\", k1=\"v1\", k2=\"v2\"}",
"sr_duration{quantile=\"0.98\", k1=\"v1\", k2=\"v2\"}",
"sr_duration{quantile=\"0.99\", k1=\"v1\", k2=\"v2\"}",
"sr_duration{quantile=\"0.999\", k1=\"v1\", k2=\"v2\"}",
"sr_duration_sum",
"sr_duration_count"
);
for (String metricName : metricNames) {
Assert.assertTrue(output.contains(metricName));
}
}
}
Loading