Skip to content

Commit

Permalink
⚡ opt unuse label (#13034)
Browse files Browse the repository at this point in the history
* ⚡ opt unuse label

* ⚡ opt unuse label

* ♻️ application metrics add commit id

* ♻️ refact application tag name

* ✅ remove unuse test line

* ✅ add application metrics test

* ✅ remove unuse test line

* ✅ remove unuse test line
  • Loading branch information
songxiaosheng authored Sep 25, 2023
1 parent 9422e78 commit 5903d0b
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ public String getApplicationName() {

@Override
public Map<String, String> getTags() {
return MetricsSupport.applicationTags(applicationModel, getExtraInfo());
return hostTags(gitTags(MetricsSupport.applicationTags(applicationModel, getExtraInfo())));
}

public Map<String, String> gitTags(Map<String, String> tags) {
return MetricsSupport.gitTags(tags);
}

public Map<String, String> hostTags(Map<String, String> tags) {
return MetricsSupport.hostTags(tags);
}


public Map<String, String> getExtraInfo() {
return extraInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,33 @@
public class MetricsSupport {

private static final String version = Version.getVersion();

private static final String commitId = Version.getLastCommitId();


public static Map<String, String> applicationTags(ApplicationModel applicationModel) {
return applicationTags(applicationModel, null);
}

public static Map<String, String> applicationTags(ApplicationModel applicationModel, @Nullable Map<String, String> extraInfo) {
Map<String, String> 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<String, String> gitTags(Map<String, String> tags) {
tags.put(MetricsKey.METADATA_GIT_COMMITID_METRIC.getName(), commitId);
tags.put(TAG_APPLICATION_VERSION_KEY, version);
return tags;
}
public static Map<String, String> hostTags( Map<String, String> tags) {
tags.put(TAG_IP, getLocalHost());
tags.put(TAG_HOSTNAME, getLocalHostName());
return tags;
}

public static Map<String, String> serviceTags(ApplicationModel applicationModel, String serviceKey, Map<String, String> extraInfo) {
Map<String, String> tags = applicationTags(applicationModel, extraInfo);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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<String, String> 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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ void test() {
Assertions.assertEquals(metric.getVersion(), version);

Map<String, String> 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);
Expand Down

0 comments on commit 5903d0b

Please sign in to comment.