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

Display indicator data in KEY, VALUE format #12273

Merged
merged 26 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
69563f3
metrics default.
hujun-w-2 May 8, 2023
01500a1
check style
hujun-w-2 May 8, 2023
c1d22a1
test fail
hujun-w-2 May 14, 2023
4f79639
metrics default update
hujun-w-2 May 14, 2023
46ea1bd
qos
hujun-w-2 May 14, 2023
c4042fe
test
hujun-w-2 May 14, 2023
1598d38
default metrics reporter
hujun-w-2 May 14, 2023
2c38e28
Merge branch '3.2' into 3.2-develop
AlbumenJ May 14, 2023
8a56e6c
Merge branch '3.2' into 3.2-develop
songxiaosheng May 16, 2023
c994e02
Merge branch '3.2' into 3.2-develop
songxiaosheng May 17, 2023
4bab435
native image
hujun-w-2 May 17, 2023
38c3628
If no specific metrics type is configured and there is no Prometheus …
hujun-w-2 May 20, 2023
590ef05
Merge branch '3.2' into 3.2-develop
hujun-w-2 May 20, 2023
f4987eb
prometheus set
hujun-w-2 May 21, 2023
c8fc064
Merge remote-tracking branch 'hujun-github/3.2-develop' into 3.2-develop
hujun-w-2 May 21, 2023
66be2d9
MeterRegistry dependency check
hujun-w-2 May 21, 2023
503670f
errorcode check fail
hujun-w-2 May 21, 2023
51b0d0e
CHECKSTYLE
hujun-w-2 May 21, 2023
709e80f
Merge branch '3.2' into 3.2-develop
songxiaosheng May 22, 2023
98be47c
1.null check
hujun-w-2 May 22, 2023
c73e2ed
default metricsreport register
hujun-w-2 May 25, 2023
cf5fe31
Merge branch '3.2' into 3.2-develop
songxiaosheng May 25, 2023
06d9673
Merge branch '3.2' into 3.2-develop
songxiaosheng May 25, 2023
65533a7
format response
hujun-w-2 May 25, 2023
43e9722
Merge remote-tracking branch 'hujun-github/3.2-develop' into 3.2-develop
hujun-w-2 May 25, 2023
f5f88e2
Merge branch '3.2' into 3.2-develop
songxiaosheng May 26, 2023
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 @@ -372,20 +372,6 @@ private void initMetricsReporter() {
applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class);
Optional<MetricsConfig> configOptional = configManager.getMetrics();

// TODO compatible with old usage of metrics, remove protocol check after new metrics is ready for use.
boolean importMetricsPrometheus; // Use package references instead of config checks
try {
Class.forName("io.micrometer.prometheus.PrometheusConfig");
importMetricsPrometheus = true;
} catch (ClassNotFoundException e) {
importMetricsPrometheus = false;
}

if (!importMetricsPrometheus) {
//use old metrics
return;
}

MetricsConfig metricsConfig = configOptional.orElse(new MetricsConfig(applicationModel));
if (StringUtils.isBlank(metricsConfig.getProtocol())) {
metricsConfig.setProtocol(PROTOCOL_PROMETHEUS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public interface MetricsReporter {
void refreshData();

String getResponse();

default String getResponseWithName(String metricsName) {
return null;
}
}
6 changes: 6 additions & 0 deletions dubbo-metrics/dubbo-metrics-default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@
<artifactId>micrometer-tracing-integration-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-qos-api</artifactId>
hujun-w-2 marked this conversation as resolved.
Show resolved Hide resolved
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.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.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) {
//name->tags->value
Map<String, Map<String, Object>> result = new HashMap<>();
StringBuilder sb = new StringBuilder();
meterRegistry.getMeters().stream().filter(meter -> {
if (metricsName != null) {
return meter.getId().getName().contains(metricsName);
hujun-w-2 marked this conversation as resolved.
Show resolved Hide resolved
}
return true;
}).forEach(meter -> {
result.putIfAbsent(meter.getId().getName(), new HashMap<>());
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);
}
result.get(meter.getId().getName()).put(meter.getId().getTags().toString(), value);
});
result.forEach((name, tags) -> {
sb.append("##metrics name=").append(name).append(System.lineSeparator());
tags.forEach((tag, value) -> {
sb.append(" #tags:").append(tag).append(System.lineSeparator()).append(" #value:").append(value)
.append(System.lineSeparator());
});
});
return sb.toString();
}

@Override
protected void doInit() {
addMeterRegistry(meterRegistry);
}

@Override
protected void doDestroy() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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.utils.JsonUtils;
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 = "reuse qos report")
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 (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 = "MetricsReporter not init";
MetricsReporter metricsReporter = applicationModel.getBeanFactory().getBean(MetricsReporter.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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default=org.apache.dubbo.metrics.report.DefaultMetricsReporterFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
metrics_default=org.apache.dubbo.metrics.report.DefaultMetricsReporterCmd