diff --git a/client/lookout-api/src/main/java/com/alipay/lookout/api/ManualClock.java b/client/lookout-api/src/main/java/com/alipay/lookout/api/ManualClock.java index 11af363..01f58c8 100644 --- a/client/lookout-api/src/main/java/com/alipay/lookout/api/ManualClock.java +++ b/client/lookout-api/src/main/java/com/alipay/lookout/api/ManualClock.java @@ -20,7 +20,7 @@ * 一个可以手动调整的Clock实现, 常用于单元测试 * * @author xiangfeng.xzc - * @date 2018/7/27 + * 2018/7/27 */ public class ManualClock implements Clock { private volatile long wallTime; diff --git a/client/lookout-api/src/main/java/com/alipay/lookout/api/Registry.java b/client/lookout-api/src/main/java/com/alipay/lookout/api/Registry.java index df9db35..968abc7 100644 --- a/client/lookout-api/src/main/java/com/alipay/lookout/api/Registry.java +++ b/client/lookout-api/src/main/java/com/alipay/lookout/api/Registry.java @@ -81,9 +81,11 @@ public interface Registry extends Iterable { /** * Register a info instance - * @param id metric id + * @param id id metric id * @param info an info instance - * @return + * @param info data + * @param info type + * @return info info */ > Info info(Id id, Y info); @@ -127,7 +129,8 @@ public interface Registry extends Iterable { * if reaching the max number,null will be returned too. * * @param id metric id - * @return Instance of the metric or null if there is no match. + * @param metric type + * @return X Instance of the metric or null if there is no match. */ X get(Id id); diff --git a/client/lookout-api/src/main/java/com/alipay/lookout/api/ResettableStep.java b/client/lookout-api/src/main/java/com/alipay/lookout/api/ResettableStep.java index 0d9ab75..d9074ed 100644 --- a/client/lookout-api/src/main/java/com/alipay/lookout/api/ResettableStep.java +++ b/client/lookout-api/src/main/java/com/alipay/lookout/api/ResettableStep.java @@ -20,13 +20,13 @@ * 暴露一个接口用于修改 step * * @author xiangfeng.xzc - * @date 2018/7/26 + * 2018/7/26 */ public interface ResettableStep { /** * 设置新的step * - * @param step 新的步长, 必须>0 + * @param step 新的步长, 必须大于 0 */ void setStep(long step); } diff --git a/client/lookout-api/src/main/java/com/alipay/lookout/api/Timer.java b/client/lookout-api/src/main/java/com/alipay/lookout/api/Timer.java index babe504..c0685c7 100644 --- a/client/lookout-api/src/main/java/com/alipay/lookout/api/Timer.java +++ b/client/lookout-api/src/main/java/com/alipay/lookout/api/Timer.java @@ -26,7 +26,7 @@ public interface Timer extends Metric { /** * @param amount Duration of a single event - * @param unit + * @param unit time unit */ void record(long amount, TimeUnit unit); diff --git a/client/lookout-api/src/main/java/com/alipay/lookout/common/top/TopUtil.java b/client/lookout-api/src/main/java/com/alipay/lookout/common/top/TopUtil.java index 4b4ff60..c9af8de 100644 --- a/client/lookout-api/src/main/java/com/alipay/lookout/common/top/TopUtil.java +++ b/client/lookout-api/src/main/java/com/alipay/lookout/common/top/TopUtil.java @@ -70,7 +70,7 @@ public static TopGauger topGauger(final Registry registry, final Id id, final in public static TopGauger topGauger(final Registry registry, final Id id, final int maxNumber, final Order order) { - if (registry instanceof NoopRegistry) { + if (registry instanceof NoopRegistry || (id == NoopRegistry.INSTANCE.createId(null))) { return NoopTopGauger.INSTANCE; } Id key = id.withTag(TOP_NUM_TAG_KEY, String.valueOf(maxNumber)); diff --git a/client/lookout-client/src/main/java/com/alipay/lookout/client/AbstractLookoutClient.java b/client/lookout-client/src/main/java/com/alipay/lookout/client/AbstractLookoutClient.java index 5662042..1cf42c4 100644 --- a/client/lookout-client/src/main/java/com/alipay/lookout/client/AbstractLookoutClient.java +++ b/client/lookout-client/src/main/java/com/alipay/lookout/client/AbstractLookoutClient.java @@ -24,8 +24,8 @@ import com.alipay.lookout.common.log.LookoutLoggerFactory; import com.alipay.lookout.common.utils.NetworkUtil; import com.alipay.lookout.core.CommonTagsAccessor; +import com.alipay.lookout.remote.report.poller.MetricsHttpExporter; import com.google.common.base.Preconditions; - import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; @@ -35,9 +35,10 @@ * Created by kevin.luy@alipay.com on 2018/4/24. */ abstract class AbstractLookoutClient implements LookoutClient { - Logger logger = LookoutLoggerFactory.getLogger(this.getClass()); - private final String appName; - private CompositeRegistry globalRegistry = new CompositeRegistry(Clock.SYSTEM); + Logger logger = LookoutLoggerFactory.getLogger(this.getClass()); + private final String appName; + private CompositeRegistry globalRegistry = new CompositeRegistry(Clock.SYSTEM); + private MetricsHttpExporter metricsHttpExporter; /** * new an abstractLookoutClient @@ -100,4 +101,19 @@ protected void addDefaultCommonTags(CommonTagsAccessor commonTagsAccessor) { commonTagsAccessor.setCommonTag("app", appName); } + @Override + public void close() throws Exception { + MetricsHttpExporter metricsHttpExporter = getMetricsHttpExporter(); + if (metricsHttpExporter != null) { + metricsHttpExporter.close(); + } + } + + protected MetricsHttpExporter getMetricsHttpExporter() { + return metricsHttpExporter; + } + + protected void setMetricsHttpExporter(MetricsHttpExporter metricsHttpExporter) { + this.metricsHttpExporter = metricsHttpExporter; + } } diff --git a/client/lookout-client/src/main/java/com/alipay/lookout/client/DefaultLookoutClient.java b/client/lookout-client/src/main/java/com/alipay/lookout/client/DefaultLookoutClient.java index dc8ccac..08e3695 100644 --- a/client/lookout-client/src/main/java/com/alipay/lookout/client/DefaultLookoutClient.java +++ b/client/lookout-client/src/main/java/com/alipay/lookout/client/DefaultLookoutClient.java @@ -18,8 +18,7 @@ import com.alipay.lookout.api.Lookout; import com.alipay.lookout.api.MetricRegistry; -import com.alipay.lookout.core.config.LookoutConfig; -import com.alipay.lookout.remote.report.poller.ResettableStepRegistry; +import com.alipay.lookout.remote.step.LookoutRegistry; /** * Created by kevin.luy@alipay.com on 2017/10/4. @@ -45,6 +44,13 @@ public synchronized void addRegistry(MetricRegistry registry) { registry.registerExtendedMetrics(); } super.addRegistry(registry); + if (registry instanceof LookoutRegistry) { + try { + setMetricsHttpExporter(PollerUtils.exportHttp((LookoutRegistry) registry)); + } catch (Exception e) { + logger.error("fail to start MetricsHttpExporter", e); + } + } } /** @@ -56,15 +62,4 @@ public synchronized void registerExtendedMetrics() { //对已有registry补偿登记 super.registerExtendedMetrics(); } - - public synchronized void registerExporter(LookoutConfig config) { - try { - ResettableStepRegistry resettableStepRegistry = PollerUtils.exportHttp(config, this) - .getController().getRegistry(); - resettableStepRegistry.registerExtendedMetrics(); - super.addRegistry(resettableStepRegistry); - } catch (Exception e) { - logger.error("fail to start MetricsHttpExporter", e); - } - } } diff --git a/client/lookout-client/src/main/java/com/alipay/lookout/client/LookoutClient.java b/client/lookout-client/src/main/java/com/alipay/lookout/client/LookoutClient.java index 7a4532e..2a98526 100644 --- a/client/lookout-client/src/main/java/com/alipay/lookout/client/LookoutClient.java +++ b/client/lookout-client/src/main/java/com/alipay/lookout/client/LookoutClient.java @@ -31,4 +31,11 @@ public interface LookoutClient { */ T getRegistry(); + /** + * Closes this resource, relinquishing any underlying resources. + * + * @throws Exception if this resource cannot be closed + */ + void close() throws Exception; + } diff --git a/client/lookout-client/src/main/java/com/alipay/lookout/client/PollerUtils.java b/client/lookout-client/src/main/java/com/alipay/lookout/client/PollerUtils.java index 8edb08e..aa3317f 100644 --- a/client/lookout-client/src/main/java/com/alipay/lookout/client/PollerUtils.java +++ b/client/lookout-client/src/main/java/com/alipay/lookout/client/PollerUtils.java @@ -16,16 +16,12 @@ */ package com.alipay.lookout.client; -import com.alipay.lookout.api.Clock; import com.alipay.lookout.api.Registry; import com.alipay.lookout.common.log.LookoutLoggerFactory; -import com.alipay.lookout.core.config.LookoutConfig; import com.alipay.lookout.remote.report.poller.Listener; import com.alipay.lookout.remote.report.poller.MetricsHttpExporter; import com.alipay.lookout.remote.report.poller.PollerController; -import com.alipay.lookout.remote.report.poller.ResettableStepRegistry; import com.alipay.lookout.remote.step.LookoutRegistry; - import org.slf4j.Logger; import java.util.ArrayList; @@ -41,39 +37,19 @@ final class PollerUtils { private PollerUtils() { } - /** - * 辅助方法, 通过HTTP暴露自身的metrics数据 - * - * @param config - * @param client - * @return - * @throws Exception - */ - static MetricsHttpExporter exportHttp(LookoutConfig config, AbstractLookoutClient client) - throws Exception { - ResettableStepRegistry resettableStepRegistry = new ResettableStepRegistry(Clock.SYSTEM, - config); - - final List lookoutRegistryList = new ArrayList(); - for (Registry r : client.getInnerCompositeRegistry().getRegistries()) { - if (r instanceof LookoutRegistry) { - lookoutRegistryList.add((LookoutRegistry) r); - } - } - PollerController controller = new PollerController(resettableStepRegistry); + static MetricsHttpExporter exportHttp(final LookoutRegistry registry) throws Exception { + //TODO check only one lookoutRegistry + PollerController controller = new PollerController(registry); controller.addListener(new Listener() { @Override public void onActive() { - for (LookoutRegistry r : lookoutRegistryList) { - r.getMetricObserverComposite().setEnabled(false); - } + registry.getMetricObserverComposite().setEnabled(false); + } @Override public void onIdle() { - for (LookoutRegistry r : lookoutRegistryList) { - r.getMetricObserverComposite().setEnabled(false); - } + registry.getMetricObserverComposite().setEnabled(true); } }); try { diff --git a/client/lookout-client/src/main/java/com/alipay/lookout/client/SimpleLookoutClient.java b/client/lookout-client/src/main/java/com/alipay/lookout/client/SimpleLookoutClient.java index 42116ad..844f680 100644 --- a/client/lookout-client/src/main/java/com/alipay/lookout/client/SimpleLookoutClient.java +++ b/client/lookout-client/src/main/java/com/alipay/lookout/client/SimpleLookoutClient.java @@ -25,7 +25,6 @@ import com.alipay.lookout.core.CommonTagsAccessor; import com.alipay.lookout.core.config.LookoutConfig; import com.alipay.lookout.core.config.MetricConfig; -import com.alipay.lookout.remote.report.poller.ResettableStepRegistry; import com.alipay.lookout.remote.step.LookoutRegistry; import java.util.concurrent.atomic.AtomicInteger; @@ -78,29 +77,20 @@ public SimpleLookoutClient(String appName, LookoutConfig config, MetricRegistry. //add jvm and other metrics registry.registerExtendedMetrics(); super.addRegistry(registry); + if (registry instanceof LookoutRegistry) { + try { + setMetricsHttpExporter(PollerUtils.exportHttp((LookoutRegistry) registry)); + } catch (Exception e) { + logger.error("fail to start MetricsHttpExporter", e); + } + } } - exportPoller(); - logger.debug("set global registry to Lookout"); // init global registry Lookout.setRegistry(getRegistry()); } - private void exportPoller() { - if (!lookoutConfig.getBoolean(LookoutConfig.POLLER_EXPORTER_ENABLED, false)) { - return; - } - try { - ResettableStepRegistry resettableStepRegistry = PollerUtils - .exportHttp(lookoutConfig, this).getController().getRegistry(); - resettableStepRegistry.registerExtendedMetrics(); - super.addRegistry(resettableStepRegistry); - } catch (Exception e) { - logger.error("fail to start MetricsHttpExporter", e); - } - } - /** * get the global configuration * diff --git a/client/lookout-client/src/test/java/com/alipay/lookout/client/DefaultLookoutClientTest.java b/client/lookout-client/src/test/java/com/alipay/lookout/client/DefaultLookoutClientTest.java index ff60b38..6512621 100644 --- a/client/lookout-client/src/test/java/com/alipay/lookout/client/DefaultLookoutClientTest.java +++ b/client/lookout-client/src/test/java/com/alipay/lookout/client/DefaultLookoutClientTest.java @@ -31,24 +31,33 @@ public class DefaultLookoutClientTest { @Test(expected = IllegalStateException.class) - public void testDefaultLookoutClientWithoutRegistry() { - LookoutClient client1 = new DefaultLookoutClient("demo"); - LookoutClient client2 = new DefaultLookoutClient("demo"); - client1.getRegistry(); + public void testDefaultLookoutClientWithoutRegistry() throws Exception { + LookoutClient client1 = null; + LookoutClient client2 = null; + try { + client1 = new DefaultLookoutClient("demo"); + client2 = new DefaultLookoutClient("demo"); + client1.getRegistry(); + } finally { + client1.close(); + client2.close(); + } + } @Test - public void testDefaultLookoutClient_addRegsitry() { + public void testDefaultLookoutClient_addRegsitry() throws Exception { LookoutConfig lookoutConfig = new LookoutConfig(); DefaultLookoutClient client = new DefaultLookoutClient("demo"); LookoutRegistry lookoutRegistry = new LookoutRegistry(lookoutConfig); client.addRegistry(lookoutRegistry); client.addRegistry(new DefaultRegistry(lookoutConfig)); Assert.assertEquals(2, ((CompositeRegistry) client.getRegistry()).getRegistries().size()); + client.close(); } @Test - public void testDefaultLookoutClient_addExtMetrics() { + public void testDefaultLookoutClient_addExtMetrics() throws Exception { LookoutConfig lookoutConfig = new LookoutConfig(); DefaultLookoutClient client = new DefaultLookoutClient("demo"); MetricRegistry r = new DefaultRegistry(lookoutConfig); @@ -56,5 +65,6 @@ public void testDefaultLookoutClient_addExtMetrics() { client.registerExtendedMetrics(); Id id = r.createId("jvm.gc"); Assert.assertNotNull(client.getRegistry().get(id)); + client.close(); } } diff --git a/client/lookout-client/src/test/java/com/alipay/lookout/client/MetricsHttpExporterTest.java b/client/lookout-client/src/test/java/com/alipay/lookout/client/MetricsHttpExporterTest.java index dee0b7e..fae34fa 100644 --- a/client/lookout-client/src/test/java/com/alipay/lookout/client/MetricsHttpExporterTest.java +++ b/client/lookout-client/src/test/java/com/alipay/lookout/client/MetricsHttpExporterTest.java @@ -23,7 +23,6 @@ import com.alipay.lookout.remote.report.poller.Listener; import com.alipay.lookout.remote.report.poller.MetricsHttpExporter; import com.alipay.lookout.remote.report.poller.PollerController; -import com.alipay.lookout.remote.report.poller.ResettableStepRegistry; import com.alipay.lookout.remote.step.LookoutRegistry; import org.apache.http.client.methods.CloseableHttpResponse; @@ -71,18 +70,18 @@ public void onIdle() { @Test public void test() throws IOException, InterruptedException { LookoutConfig config = new LookoutConfig(); - final LookoutRegistry lookoutRegistry = new LookoutRegistry(config); + final LookoutRegistry lookoutRegistry = new LookoutRegistry(Clock.SYSTEM, null, config, + null, 1000L); // 通常只会有一个LookoutRegistry final Collection lookoutRegistries = new ArrayList(1); lookoutRegistries.add(lookoutRegistry); - ResettableStepRegistry ssr = new ResettableStepRegistry(Clock.SYSTEM, config, 1000L); // 使用者需要自行构建该 PollerController // 能不能将逻辑做到 client 里? // Registry registry = client.getRegistry(); - PollerController pc = new PollerController(ssr); + PollerController pc = new PollerController(lookoutRegistry); bind(pc, lookoutRegistries); MetricsHttpExporter e = new MetricsHttpExporter(pc); diff --git a/client/lookout-client/src/test/java/com/alipay/lookout/client/SimpleLookoutClientTest.java b/client/lookout-client/src/test/java/com/alipay/lookout/client/SimpleLookoutClientTest.java index 672bb11..e40b405 100644 --- a/client/lookout-client/src/test/java/com/alipay/lookout/client/SimpleLookoutClientTest.java +++ b/client/lookout-client/src/test/java/com/alipay/lookout/client/SimpleLookoutClientTest.java @@ -48,7 +48,7 @@ public static void init() throws NoSuchFieldException, IllegalAccessException { * 该实例只能全局单例,所以就统一在一个测试方法 */ @Test - public void testSimpleLookoutClient() { + public void testSimpleLookoutClient() throws Exception { LookoutRegistry lookoutRegistry = new LookoutRegistry(new LookoutConfig()); SimpleLookoutClient client = new SimpleLookoutClient("demo", lookoutRegistry); //test addCommonTags @@ -61,6 +61,7 @@ public void testSimpleLookoutClient() { //test get Registry Assert.assertSame(lookoutRegistry, ((CompositeRegistry) client.getRegistry()) .getRegistries().iterator().next()); + client.close(); } } diff --git a/client/lookout-common/pom.xml b/client/lookout-common/pom.xml index cb711d5..ddc6012 100644 --- a/client/lookout-common/pom.xml +++ b/client/lookout-common/pom.xml @@ -32,6 +32,10 @@ com.alipay.sofa.common sofa-common-tools + + com.google.guava + guava + junit diff --git a/client/lookout-common/src/main/java/com/alipay/lookout/common/utils/CommonUtil.java b/client/lookout-common/src/main/java/com/alipay/lookout/common/utils/CommonUtil.java index ba5d433..e38aea3 100644 --- a/client/lookout-common/src/main/java/com/alipay/lookout/common/utils/CommonUtil.java +++ b/client/lookout-common/src/main/java/com/alipay/lookout/common/utils/CommonUtil.java @@ -18,6 +18,9 @@ import com.alipay.lookout.api.Id; import com.alipay.lookout.api.Tag; +import com.google.common.util.concurrent.ThreadFactoryBuilder; + +import java.util.concurrent.ThreadFactory; /** * CommonUtil @@ -42,4 +45,9 @@ public static String toMetricName(Id id) { } return buf.toString(); } + + public static ThreadFactory getNamedThreadFactory(String ThreadFactoryName) { + //使用guava包中工具类; + return new ThreadFactoryBuilder().setNameFormat(ThreadFactoryName + "-%d").build(); + } } diff --git a/client/lookout-core/src/main/java/com/alipay/lookout/core/AbstractRegistry.java b/client/lookout-core/src/main/java/com/alipay/lookout/core/AbstractRegistry.java index 3a8d70a..f9452ec 100644 --- a/client/lookout-core/src/main/java/com/alipay/lookout/core/AbstractRegistry.java +++ b/client/lookout-core/src/main/java/com/alipay/lookout/core/AbstractRegistry.java @@ -26,7 +26,6 @@ import com.alipay.lookout.core.common.NewMetricFunction; import com.alipay.lookout.core.config.MetricConfig; import com.alipay.lookout.event.MetricRegistryListener; -import com.alipay.lookout.spi.DefaultMetricsImporterLocator; import com.alipay.lookout.spi.MetricsImporter; import com.alipay.lookout.spi.MetricsImporterLocator; import org.apache.commons.configuration2.MapConfiguration; @@ -71,8 +70,7 @@ public void setConfig(MetricConfig config) { @Override public void registerExtendedMetrics() { - MetricsImporterLocator locator = new DefaultMetricsImporterLocator(); - for (MetricsImporter metricsImporter : locator.locate()) { + for (MetricsImporter metricsImporter : MetricsImporterLocator.locate()) { metricsImporter.register(this); } diff --git a/client/lookout-core/src/main/java/com/alipay/lookout/core/config/LookoutConfig.java b/client/lookout-core/src/main/java/com/alipay/lookout/core/config/LookoutConfig.java index 4528866..8c8db59 100644 --- a/client/lookout-core/src/main/java/com/alipay/lookout/core/config/LookoutConfig.java +++ b/client/lookout-core/src/main/java/com/alipay/lookout/core/config/LookoutConfig.java @@ -18,7 +18,6 @@ import com.alipay.lookout.api.PRIORITY; import com.alipay.lookout.common.Assert; - import org.apache.commons.configuration2.MapConfiguration; import java.util.HashMap; @@ -38,8 +37,11 @@ public final class LookoutConfig extends MapConfiguration implements MetricConfi public static final String LOOKOUT_REPORT_BATCH_SIZE = "lookout.report.batch.size"; public static final String LOOKOUT_REPORT_COMPRESSION_THRESHOLD = "lookout.report.compression.threshhold"; public static final String LOOKOUT_AUTOPOLL_INFO_METRIC_IGNORE = "lookout.autopoll.info.ignore"; + public static final String LOOKOUT_AUTOPOLL_OS_METRIC_IGNORE = "lookout.autopoll.os.ignore"; public static final String LOOKOUT_AGENT_SERVER_PORT = "lookout.agent.server.port"; public static final String LOOKOUT_ANT_EVENT_LOG_ENABLE = "lookout.ant.event.log.enable"; + public static final String LOOKOUT_EXPORTER_ACCESS_TOKEN = "lookout.exporter.access.token"; + public static final String LOOKOUT_PROMETHEUS_EXPORTER_SERVER_PORT = "lookout.prometheus.exporter.server.port"; // default value public static final int DEFAULT_WEB_SERVER_PORT = 8083; @@ -51,7 +53,7 @@ public final class LookoutConfig extends MapConfiguration implements MetricConfi /** * 是否提供一个接口, 让外界拉取数据 */ - public static final String POLLER_EXPORTER_ENABLED = "lookout.poller.enabled"; + // public static final String POLLER_EXPORTER_ENABLED = "lookout.poller.enabled"; /** * 多久没有请求拉取数据就进入idle状态 diff --git a/client/lookout-core/src/main/java/com/alipay/lookout/spi/DefaultMetricsImporterLocator.java b/client/lookout-core/src/main/java/com/alipay/lookout/spi/DefaultMetricsImporterLocator.java deleted file mode 100644 index 71b8f74..0000000 --- a/client/lookout-core/src/main/java/com/alipay/lookout/spi/DefaultMetricsImporterLocator.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.alipay.lookout.spi; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.ServiceLoader; - -/** - * find service providers - * Created by kevin.luy@alipay.com on 2017/2/16. - */ -public class DefaultMetricsImporterLocator implements MetricsImporterLocator { - @Override - public Collection locate() { - List metricsImporters = new ArrayList(3); - ServiceLoader mis = ServiceLoader.load(MetricsImporter.class); - for (MetricsImporter metricsImporter : mis) { - //TODO Class String name duplication check - metricsImporters.add(metricsImporter); - } - return metricsImporters; - } -} diff --git a/client/lookout-core/src/main/java/com/alipay/lookout/spi/MetricsImporterLocator.java b/client/lookout-core/src/main/java/com/alipay/lookout/spi/MetricsImporterLocator.java index 1bc3ac8..4119ae5 100644 --- a/client/lookout-core/src/main/java/com/alipay/lookout/spi/MetricsImporterLocator.java +++ b/client/lookout-core/src/main/java/com/alipay/lookout/spi/MetricsImporterLocator.java @@ -16,17 +16,34 @@ */ package com.alipay.lookout.spi; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; +import java.util.ServiceLoader; /** * Created by kevin.luy@alipay.com on 2017/2/16. */ -public interface MetricsImporterLocator { +public final class MetricsImporterLocator { + private static List metricsImporters = null; + + private MetricsImporterLocator() { + } /** - * locates all metric importers + * locates all metric importers (and locate only once) * * @return */ - Collection locate(); + public static synchronized Collection locate() { + if (metricsImporters == null) { + metricsImporters = new ArrayList(3); + ServiceLoader mis = ServiceLoader.load(MetricsImporter.class); + for (MetricsImporter metricsImporter : mis) { + //TODO Class String name duplication check + metricsImporters.add(metricsImporter); + } + } + return metricsImporters; + } } diff --git a/client/lookout-core/src/test/java/com/alipay/lookout/spi/DefaultMetricsImporterLocatorTest.java b/client/lookout-core/src/test/java/com/alipay/lookout/spi/DefaultMetricsImporterLocatorTest.java index 373827d..21e78bb 100644 --- a/client/lookout-core/src/test/java/com/alipay/lookout/spi/DefaultMetricsImporterLocatorTest.java +++ b/client/lookout-core/src/test/java/com/alipay/lookout/spi/DefaultMetricsImporterLocatorTest.java @@ -26,8 +26,6 @@ public class DefaultMetricsImporterLocatorTest { @Test public void testLocateImporters() { - DefaultMetricsImporterLocator locator = new DefaultMetricsImporterLocator(); - locator.locate(); - Assert.assertEquals(0, locator.locate().size()); + Assert.assertEquals(0, MetricsImporterLocator.locate().size()); } } diff --git a/client/lookout-core/src/test/java/com/alipay/lookout/top/TopUtilTest.java b/client/lookout-core/src/test/java/com/alipay/lookout/top/TopUtilTest.java index 73e2e36..cf66be9 100644 --- a/client/lookout-core/src/test/java/com/alipay/lookout/top/TopUtilTest.java +++ b/client/lookout-core/src/test/java/com/alipay/lookout/top/TopUtilTest.java @@ -18,6 +18,7 @@ import com.alipay.lookout.api.BasicTag; import com.alipay.lookout.api.Metric; +import com.alipay.lookout.api.NoopRegistry; import com.alipay.lookout.api.Registry; import com.alipay.lookout.common.top.TopGauger; import com.alipay.lookout.common.top.TopUtil; @@ -98,4 +99,11 @@ public void testAscTopGauge() throws NoSuchFieldException, IllegalAccessExceptio Assert.assertTrue(s.contains("select1")); } + @Test + public void testTopGaugeWithNoopId() { + TopGauger topGauger = TopUtil.topGauger(new DefaultRegistry(), + NoopRegistry.INSTANCE.createId("xx"), 4); + Assert.assertTrue(topGauger.getClass().getName().contains("NoopTopGauger")); + } + } diff --git a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/CachedMetricsImporter.java b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/CachedMetricsImporter.java index d78ce39..4794ce2 100644 --- a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/CachedMetricsImporter.java +++ b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/CachedMetricsImporter.java @@ -16,23 +16,32 @@ */ package com.alipay.lookout.os; +import com.alipay.lookout.api.Registry; +import com.alipay.lookout.common.log.LookoutLoggerFactory; +import com.alipay.lookout.core.AbstractRegistry; +import com.alipay.lookout.core.config.MetricConfig; import com.alipay.lookout.spi.MetricsImporter; +import org.slf4j.Logger; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import static com.alipay.lookout.core.config.LookoutConfig.LOOKOUT_AUTOPOLL_OS_METRIC_IGNORE; + /** - * * @author wuqin + * @author kevin.luy@alipay.com * @version $Id: CachedMetricsImporter.java, v 0.1 2017-03-18 下午4:40 wuqin Exp $$ */ public abstract class CachedMetricsImporter implements MetricsImporter { - + private final Logger logger = LookoutLoggerFactory + .getLogger(CachedMetricsImporter.class); private static int MAX_RETRY_TIME = 3; protected static long DEFAULT_TIMEOUT_MS = 5000; private final AtomicLong reloadAt; private final long timeoutMS; + protected boolean disable = false; /** * Creates a new cached metrics importer with the default timeout period. @@ -45,8 +54,8 @@ public CachedMetricsImporter() { /** * Creates a new cached metrics importer with the given timeout period. * - * @param timeout the timeout - * @param timeoutUnit the unit of {@code timeout} + * @param timeout the timeout + * @param timeoutUnit the unit of {@code timeout} */ public CachedMetricsImporter(long timeout, TimeUnit timeoutUnit) { this.reloadAt = new AtomicLong(0); @@ -54,6 +63,9 @@ public CachedMetricsImporter(long timeout, TimeUnit timeoutUnit) { } protected void loadIfNessesary() { + if (disable) { + return; + } for (int retryTimes = 0; retryTimes < MAX_RETRY_TIME; retryTimes++) { final long time = System.currentTimeMillis(); final long current = reloadAt.get(); @@ -67,6 +79,23 @@ protected void loadIfNessesary() { } } + @Override + public void register(Registry registry) { + if (registry instanceof AbstractRegistry) { + MetricConfig config = ((AbstractRegistry) registry).getConfig(); + boolean disable = config.getBoolean(LOOKOUT_AUTOPOLL_OS_METRIC_IGNORE, false); + if (disable) { + return; + } + } + if (disable) { + return; + } + doRegister(registry); + } + + protected abstract void doRegister(Registry registry); + /** * Loads the value for all metrics in the set. */ diff --git a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/CpuUsageMetricsImporter.java b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/CpuUsageMetricsImporter.java index 3a26a7f..b4314f1 100644 --- a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/CpuUsageMetricsImporter.java +++ b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/CpuUsageMetricsImporter.java @@ -26,13 +26,14 @@ import com.alipay.lookout.os.utils.NumFormatUtils; import org.slf4j.Logger; +import java.io.File; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * * @author wuqin + * @author kevin.luy@alipay.com * @version $Id: CpuUsageMetricsImporter.java, v 0.1 2017-03-18 下午5:19 wuqin Exp $$ */ public class CpuUsageMetricsImporter extends CachedMetricsImporter { @@ -69,7 +70,6 @@ public CpuUsageMetricsImporter() { } /** - * * @param filePath * @param timeout * @param timeoutUnit @@ -79,10 +79,11 @@ public CpuUsageMetricsImporter(String filePath, long timeout, TimeUnit timeoutUn this.filePath = filePath; this.cpuUsage = new float[CpuUsage.values().length]; this.lastCpuInfo = new CpuInfo(); + disable = !new File(filePath).exists(); } @Override - public void register(Registry registry) { + protected void doRegister(Registry registry) { Id id = registry.createId("os.cpu"); MixinMetric mixin = registry.mixinMetric(id); @@ -147,7 +148,7 @@ public Float value() { protected void loadValues() { CpuInfo currentCpuInfo = collectCpuInfo(); if (currentCpuInfo == null) { - logger.info("warning,collect cpu info failed!"); + logger.debug("warning,collect cpu info failed!"); lastCpuInfo = new CpuInfo(); return; } @@ -178,7 +179,6 @@ protected void loadValues() { } /** - * * @return */ private CpuInfo collectCpuInfo() { @@ -224,7 +224,7 @@ private CpuInfo collectCpuInfo() { return cpuInfo; } } catch (Exception e) { - logger.info("waring,can't parse text at /proc/stat", e.getMessage()); + logger.info("warning,can't parse text at /proc/stat", e.getMessage()); } return null; diff --git a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/DiskUsageMetricsImporter.java b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/DiskUsageMetricsImporter.java index ecf88cc..a354e87 100644 --- a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/DiskUsageMetricsImporter.java +++ b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/DiskUsageMetricsImporter.java @@ -36,6 +36,7 @@ /** * * @author wuqin + * @author kevin.luy@alipay.com * @version $Id: DiskUsageMetricsImporter.java, v 0.1 2017-03-18 下午5:30 wuqin Exp $$ */ public class DiskUsageMetricsImporter extends CachedMetricsImporter { @@ -72,11 +73,14 @@ public DiskUsageMetricsImporter(String filePath, long timeout, TimeUnit timeoutU super(timeout, timeoutUnit); this.filePath = filePath; diskUsageByDevice = new HashMap(); - loadIfNessesary(); + + disable = !new File(filePath).exists(); + if (!disable) + loadIfNessesary(); } @Override - public void register(Registry registry) { + protected void doRegister(Registry registry) { for (Map.Entry entry : diskUsageByDevice.entrySet()) { final String device = entry.getKey(); Id id = registry.createId("os.disk.usage." + device); @@ -150,7 +154,7 @@ protected void loadValues() { diskUsageByDevice.put(diskUsage.fsSpec, diskUsage); } } catch (Exception e) { - logger.info("warning,can't parse line at /proc/mounts", e.getMessage()); + logger.debug("warning,can't parse line at /proc/mounts", e.getMessage()); } } diff --git a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/IOStatsMetricsImporter.java b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/IOStatsMetricsImporter.java index 9136385..56bad16 100644 --- a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/IOStatsMetricsImporter.java +++ b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/IOStatsMetricsImporter.java @@ -26,14 +26,15 @@ import com.alipay.lookout.os.utils.NumFormatUtils; import org.slf4j.Logger; +import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** - * * @author wuqin + * @author kevin.luy@alipay.com * @version $Id: IOStatsMetricsImporter.java, v 0.1 2017-03-18 下午6:23 wuqin Exp $$ */ public class IOStatsMetricsImporter extends CachedMetricsImporter { @@ -74,7 +75,6 @@ public IOStatsMetricsImporter() { } /** - * * @param filePath * @param upTimeFilePath * @param timeout @@ -87,11 +87,13 @@ public IOStatsMetricsImporter(String filePath, String upTimeFilePath, long timeo this.upTimeFilePath = upTimeFilePath; this.statsByDevice = new HashMap(); this.lastDiskInfoMap = new HashMap(); - loadIfNessesary(); + disable = !new File(filePath).exists(); + if (!disable) + loadIfNessesary(); } @Override - public void register(Registry registry) { + protected void doRegister(Registry registry) { for (Map.Entry entry : statsByDevice.entrySet()) { final String device = entry.getKey(); Id id = registry.createId("os.io.stats." + device); @@ -144,7 +146,7 @@ public Float value() { protected void loadValues() { float deltaTime = collectUpTime(); if (deltaTime == 0.0f) { - logger.info("warning,calculate delta time failed!"); + logger.debug("warning,calculate delta time failed!"); return; } @@ -162,7 +164,6 @@ protected void loadValues() { } /** - * * @return */ private float collectUpTime() { @@ -174,12 +175,11 @@ private float collectUpTime() { return deltaTime; } catch (Exception e) { logger.info("warning,can't parse line at /proc/uptime", e.getMessage()); - return 0.0f; } + return 0.0f; } /** - * * @return */ private Map collectDiskInfo() { @@ -189,7 +189,7 @@ private Map collectDiskInfo() { for (String line : lines) { String[] stats = line.trim().split(SPLIT); if (stats == null || stats.length != TOTAL_LENGTH) { - logger.info("warning,can't parse text at /proc/diskstats, line: " + line); + logger.debug("warning,can't parse text at /proc/diskstats, line: " + line); continue; } if (Long.parseLong(stats[DiskStats.STATS.ordinal()]) == 0) { diff --git a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/NetTrafficMetricsImporter.java b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/NetTrafficMetricsImporter.java index e1d5cbf..f1cf9c4 100644 --- a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/NetTrafficMetricsImporter.java +++ b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/NetTrafficMetricsImporter.java @@ -25,6 +25,7 @@ import com.alipay.lookout.os.utils.FileUtils; import org.slf4j.Logger; +import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -35,6 +36,7 @@ /** * * @author wuqin + * @author kevin.luy@alipay.com * @version $Id: NetTrafficMetricsImporter.java, v 0.1 2017-03-18 下午6:33 wuqin Exp $$ */ public class NetTrafficMetricsImporter extends CachedMetricsImporter { @@ -92,11 +94,14 @@ public NetTrafficMetricsImporter(String filePath, long timeout, TimeUnit timeout super(timeout, timeoutUnit); this.filePath = filePath; this.statByFace = new HashMap(); - loadIfNessesary(); + + disable = !new File(filePath).exists(); + if (!disable) + loadIfNessesary(); } @Override - public void register(Registry registry) { + protected void doRegister(Registry registry) { for (final Map.Entry entry : statByFace.entrySet()) { final String face = entry.getKey(); Id id = registry.createId("os.net.stat." + face); @@ -134,7 +139,7 @@ protected void loadValues() { } } } catch (Exception e) { - logger.info("warning,can't parse text at /proc/net/dev", e.getMessage()); + logger.debug("warning,can't parse text at /proc/net/dev", e.getMessage()); } } } diff --git a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/SystemLoadMetricsImporter.java b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/SystemLoadMetricsImporter.java index ab21c01..db03dd5 100644 --- a/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/SystemLoadMetricsImporter.java +++ b/client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/SystemLoadMetricsImporter.java @@ -26,6 +26,7 @@ import com.alipay.lookout.os.utils.NumFormatUtils; import org.slf4j.Logger; +import java.io.File; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -33,6 +34,7 @@ /** * * @author wuqin + * @author kevin.luy@alipay.com * @version $Id: SystemLoadMetricsImporter.java, v 0.1 2017-03-18 下午4:59 wuqin Exp $$ */ public class SystemLoadMetricsImporter extends CachedMetricsImporter { @@ -67,10 +69,11 @@ public SystemLoadMetricsImporter(String filePath, long timeout, TimeUnit timeout super(timeout, timeoutUnit); this.filePath = filePath; this.loadAvg = new float[LoadAvg.values().length]; + disable = !new File(filePath).exists(); } @Override - public void register(Registry registry) { + protected void doRegister(Registry registry) { Id id = registry.createId("os.systemload.average"); MixinMetric mixin = registry.mixinMetric(id); diff --git a/client/lookout-ext-os/src/main/resources/META-INF/services/com.alipay.lookout.spi.MetricsImporter b/client/lookout-ext-os/src/main/resources/META-INF/services/com.alipay.lookout.spi.MetricsImporter new file mode 100644 index 0000000..10e5092 --- /dev/null +++ b/client/lookout-ext-os/src/main/resources/META-INF/services/com.alipay.lookout.spi.MetricsImporter @@ -0,0 +1,6 @@ +com.alipay.lookout.os.linux.CpuUsageMetricsImporter +com.alipay.lookout.os.linux.DiskUsageMetricsImporter +com.alipay.lookout.os.linux.IOStatsMetricsImporter +com.alipay.lookout.os.linux.NetTrafficMetricsImporter +com.alipay.lookout.os.linux.SystemLoadMetricsImporter + diff --git a/client/lookout-reg-dropwizard/src/main/java/com/alipay/lookout/dropwizard/metrics/DropWizardMetricsRegistry.java b/client/lookout-reg-dropwizard/src/main/java/com/alipay/lookout/dropwizard/metrics/DropWizardMetricsRegistry.java index fec9e50..969f985 100644 --- a/client/lookout-reg-dropwizard/src/main/java/com/alipay/lookout/dropwizard/metrics/DropWizardMetricsRegistry.java +++ b/client/lookout-reg-dropwizard/src/main/java/com/alipay/lookout/dropwizard/metrics/DropWizardMetricsRegistry.java @@ -22,7 +22,6 @@ import com.alipay.lookout.common.log.LookoutLoggerFactory; import com.alipay.lookout.core.config.LookoutConfig; import com.alipay.lookout.core.config.MetricConfig; -import com.alipay.lookout.spi.DefaultMetricsImporterLocator; import com.alipay.lookout.spi.MetricsImporter; import com.alipay.lookout.spi.MetricsImporterLocator; import org.slf4j.Logger; @@ -135,8 +134,7 @@ public MixinMetric mixinMetric(Id id) { @Override public void registerExtendedMetrics() { - MetricsImporterLocator locator = new DefaultMetricsImporterLocator(); - for (MetricsImporter metricsImporter : locator.locate()) { + for (MetricsImporter metricsImporter : MetricsImporterLocator.locate()) { metricsImporter.register(this); } } diff --git a/client/lookout-reg-prometheus/src/main/java/com/alipay/lookout/reg/prometheus/exporter/ExporterServer.java b/client/lookout-reg-prometheus/src/main/java/com/alipay/lookout/reg/prometheus/exporter/ExporterServer.java index e9a1c54..ee4db53 100644 --- a/client/lookout-reg-prometheus/src/main/java/com/alipay/lookout/reg/prometheus/exporter/ExporterServer.java +++ b/client/lookout-reg-prometheus/src/main/java/com/alipay/lookout/reg/prometheus/exporter/ExporterServer.java @@ -16,7 +16,7 @@ */ package com.alipay.lookout.reg.prometheus.exporter; -import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.alipay.lookout.common.utils.CommonUtil; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; @@ -25,7 +25,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -38,7 +37,8 @@ public class ExporterServer { public ExporterServer(int port) { final ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1, 0L, - TimeUnit.MILLISECONDS, new LinkedBlockingQueue(100), getNamedThreadFactory(), + TimeUnit.MILLISECONDS, new LinkedBlockingQueue(100), + CommonUtil.getNamedThreadFactory("prometheus-exporter-pool"), new ThreadPoolExecutor.AbortPolicy()); try { httpServer = HttpServer.create(new InetSocketAddress(port), 2); @@ -49,11 +49,6 @@ public ExporterServer(int port) { } } - private static ThreadFactory getNamedThreadFactory() { - //使用guava包中工具类; - return new ThreadFactoryBuilder().setNameFormat("prometheus-exporter-pool-%d").build(); - } - public void addMetricsQueryHandler(HttpHandler httpHandler) { httpServer.createContext("/metrics", httpHandler); } diff --git a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/MetricsHttpExporter.java b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/MetricsHttpExporter.java index ab23aba..e7af84f 100644 --- a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/MetricsHttpExporter.java +++ b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/MetricsHttpExporter.java @@ -18,11 +18,11 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alipay.lookout.common.utils.CommonUtil; import com.google.common.collect.Sets; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; - import org.apache.commons.lang3.StringUtils; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URIBuilder; @@ -34,8 +34,14 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.zip.GZIPOutputStream; +import static com.alipay.lookout.core.config.LookoutConfig.LOOKOUT_EXPORTER_ACCESS_TOKEN; + /** * @author xiangfeng.xzc * @date 2018/7/17 @@ -65,7 +71,13 @@ public MetricsHttpExporter(PollerController controller, int port, int backlog) { * @throws IOException */ public void start() throws IOException { + final ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1, 0L, + TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), + CommonUtil.getNamedThreadFactory("client-exporter-pool"), + new ThreadPoolExecutor.CallerRunsPolicy()); + httpServer = HttpServer.create(new InetSocketAddress(port), backlog); + httpServer.setExecutor(singleThreadPool); httpServer.createContext("/get", getHandler); // 测试用接口 清理掉数据 httpServer.createContext("/clear", clearHandler); @@ -87,24 +99,36 @@ public synchronized void close() { public void handle(HttpExchange exchange) throws IOException { try { + if (!isAccessAllowed(exchange)) { + sendErrResponse(exchange, 403, + "Forbidden"); + return; + } + // 解析参数 Set success = Collections.emptySet(); long newStep = controller.getStep(); int newSlotCount = controller.getSlotCount(); - for (NameValuePair nvp : parseParams(exchange)) { - String name = nvp.getName(); - String value = nvp.getValue(); - if ("step".equalsIgnoreCase(name)) { - newStep = Long.parseLong(value); - } else if ("slotCount" - .equalsIgnoreCase(name)) { - newSlotCount = Integer - .parseInt(value); - } else if ("success" - .equalsIgnoreCase(name)) { - success = parseCursors(value); + try { + for (NameValuePair nvp : parseParams(exchange)) { + String name = nvp.getName(); + String value = nvp.getValue(); + if ("step".equalsIgnoreCase(name)) { + newStep = Long.parseLong(value); + } else if ("slotCount" + .equalsIgnoreCase(name)) { + newSlotCount = Integer + .parseInt(value); + } else if ("success" + .equalsIgnoreCase(name)) { + success = parseCursors(value); + } } + } catch (NumberFormatException nfe) { + sendErrResponse(exchange, 400, + nfe.getMessage()); + return; } Object data = controller @@ -132,6 +156,11 @@ public void handle(HttpExchange exchange) public void handle(HttpExchange exchange) throws IOException { try { + if (!isAccessAllowed(exchange)) { + sendErrResponse(exchange, 403, + "Forbidden"); + return; + } controller.clear(); exchange.sendResponseHeaders(204, -1); } finally { @@ -140,6 +169,27 @@ public void handle(HttpExchange exchange) } }; + private boolean isAccessAllowed(HttpExchange exchange) { + if (controller.getMetricConfig().containsKey(LOOKOUT_EXPORTER_ACCESS_TOKEN)) { + //check access token + String requestToken = exchange.getRequestHeaders().getFirst("X-Lookout-Token"); + if (!StringUtils.equals(requestToken, + controller.getMetricConfig().getString(LOOKOUT_EXPORTER_ACCESS_TOKEN))) { + + return false; + } + } + return true; + } + + private static void sendErrResponse(HttpExchange exchange, int httpErrorStatus, String errorMsg) + throws IOException { + byte[] data = errorMsg.getBytes(); + exchange.sendResponseHeaders(httpErrorStatus, data.length); + exchange.getResponseBody().write(data); + exchange.getResponseBody().close(); + } + private static void sendResponse(HttpExchange exchange, Object bodyEntity) throws IOException { exchange.getResponseHeaders().set("Content-Type", "application/json;charset=utf-8"); exchange.getResponseHeaders().set("Content-Encoding", "gzip"); diff --git a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/PollerController.java b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/PollerController.java index 2ce0ea2..96e9dd0 100644 --- a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/PollerController.java +++ b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/PollerController.java @@ -18,44 +18,35 @@ import com.alipay.lookout.api.Gauge; import com.alipay.lookout.api.Metric; +import com.alipay.lookout.common.log.LookoutLoggerFactory; import com.alipay.lookout.common.top.RollableTopGauge; +import com.alipay.lookout.common.utils.CommonUtil; import com.alipay.lookout.core.GaugeWrapper; import com.alipay.lookout.core.InfoWrapper; import com.alipay.lookout.core.config.LookoutConfig; +import com.alipay.lookout.core.config.MetricConfig; import com.alipay.lookout.remote.model.LookoutMeasurement; +import com.alipay.lookout.remote.step.LookoutRegistry; +import com.alipay.lookout.remote.step.PollableInfoWrapper; import com.google.common.base.Preconditions; import com.google.common.primitives.Longs; - -import org.apache.commons.lang3.concurrent.BasicThreadFactory; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.Closeable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import java.util.*; +import java.util.concurrent.*; /** * @author xiangfeng.xzc * @date 2018/7/26 */ public class PollerController implements Closeable { - private static final Logger LOGGER = LoggerFactory + private static final Logger LOGGER = LookoutLoggerFactory .getLogger(PollerController.class); private static final int DEFAULT_SLOT_COUNT = 3; - private static final int DEFAULT_IDLE_SECONDS = 3600; + private static final int DEFAULT_IDLE_SECONDS = 1800; //30 min /** * 比较器 按照cursor倒序排序 */ @@ -72,7 +63,7 @@ public int compare(Slot o1, /** * 注册中心 */ - private final ResettableStepRegistry registry; + private final LookoutRegistry registry; /** * 只有1个线程的调度器 @@ -117,21 +108,24 @@ public int compare(Slot o1, /** * 多少时间没有请求就算是空闲 */ - private final int idleSeconds; + private int idleSeconds; - public PollerController(ResettableStepRegistry registry) { + public PollerController(LookoutRegistry registry) { this(registry, DEFAULT_SLOT_COUNT); } - public PollerController(ResettableStepRegistry registry, int initSlotCount) { + public PollerController(LookoutRegistry registry, int initSlotCount) { this.registry = registry; - ThreadFactory tf = new BasicThreadFactory.Builder().namingPattern("PollerController %d") - .build(); - scheduledExecutorService = new ScheduledThreadPoolExecutor(1, tf, + scheduledExecutorService = new ScheduledThreadPoolExecutor(1, + CommonUtil.getNamedThreadFactory("poller-controller"), new ThreadPoolExecutor.AbortPolicy()); idleSeconds = registry.getConfig().getInteger(LookoutConfig.POLLER_EXPORTER_IDLE_SECONDS, DEFAULT_IDLE_SECONDS); - update(registry.getFixedStepMillis(), initSlotCount); + update(registry.getCurrentStepMillis(), initSlotCount); + } + + MetricConfig getMetricConfig() { + return registry.getConfig(); } /** @@ -284,6 +278,16 @@ private List getMetricDtos() { it.remove(); } } + if (metric instanceof InfoWrapper) { + //ignore info + if (registry.getConfig().getBoolean( + LookoutConfig.LOOKOUT_AUTOPOLL_INFO_METRIC_IGNORE, true)) { + continue; + } + if (!((PollableInfoWrapper) metric).isAutoPolledAllowed(getStep())) { + continue; + } + } MetricDto dto = new MetricDto(); LookoutMeasurement lookoutMeasurement = LookoutMeasurement.from(metric, registry); @@ -312,7 +316,7 @@ private synchronized void touchTimer() { oldFuture.cancel(true); } - // 5分钟后进入idle状态 + // 空闲时间到了,进入idle状态 this.idleFuture = scheduledExecutorService.schedule(new Runnable() { @Override public void run() { @@ -325,18 +329,21 @@ public void run() { } private void triggerIdle() { + //fallback to proactive + registry.setProactive(true); // idle时不再poller 同时也清理掉cache this.metricCache.clear(); - LOGGER.warn("PollerController is now idle"); + LOGGER.warn("PollerController is now idle. reactive mode."); for (Listener listener : listeners) { listener.onIdle(); } } private void triggerActive() { - LOGGER.warn("PollerController is now active"); - + LOGGER.warn("PollerController is now active. proactive mode."); + //reactive mode. + registry.setProactive(false); for (Listener listener : listeners) { listener.onActive(); } @@ -367,8 +374,4 @@ public void close() { this.scheduledExecutorService = null; } } - - public ResettableStepRegistry getRegistry() { - return registry; - } } diff --git a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutMixinMetric.java b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutMixinMetric.java index 8736e10..8cdfc73 100644 --- a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutMixinMetric.java +++ b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutMixinMetric.java @@ -46,11 +46,7 @@ public LookoutMixinMetric(Id id, StepRegistry registry, StepClock stepClock) { @Override public void setStep(long step) { - for (Metric m : registry) { - if (m instanceof ResettableStep) { - ((ResettableStep) m).setStep(step); - } - } + registry.setStep(step); } @Override diff --git a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutRegistry.java b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutRegistry.java index a805726..68b89ed 100644 --- a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutRegistry.java +++ b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutRegistry.java @@ -30,16 +30,13 @@ import com.alipay.lookout.report.MetricObserver; import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * Created by kevin.luy@alipay.com on 2017/2/6. */ -public final class LookoutRegistry extends StepRegistry implements CommonTagsAccessor { +public final class LookoutRegistry extends ResettableStepRegistry implements CommonTagsAccessor { private SchedulerPoller poller; - private final Map commonTags = new ConcurrentHashMap(); private final MetricObserverComposite metricObserverComposite = new MetricObserverComposite(); /** @@ -56,7 +53,12 @@ public LookoutRegistry(Clock clock, MetricObserver observer, public LookoutRegistry(Clock clock, MetricObserver observer, LookoutConfig config, AddressService addressService) { - super(clock, config); + this(clock, observer, config, addressService, -1l); + } + + public LookoutRegistry(Clock clock, MetricObserver observer, + LookoutConfig config, AddressService addressService, long currentStep) { + super(clock, config, currentStep); if (observer == null) { observer = new HttpObserver(config, addressService == null ? getAddressService(config) : addressService, this, new ReportDecider()); @@ -90,28 +92,6 @@ public LookoutRegistry(MetricObserver observer) { this(Clock.SYSTEM, observer, new LookoutConfig()); } - @Override - public String getCommonTagValue(String name) { - return commonTags.get(name); - } - - @Override - public void setCommonTag(String name, String value) { - if (name != null && value != null) { - commonTags.put(name, value); - } - } - - @Override - public void removeCommonTag(String name) { - commonTags.remove(name); - } - - @Override - public Map commonTags() { - return commonTags; - } - SchedulerPoller poller() { return this.poller; } diff --git a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/ResettableStepRegistry.java b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/ResettableStepRegistry.java similarity index 79% rename from client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/ResettableStepRegistry.java rename to client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/ResettableStepRegistry.java index 9a6e163..a0805aa 100644 --- a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/poller/ResettableStepRegistry.java +++ b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/ResettableStepRegistry.java @@ -14,26 +14,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.lookout.remote.report.poller; +package com.alipay.lookout.remote.step; -import com.alipay.lookout.api.ResettableStep; import com.alipay.lookout.api.Clock; -import com.alipay.lookout.api.Id; +import com.alipay.lookout.api.ResettableStep; import com.alipay.lookout.core.CommonTagsAccessor; import com.alipay.lookout.core.config.LookoutConfig; -import com.alipay.lookout.remote.step.StepRegistry; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** - * 可调整 step 的 Registry + * 可调整 step 的 Registry reactive mode; * * @author xiangfeng.xzc * @date 2018/7/26 */ -public class ResettableStepRegistry extends StepRegistry implements ResettableStep, - CommonTagsAccessor { +class ResettableStepRegistry extends StepRegistry implements ResettableStep, CommonTagsAccessor { /** * 默认的采样间隔时间 */ @@ -41,12 +38,16 @@ public class ResettableStepRegistry extends StepRegistry implements ResettableSt private final Map commonTags = new ConcurrentHashMap(); + public ResettableStepRegistry(LookoutConfig config) { + this(Clock.SYSTEM, config); + } + public ResettableStepRegistry(Clock clock, LookoutConfig config) { super(clock, config, INIT_STEP_MILLS); } public ResettableStepRegistry(Clock clock, LookoutConfig config, long initStep) { - super(clock, config, initStep); + super(clock, config, initStep < 0 ? INIT_STEP_MILLS : initStep); } @Override @@ -73,17 +74,6 @@ public void removeCommonTag(String name) { commonTags.remove(name); } - /** - * ResettableStepRegistry 的metrics有相同的rate - * - * @param id - * @return - */ - @Override - protected long getStepMillis(Id id) { - return fixedStepMillis; - } - @Override public Map commonTags() { return commonTags; diff --git a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepClock.java b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepClock.java index 565bbf0..d47e3e4 100644 --- a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepClock.java +++ b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepClock.java @@ -17,29 +17,31 @@ package com.alipay.lookout.remote.step; import com.alipay.lookout.api.Clock; -import com.alipay.lookout.common.Assert; +import com.alipay.lookout.api.Id; /** + * wall time 对齐 * Created by kevin.luy@alipay.com on 2017/2/6. */ -public final class StepClock implements Clock { +final class StepClock implements Clock { - private final Clock impl; - private final long step; + private final StepRegistry stepRegistry; + private final Id id; - StepClock(Clock impl, long step) { - Assert.checkArg(!(impl instanceof StepClock), "the clock arg can not be StepClock!"); - this.impl = impl; - this.step = step; + StepClock(StepRegistry stepRegistry, Id id) { + this.stepRegistry = stepRegistry; + this.id = id; } @Override public long wallTime() { - return impl.wallTime() / step * step; + //support step refresh. + long step = stepRegistry.getStepMillis(id); + return stepRegistry.clock().wallTime() / step * step; } @Override public long monotonicTime() { - return impl.monotonicTime(); + return stepRegistry.clock().monotonicTime(); } } diff --git a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepRegistry.java b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepRegistry.java index e8576ba..e075268 100644 --- a/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepRegistry.java +++ b/client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/StepRegistry.java @@ -16,31 +16,28 @@ */ package com.alipay.lookout.remote.step; -import com.alipay.lookout.api.ResettableStep; -import com.alipay.lookout.api.Clock; -import com.alipay.lookout.api.Counter; -import com.alipay.lookout.api.DistributionSummary; -import com.alipay.lookout.api.Gauge; -import com.alipay.lookout.api.Id; -import com.alipay.lookout.api.Metric; -import com.alipay.lookout.api.Timer; +import com.alipay.lookout.api.*; import com.alipay.lookout.api.info.Info; import com.alipay.lookout.common.utils.PriorityTagUtil; import com.alipay.lookout.core.AbstractRegistry; import com.alipay.lookout.core.GaugeWrapper; import com.alipay.lookout.core.common.NewMetricFunction; import com.alipay.lookout.core.config.LookoutConfig; - -import java.util.concurrent.ConcurrentHashMap; +import com.google.common.base.Preconditions; /** + * proactive mode; different fixed steps by different priorities. + * reactive mode; step is determined by collector; + *

* Created by kevin.luy@alipay.com on 2017/3/26. */ public class StepRegistry extends AbstractRegistry { - protected final Clock clock; //未对齐时间 + protected final Clock clock; //未对齐时间 //for LookoutMixinMetric - protected volatile long fixedStepMillis = -1L; + protected volatile long currentStepMillis = -1L; + + private boolean proactive = true; public StepRegistry(Clock clock, LookoutConfig config) { this(clock, config, -1L); @@ -51,12 +48,16 @@ public StepRegistry(Clock clock, LookoutConfig config) { * * @param clock * @param config - * @param fixedStepMillis + * @param currentStepMillis */ - public StepRegistry(Clock clock, LookoutConfig config, long fixedStepMillis) { + public StepRegistry(Clock clock, LookoutConfig config, long currentStepMillis) { super(clock, config); this.clock = clock; - this.fixedStepMillis = fixedStepMillis; + this.currentStepMillis = currentStepMillis; + } + + public void setProactive(boolean proactive) { + this.proactive = proactive; } private LookoutConfig getLookoutConfig() { @@ -74,10 +75,12 @@ protected DistributionSummary newDistributionSummary(Id id) { } protected long getStepMillis(Id id) { - if (fixedStepMillis > 0) { - return fixedStepMillis; + if (proactive) { + return getLookoutConfig().stepMillis(PriorityTagUtil.resolve(id.tags())); } - return getLookoutConfig().stepMillis(PriorityTagUtil.resolve(id.tags())); + //reactive mode + Preconditions.checkState(currentStepMillis > 0); + return currentStepMillis; } @Override @@ -88,16 +91,17 @@ protected Timer newTimer(Id id) { @Override protected Metric newMixinMetric(Id id) { long stepSize = getStepMillis(id); + //mixin 的 step registry ,mode 不需要切换了,因为有了 stepClock; return new LookoutMixinMetric(id, new StepRegistry(clock, getLookoutConfig(), stepSize), - stepClock(stepSize)); + stepClock(id)); } @Override public Gauge gauge(Id id, final Gauge gauge) { return (Gauge) computeIfAbsent(id, new NewMetricFunction() { @Override - public Metric apply(Id id) { - return new GaugeWrapper(id, gauge, stepClock(getStepMillis(id))); + public Metric apply(final Id id) { + return new GaugeWrapper(id, gauge, stepClock(id)); } @Override @@ -112,7 +116,7 @@ public > Info info(Id id, final Y info) { return (Info) computeIfAbsent(id, new NewMetricFunction() { @Override public Metric apply(Id id) { - return new PollableInfoWrapper(id, info, stepClock(getStepMillis(id))); + return new PollableInfoWrapper(id, info, stepClock(id)); } @Override @@ -122,34 +126,27 @@ public Metric noopMetric() { }); } - private static final ConcurrentHashMap stepClockCache = new ConcurrentHashMap(); - /** * 步长对齐时钟 * - * @param stepMills + * @param id * @return */ - private StepClock stepClock(long stepMills) { - StepClock stepClock = stepClockCache.get(stepMills); - if (stepClock == null) { - stepClock = new StepClock(clock, stepMills); - StepClock old = stepClockCache.putIfAbsent(stepMills, stepClock); - stepClock = old != null ? old : stepClock;//old first - } - return stepClock; + private StepClock stepClock(Id id) { + return new StepClock(this, id); } /** - * 重新设置step, 将会修改所有的metric + * reactive mode. + * 重新设置step, 修改所有的metric * * @param step */ protected synchronized void setStep(long step) { - if (this.fixedStepMillis == step) { + if (this.currentStepMillis == step) { return; } - this.fixedStepMillis = step; + this.currentStepMillis = step; for (Metric m : this) { if (m instanceof ResettableStep) { ((ResettableStep) m).setStep(step); @@ -158,11 +155,12 @@ protected synchronized void setStep(long step) { } /** - * 获取采样间隔时间 + * reactive mode. + * 获取当前使用的采样间隔时间 * * @return */ - public long getFixedStepMillis() { - return fixedStepMillis; + public long getCurrentStepMillis() { + return currentStepMillis; } } diff --git a/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/poller/PollerControllerTest.java b/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/poller/PollerControllerTest.java index 4eb3575..c262541 100644 --- a/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/poller/PollerControllerTest.java +++ b/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/poller/PollerControllerTest.java @@ -18,8 +18,10 @@ import com.alipay.lookout.api.Clock; import com.alipay.lookout.core.config.LookoutConfig; +import com.alipay.lookout.remote.step.LookoutRegistry; import com.google.common.collect.Sets; - +import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import java.util.Collections; @@ -34,14 +36,21 @@ * @date 2018/7/30 */ public class PollerControllerTest { - @Test - public void test() throws Exception { + static PollerController controller; + + @BeforeClass + public static void init() { LookoutConfig config = new LookoutConfig(); - ResettableStepRegistry r = new ResettableStepRegistry(Clock.SYSTEM, config, 1000L); + LookoutRegistry r = new LookoutRegistry(Clock.SYSTEM, null, config, null, 1000L); r.counter(r.createId("foo")); - PollerController controller = new PollerController(r, 10); + controller = new PollerController(r, 10); + + } + + @Test + public void test() throws Exception { try { Set success = new HashSet(); @@ -77,6 +86,10 @@ public void test() throws Exception { } + public void testGetConfig() { + Assert.assertNotNull(controller.getMetricConfig()); + } + private Set extractCursors(List data) { Set set = Sets.newHashSetWithExpectedSize(data.size()); for (Slot s : data) { diff --git a/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/poller/ResettableStepRegistryTest.java b/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/poller/ResettableStepRegistryTest.java deleted file mode 100644 index faa9092..0000000 --- a/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/poller/ResettableStepRegistryTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.alipay.lookout.remote.report.poller; - -import com.alipay.lookout.api.Clock; -import com.alipay.lookout.api.Counter; -import com.alipay.lookout.api.Id; -import com.alipay.lookout.core.config.LookoutConfig; -import com.alipay.lookout.remote.step.LookoutCounter; - -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author xiangfeng.xzc - * @date 2018/7/30 - */ -public class ResettableStepRegistryTest { - @Test - public void test() { - ResettableStepRegistry r = new ResettableStepRegistry(Clock.SYSTEM, new LookoutConfig(), - 1000L); - assertThat(r.getCommonTagValue("foo")).isNull(); - r.setCommonTag("foo", "bar"); - assertThat(r.getCommonTagValue("foo")).isEqualTo("bar"); - assertThat(r.commonTags()).hasSize(1).containsEntry("foo", "bar"); - r.setCommonTag("foo", null); - assertThat(r.commonTags()).isEmpty(); - r.setCommonTag("foo", "bar"); - assertThat(r.commonTags()).isNotEmpty(); - r.removeCommonTag("foo"); - assertThat(r.commonTags()).isEmpty(); - - Id bazId = r.createId("baz"); - Counter bazCounter = r.counter(bazId); - assertThat(bazCounter).isInstanceOf(LookoutCounter.class); - assertThat(r.get(bazId)).isNotNull(); - } -} \ No newline at end of file diff --git a/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/step/CompositeRegistryTest.java b/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/step/CompositeRegistryTest.java index 79f3248..69e846d 100644 --- a/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/step/CompositeRegistryTest.java +++ b/client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/step/CompositeRegistryTest.java @@ -29,7 +29,6 @@ import com.alipay.lookout.core.DefaultRegistry; import com.alipay.lookout.core.config.LookoutConfig; import com.alipay.lookout.remote.report.SchedulerPoller; -import com.alipay.lookout.remote.report.poller.ResettableStepRegistry; import org.junit.Assert; import org.junit.Before; diff --git a/client/lookout-sofa-boot-starter/src/main/java/com/alipay/lookout/starter/autoConfiguration/LookoutAutoConfiguration.java b/client/lookout-sofa-boot-starter/src/main/java/com/alipay/lookout/starter/autoConfiguration/LookoutAutoConfiguration.java index e34e671..09a6840 100644 --- a/client/lookout-sofa-boot-starter/src/main/java/com/alipay/lookout/starter/autoConfiguration/LookoutAutoConfiguration.java +++ b/client/lookout-sofa-boot-starter/src/main/java/com/alipay/lookout/starter/autoConfiguration/LookoutAutoConfiguration.java @@ -20,6 +20,7 @@ import com.alipay.lookout.api.PRIORITY; import com.alipay.lookout.api.Registry; import com.alipay.lookout.client.DefaultLookoutClient; +import com.alipay.lookout.client.LookoutClient; import com.alipay.lookout.core.config.LookoutConfig; import com.alipay.lookout.remote.model.LookoutMeasurement; import com.alipay.lookout.remote.report.AddressService; @@ -136,12 +137,23 @@ public SpringBootActuatorRegistryFactory springBootActuatorServerRegistryFactory return new SpringBootActuatorRegistryFactory(); } - @Bean - public Registry registry(List metricsRegistryFactoryList, - LookoutConfig lookoutConfig) { + @Bean(destroyMethod = "close") + public LookoutClient lookoutClient(List metricsRegistryFactoryList, + LookoutConfig lookoutConfig) { + if (!lookoutConfig.getBoolean(LOOKOUT_ENABLE, true)) { - return NoopRegistry.INSTANCE; + return new LookoutClient() { + @Override + public T getRegistry() { + return (T) NoopRegistry.INSTANCE; + } + + @Override + public void close() throws Exception { + } + }; } + // lookout enable DefaultLookoutClient lookoutClient = new DefaultLookoutClient( lookoutConfig.getString(APP_NAME)); for (MetricsRegistryFactory metricsRegistryFactory : metricsRegistryFactoryList) { @@ -149,12 +161,13 @@ public Registry registry(List metricsRegistryFactoryList } logger.info("register extended metrics."); lookoutClient.registerExtendedMetrics(); + return lookoutClient; + } - if (lookoutConfig.getBoolean(LookoutConfig.POLLER_EXPORTER_ENABLED, true)) { - lookoutClient.registerExporter(lookoutConfig); - } - - return lookoutClient.getRegistry(); + @Bean + public Registry registry(List metricsRegistryFactoryList, + LookoutConfig lookoutConfig) { + return lookoutClient(metricsRegistryFactoryList, lookoutConfig).getRegistry(); } protected LookoutConfig buildLookoutConfig(LookoutClientProperties lookoutClientProperties) { diff --git a/client/pom.xml b/client/pom.xml index 8f7c66c..d12ded7 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -16,7 +16,7 @@ UTF-8 1.6 3.1.2 - 1.2.47 + 1.2.49 2.3 4.0.36.Final 3.0 diff --git a/client/samples/lookout-client-samples-boot/pom.xml b/client/samples/lookout-client-samples-boot/pom.xml index 4466482..14ed066 100644 --- a/client/samples/lookout-client-samples-boot/pom.xml +++ b/client/samples/lookout-client-samples-boot/pom.xml @@ -21,7 +21,7 @@ UTF-8 UTF-8 1.8 - 1.4.3 + 1.5.0 diff --git a/client/samples/lookout-client-samples-boot/src/main/resources/application.properties b/client/samples/lookout-client-samples-boot/src/main/resources/application.properties index f7aea25..624ebcd 100644 --- a/client/samples/lookout-client-samples-boot/src/main/resources/application.properties +++ b/client/samples/lookout-client-samples-boot/src/main/resources/application.properties @@ -1,3 +1,3 @@ management.security.enabled=false spring.application.name=lookout-client-samples-boot -#com.alipay.sofa.lookout.agent-host-address=localhost \ No newline at end of file +com.alipay.sofa.lookout.agent-host-address=lookout-agent.alibaba.net \ No newline at end of file diff --git a/client/samples/lookout-client-samples-java/pom.xml b/client/samples/lookout-client-samples-java/pom.xml index d2cb17a..18c9f36 100644 --- a/client/samples/lookout-client-samples-java/pom.xml +++ b/client/samples/lookout-client-samples-java/pom.xml @@ -23,7 +23,7 @@ com.alipay.sofa.lookout lookout-client - 1.4.3 + 1.5.0 diff --git a/client/samples/lookout-client-samples-java/src/main/java/com/alipay/sofa/lookout/client/samples/DefaultLookoutClientDemo.java b/client/samples/lookout-client-samples-java/src/main/java/com/alipay/sofa/lookout/client/samples/DefaultLookoutClientDemo.java index 9886492..cb8c30c 100644 --- a/client/samples/lookout-client-samples-java/src/main/java/com/alipay/sofa/lookout/client/samples/DefaultLookoutClientDemo.java +++ b/client/samples/lookout-client-samples-java/src/main/java/com/alipay/sofa/lookout/client/samples/DefaultLookoutClientDemo.java @@ -35,7 +35,9 @@ public static void main(String[] args) { //构建一个全局的客户端实例 final DefaultLookoutClient client = new DefaultLookoutClient("appName"); + //主动上报型 LookoutRegistry lookoutRegistry = new LookoutRegistry(new LookoutConfig()); + //被动采集型 //set common tag example lookoutRegistry.setCommonTag("instant", "machine-a");