From 089f16f8e16b1f75a709dfe851e76e24bf95f1b4 Mon Sep 17 00:00:00 2001 From: liu Date: Sat, 3 Aug 2024 13:52:05 +0800 Subject: [PATCH] feat(client): Add more observability in apollo config client --- .../apollo/internals/AbstractConfigFile.java | 3 +- .../internals/AbstractConfigRepository.java | 3 +- .../internals/ConfigMonitorInitializer.java | 33 +++-- .../internals/ConfigServiceLocator.java | 5 +- .../apollo/internals/DefaultConfig.java | 3 +- .../internals/DefaultConfigManager.java | 14 +- .../apollo/internals/DefaultInjector.java | 4 +- .../internals/LocalFileConfigRepository.java | 3 +- .../RemoteConfigLongPollService.java | 10 +- .../internals/RemoteConfigRepository.java | 27 ++-- .../apollo/internals/SimpleConfig.java | 3 +- .../apollo/internals/YamlConfigFile.java | 3 +- .../ApolloClientBootstrapArgsMonitorApi.java | 26 ++++ .../api/ApolloClientExceptionMonitorApi.java | 31 +++++ .../api/ApolloClientNamespaceMonitorApi.java | 30 +++++ .../api/ApolloClientThreadPoolMonitorApi.java | 27 ++++ .../apollo/monitor/api/ConfigMonitor.java | 10 +- .../ApolloJmxBootstrapArgsApi.java} | 11 +- .../ApolloJmxExceptionApi.java} | 13 +- .../ApolloJmxNamespaceApi.java} | 8 +- .../ApolloJmxThreadPoolApi.java} | 5 +- .../internal/DefaultConfigMonitor.java | 42 +++--- .../monitor/internal/MonitorConstant.java | 13 +- ...=> NullClientBootstrapArgsMonitorApi.java} | 6 +- ...ava => NullClientExceptionMonitorApi.java} | 12 +- ...ava => NullClientNamespaceMonitorApi.java} | 14 +- ...va => NullClientThreadPoolMonitorApi.java} | 4 +- ...ltApolloClientBootstrapArgsCollector.java} | 19 ++- ...efaultApolloClientExceptionCollector.java} | 38 +++--- ...efaultApolloClientNamespaceCollector.java} | 124 ++++++++++-------- ...faultApolloClientThreadPoolCollector.java} | 16 +-- .../DefaultMetricsCollectorManager.java | 4 +- .../DefaultMetricsExporterFactory.java | 24 ++-- .../monitor/internal/model/CounterModel.java | 49 ++----- .../monitor/internal/model/GaugeModel.java | 53 ++------ .../monitor/internal/model/MetricsEvent.java | 97 ++++++-------- .../internal/model/MetricsEventPool.java | 30 +++++ .../monitor/internal/model/MetricsModel.java | 3 +- ...java => ClientMonitorMessageProducer.java} | 88 ++++++++----- .../tracer/MessageProducerComposite.java | 43 +++--- .../AbstractApolloMetricsCollectorTest.java | 90 ------------- .../AbstractApolloMetricsExporterTest.java | 123 ----------------- ...faultApolloMetricsExporterFactoryTest.java | 60 --------- .../prometheus/PrometheusMetricExporter.java | 1 + 44 files changed, 525 insertions(+), 700 deletions(-) create mode 100644 apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientBootstrapArgsMonitorApi.java create mode 100644 apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientExceptionMonitorApi.java create mode 100644 apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientNamespaceMonitorApi.java create mode 100644 apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientThreadPoolMonitorApi.java rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/{ApolloRunningParamsMonitorApi.java => jmx/ApolloJmxBootstrapArgsApi.java} (88%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/{ApolloExceptionMonitorApi.java => jmx/ApolloJmxExceptionApi.java} (79%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/{ApolloNamespaceMonitorApi.java => jmx/ApolloJmxNamespaceApi.java} (91%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/{ApolloThreadPoolMonitorApi.java => jmx/ApolloJmxThreadPoolApi.java} (96%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/{NullRunningParamsMonitorApi.java => NullClientBootstrapArgsMonitorApi.java} (91%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/{NullExceptionMonitorApi.java => NullClientExceptionMonitorApi.java} (74%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/{NullNamespaceMonitorApi.java => NullClientNamespaceMonitorApi.java} (81%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/{NullThreadPoolMonitorApi.java => NullClientThreadPoolMonitorApi.java} (95%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/{internal/DefaultApolloRunningParamsCollector.java => impl/DefaultApolloClientBootstrapArgsCollector.java} (91%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/{internal/DefaultApolloExceptionCollector.java => impl/DefaultApolloClientExceptionCollector.java} (68%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/{internal/DefaultApolloNamespaceCollector.java => impl/DefaultApolloClientNamespaceCollector.java} (72%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/{internal/DefaultApolloThreadPoolCollector.java => impl/DefaultApolloClientThreadPoolCollector.java} (94%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/{internal => impl}/DefaultMetricsCollectorManager.java (89%) rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/{internals => impl}/DefaultMetricsExporterFactory.java (84%) create mode 100644 apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEventPool.java rename apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/{MonitorMessageProducer.java => ClientMonitorMessageProducer.java} (56%) delete mode 100644 apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/collector/AbstractApolloMetricsCollectorTest.java delete mode 100644 apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/AbstractApolloMetricsExporterTest.java delete mode 100644 apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/DefaultApolloMetricsExporterFactoryTest.java diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java index 8fc574cf..7a2d8fb2 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java @@ -17,8 +17,7 @@ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGCHANGES; - +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; import com.ctrip.framework.apollo.enums.ConfigSourceType; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java index 62d3e9ab..c194f1a4 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java @@ -16,8 +16,7 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_EXCEPTION; - +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.util.factory.PropertiesFactory; import java.util.List; 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 8256125b..4b48099b 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 @@ -22,15 +22,15 @@ import com.ctrip.framework.apollo.monitor.internal.DefaultConfigMonitor; import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollector; import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollectorManager; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloExceptionCollector; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloRunningParamsCollector; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloThreadPoolCollector; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultMetricsCollectorManager; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientExceptionCollector; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientBootstrapArgsCollector; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientThreadPoolCollector; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultMetricsCollectorManager; import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporter; import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporterFactory; import com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite; -import com.ctrip.framework.apollo.monitor.internal.tracer.MonitorMessageProducer; +import com.ctrip.framework.apollo.monitor.internal.tracer.ClientMonitorMessageProducer; import com.ctrip.framework.apollo.tracer.internals.NullMessageProducer; import com.ctrip.framework.apollo.tracer.internals.cat.CatMessageProducer; import com.ctrip.framework.apollo.tracer.internals.cat.CatNames; @@ -73,14 +73,13 @@ private static List initializeCollectors( DefaultMetricsCollectorManager manager) { DefaultConfigManager configManager = (DefaultConfigManager) ApolloInjector.getInstance( ConfigManager.class); - DefaultApolloExceptionCollector exceptionCollector = new DefaultApolloExceptionCollector(); - DefaultApolloThreadPoolCollector threadPoolCollector = new DefaultApolloThreadPoolCollector( + DefaultApolloClientExceptionCollector exceptionCollector = new DefaultApolloClientExceptionCollector(); + DefaultApolloClientThreadPoolCollector threadPoolCollector = new DefaultApolloClientThreadPoolCollector( RemoteConfigRepository.m_executorService, AbstractConfig.m_executorService, AbstractConfigFile.m_executorService); - DefaultApolloNamespaceCollector namespaceCollector = new DefaultApolloNamespaceCollector( - configManager.m_configs, configManager.m_configLocks, configManager.m_configFiles, - configManager.m_configFileLocks); - DefaultApolloRunningParamsCollector startupCollector = new DefaultApolloRunningParamsCollector( + DefaultApolloClientNamespaceCollector namespaceCollector = new DefaultApolloClientNamespaceCollector( + configManager.m_configs, configManager.m_configFiles); + DefaultApolloClientBootstrapArgsCollector startupCollector = new DefaultApolloClientBootstrapArgsCollector( m_configUtil); List collectors = Lists.newArrayList(exceptionCollector, namespaceCollector, @@ -99,13 +98,13 @@ private static void initializeConfigMonitor(List collectors, MetricsExporter metricsExporter) { DefaultConfigMonitor defaultConfigMonitor = (DefaultConfigMonitor) ApolloInjector.getInstance( ConfigMonitor.class); - DefaultApolloExceptionCollector exceptionCollector = (DefaultApolloExceptionCollector) collectors.get( + DefaultApolloClientExceptionCollector exceptionCollector = (DefaultApolloClientExceptionCollector) collectors.get( 0); - DefaultApolloNamespaceCollector namespaceCollector = (DefaultApolloNamespaceCollector) collectors.get( + DefaultApolloClientNamespaceCollector namespaceCollector = (DefaultApolloClientNamespaceCollector) collectors.get( 1); - DefaultApolloThreadPoolCollector threadPoolCollector = (DefaultApolloThreadPoolCollector) collectors.get( + DefaultApolloClientThreadPoolCollector threadPoolCollector = (DefaultApolloClientThreadPoolCollector) collectors.get( 2); - DefaultApolloRunningParamsCollector startupCollector = (DefaultApolloRunningParamsCollector) collectors.get( + DefaultApolloClientBootstrapArgsCollector startupCollector = (DefaultApolloClientBootstrapArgsCollector) collectors.get( 3); defaultConfigMonitor.init(namespaceCollector, threadPoolCollector, exceptionCollector, startupCollector, metricsExporter); @@ -118,7 +117,7 @@ public static MessageProducerComposite initializeMessageProducerComposite() { // The producer that comes with the client if (m_configUtil.isClientMonitorEnabled()) { - producers.add(new MonitorMessageProducer()); + producers.add(new ClientMonitorMessageProducer()); } if (ClassLoaderUtil.isClassPresent(CatNames.CAT_CLASS)) { diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java index 281e440e..8391f208 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java @@ -16,10 +16,7 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_EXCEPTION; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_SERVICES; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_META_SERVICE; - +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.ApolloClientSystemConsts; import com.ctrip.framework.apollo.core.ServiceNameConsts; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java index b859e32e..0497b3e5 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java @@ -16,8 +16,7 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGCHANGES; - +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; import com.ctrip.framework.apollo.enums.ConfigSourceType; import com.google.common.collect.Maps; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java index 54243307..74b95534 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java @@ -16,17 +16,16 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_MONITOR; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_USAGE_COUNT; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.APOLLO_CLIENT_NAMESPACE_USAGE; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; -import com.ctrip.framework.apollo.monitor.internal.MonitorConstant; -import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; +import com.ctrip.framework.apollo.enums.ConfigSourceType; import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactoryManager; +import com.ctrip.framework.apollo.tracer.Tracer; import com.google.common.collect.Maps; import java.util.Map; @@ -62,10 +61,9 @@ public Config getConfig(String namespace) { } } } - - MetricsEvent.builder().withName(NAMESPACE_USAGE_COUNT) - .putAttachment(MonitorConstant.NAMESPACE, namespace) - .withTag(NAMESPACE_MONITOR).push(); + if(!ConfigSourceType.NONE.equals(config.getSourceType())) { + Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_USAGE, namespace); + } return config; } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultInjector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultInjector.java index 8aae65e0..9903a63c 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultInjector.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultInjector.java @@ -19,9 +19,9 @@ import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.monitor.api.ConfigMonitor; import com.ctrip.framework.apollo.monitor.internal.DefaultConfigMonitor; -import com.ctrip.framework.apollo.monitor.internal.exporter.internals.DefaultMetricsExporterFactory; +import com.ctrip.framework.apollo.monitor.internal.exporter.impl.DefaultMetricsExporterFactory; import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollectorManager; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultMetricsCollectorManager; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultMetricsCollectorManager; import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporterFactory; import com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer; import com.ctrip.framework.apollo.spi.ConfigFactory; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java index 13ca7929..ce9517f2 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java @@ -16,8 +16,7 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_EXCEPTION; - +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; import com.ctrip.framework.apollo.enums.ConfigSourceType; import java.io.File; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java index 1ab51080..7e230bc3 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java @@ -16,8 +16,8 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.NAMESPACE; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_EXCEPTION; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.APOLLO_CLIENT_NAMESPACE_TIMEOUT; import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.ConfigConsts; @@ -31,8 +31,6 @@ import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector; -import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; import com.ctrip.framework.apollo.spi.ConfigServiceLoadBalancerClient; import com.ctrip.framework.apollo.tracer.Tracer; import com.ctrip.framework.apollo.tracer.spi.Transaction; @@ -219,9 +217,7 @@ private void doLongPollingRefresh(String appId, String cluster, String dataCente transaction.setStatus(ex); long sleepTimeInSecond = m_longPollFailSchedulePolicyInSecond.fail(); if (ex.getCause() instanceof SocketTimeoutException) { - MetricsEvent.builder().withName(DefaultApolloNamespaceCollector.NAMESPACE_TIMEOUT) - .putAttachment(NAMESPACE, assembleNamespaces()) - .withTag(DefaultApolloNamespaceCollector.NAMESPACE_MONITOR).push(); + Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_TIMEOUT,assembleNamespaces()); } logger.warn( "Long polling failed, will retry in {} seconds. appId: {}, cluster: {}, namespaces: {}, long polling url: {}, reason: {}", diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java index 6ca75336..3d9573ee 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java @@ -16,14 +16,10 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.NAMESPACE; -import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.TIMESTAMP; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_MONITOR; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGS; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGMETA; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_VERSION; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIGSERVICE; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_EXCEPTION; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.NAMESPACE_MONITOR; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.APOLLO_CLIENT_NAMESPACE_NOT_FOUND; import com.ctrip.framework.apollo.Apollo; import com.ctrip.framework.apollo.build.ApolloInjector; @@ -40,7 +36,6 @@ import com.ctrip.framework.apollo.enums.ConfigSourceType; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector; import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; import com.ctrip.framework.apollo.tracer.Tracer; import com.ctrip.framework.apollo.tracer.spi.Transaction; @@ -124,10 +119,12 @@ public Properties getConfig() { if (m_configCache.get() == null) { long start = System.currentTimeMillis(); this.sync(); - MetricsEvent.builder().withName(DefaultApolloNamespaceCollector.NAMESPACE_FIRST_LOAD_SPEND).withTag( - NAMESPACE_MONITOR) - .putAttachment(NAMESPACE, m_namespace) - .putAttachment(TIMESTAMP, System.currentTimeMillis() - start).push(); + Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND+m_namespace, + String.valueOf(System.currentTimeMillis() - start)); +// MetricsEvent.builder().withName(APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND).withTag( +// NAMESPACE_MONITOR) +// .putAttachment(NAMESPACE, m_namespace) +// .putAttachment(TIMESTAMP, System.currentTimeMillis() - start).push(); } return transformApolloConfigToProperties(m_configCache.get()); } @@ -278,8 +275,8 @@ private ApolloConfig loadApolloConfig() { appId, cluster, m_namespace); statusCodeException = new ApolloConfigStatusCodeException(ex.getStatusCode(), message); - MetricsEvent.builder().withName(DefaultApolloNamespaceCollector.NAMESPACE_NOT_FOUND).withTag( - NAMESPACE_MONITOR).putAttachment(NAMESPACE, m_namespace).push(); + Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_NOT_FOUND,m_namespace); + } Tracer.logEvent(APOLLO_CONFIG_EXCEPTION, ExceptionUtil.getDetailMessage(statusCodeException)); transaction.setStatus(statusCodeException); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java index 10ba29af..3ec0cb99 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java @@ -16,8 +16,7 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGCHANGES; - +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; import com.ctrip.framework.apollo.enums.ConfigSourceType; import java.util.Collections; import java.util.List; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/YamlConfigFile.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/YamlConfigFile.java index d7757895..bd7905c8 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/YamlConfigFile.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/YamlConfigFile.java @@ -16,8 +16,7 @@ */ package com.ctrip.framework.apollo.internals; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_EXCEPTION; - +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.*; import com.ctrip.framework.apollo.util.ExceptionUtil; import java.util.Properties; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientBootstrapArgsMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientBootstrapArgsMonitorApi.java new file mode 100644 index 00000000..eacce941 --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientBootstrapArgsMonitorApi.java @@ -0,0 +1,26 @@ +/* + * Copyright 2022 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.monitor.api; + +import com.ctrip.framework.apollo.monitor.api.jmx.ApolloJmxBootstrapArgsApi; + +/** + * @author Rawven + */ +public interface ApolloClientBootstrapArgsMonitorApi extends ApolloJmxBootstrapArgsApi { + +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientExceptionMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientExceptionMonitorApi.java new file mode 100644 index 00000000..4595a743 --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientExceptionMonitorApi.java @@ -0,0 +1,31 @@ +/* + * 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.monitor.api; + +import com.ctrip.framework.apollo.monitor.api.jmx.ApolloJmxExceptionApi; +import java.util.List; + +/** + * @author Rawven + */ +public interface ApolloClientExceptionMonitorApi extends ApolloJmxExceptionApi { + + /** + * get exception details + */ + List getExceptionList(); +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientNamespaceMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientNamespaceMonitorApi.java new file mode 100644 index 00000000..be646780 --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientNamespaceMonitorApi.java @@ -0,0 +1,30 @@ +/* + * 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.monitor.api; + +import com.ctrip.framework.apollo.monitor.api.jmx.ApolloJmxNamespaceApi; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.NamespaceMetrics; +import java.util.Map; + +/** + * @author Rawven + */ +public interface ApolloClientNamespaceMonitorApi extends ApolloJmxNamespaceApi { + + Map getNamespaceMetrics(); + +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientThreadPoolMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientThreadPoolMonitorApi.java new file mode 100644 index 00000000..fad6256d --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloClientThreadPoolMonitorApi.java @@ -0,0 +1,27 @@ +/* + * 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.monitor.api; + +import com.ctrip.framework.apollo.monitor.api.jmx.ApolloJmxThreadPoolApi; + +/** + * @author Rawven + */ +public interface ApolloClientThreadPoolMonitorApi extends ApolloJmxThreadPoolApi { + + +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java index 898f7739..a808cfb0 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java @@ -21,13 +21,13 @@ */ public interface ConfigMonitor { - ApolloThreadPoolMonitorApi getThreadPoolMonitorApi(); + ApolloClientThreadPoolMonitorApi getThreadPoolMonitorApi(); - ApolloExceptionMonitorApi getExceptionMonitorApi(); + ApolloClientExceptionMonitorApi getExceptionMonitorApi(); - ApolloNamespaceMonitorApi getNamespaceMonitorApi(); + ApolloClientNamespaceMonitorApi getNamespaceMonitorApi(); - ApolloRunningParamsMonitorApi getRunningParamsMonitorApi(); + ApolloClientBootstrapArgsMonitorApi getRunningParamsMonitorApi(); - String getDataWithCurrentMonitoringSystemFormat(); + String getExporterData(); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloRunningParamsMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxBootstrapArgsApi.java similarity index 88% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloRunningParamsMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxBootstrapArgsApi.java index ff9e2205..9ae5c432 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloRunningParamsMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxBootstrapArgsApi.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Authors + * 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. @@ -14,15 +14,12 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.api; +package com.ctrip.framework.apollo.monitor.api.jmx; import javax.management.MXBean; -/** - * @author Rawven - */ @MXBean -public interface ApolloRunningParamsMonitorApi { +public interface ApolloJmxBootstrapArgsApi { String getStartupParams(String key); @@ -54,7 +51,7 @@ public interface ApolloRunningParamsMonitorApi { long getClientMonitorExternalExportPeriod(); - String getMeta(); + String getApolloMeta(); String getMetaLatestFreshTime(); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloExceptionMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxExceptionApi.java similarity index 79% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloExceptionMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxExceptionApi.java index 6a826a56..626188c6 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloExceptionMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxExceptionApi.java @@ -14,22 +14,13 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.api; +package com.ctrip.framework.apollo.monitor.api.jmx; import java.util.List; import javax.management.MXBean; -/** - * @author Rawven - */ @MXBean -public interface ApolloExceptionMonitorApi { - - - /** - * get the number of exceptions - */ - Integer getExceptionNum(); +public interface ApolloJmxExceptionApi { /** * get exception details diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloNamespaceMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxNamespaceApi.java similarity index 91% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloNamespaceMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxNamespaceApi.java index d9ce8ee9..dae3fafc 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloNamespaceMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxNamespaceApi.java @@ -14,16 +14,13 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.api; +package com.ctrip.framework.apollo.monitor.api.jmx; import java.util.List; import javax.management.MXBean; -/** - * @author Rawven - */ @MXBean -public interface ApolloNamespaceMonitorApi { +public interface ApolloJmxNamespaceApi { String getNamespaceReleaseKey(String namespace); @@ -50,4 +47,5 @@ public interface ApolloNamespaceMonitorApi { List getAllNamespaceFirstLoadSpend(); List getAllNamespaceItemName(); + } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloThreadPoolMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxThreadPoolApi.java similarity index 96% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloThreadPoolMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxThreadPoolApi.java index 33c09e77..e83dc1de 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ApolloThreadPoolMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/jmx/ApolloJmxThreadPoolApi.java @@ -14,7 +14,7 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.api; +package com.ctrip.framework.apollo.monitor.api.jmx; import javax.management.MXBean; @@ -22,7 +22,7 @@ * @author Rawven */ @MXBean -public interface ApolloThreadPoolMonitorApi { +public interface ApolloJmxThreadPoolApi { int getRemoteConfigRepositoryThreadPoolActiveCount(); @@ -85,5 +85,4 @@ public interface ApolloThreadPoolMonitorApi { int getAbstractConfigFileThreadPoolRemainingCapacity(); double getAbstractConfigFileThreadPoolCurrentLoad(); - } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java index cbd33505..a71ad1f7 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java @@ -16,10 +16,10 @@ */ package com.ctrip.framework.apollo.monitor.internal; -import com.ctrip.framework.apollo.monitor.api.ApolloExceptionMonitorApi; -import com.ctrip.framework.apollo.monitor.api.ApolloNamespaceMonitorApi; -import com.ctrip.framework.apollo.monitor.api.ApolloRunningParamsMonitorApi; -import com.ctrip.framework.apollo.monitor.api.ApolloThreadPoolMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientExceptionMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientNamespaceMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientThreadPoolMonitorApi; import com.ctrip.framework.apollo.monitor.api.ConfigMonitor; import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporter; @@ -31,48 +31,48 @@ public class DefaultConfigMonitor implements ConfigMonitor { private MetricsExporter reporter; - private ApolloThreadPoolMonitorApi threadPoolMonitorApi = new NullThreadPoolMonitorApi(); - private ApolloExceptionMonitorApi exceptionMonitorApi = new NullExceptionMonitorApi(); - private ApolloNamespaceMonitorApi apolloNamespaceMonitorApi = new NullNamespaceMonitorApi(); - private ApolloRunningParamsMonitorApi apolloRunningParamsMonitorApi = new NullRunningParamsMonitorApi(); + private ApolloClientThreadPoolMonitorApi threadPoolMonitorApi = new NullClientThreadPoolMonitorApi(); + private ApolloClientExceptionMonitorApi exceptionMonitorApi = new NullClientExceptionMonitorApi(); + private ApolloClientNamespaceMonitorApi apolloClientNamespaceMonitorApi = new NullClientNamespaceMonitorApi(); + private ApolloClientBootstrapArgsMonitorApi apolloClientBootstrapArgsMonitorApi = new NullClientBootstrapArgsMonitorApi(); @Override - public ApolloThreadPoolMonitorApi getThreadPoolMonitorApi() { + public ApolloClientThreadPoolMonitorApi getThreadPoolMonitorApi() { return threadPoolMonitorApi; } @Override - public ApolloExceptionMonitorApi getExceptionMonitorApi() { + public ApolloClientExceptionMonitorApi getExceptionMonitorApi() { return exceptionMonitorApi; } @Override - public ApolloNamespaceMonitorApi getNamespaceMonitorApi() { - return apolloNamespaceMonitorApi; + public ApolloClientNamespaceMonitorApi getNamespaceMonitorApi() { + return apolloClientNamespaceMonitorApi; } @Override - public ApolloRunningParamsMonitorApi getRunningParamsMonitorApi() { - return apolloRunningParamsMonitorApi; + public ApolloClientBootstrapArgsMonitorApi getRunningParamsMonitorApi() { + return apolloClientBootstrapArgsMonitorApi; } @Override - public String getDataWithCurrentMonitoringSystemFormat() { + public String getExporterData() { if (reporter == null) { return "No MonitoringSystem Use"; } return reporter.response(); } - public void init(ApolloNamespaceMonitorApi apolloNamespaceMonitorApi, - ApolloThreadPoolMonitorApi threadPoolMonitorApi, - ApolloExceptionMonitorApi exceptionMonitorApi, - ApolloRunningParamsMonitorApi apolloRunningParamsMonitorApi, + public void init(ApolloClientNamespaceMonitorApi apolloClientNamespaceMonitorApi, + ApolloClientThreadPoolMonitorApi threadPoolMonitorApi, + ApolloClientExceptionMonitorApi exceptionMonitorApi, + ApolloClientBootstrapArgsMonitorApi apolloClientBootstrapArgsMonitorApi, MetricsExporter reporter) { - this.apolloNamespaceMonitorApi = apolloNamespaceMonitorApi; + this.apolloClientNamespaceMonitorApi = apolloClientNamespaceMonitorApi; this.threadPoolMonitorApi = threadPoolMonitorApi; this.exceptionMonitorApi = exceptionMonitorApi; - this.apolloRunningParamsMonitorApi = apolloRunningParamsMonitorApi; + this.apolloClientBootstrapArgsMonitorApi = apolloClientBootstrapArgsMonitorApi; this.reporter = reporter; } } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/MonitorConstant.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/MonitorConstant.java index ea289e1f..a404be95 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/MonitorConstant.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/MonitorConstant.java @@ -17,8 +17,6 @@ package com.ctrip.framework.apollo.monitor.internal; /** - * metrics constant - * * @author Rawven */ public class MonitorConstant { @@ -26,4 +24,15 @@ public class MonitorConstant { public static final String NAMESPACE = "namespace"; public static final String TIMESTAMP = "timestamp"; public static final String MBEAN_NAME = "apollo.client.monitor:type="; + public static final String ERROR_METRICS = "errorMetrics"; + public static final String THROWABLE = ERROR_METRICS + ".throwable"; + public static final String APOLLO_CLIENT_CONFIGCHANGES = "Apollo.Client.ConfigChanges"; + public static final String APOLLO_CONFIG_EXCEPTION = "ApolloConfigException"; + public static final String APOLLO_META_SERVICE = "Apollo.MetaService"; + public static final String APOLLO_CONFIG_SERVICES = "Apollo.Config.Services"; + public static final String APOLLO_CLIENT_VERSION = "Apollo.Client.Version"; + public static final String APOLLO_CONFIGSERVICE = "Apollo.ConfigService"; + public static final String APOLLO_CLIENT_CONFIGS = "Apollo.Client.Configs."; + public static final String APOLLO_CLIENT_CONFIGMETA = "Apollo.Client.ConfigMeta"; + public static final String HELP_STR = "periodicRefresh: "; } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullRunningParamsMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientBootstrapArgsMonitorApi.java similarity index 91% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullRunningParamsMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientBootstrapArgsMonitorApi.java index d5a019d5..71af4f99 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullRunningParamsMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientBootstrapArgsMonitorApi.java @@ -16,9 +16,9 @@ */ package com.ctrip.framework.apollo.monitor.internal; -import com.ctrip.framework.apollo.monitor.api.ApolloRunningParamsMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi; -public class NullRunningParamsMonitorApi implements ApolloRunningParamsMonitorApi { +public class NullClientBootstrapArgsMonitorApi implements ApolloClientBootstrapArgsMonitorApi { @Override public String getStartupParams(String key) { @@ -96,7 +96,7 @@ public long getClientMonitorExternalExportPeriod() { } @Override - public String getMeta() { + public String getApolloMeta() { return ""; } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullExceptionMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientExceptionMonitorApi.java similarity index 74% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullExceptionMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientExceptionMonitorApi.java index d700baee..344a1a94 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullExceptionMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientExceptionMonitorApi.java @@ -16,15 +16,19 @@ */ package com.ctrip.framework.apollo.monitor.internal; -import com.ctrip.framework.apollo.monitor.api.ApolloExceptionMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientExceptionMonitorApi; import java.util.Collections; import java.util.List; -public class NullExceptionMonitorApi implements ApolloExceptionMonitorApi { +/** + * @author Rawven + */ +public class NullClientExceptionMonitorApi implements ApolloClientExceptionMonitorApi { + @Override - public Integer getExceptionNum() { - return 0; + public List getExceptionList() { + return Collections.emptyList(); } @Override diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullNamespaceMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientNamespaceMonitorApi.java similarity index 81% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullNamespaceMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientNamespaceMonitorApi.java index 5377b6d9..42400d82 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullNamespaceMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientNamespaceMonitorApi.java @@ -16,11 +16,21 @@ */ package com.ctrip.framework.apollo.monitor.internal; -import com.ctrip.framework.apollo.monitor.api.ApolloNamespaceMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientNamespaceMonitorApi; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.NamespaceMetrics; import java.util.Collections; import java.util.List; +import java.util.Map; -public class NullNamespaceMonitorApi implements ApolloNamespaceMonitorApi { +/** + * @author Rawven + */ +public class NullClientNamespaceMonitorApi implements ApolloClientNamespaceMonitorApi { + + @Override + public Map getNamespaceMetrics() { + return Collections.emptyMap(); + } @Override public String getNamespaceReleaseKey(String namespace) { diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullThreadPoolMonitorApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientThreadPoolMonitorApi.java similarity index 95% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullThreadPoolMonitorApi.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientThreadPoolMonitorApi.java index 34384df0..bc244cca 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullThreadPoolMonitorApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/NullClientThreadPoolMonitorApi.java @@ -16,9 +16,9 @@ */ package com.ctrip.framework.apollo.monitor.internal; -import com.ctrip.framework.apollo.monitor.api.ApolloThreadPoolMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientThreadPoolMonitorApi; -public class NullThreadPoolMonitorApi implements ApolloThreadPoolMonitorApi { +public class NullClientThreadPoolMonitorApi implements ApolloClientThreadPoolMonitorApi { @Override public int getRemoteConfigRepositoryThreadPoolActiveCount() { diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloRunningParamsCollector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientBootstrapArgsCollector.java similarity index 91% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloRunningParamsCollector.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientBootstrapArgsCollector.java index a5133af5..61c513ff 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloRunningParamsCollector.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientBootstrapArgsCollector.java @@ -14,7 +14,7 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.internal.collector.internal; +package com.ctrip.framework.apollo.monitor.internal.collector.impl; import static com.ctrip.framework.apollo.core.ApolloClientSystemConsts.APOLLO_ACCESS_KEY_SECRET; import static com.ctrip.framework.apollo.core.ApolloClientSystemConsts.APOLLO_CACHE_DIR; @@ -35,27 +35,31 @@ import static com.ctrip.framework.apollo.spring.config.PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES; import com.ctrip.framework.apollo.Apollo; -import com.ctrip.framework.apollo.monitor.api.ApolloRunningParamsMonitorApi; +import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; +import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi; import com.ctrip.framework.apollo.monitor.internal.collector.AbstractMetricsCollector; import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; import com.ctrip.framework.apollo.util.ConfigUtil; import com.google.common.collect.Maps; import java.util.Map; +import org.slf4j.Logger; /** * @author Rawven */ -public class DefaultApolloRunningParamsCollector extends AbstractMetricsCollector implements - ApolloRunningParamsMonitorApi { +public class DefaultApolloClientBootstrapArgsCollector extends AbstractMetricsCollector implements + ApolloClientBootstrapArgsMonitorApi { public static final String ENV = "env"; public static final String VERSION = "version"; public static final String RUNNING_PARAMS = "RunningParams"; public static final String META_FRESH = "metaFreshTime"; public static final String CONFIG_SERVICE_URL = "configServiceUrl"; + private static final Logger logger = DeferredLoggerFactory.getLogger( + DefaultApolloClientBootstrapArgsCollector.class); private final Map map = Maps.newHashMap(); - public DefaultApolloRunningParamsCollector(ConfigUtil configUtil) { + public DefaultApolloClientBootstrapArgsCollector(ConfigUtil configUtil) { super(RUNNING_PARAMS, RUNNING_PARAMS); map.put(APOLLO_ACCESS_KEY_SECRET, configUtil.getAccessKeySecret()); map.put(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES, @@ -102,6 +106,7 @@ public void collect0(MetricsEvent event) { map.put(CONFIG_SERVICE_URL, event.getAttachmentValue(CONFIG_SERVICE_URL)); break; default: + logger.warn("Unhandled event name: {}", event.getName()); break; } } @@ -113,6 +118,8 @@ public boolean isSamplesUpdated() { @Override public void export0() { + // do nothing + return; } @Override @@ -193,7 +200,7 @@ public long getClientMonitorExternalExportPeriod() { } @Override - public String getMeta() { + public String getApolloMeta() { return map.get(APOLLO_META).toString(); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloExceptionCollector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientExceptionCollector.java similarity index 68% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloExceptionCollector.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientExceptionCollector.java index f4895592..344813e5 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloExceptionCollector.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientExceptionCollector.java @@ -14,27 +14,27 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.internal.collector.internal; +package com.ctrip.framework.apollo.monitor.internal.collector.impl; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.ERROR_METRICS; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.THROWABLE; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.ERROR_METRICS; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.THROWABLE; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; -import com.ctrip.framework.apollo.monitor.api.ApolloExceptionMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientExceptionMonitorApi; import com.ctrip.framework.apollo.monitor.internal.collector.AbstractMetricsCollector; import com.ctrip.framework.apollo.monitor.internal.model.CounterModel; import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; /** * @author Rawven */ -public class DefaultApolloExceptionCollector extends AbstractMetricsCollector implements - ApolloExceptionMonitorApi { +public class DefaultApolloClientExceptionCollector extends AbstractMetricsCollector implements + ApolloClientExceptionMonitorApi { public static final String EXCEPTION_NUM = "exception_num"; @@ -44,19 +44,13 @@ public class DefaultApolloExceptionCollector extends AbstractMetricsCollector im private final AtomicInteger exceptionNum = new AtomicInteger(0); - public DefaultApolloExceptionCollector() { + public DefaultApolloClientExceptionCollector() { super(ERROR_METRICS, ERROR_METRICS); } @Override - public Integer getExceptionNum() { - return exceptionNum.get(); - } - - @Override - public List getExceptionDetails() { - return exceptions.stream().map(ApolloConfigException::getMessage) - .collect(Collectors.toList()); + public List getExceptionList() { + return new ArrayList<>(exceptions); } @Override @@ -69,15 +63,23 @@ public void collect0(MetricsEvent event) { exceptions.add(exception); exceptionNum.incrementAndGet(); } + } @Override public void export0() { if (!counterSamples.containsKey(EXCEPTION_NUM)) { - counterSamples.put(EXCEPTION_NUM, CounterModel.builder().name(EXCEPTION_NUM).value(0) - .build()); + counterSamples.put(EXCEPTION_NUM, CounterModel.create(EXCEPTION_NUM, 0)); } counterSamples.get(EXCEPTION_NUM).updateValue(exceptionNum.get()); } + @Override + public List getExceptionDetails() { + List exceptionDetails = new ArrayList<>(); + for (ApolloConfigException exception : new ArrayList<>(exceptions)) { + exceptionDetails.add(exception.getMessage()); + } + return exceptionDetails; + } } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloNamespaceCollector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientNamespaceCollector.java similarity index 72% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloNamespaceCollector.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientNamespaceCollector.java index 8caa5a9b..faf3f34a 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloNamespaceCollector.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientNamespaceCollector.java @@ -14,7 +14,7 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.internal.collector.internal; +package com.ctrip.framework.apollo.monitor.internal.collector.impl; import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.NAMESPACE; @@ -24,7 +24,7 @@ import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; -import com.ctrip.framework.apollo.monitor.api.ApolloNamespaceMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientNamespaceMonitorApi; import com.ctrip.framework.apollo.monitor.internal.MonitorConstant; import com.ctrip.framework.apollo.monitor.internal.collector.AbstractMetricsCollector; import com.ctrip.framework.apollo.monitor.internal.model.CounterModel; @@ -39,86 +39,93 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.function.ToDoubleFunction; import org.slf4j.Logger; /** * @author Rawven */ -public class DefaultApolloNamespaceCollector extends AbstractMetricsCollector implements - ApolloNamespaceMonitorApi { +public class DefaultApolloClientNamespaceCollector extends AbstractMetricsCollector implements + ApolloClientNamespaceMonitorApi { public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()); public static final String NAMESPACE_MONITOR = "namespace_monitor"; public static final String NAMESPACE_LATEST_UPDATE_TIME = "namespace_latest_update_time"; - public static final String NAMESPACE_FIRST_LOAD_SPEND = "namespace_first_load_spend_time"; - public static final String NAMESPACE_USAGE_COUNT = "namespace_usage_count"; + public static final String APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND = "namespace_first_load_spend_time"; + public static final String APOLLO_CLIENT_NAMESPACE_USAGE = "namespace_usage"; public static final String NAMESPACE_RELEASE_KEY = "namespace_release_key"; public static final String NAMESPACE_ITEM_NUM = "namespace_item_num"; public static final String CONFIG_FILE_NUM = "config_file_num"; - public static final String NAMESPACE_NOT_FOUND = "namespace_not_found"; - public static final String NAMESPACE_TIMEOUT = "namespace_timeout"; + public static final String APOLLO_CLIENT_NAMESPACE_NOT_FOUND = "namespace_not_found"; + public static final String APOLLO_CLIENT_NAMESPACE_TIMEOUT = "namespace_timeout"; private static final Logger logger = DeferredLoggerFactory.getLogger( - DefaultApolloNamespaceCollector.class); + DefaultApolloClientNamespaceCollector.class); private final Map m_configs; - private final Map m_configLocks; private final Map m_configFiles; - private final Map m_configFileLocks; private final Map namespaces = Maps.newConcurrentMap(); + private final List namespace404 = Lists.newCopyOnWriteArrayList(); private final List namespaceTimeout = Lists.newCopyOnWriteArrayList(); - public DefaultApolloNamespaceCollector(Map m_configs, - Map m_configLocks, - Map m_configFiles, - Map m_configFileLocks) { + public DefaultApolloClientNamespaceCollector(Map m_configs, + Map m_configFiles + ) { super(NAMESPACE_MONITOR, NAMESPACE_MONITOR); this.m_configs = m_configs; - this.m_configLocks = m_configLocks; this.m_configFiles = m_configFiles; - this.m_configFileLocks = m_configFileLocks; } - - @Override - public void collect0(MetricsEvent event) { +@Override +public void collect0(MetricsEvent event) { String namespace = event.getAttachmentValue(NAMESPACE); - NamespaceMetrics namespaceMetrics = namespaces.computeIfAbsent(namespace, - k -> new NamespaceMetrics()); - switch (event.getName()) { - case NAMESPACE_USAGE_COUNT: - namespaceMetrics.incrementUsageCount(); - break; - case NAMESPACE_LATEST_UPDATE_TIME: - long updateTime = event.getAttachmentValue(MonitorConstant.TIMESTAMP); - namespaceMetrics.setLatestUpdateTime(updateTime); - break; - case NAMESPACE_FIRST_LOAD_SPEND: - long firstLoadSpendTime = event.getAttachmentValue(MonitorConstant.TIMESTAMP); - namespaceMetrics.setFirstLoadSpend(firstLoadSpendTime); - break; - case NAMESPACE_RELEASE_KEY: - String releaseKey = event.getAttachmentValue(NAMESPACE_RELEASE_KEY); - namespaceMetrics.setReleaseKey(releaseKey); - break; - case NAMESPACE_TIMEOUT: - namespaceTimeout.add(namespace); - break; - case NAMESPACE_NOT_FOUND: - namespace404.add(namespace); - break; - default: - logger.warn("Unknown event: {}", event); - break; + String eventName = event.getName(); + + switch (eventName) { + case APOLLO_CLIENT_NAMESPACE_NOT_FOUND: + namespace404.add(namespace); + break; + case APOLLO_CLIENT_NAMESPACE_TIMEOUT: + namespaceTimeout.add(namespace); + break; + default: + NamespaceMetrics namespaceMetrics = namespaces.computeIfAbsent(namespace, k -> new NamespaceMetrics()); + handleNamespaceMetricsEvent(event, namespaceMetrics); + break; } - } +} + +private void handleNamespaceMetricsEvent(MetricsEvent event, NamespaceMetrics namespaceMetrics) { + String eventName = event.getName(); + switch (eventName) { + case APOLLO_CLIENT_NAMESPACE_USAGE: + namespaceMetrics.incrementUsageCount(); + break; + case NAMESPACE_LATEST_UPDATE_TIME: + long updateTime = event.getAttachmentValue(MonitorConstant.TIMESTAMP); + namespaceMetrics.setLatestUpdateTime(updateTime); + break; + case APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND: + long firstLoadSpendTime = event.getAttachmentValue(MonitorConstant.TIMESTAMP); + namespaceMetrics.setFirstLoadSpend(firstLoadSpendTime); + break; + case NAMESPACE_RELEASE_KEY: + String releaseKey = event.getAttachmentValue(NAMESPACE_RELEASE_KEY); + namespaceMetrics.setReleaseKey(releaseKey); + break; + default: + logger.warn("Unhandled event name: {}", eventName); + break; + } +} @Override public void export0() { namespaces.forEach((namespace, metrics) -> { - updateCounterSample(NAMESPACE_USAGE_COUNT, namespace, metrics.getUsageCount()); - updateGaugeSample(NAMESPACE_FIRST_LOAD_SPEND, namespace, metrics.getFirstLoadSpend(), + updateCounterSample(APOLLO_CLIENT_NAMESPACE_USAGE, namespace, metrics.getUsageCount()); + updateGaugeSample(APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND, namespace, + metrics.getFirstLoadSpend(), LONG_CONVERTER); updateGaugeSample(NAMESPACE_LATEST_UPDATE_TIME, namespace, metrics.getLatestUpdateTime(), LONG_CONVERTER); @@ -126,18 +133,18 @@ public void export0() { m_configs.get(namespace).getPropertyNames().size(), INT_CONVERTER); updateGaugeSample(CONFIG_FILE_NUM, namespace, m_configFiles.size(), INT_CONVERTER); }); - updateGaugeSample(NAMESPACE_NOT_FOUND, "", namespace404.size(), INT_CONVERTER); - updateGaugeSample(NAMESPACE_TIMEOUT, "", namespaceTimeout.size(), INT_CONVERTER); + updateGaugeSample(APOLLO_CLIENT_NAMESPACE_NOT_FOUND, "", namespace404.size(), INT_CONVERTER); + updateGaugeSample(APOLLO_CLIENT_NAMESPACE_TIMEOUT, "", namespaceTimeout.size(), INT_CONVERTER); } private void updateCounterSample(String key, String namespace, double value) { String mapKey = namespace + key; if (!counterSamples.containsKey(mapKey)) { - CounterModel.CounterBuilder builder = CounterModel.builder().name(key).value(0); + CounterModel builder = CounterModel.create(key, 0); if (!namespace.isEmpty()) { builder.putTag(NAMESPACE, namespace); } - counterSamples.put(mapKey, builder.build()); + counterSamples.put(mapKey, builder); } counterSamples.get(mapKey).updateValue(value); } @@ -147,16 +154,19 @@ private void updateGaugeSample(String key, String namespace, Object value, ToDoubleFunction applyFunction) { String mapKey = namespace + key; if (!gaugeSamples.containsKey(mapKey)) { - GaugeModel.GaugeBuilder builder = GaugeModel.builder().name(key).value(0) - .apply(applyFunction); + GaugeModel builder = GaugeModel.create(key, 0, applyFunction); if (!namespace.isEmpty()) { builder.putTag(NAMESPACE, namespace); } - gaugeSamples.put(mapKey, builder.build()); + gaugeSamples.put(mapKey, builder); } gaugeSamples.get(mapKey).updateValue(value); } + @Override + public Map getNamespaceMetrics() { + return namespaces; + } @Override public String getNamespaceReleaseKey(String namespace) { diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloThreadPoolCollector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientThreadPoolCollector.java similarity index 94% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloThreadPoolCollector.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientThreadPoolCollector.java index 96312168..38c8f192 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultApolloThreadPoolCollector.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultApolloClientThreadPoolCollector.java @@ -14,12 +14,12 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.internal.collector.internal; +package com.ctrip.framework.apollo.monitor.internal.collector.impl; import com.ctrip.framework.apollo.internals.AbstractConfig; import com.ctrip.framework.apollo.internals.AbstractConfigFile; import com.ctrip.framework.apollo.internals.RemoteConfigRepository; -import com.ctrip.framework.apollo.monitor.api.ApolloThreadPoolMonitorApi; +import com.ctrip.framework.apollo.monitor.api.ApolloClientThreadPoolMonitorApi; import com.ctrip.framework.apollo.monitor.internal.collector.AbstractMetricsCollector; import com.ctrip.framework.apollo.monitor.internal.model.GaugeModel; import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; @@ -33,8 +33,8 @@ /** * @author Rawven */ -public class DefaultApolloThreadPoolCollector extends AbstractMetricsCollector implements - ApolloThreadPoolMonitorApi { +public class DefaultApolloClientThreadPoolCollector extends AbstractMetricsCollector implements + ApolloClientThreadPoolMonitorApi { public static final String THREAD_POOL_METRICS = "ThreadPoolMetrics"; public static final String[] THREAD_POOL_PARAMS = new String[]{"ThreadPoolName", @@ -47,7 +47,7 @@ public class DefaultApolloThreadPoolCollector extends AbstractMetricsCollector i private final ThreadPoolExecutor abstractConfigExecutorService; private final ThreadPoolExecutor abstractConfigFileExecutorService; - public DefaultApolloThreadPoolCollector( + public DefaultApolloClientThreadPoolCollector( ScheduledExecutorService remoteConfigRepositoryExecutorService, ExecutorService abstractConfigExecutorService, ExecutorService abstractConfigFileExecutorService) { @@ -94,9 +94,9 @@ public void exportThreadPoolMetrics(ThreadPoolExecutor executor, for (int i = 0; i < metrics.size(); i++) { if (!gaugeSamples.containsKey(name + THREAD_POOL_PARAMS[i + 1])) { gaugeSamples.put(name + THREAD_POOL_PARAMS[i + 1], - GaugeModel.builder().putTag(THREAD_POOL_PARAMS[0], name) - .name(THREAD_POOL_PARAMS[i + 1]) - .value(0).apply(GaugeModel.DOUBLE_CONVERTER).build()); + GaugeModel.create(THREAD_POOL_PARAMS[i + 1], 0, GaugeModel.DOUBLE_CONVERTER) + .putTag(THREAD_POOL_PARAMS[0], name)); + } gaugeSamples.get(name + THREAD_POOL_PARAMS[i + 1]).updateValue(metrics.get(i)); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultMetricsCollectorManager.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultMetricsCollectorManager.java similarity index 89% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultMetricsCollectorManager.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultMetricsCollectorManager.java index 972b4da0..01a1f302 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/internal/DefaultMetricsCollectorManager.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/collector/impl/DefaultMetricsCollectorManager.java @@ -14,10 +14,11 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.internal.collector.internal; +package com.ctrip.framework.apollo.monitor.internal.collector.impl; import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollector; import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollectorManager; +import java.util.Collections; import java.util.List; /** @@ -28,6 +29,7 @@ public class DefaultMetricsCollectorManager implements MetricsCollectorManager { private List collectors; public DefaultMetricsCollectorManager() { + collectors = Collections.emptyList(); } @Override diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/internals/DefaultMetricsExporterFactory.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/DefaultMetricsExporterFactory.java similarity index 84% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/internals/DefaultMetricsExporterFactory.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/DefaultMetricsExporterFactory.java index 5a0279b1..02fd166c 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/internals/DefaultMetricsExporterFactory.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/DefaultMetricsExporterFactory.java @@ -14,7 +14,7 @@ * limitations under the License. * */ -package com.ctrip.framework.apollo.monitor.internal.exporter.internals; +package com.ctrip.framework.apollo.monitor.internal.exporter.impl; import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.MBEAN_NAME; @@ -22,7 +22,6 @@ import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollector; -import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultMetricsCollectorManager; import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporter; import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporterFactory; import com.ctrip.framework.apollo.monitor.internal.util.JMXUtil; @@ -32,10 +31,13 @@ import java.util.List; import org.slf4j.Logger; +/** + * @author Rawven + */ public class DefaultMetricsExporterFactory implements MetricsExporterFactory { private static final Logger logger = DeferredLoggerFactory.getLogger( - DefaultMetricsCollectorManager.class); + DefaultMetricsExporterFactory.class); private final ConfigUtil m_configUtil; public DefaultMetricsExporterFactory() { @@ -55,14 +57,14 @@ public MetricsExporter getMetricsReporter(List collectors) { if (externalSystemType != null) { List metricsExporters = ServiceBootstrap.loadAllOrdered( MetricsExporter.class); - for (MetricsExporter metricsExporter : metricsExporters) { - if (metricsExporter.isSupport(externalSystemType)) { - reporter = metricsExporter; - reporter.init(collectors, m_configUtil.getMonitorExternalExportPeriod()); - break; - } - } - if (reporter == null) { + reporter = metricsExporters.stream() + .filter(metricsExporter -> metricsExporter.isSupport(externalSystemType)) + .findFirst() + .orElse(null); + + if (reporter != null) { + reporter.init(collectors, m_configUtil.getMonitorExternalExportPeriod()); + } else { String errorMessage = "No matching exporter found with monitor-external-type " + externalSystemType; ApolloConfigException exception = new ApolloConfigException(errorMessage); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/CounterModel.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/CounterModel.java index 67d5c1ba..885f82d1 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/CounterModel.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/CounterModel.java @@ -18,7 +18,6 @@ import com.ctrip.framework.apollo.monitor.internal.util.MeterType; import java.util.HashMap; -import java.util.Map; /** * @author Rawven @@ -28,7 +27,7 @@ public class CounterModel extends MetricsModel { private double nowValue; private double increaseValue; - public CounterModel(String name, double num) { + private CounterModel(String name, double num) { if (name == null || name.isEmpty()) { throw new IllegalArgumentException("Name cannot be null or empty"); } @@ -41,8 +40,16 @@ public CounterModel(String name, double num) { this.type = MeterType.COUNTER; } - public static CounterBuilder builder() { - return new CounterBuilder(); + public static CounterModel create(String name, double value) { + return new CounterModel(name, value); + } + + public CounterModel putTag(String key, String value) { + if (this.tags == null) { + this.tags = new HashMap<>(); + } + this.tags.put(key, value); + return this; } public void updateValue(double value) { @@ -63,38 +70,4 @@ public void setValue(Double value) { } this.nowValue = value; } - - public static class CounterBuilder { - - private final Map tags = new HashMap<>(); - private String name; - private double value; - - public CounterBuilder name(String name) { - if (name == null || name.isEmpty()) { - throw new IllegalArgumentException("Name cannot be null or empty"); - } - this.name = name; - return this; - } - - public CounterBuilder value(double value) { - if (Double.isNaN(value) || Double.isInfinite(value)) { - throw new IllegalArgumentException("Value must be a valid double"); - } - this.value = value; - return this; - } - - public CounterBuilder putTag(String key, String value) { - this.tags.put(key, value); - return this; - } - - public CounterModel build() { - CounterModel sample = new CounterModel(name, value); - sample.tags.putAll(tags); - return sample; - } - } } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/GaugeModel.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/GaugeModel.java index b7026012..3df395d2 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/GaugeModel.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/GaugeModel.java @@ -18,7 +18,6 @@ import com.ctrip.framework.apollo.monitor.internal.util.MeterType; import java.util.HashMap; -import java.util.Map; import java.util.function.ToDoubleFunction; public class GaugeModel extends MetricsModel { @@ -30,15 +29,26 @@ public class GaugeModel extends MetricsModel { private T value; private ToDoubleFunction apply; - public GaugeModel(String name, T value, ToDoubleFunction apply) { + private GaugeModel(String name, T value, ToDoubleFunction apply) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Name cannot be null or empty"); + } this.name = name; this.value = value; this.apply = apply; this.type = MeterType.GAUGE; } - public static GaugeBuilder builder() { - return new GaugeBuilder<>(); + public static GaugeModel create(String name, T value, ToDoubleFunction apply) { + return new GaugeModel<>(name, value, apply); + } + + public GaugeModel putTag(String key, String value) { + if (this.tags == null) { + this.tags = new HashMap<>(); + } + this.tags.put(key, value); + return this; } public T getValue() { @@ -60,39 +70,4 @@ public void setApply(ToDoubleFunction apply) { public double getApplyValue() { return getApply().applyAsDouble(getValue()); } - - public static class GaugeBuilder { - - private final Map tags = new HashMap<>(1); - private String name; - private T value; - private ToDoubleFunction apply; - - public GaugeBuilder name(String name) { - this.name = name; - return this; - } - - public GaugeBuilder value(T value) { - this.value = value; - return this; - } - - public GaugeBuilder apply(ToDoubleFunction apply) { - this.apply = apply; - return this; - } - - public GaugeBuilder putTag(String key, String value) { - this.tags.put(key, value); - return this; - } - - public GaugeModel build() { - GaugeModel sample = new GaugeModel<>(name, value, apply); - sample.tags.putAll(tags); - return sample; - } - } } - diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEvent.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEvent.java index f8c5db31..d60333f4 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEvent.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEvent.java @@ -1,43 +1,48 @@ -/* - * 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.monitor.internal.model; import java.util.HashMap; import java.util.Map; -/** - * metrics event model - * - * @author Rawven - */ public class MetricsEvent { + private String name; + private String tag; + private Map attachments; - private final String name; - private final String tag; - private final Map attachments; + public MetricsEvent(String name, String tag, Map attachments) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Name cannot be null or empty"); + } + this.name = name; + this.tag = tag; + this.attachments = attachments != null ? new HashMap<>(attachments) : new HashMap<>(); + } + + public void reset(String name) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Name cannot be null or empty"); + } + this.name = name; + this.tag = null; + this.attachments.clear(); + } - private MetricsEvent(Builder builder) { - this.name = builder.name; - this.attachments = builder.attachment; - this.tag = builder.tag; + public static MetricsEvent create(String name) { + return MetricsEventPoolHolder.INSTANCE.acquire(name); } - public static Builder builder() { - return new Builder(); + public MetricsEvent withTag(String tag) { + this.tag = tag; + return this; + } + + public MetricsEvent putAttachment(String key, Object value) { + this.attachments.put(key, value); + return this; + } + + public void push() { + MetricsEventPusher.push(this); + MetricsEventPoolHolder.INSTANCE.release(this); } public String getName() { @@ -70,33 +75,7 @@ public String toString() { '}'; } - public static class Builder { - - private final Map attachment = new HashMap<>(3); - private String name; - private String tag; - - public Builder withName(String name) { - this.name = name; - return this; - } - - public Builder putAttachment(String k, Object v) { - this.attachment.put(k, v); - return this; - } - - public Builder withTag(String tag) { - this.tag = tag; - return this; - } - - public void push() { - MetricsEventPusher.push(this.build()); - } - - public MetricsEvent build() { - return new MetricsEvent(this); - } + private static class MetricsEventPoolHolder { + private static final MetricsEventPool INSTANCE = new MetricsEventPool(); } -} +} \ No newline at end of file diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEventPool.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEventPool.java new file mode 100644 index 00000000..f1083f4c --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsEventPool.java @@ -0,0 +1,30 @@ +package com.ctrip.framework.apollo.monitor.internal.model; + +import java.util.HashMap; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; + +public class MetricsEventPool { + private static final int MAX_POOL_SIZE = 100; + private final Queue pool; + + public MetricsEventPool() { + pool = new ConcurrentLinkedQueue<>(); + } + + public MetricsEvent acquire(String name) { + MetricsEvent event = pool.poll(); + if (event == null) { + event = new MetricsEvent(name, null, new HashMap<>()); + } else { + event.reset(name); + } + return event; + } + + public void release(MetricsEvent event) { + if (pool.size() < MAX_POOL_SIZE) { + pool.offer(event); + } + } +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsModel.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsModel.java index e293ce6f..eb574ed7 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsModel.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/model/MetricsModel.java @@ -17,7 +17,6 @@ package com.ctrip.framework.apollo.monitor.internal.model; import com.ctrip.framework.apollo.monitor.internal.util.MeterType; -import java.util.HashMap; import java.util.Map; /** @@ -25,7 +24,7 @@ */ public class MetricsModel { - protected final Map tags = new HashMap<>(); + protected Map tags; protected String name; protected MeterType type; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/MonitorMessageProducer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ClientMonitorMessageProducer.java similarity index 56% rename from apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/MonitorMessageProducer.java rename to apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ClientMonitorMessageProducer.java index 83625f20..9e9923d6 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/MonitorMessageProducer.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ClientMonitorMessageProducer.java @@ -16,29 +16,34 @@ */ package com.ctrip.framework.apollo.monitor.internal.tracer; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_CLIENT_CONFIGCHANGES; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_CLIENT_CONFIGMETA; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_CLIENT_CONFIGS; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_CLIENT_VERSION; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_CONFIGSERVICE; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_CONFIG_EXCEPTION; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_CONFIG_SERVICES; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.APOLLO_META_SERVICE; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.ERROR_METRICS; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.HELP_STR; import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.NAMESPACE; +import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.THROWABLE; import static com.ctrip.framework.apollo.monitor.internal.MonitorConstant.TIMESTAMP; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.DATE_FORMATTER; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_LATEST_UPDATE_TIME; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_MONITOR; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_RELEASE_KEY; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloRunningParamsCollector.CONFIG_SERVICE_URL; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloRunningParamsCollector.META_FRESH; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloRunningParamsCollector.RUNNING_PARAMS; -import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloRunningParamsCollector.VERSION; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGCHANGES; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGMETA; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_CONFIGS; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CLIENT_VERSION; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIGSERVICE; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_EXCEPTION; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_CONFIG_SERVICES; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.APOLLO_META_SERVICE; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.ERROR_METRICS; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.HELP_STR; -import static com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite.THROWABLE; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientBootstrapArgsCollector.CONFIG_SERVICE_URL; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientBootstrapArgsCollector.META_FRESH; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientBootstrapArgsCollector.RUNNING_PARAMS; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientBootstrapArgsCollector.VERSION; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.APOLLO_CLIENT_NAMESPACE_NOT_FOUND; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.APOLLO_CLIENT_NAMESPACE_TIMEOUT; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.APOLLO_CLIENT_NAMESPACE_USAGE; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.DATE_FORMATTER; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.NAMESPACE_LATEST_UPDATE_TIME; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.NAMESPACE_MONITOR; +import static com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector.NAMESPACE_RELEASE_KEY; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; +import com.ctrip.framework.apollo.monitor.internal.MonitorConstant; +import com.ctrip.framework.apollo.monitor.internal.collector.impl.DefaultApolloClientNamespaceCollector; import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; import com.ctrip.framework.apollo.tracer.spi.MessageProducer; import com.ctrip.framework.apollo.tracer.spi.Transaction; @@ -48,11 +53,9 @@ import java.util.List; /** - * metrics message producer - * * @author Rawven */ -public class MonitorMessageProducer implements MessageProducer { +public class ClientMonitorMessageProducer implements MessageProducer { public static final List TAGS = Collections.unmodifiableList( Arrays.asList( @@ -62,13 +65,16 @@ public class MonitorMessageProducer implements MessageProducer { APOLLO_CONFIG_SERVICES, APOLLO_CLIENT_VERSION, APOLLO_CONFIGSERVICE, - APOLLO_CLIENT_CONFIGMETA + APOLLO_CLIENT_CONFIGMETA, + APOLLO_CLIENT_NAMESPACE_TIMEOUT, + APOLLO_CLIENT_NAMESPACE_USAGE, + APOLLO_CLIENT_NAMESPACE_NOT_FOUND ) ); @Override public void logError(Throwable cause) { - MetricsEvent.builder().withName(ERROR_METRICS) + MetricsEvent.create(ERROR_METRICS) .withTag(ERROR_METRICS) .putAttachment(THROWABLE, cause) .push(); @@ -76,7 +82,7 @@ public void logError(Throwable cause) { @Override public void logError(String message, Throwable cause) { - MetricsEvent.builder().withName(ERROR_METRICS) + MetricsEvent.create(ERROR_METRICS) .withTag(ERROR_METRICS) .putAttachment(THROWABLE, cause).push(); } @@ -98,8 +104,7 @@ private void handleTaggedEvent(String type, String name) { } case APOLLO_CLIENT_CONFIGCHANGES: { namespace = name; - MetricsEvent.builder() - .withName(NAMESPACE_LATEST_UPDATE_TIME) + MetricsEvent.create(NAMESPACE_LATEST_UPDATE_TIME) .putAttachment(NAMESPACE, namespace) .putAttachment(TIMESTAMP, System.currentTimeMillis()) .withTag(NAMESPACE_MONITOR) @@ -111,8 +116,7 @@ private void handleTaggedEvent(String type, String name) { break; } case APOLLO_META_SERVICE: { - MetricsEvent.builder() - .withName(META_FRESH) + MetricsEvent.create(META_FRESH) .withTag(RUNNING_PARAMS) .putAttachment(META_FRESH, DATE_FORMATTER.format(Instant.ofEpochMilli(System.currentTimeMillis()))) @@ -120,21 +124,36 @@ private void handleTaggedEvent(String type, String name) { break; } case APOLLO_CONFIG_SERVICES: { - MetricsEvent.builder() - .withName(CONFIG_SERVICE_URL) + MetricsEvent.create(CONFIG_SERVICE_URL) .withTag(RUNNING_PARAMS) .putAttachment(CONFIG_SERVICE_URL, name) .push(); break; } case APOLLO_CLIENT_VERSION: { - MetricsEvent.builder() - .withName(VERSION) + MetricsEvent.create(VERSION) .withTag(RUNNING_PARAMS) .putAttachment(VERSION, name) .push(); break; } + case APOLLO_CLIENT_NAMESPACE_TIMEOUT: { + MetricsEvent.create(APOLLO_CLIENT_NAMESPACE_TIMEOUT) + .putAttachment(NAMESPACE, name) + .withTag(DefaultApolloClientNamespaceCollector.NAMESPACE_MONITOR).push(); + break; + } + case APOLLO_CLIENT_NAMESPACE_USAGE: { + MetricsEvent.create(APOLLO_CLIENT_NAMESPACE_USAGE) + .putAttachment(MonitorConstant.NAMESPACE, name) + .withTag(NAMESPACE_MONITOR).push(); + break; + } + case APOLLO_CLIENT_NAMESPACE_NOT_FOUND: { + MetricsEvent.create(APOLLO_CLIENT_NAMESPACE_NOT_FOUND).withTag( + NAMESPACE_MONITOR).putAttachment(NAMESPACE, name).push(); + break; + } case APOLLO_CLIENT_CONFIGMETA: // 不需要收集 break; @@ -147,8 +166,7 @@ private void handleClientConfigEvent(String type, String name) { int len = APOLLO_CLIENT_CONFIGS.length(); String namespace = type.substring(len); String releaseKey = name; - MetricsEvent.builder() - .withName(NAMESPACE_RELEASE_KEY) + MetricsEvent.create(NAMESPACE_RELEASE_KEY) .withTag(NAMESPACE_MONITOR) .putAttachment(NAMESPACE_RELEASE_KEY, releaseKey) .putAttachment(NAMESPACE, namespace) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/MessageProducerComposite.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/MessageProducerComposite.java index 91a0db6b..23c075b1 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/MessageProducerComposite.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/MessageProducerComposite.java @@ -29,17 +29,7 @@ */ public class MessageProducerComposite implements MessageProducer { - public static final String ERROR_METRICS = "errorMetrics"; - public static final String THROWABLE = ERROR_METRICS + ".throwable"; - public static final String APOLLO_CLIENT_CONFIGCHANGES = "Apollo.Client.ConfigChanges"; - public static final String APOLLO_CONFIG_EXCEPTION = "ApolloConfigException"; - public static final String APOLLO_META_SERVICE = "Apollo.MetaService"; - public static final String APOLLO_CONFIG_SERVICES = "Apollo.Config.Services"; - public static final String APOLLO_CLIENT_VERSION = "Apollo.Client.Version"; - public static final String APOLLO_CONFIGSERVICE = "Apollo.ConfigService"; - public static final String APOLLO_CLIENT_CONFIGS = "Apollo.Client.Configs."; - public static final String APOLLO_CLIENT_CONFIGMETA = "Apollo.Client.ConfigMeta"; - public static final String HELP_STR = "periodicRefresh: "; + private static final NullTransaction NULL_TRANSACTION = new NullTransaction(); private List producers; @@ -50,35 +40,42 @@ public MessageProducerComposite(List producers) { @Override public void logError(Throwable cause) { - producers.forEach(producer -> producer.logError(cause)); + for (MessageProducer producer : producers) { + producer.logError(cause); + } } @Override public void logError(String message, Throwable cause) { - producers.forEach(producer -> producer.logError(message, cause)); + for (MessageProducer producer : producers) { + producer.logError(message, cause); + } } @Override public void logEvent(String type, String name) { - producers.forEach(producer -> producer.logEvent(type, name)); + for (MessageProducer producer : producers) { + producer.logEvent(type, name); + } } @Override public void logEvent(String type, String name, String status, String nameValuePairs) { - producers.forEach(producer -> producer.logEvent(type, name, status, nameValuePairs)); + for (MessageProducer producer : producers) { + producer.logEvent(type, name, status, nameValuePairs); + } } @Override public Transaction newTransaction(String type, String name) { - return producers.stream() - .map(producer -> producer.newTransaction(type, name)) - .filter(Objects::nonNull) - .findFirst() - .orElse(NULL_TRANSACTION); + for (MessageProducer producer : producers) { + Transaction transaction = producer.newTransaction(type, name); + if (transaction != null) { + return transaction; + } + } + return NULL_TRANSACTION; } - public List getProducers() { - return producers; - } } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/collector/AbstractApolloMetricsCollectorTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/collector/AbstractApolloMetricsCollectorTest.java deleted file mode 100644 index 4a3da10c..00000000 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/collector/AbstractApolloMetricsCollectorTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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.monitor.internal.collector; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.when; - -import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; -import com.ctrip.framework.apollo.monitor.internal.model.MetricsModel; -import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -public class AbstractApolloMetricsCollectorTest { - - private AbstractMetricsCollector metricsCollector; - - @Before - public void setUp() { - metricsCollector = new AbstractMetricsCollector("mock","tag1", "tag2") { - @Override - public String name() { - return "MockMetricsCollector"; - } - - @Override - public void collect0(MetricsEvent event) { - // 模拟实现 - } - - @Override - public void export0() { - // 模拟实现 - } - }; - } - - @Test - public void testConstructorInitialization() { - assertNotNull(metricsCollector); - } - - @Test - public void testIsSupport() { - MetricsEvent event = Mockito.mock(MetricsEvent.class); - - when(event.getTag()).thenReturn("tag1"); - assertTrue(metricsCollector.isSupport(event)); - - when(event.getTag()).thenReturn("tag3"); - assertFalse(metricsCollector.isSupport(event)); - } - - @Test - public void testCollect() { - MetricsEvent event = Mockito.mock(MetricsEvent.class); - metricsCollector.collect(event); - assertTrue(metricsCollector.isSamplesUpdated()); - } - - @Test - public void testIsSamplesUpdated() { - MetricsEvent event = Mockito.mock(MetricsEvent.class); - metricsCollector.collect(event); - assertTrue(metricsCollector.isSamplesUpdated()); - assertFalse(metricsCollector.isSamplesUpdated()); - } - - @Test - public void testExport() { - List samples = metricsCollector.export(); - assertNotNull(samples); - } -} diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/AbstractApolloMetricsExporterTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/AbstractApolloMetricsExporterTest.java deleted file mode 100644 index e7122eb4..00000000 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/AbstractApolloMetricsExporterTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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.monitor.internal.exporter; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollector; -import com.ctrip.framework.apollo.monitor.internal.model.CounterModel; -import com.ctrip.framework.apollo.monitor.internal.model.GaugeModel; -import com.ctrip.framework.apollo.monitor.internal.model.MetricsModel; -import com.ctrip.framework.apollo.monitor.internal.util.MeterType; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class AbstractApolloMetricsExporterTest { - - @Mock - private MetricsCollector mockCollector; - - @InjectMocks - private final AbstractMetricsExporter reporter = new AbstractMetricsExporter() { - @Override - protected void doInit() { - // Do nothing for test purposes - } - - @Override - public void registerGaugeSample(GaugeModel sample) { - // Mock implementation for test purposes - } - - @Override - public String response() { - return "test"; - } - - @Override - public boolean isSupport(String form) { - return "mock".equals(form); - } - - @Override - public void registerCounterSample(CounterModel sample) { - // Mock implementation for test purposes - } - }; - - - - @Test - public void testInit() { - List collectors = Collections.singletonList(mockCollector); - long collectPeriod = 10L; - - reporter.init(collectors, collectPeriod); - - assertNotNull(reporter.m_executorService); - } - @Test - public void testIsSupport(){ - assertTrue(reporter.isSupport("mock")); - assertFalse(reporter.isSupport("mock1")); - } - - @Test - public void testUpdateMetricsData() { - MetricsModel mockSample = mock(MetricsModel.class); - when(mockSample.getType()).thenReturn(MeterType.GAUGE); - when(mockCollector.isSamplesUpdated()).thenReturn(true); - when(mockCollector.export()).thenReturn(Collections.singletonList(mockSample)); - - reporter.init(Collections.singletonList(mockCollector), 10L); - reporter.updateMetricsData(); - - verify(mockCollector, times(1)).isSamplesUpdated(); - verify(mockCollector, times(1)).export(); - verify(mockSample, times(1)).getType(); - } - - @Test - public void testGetTags() { - MetricsModel sample = mock(MetricsModel.class); - Map tags = new HashMap<>(); - tags.put("key1", "value1"); - tags.put("key2", "value2"); - - when(sample.getTags()).thenReturn(tags); - - String[][] result = reporter.getTags(sample); - - assertArrayEquals(new String[]{"key1", "key2"}, result[0]); - assertArrayEquals(new String[]{"value1", "value2"}, result[1]); - } -} diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/DefaultApolloMetricsExporterFactoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/DefaultApolloMetricsExporterFactoryTest.java deleted file mode 100644 index ca4e807e..00000000 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/exporter/DefaultApolloMetricsExporterFactoryTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.monitor.internal.exporter; - -import static org.junit.Assert.assertNull; - -import com.ctrip.framework.apollo.build.MockInjector; -import com.ctrip.framework.apollo.core.ApolloClientSystemConsts; -import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollector; -import com.ctrip.framework.apollo.monitor.internal.exporter.internals.DefaultMetricsExporterFactory; -import com.ctrip.framework.apollo.util.ConfigUtil; -import java.util.ArrayList; -import java.util.List; -import org.junit.Test; - -public class DefaultApolloMetricsExporterFactoryTest { - - private DefaultMetricsExporterFactory defaultMetricsReporterFactory; - - public void init(String form, String period) { - if (form!=null){ - System.setProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE,form); - } - if (period!=null){ - System.setProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD,period); - } - ConfigUtil mockConfigUtil = new ConfigUtil(); - defaultMetricsReporterFactory = new DefaultMetricsExporterFactory(); - MockInjector.setInstance(ConfigUtil.class, mockConfigUtil); - } - - @Test - public void testGetMetricsReporter_NoSupportedReporter() { - init("prometheus","300"); - List collectors = new ArrayList<>(); - assertNull(defaultMetricsReporterFactory.getMetricsReporter(collectors)); - } - - @Test - public void testGetMetricsReporter_NullForm() { - init(null,null); - List collectors = new ArrayList<>(); - MetricsExporter reporter = defaultMetricsReporterFactory.getMetricsReporter(collectors); - assertNull(reporter); - } -} diff --git a/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/plugin/prometheus/PrometheusMetricExporter.java b/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/plugin/prometheus/PrometheusMetricExporter.java index 76c921b5..c2e53943 100644 --- a/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/plugin/prometheus/PrometheusMetricExporter.java +++ b/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/plugin/prometheus/PrometheusMetricExporter.java @@ -56,6 +56,7 @@ public void doInit() { public boolean isSupport(String form) { return PROMETHEUS.equals(form); } + @Override public void registerCounterSample(CounterModel sample) {