diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java index 20d03ca523f..c6d2734a8aa 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java @@ -40,9 +40,18 @@ public String getApplicationName() { @Override public Map getTags() { - return MetricsSupport.applicationTags(applicationModel, getExtraInfo()); + return hostTags(gitTags(MetricsSupport.applicationTags(applicationModel, getExtraInfo()))); } + public Map gitTags(Map tags) { + return MetricsSupport.gitTags(tags); + } + + public Map hostTags(Map tags) { + return MetricsSupport.hostTags(tags); + } + + public Map getExtraInfo() { return extraInfo; } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java index dfaec1fb8e0..d6510a503f3 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java @@ -59,25 +59,33 @@ public class MetricsSupport { private static final String version = Version.getVersion(); + private static final String commitId = Version.getLastCommitId(); + public static Map applicationTags(ApplicationModel applicationModel) { return applicationTags(applicationModel, null); } public static Map applicationTags(ApplicationModel applicationModel, @Nullable Map extraInfo) { Map tags = new HashMap<>(); - tags.put(TAG_IP, getLocalHost()); - tags.put(TAG_HOSTNAME, getLocalHostName()); tags.put(TAG_APPLICATION_NAME, applicationModel.getApplicationName()); tags.put(TAG_APPLICATION_MODULE, applicationModel.getInternalId()); - tags.put(TAG_APPLICATION_VERSION_KEY, version); - tags.put(MetricsKey.METADATA_GIT_COMMITID_METRIC.getName(), commitId); if (CollectionUtils.isNotEmptyMap(extraInfo)) { tags.putAll(extraInfo); } return tags; } + public static Map gitTags(Map tags) { + tags.put(MetricsKey.METADATA_GIT_COMMITID_METRIC.getName(), commitId); + tags.put(TAG_APPLICATION_VERSION_KEY, version); + return tags; + } + public static Map hostTags( Map tags) { + tags.put(TAG_IP, getLocalHost()); + tags.put(TAG_HOSTNAME, getLocalHostName()); + return tags; + } public static Map serviceTags(ApplicationModel applicationModel, String serviceKey, Map extraInfo) { Map tags = applicationTags(applicationModel, extraInfo); diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/model/ApplicationMetricTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/model/ApplicationMetricTest.java new file mode 100644 index 00000000000..c6717d9fea1 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/model/ApplicationMetricTest.java @@ -0,0 +1,99 @@ +/* + * 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.model; + +import org.apache.dubbo.common.Version; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.apache.dubbo.common.constants.MetricsConstants.*; +import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; +import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName; +import static org.apache.dubbo.metrics.model.key.MetricsKey.METADATA_GIT_COMMITID_METRIC; +import static org.junit.jupiter.api.Assertions.*; + +class ApplicationMetricTest { + + @Test + void getApplicationModel() { + ApplicationMetric applicationMetric = new ApplicationMetric(ApplicationModel.defaultModel()); + Assertions.assertNotNull(applicationMetric.getApplicationModel()); + } + + @Test + void getApplicationName() { + ApplicationModel applicationModel = ApplicationModel.defaultModel(); + String mockMetrics = "MockMetrics"; + applicationModel.getApplicationConfigManager().setApplication(new org.apache.dubbo.config.ApplicationConfig(mockMetrics)); + ApplicationMetric applicationMetric = new ApplicationMetric(applicationModel); + Assertions.assertNotNull(applicationMetric); + Assertions.assertEquals(mockMetrics,applicationMetric.getApplicationName()); + } + + @Test + void getTags() { + ApplicationModel applicationModel = ApplicationModel.defaultModel(); + String mockMetrics = "MockMetrics"; + applicationModel.getApplicationConfigManager().setApplication(new org.apache.dubbo.config.ApplicationConfig(mockMetrics)); + ApplicationMetric applicationMetric = new ApplicationMetric(applicationModel); + Map tags = applicationMetric.getTags(); + Assertions.assertEquals(tags.get(TAG_IP), getLocalHost()); + Assertions.assertEquals(tags.get(TAG_HOSTNAME), getLocalHostName()); + Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME), applicationModel.getApplicationName()); + Assertions.assertEquals(tags.get(METADATA_GIT_COMMITID_METRIC.getName()), Version.getLastCommitId()); + } + + @Test + void gitTags() { + ApplicationModel applicationModel = ApplicationModel.defaultModel(); + String mockMetrics = "MockMetrics"; + applicationModel.getApplicationConfigManager().setApplication(new org.apache.dubbo.config.ApplicationConfig(mockMetrics)); + ApplicationMetric applicationMetric = new ApplicationMetric(applicationModel); + Map tags = applicationMetric.getTags(); + Assertions.assertEquals(tags.get(METADATA_GIT_COMMITID_METRIC.getName()), Version.getLastCommitId()); + } + + @Test + void hostTags() { + ApplicationModel applicationModel = ApplicationModel.defaultModel(); + String mockMetrics = "MockMetrics"; + applicationModel.getApplicationConfigManager().setApplication(new org.apache.dubbo.config.ApplicationConfig(mockMetrics)); + ApplicationMetric applicationMetric = new ApplicationMetric(applicationModel); + Map tags = applicationMetric.getTags(); + Assertions.assertEquals(tags.get(TAG_IP), getLocalHost()); + Assertions.assertEquals(tags.get(TAG_HOSTNAME), getLocalHostName()); + } + + @Test + void getExtraInfo() { + } + + @Test + void setExtraInfo() { + } + + @Test + void testEquals() { + } + + @Test + void testHashCode() { + } +} diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java index d894f15a5d7..93f1c6593f7 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java @@ -81,8 +81,6 @@ void test() { Assertions.assertEquals(metric.getVersion(), version); Map tags = metric.getTags(); - Assertions.assertEquals(tags.get(TAG_IP), getLocalHost()); - Assertions.assertEquals(tags.get(TAG_HOSTNAME), getLocalHostName()); Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME), applicationModel.getApplicationName()); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), interfaceName);