Skip to content

Commit

Permalink
fix(): 优化代码以及增加DateUtil测试类
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed Nov 30, 2024
2 parents 2b7e717 + bec94fe commit 648e5d1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +34,8 @@

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;

/**
Expand Down Expand Up @@ -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<String> date = DateUtil.formatLocalDateTime(LocalDateTime.now());
date.ifPresent(s -> putAttachmentValue(META_FRESH, s));
putAttachmentValue(CONFIG_SERVICE_URL,"");

}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -183,7 +184,8 @@ public Map<String, NamespaceMetricsString> getNamespaceMetricsString() {
namespaces.forEach((namespace, metrics) -> {
NamespaceMetricsString namespaceMetricsString = new NamespaceMetricsString();
namespaceMetricsString.setFirstLoadTimeSpendInMs(metrics.getFirstLoadTimeSpendInMs());
namespaceMetricsString.setLatestUpdateTime(DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime()));
Optional<String> date = DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime());
date.ifPresent(namespaceMetricsString::setLatestUpdateTime);
namespaceMetricsString.setUsageCount(metrics.getUsageCount());
namespaceMetricsString.setReleaseKey(metrics.getReleaseKey());
namespaceMetricsStringMap.put(namespace, namespaceMetricsString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* @author Rawven
Expand Down Expand Up @@ -123,10 +124,11 @@ private void publishConfigChangeEvent(String name) {
}

private void publishMetaServiceEvent() {
Optional<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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, "");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

/**
* @author Rawven
Expand All @@ -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<String> formatLocalDateTime(LocalDateTime localDateTime) {
return Optional.ofNullable(localDateTime)
.map(dt -> dt.format(MEDIUM_FORMATTER));
}
}
Original file line number Diff line number Diff line change
@@ -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<String> formattedDate = DateUtil.formatLocalDateTime(dateTime);

assertTrue(formattedDate.isPresent());
assertEquals("2024-12-01 10:30:00", formattedDate.get());
}

@Test
public void testFormatLocalDateTime_nullDate() {
Optional<String> result = DateUtil.formatLocalDateTime(null);

assertFalse(result.isPresent());
}
}

0 comments on commit 648e5d1

Please sign in to comment.