Skip to content

Commit 8612ef5

Browse files
committed
Merge branch 'fix/monitor-bug' into fix/monitor-latest
2 parents 2b7e717 + bec94fe commit 8612ef5

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ private static void initializeMetricsEventListener() {
103103

104104
private static void initializeMetricsExporter(
105105
) {
106-
if (StringUtils.isEmpty(m_configUtil.getMonitorExternalType()) || "NONE".equals(m_configUtil.
107-
getMonitorExternalType())) {
106+
if (StringUtils.isEmpty(m_configUtil.getMonitorExternalType())) {
108107
return;
109108
}
110109
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(

apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.ctrip.framework.apollo.Apollo;
2525
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
26+
import com.ctrip.framework.apollo.core.utils.StringUtils;
2627
import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi;
2728
import com.ctrip.framework.apollo.monitor.internal.jmx.mbean.ApolloClientJmxBootstrapArgsMBean;
2829
import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener;
@@ -33,6 +34,8 @@
3334

3435
import java.time.LocalDateTime;
3536
import java.util.Map;
37+
import java.util.Optional;
38+
3639
import org.slf4j.Logger;
3740

3841
/**
@@ -76,7 +79,8 @@ public DefaultApolloClientBootstrapArgsApi(ConfigUtil configUtil) {
7679
putAttachmentValue(APP_ID, configUtil.getAppId());
7780
putAttachmentValue(ENV, configUtil.getApolloEnv());
7881
putAttachmentValue(VERSION, Apollo.VERSION);
79-
putAttachmentValue(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now()));
82+
Optional<String> date = DateUtil.formatLocalDateTime(LocalDateTime.now());
83+
date.ifPresent(s -> putAttachmentValue(META_FRESH, s));
8084
putAttachmentValue(CONFIG_SERVICE_URL,"");
8185

8286
}
@@ -92,8 +96,11 @@ public void collect0(ApolloClientMonitorEvent event) {
9296
}
9397

9498
private void putAttachmentValue(String argName, Object value) {
99+
if(StringUtils.isBlank(argName) || value == null) {
100+
return;
101+
}
95102
bootstrapArgs.put(argName, value);
96-
bootstrapArgsString.put(argName, value == null ? null : value.toString());
103+
bootstrapArgsString.put(argName, value.toString());
97104
}
98105

99106
@Override

apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Collections;
3737
import java.util.List;
3838
import java.util.Map;
39+
import java.util.Optional;
3940
import java.util.Set;
4041
import org.slf4j.Logger;
4142

@@ -183,7 +184,8 @@ public Map<String, NamespaceMetricsString> getNamespaceMetricsString() {
183184
namespaces.forEach((namespace, metrics) -> {
184185
NamespaceMetricsString namespaceMetricsString = new NamespaceMetricsString();
185186
namespaceMetricsString.setFirstLoadTimeSpendInMs(metrics.getFirstLoadTimeSpendInMs());
186-
namespaceMetricsString.setLatestUpdateTime(DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime()));
187+
Optional<String> date = DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime());
188+
date.ifPresent(namespaceMetricsString::setLatestUpdateTime);
187189
namespaceMetricsString.setUsageCount(metrics.getUsageCount());
188190
namespaceMetricsString.setReleaseKey(metrics.getReleaseKey());
189191
namespaceMetricsStringMap.put(namespace, namespaceMetricsString);

apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Arrays;
3333
import java.util.Collections;
3434
import java.util.List;
35+
import java.util.Optional;
3536

3637
/**
3738
* @author Rawven
@@ -123,10 +124,11 @@ private void publishConfigChangeEvent(String name) {
123124
}
124125

125126
private void publishMetaServiceEvent() {
127+
Optional<String> date = DateUtil.formatLocalDateTime(LocalDateTime.now());
126128
ApolloClientMonitorEventPublisher.publish(
127129
ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH)
128130
.withTag(TAG_BOOTSTRAP)
129-
.putAttachment(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now())));
131+
.putAttachment(META_FRESH, date.orElse("")));
130132
}
131133

132134
private void publishConfigServiceEvent(String name) {

apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class ConfigUtil {
7474
private boolean propertyKubernetesCacheEnabled = false;
7575
private boolean clientMonitorEnabled = false;
7676
private boolean clientMonitorJmxEnabled = false;
77-
private String monitorExternalType = "NONE";
77+
private String monitorExternalType = "";
7878
private long monitorExternalExportPeriod = 10;
7979
private int monitorExceptionQueueSize = 25;
8080

@@ -556,7 +556,7 @@ private void initClientMonitorExternalType() {
556556
monitorExternalType = System.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE);
557557
if (Strings.isNullOrEmpty(monitorExternalType)) {
558558
monitorExternalType = Foundation.app()
559-
.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, "NONE");
559+
.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, "");
560560
}
561561
}
562562

apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.LocalDateTime;
2020
import java.time.format.DateTimeFormatter;
21+
import java.util.Optional;
2122

2223
/**
2324
* @author Rawven
@@ -32,7 +33,8 @@ public class DateUtil {
3233
* @param localDateTime the LocalDateTime to format, can be null
3334
* @return the formatted date-time string, or null if the input is null
3435
*/
35-
public static String formatLocalDateTime(LocalDateTime localDateTime) {
36-
return localDateTime != null ? localDateTime.format(MEDIUM_FORMATTER) : null;
36+
public static Optional<String> formatLocalDateTime(LocalDateTime localDateTime) {
37+
return Optional.ofNullable(localDateTime)
38+
.map(dt -> dt.format(MEDIUM_FORMATTER));
3739
}
3840
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2022 Apollo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package com.ctrip.framework.apollo.util.date;
18+
19+
import org.junit.Test;
20+
import static org.junit.Assert.*;
21+
import java.time.LocalDateTime;
22+
import java.util.Optional;
23+
24+
public class DateUtilTest {
25+
26+
@Test
27+
public void testFormatLocalDateTime_validDate() {
28+
// 给定一个有效的 LocalDateTime
29+
LocalDateTime dateTime = LocalDateTime.of(2024, 12, 1, 10, 30, 0);
30+
31+
// 调用格式化方法
32+
Optional<String> formattedDate = DateUtil.formatLocalDateTime(dateTime);
33+
34+
// 验证返回值是否为期望格式的日期字符串
35+
assertTrue(formattedDate.isPresent()); // 使用 isPresent() 检查 Optional 是否包含值
36+
assertEquals("2024-12-01 10:30:00", formattedDate.get());
37+
}
38+
39+
@Test
40+
public void testFormatLocalDateTime_nullDate() {
41+
// 传入 null
42+
Optional<String> result = DateUtil.formatLocalDateTime(null);
43+
44+
// 验证返回的 Optional 是否为空
45+
assertFalse(result.isPresent()); // 使用 isPresent() 判断 Optional 是否为空
46+
}
47+
}

0 commit comments

Comments
 (0)