Skip to content

Commit b3f7f47

Browse files
guanchao-yangluyiisme
authored andcommitted
SOFALookout as Actuator default Metrics Impl enhancement (#18)
Actuator API adapter to lookout
1 parent 20f2768 commit b3f7f47

File tree

25 files changed

+986
-49
lines changed

25 files changed

+986
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.lookout.common.utils;
18+
19+
import com.alipay.lookout.api.Id;
20+
import com.alipay.lookout.api.Tag;
21+
22+
/**
23+
* CommonUtil
24+
*
25+
* @author yangguanchao
26+
* @since 2018/06/19
27+
*/
28+
public class CommonUtil {
29+
30+
/**
31+
* Convert a dimensional metric id {@link Id} to a hierarchical metric name.
32+
*
33+
* @param id a dimensional metric id
34+
* @return hierarchical metric name
35+
*/
36+
public static String toMetricName(Id id) {
37+
StringBuilder buf = new StringBuilder();
38+
buf.append(id.name());
39+
for (Tag t : id.tags()) {
40+
buf.append('.').append(t.key()).append('-').append(t.value());
41+
}
42+
return buf.toString();
43+
}
44+
}

client/lookout-ext-jvm/src/main/java/com/alipay/lookout/jvm/JvmSystemPropertiesInfoMetricImporter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public class JvmSystemPropertiesInfoMetricImporter implements MetricsImporter {
3131

3232
@Override
3333
public void register(Registry registry) {
34-
Id id = registry.createId("jvm.system.properties");
34+
Id id = registry.createId(LookoutIdNameConstants.JVM_SYSTEM_PROP_NAME);
3535
registry.info(id, new Info<Properties>() {
3636
@Override
3737
public Properties value() {
3838
return System.getProperties();
3939
}
4040
});
4141

42-
Id envId = registry.createId("jvm.system.env");
42+
Id envId = registry.createId(LookoutIdNameConstants.JVM_SYSTEM_ENV_NAME);
4343
registry.info(envId, new Info<Map<String, String>>() {
4444
@Override
4545
public Map<String, String> value() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.lookout.jvm;
18+
19+
/**
20+
* LookoutIdNameConstants
21+
*
22+
* @author yangguanchao
23+
* @since 2018/01/27
24+
*/
25+
public class LookoutIdNameConstants {
26+
27+
public static final String JVM_SYSTEM_PROP_NAME = "jvm.system.properties";
28+
29+
public static final String JVM_SYSTEM_ENV_NAME = "jvm.system.env";
30+
}

client/lookout-reg-dropwizard/src/main/java/com/alipay/lookout/dropwizard/metrics/DropWizardMetricsRegistry.java

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
/**
3636
* Created by [email protected] on 2017/1/26.
3737
*/
38+
@Deprecated
3839
public class DropWizardMetricsRegistry extends MetricRegistry {
3940
protected final Logger logger = LookoutLoggerFactory
4041
.getLogger(DropWizardMetricsRegistry.class);

client/lookout-reg-dropwizard/src/main/java/com/alipay/lookout/dropwizard/metrics/NameUtils.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.alipay.lookout.dropwizard.metrics;
1818

1919
import com.alipay.lookout.api.Id;
20-
import com.alipay.lookout.api.Tag;
20+
import com.alipay.lookout.common.utils.CommonUtil;
2121

2222
/**
2323
* http://metrics20.org/
@@ -36,12 +36,7 @@ private NameUtils() {
3636
* @return hierarchical metric name
3737
*/
3838
static String toMetricName(Id id) {
39-
StringBuilder buf = new StringBuilder();
40-
buf.append(id.name());
41-
for (Tag t : id.tags()) {
42-
buf.append('.').append(t.key()).append('-').append(t.value());
43-
}
44-
return buf.toString();
39+
return CommonUtil.toMetricName(id);
4540
}
4641

4742
}

client/lookout-reg-prometheus/src/main/java/com/alipay/lookout/reg/prometheus/exporter/ExporterServer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public ExporterServer(int port) {
3939
httpServer = HttpServer.create(new InetSocketAddress(port), 2);
4040
httpServer.setExecutor(singleThreadPool);
4141
} catch (IOException e) {
42-
throw new RuntimeException("prometheus exporter server create err!", e);
42+
throw new RuntimeException("prometheus exporter server create err! And port = " + port,
43+
e);
4344
}
4445
}
4546

client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/step/LookoutRegistry.java

+25-10
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,43 @@ public final class LookoutRegistry extends StepRegistry implements CommonTagsAcc
4747
*/
4848
public LookoutRegistry(Clock clock, MetricObserver<LookoutMeasurement> observer,
4949
LookoutConfig config) {
50+
this(clock, observer, config, null);
51+
}
52+
53+
public LookoutRegistry(Clock clock, MetricObserver<LookoutMeasurement> observer,
54+
LookoutConfig config, AddressService addressService) {
5055
super(clock, config);
5156
if (observer == null) {
52-
observer = new HttpObserver(config, getAddressService(config), this,
53-
new ReportDecider());
57+
observer = new HttpObserver(config, addressService == null ? getAddressService(config)
58+
: addressService, this, new ReportDecider());
5459
}
5560
addMetricObserver(observer);
5661
this.poller = new SchedulerPoller(this, config, metricObserverComposite);
5762
this.poller.start();
5863
}
5964

65+
/***
66+
* Generate Lookout AddressService
67+
* @param config lookout config
68+
* @return AddressService generated
69+
*/
70+
public static AddressService getAddressService(LookoutConfig config) {
71+
String addressServiceClassName = config.getString(LookoutConfig.ADDRESS_SERVICE_CLASS_NAME,
72+
DefaultAddressService.class.getName());
73+
return ClassUtil.newInstance(addressServiceClassName, new Class[] { String.class },
74+
new Object[] { config.getString(LookoutConfig.APP_NAME) });
75+
}
76+
6077
public LookoutRegistry(LookoutConfig config) {
6178
this(Clock.SYSTEM, null, config);
6279
}
6380

64-
public LookoutRegistry(MetricObserver<LookoutMeasurement> observer) {
65-
this(Clock.SYSTEM, observer, new LookoutConfig());
81+
public LookoutRegistry(LookoutConfig config, AddressService addressService) {
82+
this(Clock.SYSTEM, null, config, addressService);
6683
}
6784

68-
protected AddressService getAddressService(LookoutConfig config) {
69-
String addressServiceClassName = config.getString(LookoutConfig.ADDRESS_SERVICE_CLASS_NAME,
70-
DefaultAddressService.class.getName());
71-
return ClassUtil.newInstance(addressServiceClassName, new Class[] { String.class },
72-
new Object[] { config.getString(LookoutConfig.APP_NAME) });
85+
public LookoutRegistry(MetricObserver<LookoutMeasurement> observer) {
86+
this(Clock.SYSTEM, observer, new LookoutConfig());
7387
}
7488

7589
@Override
@@ -79,8 +93,9 @@ public String getCommonTagValue(String name) {
7993

8094
@Override
8195
public void setCommonTag(String name, String value) {
82-
if (name != null && value != null)
96+
if (name != null && value != null) {
8397
commonTags.put(name, value);
98+
}
8499
}
85100

86101
@Override

client/lookout-sofa-boot-starter/pom.xml

+10-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>lookout-reg-prometheus</artifactId>
5555
<scope>provided</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>io.dropwizard.metrics</groupId>
59+
<artifactId>metrics-core</artifactId>
60+
<scope>provided</scope>
61+
</dependency>
5762
<!-- adapter spring boot v1 actuator begin-->
5863
<dependency>
5964
<groupId>org.springframework.boot</groupId>
@@ -65,11 +70,6 @@
6570
<artifactId>lookout-reg-dropwizard</artifactId>
6671
<scope>provided</scope>
6772
</dependency>
68-
<dependency>
69-
<groupId>io.dropwizard.metrics</groupId>
70-
<artifactId>metrics-core</artifactId>
71-
<scope>provided</scope>
72-
</dependency>
7373
<dependency>
7474
<groupId>org.springframework.boot</groupId>
7575
<artifactId>spring-boot-starter-actuator</artifactId>
@@ -81,6 +81,11 @@
8181
<artifactId>spring-boot-starter-test</artifactId>
8282
<scope>test</scope>
8383
</dependency>
84+
<dependency>
85+
<groupId>org.springframework.boot</groupId>
86+
<artifactId>spring-boot-starter-web</artifactId>
87+
<scope>test</scope>
88+
</dependency>
8489
<dependency>
8590
<groupId>junit</groupId>
8691
<artifactId>junit</artifactId>

client/lookout-sofa-boot-starter/src/main/java/com/alipay/lookout/starter/LookoutClientProperties.java

+2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ public class LookoutClientProperties {
3838
private int maxMetricsNum = DEFAULT_MAX_METRICS_NUM;
3939

4040
private int reportBatchSize = DEFAULT_REPORT_BATCH_SIZE;
41+
4142
private boolean autopollEnable = true;
4243

4344
private boolean autopollInfoIgnore = true;
45+
4446
private int prometheusExporterServerPort = DEFAULT_PROMETHEUS_EXPORTER_SERVER_PORT;
4547

4648
public long getPollingInterval() {

client/lookout-sofa-boot-starter/src/main/java/com/alipay/lookout/starter/autoConfiguration/LookoutAutoConfiguration.java

+53-7
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,29 @@
2222
import com.alipay.lookout.client.DefaultLookoutClient;
2323
import com.alipay.lookout.core.config.LookoutConfig;
2424
import com.alipay.lookout.remote.model.LookoutMeasurement;
25+
import com.alipay.lookout.remote.report.AddressService;
26+
import com.alipay.lookout.remote.step.LookoutRegistry;
2527
import com.alipay.lookout.report.MetricObserver;
2628
import com.alipay.lookout.starter.LookoutClientProperties;
27-
import com.alipay.lookout.starter.support.reg.DropWizardMetricsRegistryFactory;
28-
import com.alipay.lookout.starter.support.reg.LookoutServerRegistryFactory;
29-
import com.alipay.lookout.starter.support.reg.MetricsRegistryFactory;
30-
import com.alipay.lookout.starter.support.reg.PrometheusMetricsRegistryFactory;
29+
import com.alipay.lookout.starter.support.actuator.LookoutSpringBootMetricsImpl;
30+
import com.alipay.lookout.starter.support.actuator.SpringBootActuatorRegistry;
31+
import com.alipay.lookout.starter.support.reader.LookoutRegistryMetricReader;
32+
import com.alipay.lookout.starter.support.reg.*;
3133
import org.slf4j.Logger;
3234
import org.slf4j.LoggerFactory;
3335
import org.springframework.beans.BeansException;
3436
import org.springframework.beans.factory.BeanFactory;
3537
import org.springframework.beans.factory.BeanFactoryAware;
3638
import org.springframework.beans.factory.annotation.Autowired;
39+
import org.springframework.boot.actuate.autoconfigure.MetricsDropwizardAutoConfiguration;
40+
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
41+
import org.springframework.boot.actuate.metrics.CounterService;
42+
import org.springframework.boot.actuate.metrics.GaugeService;
43+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
3744
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
45+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3846
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
47+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3948
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4049
import org.springframework.context.annotation.Bean;
4150
import org.springframework.context.annotation.Configuration;
@@ -52,6 +61,7 @@
5261
@AutoConfigureOrder(-100)
5362
@Configuration
5463
@EnableConfigurationProperties(LookoutClientProperties.class)
64+
@AutoConfigureBefore(MetricsDropwizardAutoConfiguration.class)
5565
public class LookoutAutoConfiguration implements BeanFactoryAware {
5666
private static final Logger logger = LoggerFactory
5767
.getLogger(LookoutAutoConfiguration.class);
@@ -76,9 +86,22 @@ public LookoutConfig lookoutConfig(LookoutClientProperties lookoutClientProperti
7686
}
7787

7888
@Bean
89+
@ConditionalOnMissingBean
7990
@ConditionalOnClass(name = "com.alipay.lookout.remote.step.LookoutRegistry")
80-
public MetricsRegistryFactory lookoutServerRegistryFactory() {
81-
return new LookoutServerRegistryFactory(metricObservers);
91+
public AddressService lookoutAddressService(LookoutConfig config) {
92+
return LookoutRegistry.getAddressService(config);
93+
}
94+
95+
@Bean
96+
@ConditionalOnClass(name = "com.alipay.lookout.remote.step.LookoutRegistry")
97+
public MetricsRegistryFactory lookoutServerRegistryFactory(AddressService addressService) {
98+
return new LookoutServerRegistryFactory(metricObservers, addressService);
99+
}
100+
101+
@Bean
102+
@ConditionalOnClass(name = "org.springframework.boot.actuate.metrics.Metric")
103+
public SpringBootActuatorRegistryFactory springBootActuatorServerRegistryFactory() {
104+
return new SpringBootActuatorRegistryFactory();
82105
}
83106

84107
@Bean
@@ -130,6 +153,30 @@ public Registry registry(List<MetricsRegistryFactory> metricsRegistryFactoryList
130153
return lookoutClient.getRegistry();
131154
}
132155

156+
@Bean
157+
@ConditionalOnMissingBean({ LookoutSpringBootMetricsImpl.class, CounterService.class,
158+
GaugeService.class })
159+
public LookoutSpringBootMetricsImpl lookoutMetricServices(Registry lookoutMetricRegistry) {
160+
logger.info("Spring Boot Metrics binding to SOFALookout Implementation!");
161+
return new LookoutSpringBootMetricsImpl(lookoutMetricRegistry);
162+
}
163+
164+
@Bean
165+
@ConditionalOnMissingBean
166+
@ConditionalOnBean(SpringBootActuatorRegistryFactory.class)
167+
public LookoutRegistryMetricReader lookoutRegistryMetricReader(SpringBootActuatorRegistryFactory springBootActuatorRegistryFactory,
168+
LookoutConfig lookoutConfig) {
169+
SpringBootActuatorRegistry springBootActuatorRegistry = springBootActuatorRegistryFactory
170+
.get(lookoutConfig);
171+
return new LookoutRegistryMetricReader(springBootActuatorRegistry);
172+
}
173+
174+
@Bean
175+
@ConditionalOnBean(LookoutRegistryMetricReader.class)
176+
public MetricReaderPublicMetrics lookoutPublicMetrics(LookoutRegistryMetricReader lookoutRegistryMetricReader) {
177+
return new MetricReaderPublicMetrics(lookoutRegistryMetricReader);
178+
}
179+
133180
protected LookoutConfig buildLookoutConfig(LookoutClientProperties lookoutClientProperties) {
134181
LookoutConfig lookoutConfig = new LookoutConfig();
135182
lookoutConfig.setProperty(LOOKOUT_ENABLE, lookoutClientProperties.isEnable());
@@ -153,5 +200,4 @@ protected LookoutConfig buildLookoutConfig(LookoutClientProperties lookoutClient
153200
}
154201
return lookoutConfig;
155202
}
156-
157203
}

0 commit comments

Comments
 (0)