diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java index 615b33a5..3d8a7d7e 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java @@ -103,8 +103,7 @@ private static void initializeMetricsEventListener() { private static void initializeMetricsExporter( ) { - if (StringUtils.isEmpty(m_configUtil.getMonitorExternalType()) || "NONE".equals(m_configUtil. - getMonitorExternalType())) { + if (StringUtils.isBlank(m_configUtil.getMonitorExternalType())) { return; } ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance( diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java index 36642e28..da7aa598 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java @@ -23,6 +23,7 @@ import com.ctrip.framework.apollo.Apollo; import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; +import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi; import com.ctrip.framework.apollo.monitor.internal.jmx.mbean.ApolloClientJmxBootstrapArgsMBean; import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener; @@ -33,6 +34,8 @@ import java.time.LocalDateTime; import java.util.Map; +import java.util.Optional; + import org.slf4j.Logger; /** @@ -76,7 +79,8 @@ public DefaultApolloClientBootstrapArgsApi(ConfigUtil configUtil) { putAttachmentValue(APP_ID, configUtil.getAppId()); putAttachmentValue(ENV, configUtil.getApolloEnv()); putAttachmentValue(VERSION, Apollo.VERSION); - putAttachmentValue(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now())); + Optional date = DateUtil.formatLocalDateTime(LocalDateTime.now()); + date.ifPresent(s -> putAttachmentValue(META_FRESH, s)); putAttachmentValue(CONFIG_SERVICE_URL,""); } @@ -92,8 +96,11 @@ public void collect0(ApolloClientMonitorEvent event) { } private void putAttachmentValue(String argName, Object value) { + if(StringUtils.isBlank(argName) || value == null) { + return; + } bootstrapArgs.put(argName, value); - bootstrapArgsString.put(argName, value == null ? null : value.toString()); + bootstrapArgsString.put(argName, value.toString()); } @Override diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java index 4e58a631..286cb84d 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.slf4j.Logger; @@ -183,7 +184,8 @@ public Map getNamespaceMetricsString() { namespaces.forEach((namespace, metrics) -> { NamespaceMetricsString namespaceMetricsString = new NamespaceMetricsString(); namespaceMetricsString.setFirstLoadTimeSpendInMs(metrics.getFirstLoadTimeSpendInMs()); - namespaceMetricsString.setLatestUpdateTime(DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime())); + Optional date = DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime()); + date.ifPresent(namespaceMetricsString::setLatestUpdateTime); namespaceMetricsString.setUsageCount(metrics.getUsageCount()); namespaceMetricsString.setReleaseKey(metrics.getReleaseKey()); namespaceMetricsStringMap.put(namespace, namespaceMetricsString); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java index 1bc93a10..07adfddf 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java @@ -32,6 +32,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; /** * @author Rawven @@ -123,10 +124,11 @@ private void publishConfigChangeEvent(String name) { } private void publishMetaServiceEvent() { + Optional date = DateUtil.formatLocalDateTime(LocalDateTime.now()); ApolloClientMonitorEventPublisher.publish( ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH) .withTag(TAG_BOOTSTRAP) - .putAttachment(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now()))); + .putAttachment(META_FRESH, date.orElse(""))); } private void publishConfigServiceEvent(String name) { diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java index 457f3f32..af18c988 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java @@ -74,7 +74,7 @@ public class ConfigUtil { private boolean propertyKubernetesCacheEnabled = false; private boolean clientMonitorEnabled = false; private boolean clientMonitorJmxEnabled = false; - private String monitorExternalType = "NONE"; + private String monitorExternalType = ""; private long monitorExternalExportPeriod = 10; private int monitorExceptionQueueSize = 25; @@ -556,7 +556,7 @@ private void initClientMonitorExternalType() { monitorExternalType = System.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE); if (Strings.isNullOrEmpty(monitorExternalType)) { monitorExternalType = Foundation.app() - .getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, "NONE"); + .getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, ""); } } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java index 1b4b1a32..7a9bf6fa 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java @@ -18,6 +18,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Optional; /** * @author Rawven @@ -32,7 +33,8 @@ public class DateUtil { * @param localDateTime the LocalDateTime to format, can be null * @return the formatted date-time string, or null if the input is null */ - public static String formatLocalDateTime(LocalDateTime localDateTime) { - return localDateTime != null ? localDateTime.format(MEDIUM_FORMATTER) : null; + public static Optional formatLocalDateTime(LocalDateTime localDateTime) { + return Optional.ofNullable(localDateTime) + .map(dt -> dt.format(MEDIUM_FORMATTER)); } } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/date/DateUtilTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/date/DateUtilTest.java new file mode 100644 index 00000000..8bb54d08 --- /dev/null +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/date/DateUtilTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022 Apollo Authors + * + * Licensed 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 com.ctrip.framework.apollo.util.date; + +import org.junit.Test; +import static org.junit.Assert.*; +import java.time.LocalDateTime; +import java.util.Optional; + +public class DateUtilTest { + + @Test + public void testFormatLocalDateTime_validDate() { + LocalDateTime dateTime = LocalDateTime.of(2024, 12, 1, 10, 30, 0); + + Optional formattedDate = DateUtil.formatLocalDateTime(dateTime); + + assertTrue(formattedDate.isPresent()); + assertEquals("2024-12-01 10:30:00", formattedDate.get()); + } + + @Test + public void testFormatLocalDateTime_nullDate() { + Optional result = DateUtil.formatLocalDateTime(null); + + assertFalse(result.isPresent()); + } +}