-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display indicator data in KEY, VALUE format (#12273)
* metrics default. * check style * test fail * metrics default update * qos * test * default metrics reporter * native image * If no specific metrics type is configured and there is no Prometheus dependency in the dependencies. * prometheus set * MeterRegistry dependency check * errorcode check fail * CHECKSTYLE * 1.null check 2.method replace * default metricsreport register * format response --------- Co-authored-by: Albumen Kevin <jhq0812@gmail.com> Co-authored-by: songxiaosheng <songxiaosheng@elastic.link>
- Loading branch information
1 parent
50798b9
commit 1697a33
Showing
12 changed files
with
303 additions
and
8 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
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
100 changes: 100 additions & 0 deletions
100
...metrics-default/src/main/java/org/apache/dubbo/metrics/report/DefaultMetricsReporter.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,100 @@ | ||
/* | ||
* 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.dubbo.metrics.report; | ||
|
||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.Gauge; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Timer; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
|
||
import org.apache.dubbo.common.URL; | ||
import org.apache.dubbo.rpc.model.ApplicationModel; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class DefaultMetricsReporter extends AbstractMetricsReporter { | ||
|
||
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry(); | ||
|
||
protected DefaultMetricsReporter(URL url, ApplicationModel applicationModel) { | ||
super(url, applicationModel); | ||
} | ||
|
||
@Override | ||
public String getResponse() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getResponseWithName(String metricsName) { | ||
Map<String, List<Tag>> metricsTags = new HashMap<>(); | ||
Map<String, Object> metricsValue = new HashMap<>(); | ||
StringBuilder sb = new StringBuilder(); | ||
meterRegistry.getMeters().stream().filter(meter -> { | ||
if (meter == null || meter.getId() == null || meter.getId().getName() == null) { | ||
return false; | ||
} | ||
if (metricsName != null) { | ||
return meter.getId().getName().contains(metricsName); | ||
} | ||
return true; | ||
}).forEach(meter -> { | ||
Object value = null; | ||
if (meter instanceof Counter) { | ||
Counter counter = (Counter) meter; | ||
value = counter.count(); | ||
} | ||
if (meter instanceof Gauge) { | ||
Gauge gauge = (Gauge) meter; | ||
value = gauge.value(); | ||
} | ||
if (meter instanceof Timer) { | ||
Timer timer = (Timer) meter; | ||
value = timer.totalTime(TimeUnit.MILLISECONDS); | ||
} | ||
metricsTags.put(meter.getId().getName(), meter.getId().getTags()); | ||
metricsValue.put(meter.getId().getName(), value); | ||
}); | ||
metricsValue.forEach((key, value) -> { | ||
sb.append(key).append("{"); | ||
List<Tag> tags = metricsTags.get(key); | ||
if (tags != null && tags.size() > 0) { | ||
tags.forEach(tag -> { | ||
sb.append(tag.getKey()).append("=").append(tag.getValue()).append(","); | ||
}); | ||
} | ||
sb.append("} ").append(value).append(System.lineSeparator()); | ||
}); | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
protected void doInit() { | ||
addMeterRegistry(meterRegistry); | ||
} | ||
|
||
@Override | ||
protected void doDestroy() { | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...-default/src/main/java/org/apache/dubbo/metrics/report/DefaultMetricsReporterFactory.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,35 @@ | ||
/* | ||
* 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.dubbo.metrics.report; | ||
|
||
import org.apache.dubbo.common.URL; | ||
import org.apache.dubbo.rpc.model.ApplicationModel; | ||
|
||
public class DefaultMetricsReporterFactory extends AbstractMetricsReporterFactory { | ||
private final ApplicationModel applicationModel; | ||
|
||
public DefaultMetricsReporterFactory(ApplicationModel applicationModel) { | ||
super(applicationModel); | ||
this.applicationModel = applicationModel; | ||
} | ||
|
||
@Override | ||
public MetricsReporter createMetricsReporter(URL url) { | ||
return new DefaultMetricsReporter(url, applicationModel); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...ain/resources/META-INF/dubbo/internal/org.apache.dubbo.metrics.collector.MetricsCollector
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
default-collector=org.apache.dubbo.metrics.collector.DefaultMetricsCollector | ||
aggregateMetricsCollector=org.apache.dubbo.metrics.collector.AggregateMetricsCollector | ||
histogramMetricsCollector=org.apache.dubbo.metrics.collector.HistogramMetricsCollector |
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/dubbo/internal/org.apache.dubbo.metrics.report.MetricsReporterFactory
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 @@ | ||
default=org.apache.dubbo.metrics.report.DefaultMetricsReporterFactory |
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
121 changes: 121 additions & 0 deletions
121
.../dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/DefaultMetricsReporterCmd.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,121 @@ | ||
/* | ||
* 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.dubbo.qos.command.impl; | ||
|
||
import org.apache.dubbo.common.utils.JsonUtils; | ||
import org.apache.dubbo.metrics.report.DefaultMetricsReporter; | ||
import org.apache.dubbo.metrics.report.MetricsReporter; | ||
import org.apache.dubbo.qos.api.BaseCommand; | ||
import org.apache.dubbo.qos.api.Cmd; | ||
import org.apache.dubbo.qos.api.CommandContext; | ||
import org.apache.dubbo.rpc.model.ApplicationModel; | ||
import org.apache.dubbo.rpc.model.FrameworkModel; | ||
|
||
import java.io.CharArrayReader; | ||
import java.io.IOException; | ||
import java.io.LineNumberReader; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Cmd(name = "metrics_default", summary = "display metrics information") | ||
public class DefaultMetricsReporterCmd implements BaseCommand { | ||
|
||
public FrameworkModel frameworkModel; | ||
|
||
public DefaultMetricsReporterCmd(FrameworkModel frameworkModel) { | ||
this.frameworkModel = frameworkModel; | ||
} | ||
|
||
@Override | ||
public String execute(CommandContext commandContext, String[] args) { | ||
List<ApplicationModel> models = frameworkModel.getApplicationModels(); | ||
String result = "There is no application with data"; | ||
if (notSpecifyApplication(args)) { | ||
result = useFirst(models, result, null); | ||
} else if (args.length == 1) { | ||
result = specifyApplication(args[0], models, null); | ||
} else if (args.length == 2) { | ||
result = specifyApplication(args[0], models, args[1]); | ||
} | ||
return result; | ||
} | ||
|
||
private boolean notSpecifyApplication(String[] args) { | ||
return args == null || args.length == 0; | ||
} | ||
|
||
private String useFirst(List<ApplicationModel> models, String result, String metricsName) { | ||
for (ApplicationModel model : models) { | ||
String current = getResponseByApplication(model, metricsName); | ||
if (current != null && getLineNumber(current) > 0) { | ||
result = current; | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private String specifyApplication(String appName, List<ApplicationModel> models, String metricsName) { | ||
if ("application_all".equals(appName)) { | ||
return allApplication(models); | ||
} else { | ||
return specifySingleApplication(appName, models, metricsName); | ||
} | ||
} | ||
|
||
private String specifySingleApplication(String appName, List<ApplicationModel> models, String metricsName) { | ||
Optional<ApplicationModel> modelOptional = models.stream() | ||
.filter(applicationModel -> appName.equals(applicationModel.getApplicationName())).findFirst(); | ||
if (modelOptional.isPresent()) { | ||
return getResponseByApplication(modelOptional.get(), metricsName); | ||
} else { | ||
return "Not exist application: " + appName; | ||
} | ||
} | ||
|
||
private String allApplication(List<ApplicationModel> models) { | ||
Map<String, String> appResultMap = new HashMap<>(); | ||
for (ApplicationModel model : models) { | ||
appResultMap.put(model.getApplicationName(), getResponseByApplication(model, null)); | ||
} | ||
return JsonUtils.toJson(appResultMap); | ||
} | ||
|
||
private String getResponseByApplication(ApplicationModel applicationModel, String metricsName) { | ||
String response = "DefaultMetricsReporter not init"; | ||
MetricsReporter metricsReporter = applicationModel.getBeanFactory().getBean(DefaultMetricsReporter.class); | ||
if (metricsReporter != null) { | ||
metricsReporter.refreshData(); | ||
response = metricsReporter.getResponseWithName(metricsName); | ||
} | ||
return response; | ||
} | ||
|
||
|
||
private static long getLineNumber(String content) { | ||
LineNumberReader lnr = new LineNumberReader(new CharArrayReader(content.toCharArray())); | ||
try { | ||
lnr.skip(Long.MAX_VALUE); | ||
lnr.close(); | ||
} catch (IOException ignore) { | ||
} | ||
return lnr.getLineNumber(); | ||
} | ||
} |
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