Skip to content

Commit e64c35b

Browse files
authored
fix: monitor arg cause npe (#86)
* fix(): 解决metaFreshTime和configServiceUrl的NPE以及时间格式问题 * fix(): 消除external-type在启用monitor下但又未配置时的无用日志打印 * fix(): PrometheusApolloClientMetricsExporter下错误的logger配置 * fix(): ConfigMoniotr 方法命名与API不统一问题 * fix(): 优化代码以及增加DateUtil测试类
1 parent d4b76f8 commit e64c35b

File tree

12 files changed

+144
-42
lines changed

12 files changed

+144
-42
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Apollo Java 2.4.0
1111
* [Add more observability in apollo config client](https://github.com/apolloconfig/apollo-java/pull/74)
1212
* [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79)
1313
* [Feature support pulling configuration information from multiple AppIds](https://github.com/apolloconfig/apollo-java/pull/70)
14-
14+
* [Fix monitor arg cause npe](https://github.com/apolloconfig/apollo-java/pull/86)
1515

1616
------------------
1717
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.ctrip.framework.apollo.build.ApolloInjector;
2222
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
23+
import com.ctrip.framework.apollo.core.utils.StringUtils;
2324
import com.ctrip.framework.apollo.monitor.internal.exporter.AbstractApolloClientMetricsExporter;
2425
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporter;
2526
import com.ctrip.framework.apollo.monitor.internal.jmx.ApolloClientJmxMBeanRegister;
@@ -102,10 +103,13 @@ private static void initializeMetricsEventListener() {
102103

103104
private static void initializeMetricsExporter(
104105
) {
106+
if (StringUtils.isBlank(m_configUtil.getMonitorExternalType())) {
107+
return;
108+
}
105109
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(
106-
ApolloClientMetricsExporterFactory.class);
110+
ApolloClientMetricsExporterFactory.class);
107111
ApolloClientMetricsExporter metricsReporter = exporterFactory.getMetricsReporter(
108-
MONITOR_CONTEXT.getApolloClientMonitorEventListeners());
112+
MONITOR_CONTEXT.getApolloClientMonitorEventListeners());
109113
if (metricsReporter != null) {
110114
MONITOR_CONTEXT.setApolloClientMetricsExporter(metricsReporter);
111115
}

apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public interface ConfigMonitor {
3939
/**
4040
* get running params monitor api
4141
*/
42-
ApolloClientBootstrapArgsMonitorApi getRunningParamsMonitorApi();
42+
ApolloClientBootstrapArgsMonitorApi getBootstrapArgsMonitorApi();
4343

4444
/**
4545
* get monitor external system data

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ApolloClientNamespaceMonitorApi getNamespaceMonitorApi() {
4949
}
5050

5151
@Override
52-
public ApolloClientBootstrapArgsMonitorApi getRunningParamsMonitorApi() {
52+
public ApolloClientBootstrapArgsMonitorApi getBootstrapArgsMonitorApi() {
5353
return apolloClientMonitorContext.getBootstrapArgsApi();
5454
}
5555

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

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@
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;
2930
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent;
3031
import com.ctrip.framework.apollo.util.ConfigUtil;
32+
import com.ctrip.framework.apollo.util.date.DateUtil;
3133
import com.google.common.collect.Maps;
34+
35+
import java.time.LocalDateTime;
3236
import java.util.Map;
37+
import java.util.Optional;
38+
3339
import org.slf4j.Logger;
3440

3541
/**
@@ -46,50 +52,55 @@ public class DefaultApolloClientBootstrapArgsApi extends
4652

4753
public DefaultApolloClientBootstrapArgsApi(ConfigUtil configUtil) {
4854
super(TAG_BOOTSTRAP);
49-
bootstrapArgs.put(APOLLO_ACCESS_KEY_SECRET, configUtil.getAccessKeySecret());
50-
bootstrapArgs.put(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES,
55+
putAttachmentValue(APOLLO_ACCESS_KEY_SECRET, configUtil.getAccessKeySecret());
56+
putAttachmentValue(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES,
5157
configUtil.isAutoUpdateInjectedSpringPropertiesEnabled());
52-
bootstrapArgs.put(APOLLO_BOOTSTRAP_ENABLED,
58+
putAttachmentValue(APOLLO_BOOTSTRAP_ENABLED,
5359
Boolean.parseBoolean(System.getProperty(APOLLO_BOOTSTRAP_ENABLED)));
54-
bootstrapArgs.put(APOLLO_BOOTSTRAP_NAMESPACES,
60+
putAttachmentValue(APOLLO_BOOTSTRAP_NAMESPACES,
5561
System.getProperty(APOLLO_BOOTSTRAP_NAMESPACES));
56-
bootstrapArgs.put(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED,
62+
putAttachmentValue(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED,
5763
Boolean.parseBoolean(System.getProperty(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED)));
58-
bootstrapArgs.put(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, configUtil.isOverrideSystemProperties());
59-
bootstrapArgs.put(APOLLO_CACHE_DIR, configUtil.getDefaultLocalCacheDir());
60-
bootstrapArgs.put(APOLLO_CLUSTER, configUtil.getCluster());
61-
bootstrapArgs.put(APOLLO_CONFIG_SERVICE,
64+
putAttachmentValue(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, configUtil.isOverrideSystemProperties());
65+
putAttachmentValue(APOLLO_CACHE_DIR, configUtil.getDefaultLocalCacheDir());
66+
putAttachmentValue(APOLLO_CLUSTER, configUtil.getCluster());
67+
putAttachmentValue(APOLLO_CONFIG_SERVICE,
6268
System.getProperty(APOLLO_CONFIG_SERVICE));
63-
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, configUtil.getMonitorExternalType());
64-
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_ENABLED, configUtil.isClientMonitorEnabled());
65-
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD,
69+
putAttachmentValue(APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, configUtil.getMonitorExternalType());
70+
putAttachmentValue(APOLLO_CLIENT_MONITOR_ENABLED, configUtil.isClientMonitorEnabled());
71+
putAttachmentValue(APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD,
6672
configUtil.getMonitorExternalExportPeriod());
67-
bootstrapArgs.put(APOLLO_META, configUtil.getMetaServerDomainName());
68-
bootstrapArgs.put(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, configUtil.isPropertyNamesCacheEnabled());
69-
bootstrapArgs.put(APOLLO_PROPERTY_ORDER_ENABLE, configUtil.isPropertiesOrderEnabled());
70-
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_JMX_ENABLED, configUtil.isClientMonitorJmxEnabled());
71-
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE,
73+
putAttachmentValue(APOLLO_META, configUtil.getMetaServerDomainName());
74+
putAttachmentValue(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, configUtil.isPropertyNamesCacheEnabled());
75+
putAttachmentValue(APOLLO_PROPERTY_ORDER_ENABLE, configUtil.isPropertiesOrderEnabled());
76+
putAttachmentValue(APOLLO_CLIENT_MONITOR_JMX_ENABLED, configUtil.isClientMonitorJmxEnabled());
77+
putAttachmentValue(APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE,
7278
configUtil.getMonitorExceptionQueueSize());
73-
bootstrapArgs.put(APP_ID, configUtil.getAppId());
74-
bootstrapArgs.put(ENV, configUtil.getApolloEnv());
75-
bootstrapArgs.put(VERSION, Apollo.VERSION);
76-
bootstrapArgs.forEach((key, value) -> {
77-
if (value != null) {
78-
bootstrapArgsString.put(key, value.toString());
79-
}
80-
});
81-
79+
putAttachmentValue(APP_ID, configUtil.getAppId());
80+
putAttachmentValue(ENV, configUtil.getApolloEnv());
81+
putAttachmentValue(VERSION, Apollo.VERSION);
82+
DateUtil.formatLocalDateTime(LocalDateTime.now())
83+
.ifPresent(s -> putAttachmentValue(META_FRESH, s));
84+
putAttachmentValue(CONFIG_SERVICE_URL,"");
8285
}
8386

8487
@Override
8588
public void collect0(ApolloClientMonitorEvent event) {
8689
String argName = event.getName();
8790
if (bootstrapArgs.containsKey(argName)) {
88-
bootstrapArgs.put(argName, event.getAttachmentValue(argName));
91+
putAttachmentValue(argName, event.getAttachmentValue(argName));
8992
} else {
9093
logger.warn("Unhandled event name: {}", argName);
9194
}
9295
}
96+
97+
private void putAttachmentValue(String argName, Object value) {
98+
if(StringUtils.isBlank(argName) || value == null) {
99+
return;
100+
}
101+
bootstrapArgs.put(argName, value);
102+
bootstrapArgsString.put(argName, String.valueOf(value));
103+
}
93104

94105
@Override
95106
public boolean isMetricsSampleUpdated() {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
import com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant;
2929
import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener;
3030
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent;
31+
import com.ctrip.framework.apollo.util.date.DateUtil;
3132
import com.google.common.collect.Maps;
3233
import com.google.common.collect.Sets;
3334
import java.time.LocalDateTime;
3435
import java.util.ArrayList;
3536
import java.util.Collections;
3637
import java.util.List;
3738
import java.util.Map;
39+
import java.util.Optional;
3840
import java.util.Set;
3941
import org.slf4j.Logger;
4042

@@ -182,7 +184,8 @@ public Map<String, NamespaceMetricsString> getNamespaceMetricsString() {
182184
namespaces.forEach((namespace, metrics) -> {
183185
NamespaceMetricsString namespaceMetricsString = new NamespaceMetricsString();
184186
namespaceMetricsString.setFirstLoadTimeSpendInMs(metrics.getFirstLoadTimeSpendInMs());
185-
namespaceMetricsString.setLatestUpdateTime(metrics.getLatestUpdateTime().toString());
187+
DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime())
188+
.ifPresent(namespaceMetricsString::setLatestUpdateTime);
186189
namespaceMetricsString.setUsageCount(metrics.getUsageCount());
187190
namespaceMetricsString.setReleaseKey(metrics.getReleaseKey());
188191
namespaceMetricsStringMap.put(namespace, namespaceMetricsString);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEventPublisher;
2626
import com.ctrip.framework.apollo.tracer.spi.MessageProducer;
2727
import com.ctrip.framework.apollo.tracer.spi.Transaction;
28+
import com.ctrip.framework.apollo.util.date.DateUtil;
2829

2930
import java.time.LocalDate;
31+
import java.time.LocalDateTime;
3032
import java.util.Arrays;
3133
import java.util.Collections;
3234
import java.util.List;
35+
import java.util.Optional;
3336

3437
/**
3538
* @author Rawven
@@ -97,7 +100,7 @@ private void handleTaggedEvent(String type, String name) {
97100
publishNamespaceNotFoundEvent(name);
98101
break;
99102
case APOLLO_CLIENT_CONFIGMETA:
100-
// 不需要收集
103+
// No need to collect
101104
break;
102105
default:
103106
break;
@@ -122,9 +125,9 @@ private void publishConfigChangeEvent(String name) {
122125

123126
private void publishMetaServiceEvent() {
124127
ApolloClientMonitorEventPublisher.publish(
125-
ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH)
126-
.withTag(TAG_BOOTSTRAP)
127-
.putAttachment(META_FRESH, LocalDate.now().toString()));
128+
ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH)
129+
.withTag(TAG_BOOTSTRAP)
130+
.putAttachment(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now()).orElse("")));
128131
}
129132

130133
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 java.time.LocalDateTime;
20+
import java.time.format.DateTimeFormatter;
21+
import java.util.Optional;
22+
23+
/**
24+
* @author Rawven
25+
* @date 2024/10/19
26+
*/
27+
public class DateUtil {
28+
public static final DateTimeFormatter MEDIUM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
29+
30+
/**
31+
* Formats a LocalDateTime object to a string using the MEDIUM_FORMATTER.
32+
*
33+
* @param localDateTime the LocalDateTime to format, can be null
34+
* @return the formatted date-time string, or null if the input is null
35+
*/
36+
public static Optional<String> formatLocalDateTime(LocalDateTime localDateTime) {
37+
return Optional.ofNullable(localDateTime)
38+
.map(dt -> dt.format(MEDIUM_FORMATTER));
39+
}
40+
}

apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.junit.Assert.*;
1919
import static org.mockito.Mockito.*;
2020

21-
import com.ctrip.framework.apollo.build.ApolloInjector;
2221
import com.ctrip.framework.apollo.build.MockInjector;
2322
import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi;
2423
import com.ctrip.framework.apollo.monitor.api.ApolloClientExceptionMonitorApi;
@@ -65,7 +64,7 @@ public void setUp(){
6564
public void testApis(){
6665
assertSame(exceptionMonitorApi, configMonitor.getExceptionMonitorApi());
6766
assertSame(namespaceMonitorApi, configMonitor.getNamespaceMonitorApi());
68-
assertSame(bootstrapArgsMonitorApi, configMonitor.getRunningParamsMonitorApi());
67+
assertSame(bootstrapArgsMonitorApi, configMonitor.getBootstrapArgsMonitorApi());
6968
assertSame(threadPoolMonitorApi, configMonitor.getThreadPoolMonitorApi());
7069
}
7170

0 commit comments

Comments
 (0)