Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: monitor arg cause npe #86

Merged
merged 7 commits into from
Dec 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.monitor.internal.exporter.AbstractApolloClientMetricsExporter;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporter;
import com.ctrip.framework.apollo.monitor.internal.jmx.ApolloClientJmxMBeanRegister;
Expand Down Expand Up @@ -102,10 +103,14 @@ private static void initializeMetricsEventListener() {

private static void initializeMetricsExporter(
) {
if (StringUtils.isEmpty(m_configUtil.getMonitorExternalType()) || "NONE".equals(m_configUtil.
getMonitorExternalType())) {
return;
}
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(
ApolloClientMetricsExporterFactory.class);
ApolloClientMetricsExporterFactory.class);
ApolloClientMetricsExporter metricsReporter = exporterFactory.getMetricsReporter(
MONITOR_CONTEXT.getApolloClientMonitorEventListeners());
MONITOR_CONTEXT.getApolloClientMonitorEventListeners());
if (metricsReporter != null) {
MONITOR_CONTEXT.setApolloClientMetricsExporter(metricsReporter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.util.date.DateUtil;
import com.google.common.collect.Maps;

import java.time.LocalDateTime;
import java.util.Map;
import org.slf4j.Logger;

Expand All @@ -46,50 +49,52 @@ public class DefaultApolloClientBootstrapArgsApi extends

public DefaultApolloClientBootstrapArgsApi(ConfigUtil configUtil) {
super(TAG_BOOTSTRAP);
bootstrapArgs.put(APOLLO_ACCESS_KEY_SECRET, configUtil.getAccessKeySecret());
bootstrapArgs.put(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES,
putAttachmentValue(APOLLO_ACCESS_KEY_SECRET, configUtil.getAccessKeySecret());
putAttachmentValue(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES,
configUtil.isAutoUpdateInjectedSpringPropertiesEnabled());
bootstrapArgs.put(APOLLO_BOOTSTRAP_ENABLED,
putAttachmentValue(APOLLO_BOOTSTRAP_ENABLED,
Boolean.parseBoolean(System.getProperty(APOLLO_BOOTSTRAP_ENABLED)));
bootstrapArgs.put(APOLLO_BOOTSTRAP_NAMESPACES,
putAttachmentValue(APOLLO_BOOTSTRAP_NAMESPACES,
System.getProperty(APOLLO_BOOTSTRAP_NAMESPACES));
bootstrapArgs.put(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED,
putAttachmentValue(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED,
Boolean.parseBoolean(System.getProperty(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED)));
bootstrapArgs.put(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, configUtil.isOverrideSystemProperties());
bootstrapArgs.put(APOLLO_CACHE_DIR, configUtil.getDefaultLocalCacheDir());
bootstrapArgs.put(APOLLO_CLUSTER, configUtil.getCluster());
bootstrapArgs.put(APOLLO_CONFIG_SERVICE,
putAttachmentValue(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, configUtil.isOverrideSystemProperties());
putAttachmentValue(APOLLO_CACHE_DIR, configUtil.getDefaultLocalCacheDir());
putAttachmentValue(APOLLO_CLUSTER, configUtil.getCluster());
putAttachmentValue(APOLLO_CONFIG_SERVICE,
System.getProperty(APOLLO_CONFIG_SERVICE));
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, configUtil.getMonitorExternalType());
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_ENABLED, configUtil.isClientMonitorEnabled());
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD,
putAttachmentValue(APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, configUtil.getMonitorExternalType());
putAttachmentValue(APOLLO_CLIENT_MONITOR_ENABLED, configUtil.isClientMonitorEnabled());
putAttachmentValue(APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD,
configUtil.getMonitorExternalExportPeriod());
bootstrapArgs.put(APOLLO_META, configUtil.getMetaServerDomainName());
bootstrapArgs.put(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, configUtil.isPropertyNamesCacheEnabled());
bootstrapArgs.put(APOLLO_PROPERTY_ORDER_ENABLE, configUtil.isPropertiesOrderEnabled());
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_JMX_ENABLED, configUtil.isClientMonitorJmxEnabled());
bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE,
putAttachmentValue(APOLLO_META, configUtil.getMetaServerDomainName());
putAttachmentValue(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, configUtil.isPropertyNamesCacheEnabled());
putAttachmentValue(APOLLO_PROPERTY_ORDER_ENABLE, configUtil.isPropertiesOrderEnabled());
putAttachmentValue(APOLLO_CLIENT_MONITOR_JMX_ENABLED, configUtil.isClientMonitorJmxEnabled());
putAttachmentValue(APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE,
configUtil.getMonitorExceptionQueueSize());
bootstrapArgs.put(APP_ID, configUtil.getAppId());
bootstrapArgs.put(ENV, configUtil.getApolloEnv());
bootstrapArgs.put(VERSION, Apollo.VERSION);
bootstrapArgs.forEach((key, value) -> {
if (value != null) {
bootstrapArgsString.put(key, value.toString());
}
});
putAttachmentValue(APP_ID, configUtil.getAppId());
putAttachmentValue(ENV, configUtil.getApolloEnv());
putAttachmentValue(VERSION, Apollo.VERSION);
putAttachmentValue(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now()));
putAttachmentValue(CONFIG_SERVICE_URL,"");

}

@Override
public void collect0(ApolloClientMonitorEvent event) {
String argName = event.getName();
if (bootstrapArgs.containsKey(argName)) {
bootstrapArgs.put(argName, event.getAttachmentValue(argName));
putAttachmentValue(argName, event.getAttachmentValue(argName));
} else {
logger.warn("Unhandled event name: {}", argName);
}
}

private void putAttachmentValue(String argName, Object value) {
bootstrapArgs.put(argName, value);
bootstrapArgsString.put(argName, value == null ? null : value.toString());
}

@Override
public boolean isMetricsSampleUpdated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant;
import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent;
import com.ctrip.framework.apollo.util.date.DateUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -182,7 +183,7 @@ public Map<String, NamespaceMetricsString> getNamespaceMetricsString() {
namespaces.forEach((namespace, metrics) -> {
NamespaceMetricsString namespaceMetricsString = new NamespaceMetricsString();
namespaceMetricsString.setFirstLoadTimeSpendInMs(metrics.getFirstLoadTimeSpendInMs());
namespaceMetricsString.setLatestUpdateTime(metrics.getLatestUpdateTime().toString());
namespaceMetricsString.setLatestUpdateTime(DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime()));
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 @@ -25,8 +25,10 @@
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEventPublisher;
import com.ctrip.framework.apollo.tracer.spi.MessageProducer;
import com.ctrip.framework.apollo.tracer.spi.Transaction;
import com.ctrip.framework.apollo.util.date.DateUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -97,7 +99,7 @@ private void handleTaggedEvent(String type, String name) {
publishNamespaceNotFoundEvent(name);
break;
case APOLLO_CLIENT_CONFIGMETA:
// 不需要收集
// No need to collect
break;
default:
break;
Expand All @@ -124,7 +126,7 @@ private void publishMetaServiceEvent() {
ApolloClientMonitorEventPublisher.publish(
ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH)
.withTag(TAG_BOOTSTRAP)
.putAttachment(META_FRESH, LocalDate.now().toString()));
.putAttachment(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now())));
}

private void publishConfigServiceEvent(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author Rawven
* @date 2024/10/19
*/
public class DateUtil {
public static final DateTimeFormatter MEDIUM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

/**
* Formats a LocalDateTime object to a string using the MEDIUM_FORMATTER.
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class PrometheusApolloClientMetricsExporter extends

private static final String PROMETHEUS = "prometheus";
private final Logger logger = DeferredLoggerFactory.getLogger(
DefaultApolloClientNamespaceApi.class);
PrometheusApolloClientMetricsExporter.class);
protected CollectorRegistry registry;
protected Map<String, Collector.Describable> map;

Expand Down
Loading