Skip to content

Commit 5852af0

Browse files
authored
upgrade to 1.5.1 and do some refactors (#41)
* fix log debug npe bug * optimize the usage of lookoutclient * change wiki link to doc site in readme
1 parent 44c68b1 commit 5852af0

File tree

29 files changed

+77
-66
lines changed

29 files changed

+77
-66
lines changed

client/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SOFALookout Client 项目支持 Maven 3.2.5+,JDK 6+ 进行编译。
1010

1111
## 2. API 埋点需知
1212

13-
lookout-api 支持被单独依赖和使用,方便植入您的项目代码,收集需要的 metrics,更多信息参考 [WIKI 文档](http://www.sofastack.tech/sofa-lookout/docs/Home)
13+
lookout-api 支持被单独依赖和使用,方便植入您的项目代码,收集需要的 metrics,更多信息参考 [官方文档](http://www.sofastack.tech/sofa-lookout/docs/Home)
1414

1515
## 3. 扩展能力
1616

@@ -49,4 +49,4 @@ lookout 客户端提供了 SPI 机制(只需要实现 `com.alipay.lookout.spi.
4949

5050
## 6.如何使用
5151

52-
参考 [WIKI 文档](http://www.sofastack.tech/sofa-lookout/docs/Home)的快速开始和用户手册。
52+
参考 [官方文档](http://www.sofastack.tech/sofa-lookout/docs/Home)的快速开始和用户手册。

client/lookout-api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.alipay.sofa.lookout</groupId>
77
<artifactId>lookout-client-parent</artifactId>
8-
<version>1.5.0</version>
8+
<version>1.5.1</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

client/lookout-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-client/src/main/java/com/alipay/lookout/client/AbstractLookoutClient.java

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public AbstractLookoutClient(String appName) {
5050
Assert.checkArg(StringUtils.isNotEmpty(appName), "appName is required!");
5151
}
5252

53+
protected String getAppName() {
54+
return appName;
55+
}
56+
5357
protected void addRegistry(MetricRegistry registry) {
5458
Preconditions.checkArgument(!(registry instanceof CompositeRegistry),
5559
"The registry can not be compositeRegistry!");

client/lookout-client/src/main/java/com/alipay/lookout/client/DefaultLookoutClient.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.alipay.lookout.api.MetricRegistry;
2121
import com.alipay.lookout.core.config.MetricConfig;
2222
import com.alipay.lookout.remote.step.LookoutRegistry;
23-
23+
import static com.alipay.lookout.core.config.LookoutConfig.APP_NAME;
2424
import static com.alipay.lookout.core.config.LookoutConfig.LOOKOUT_EXPORTER_ENABLE;
2525

2626
/**
@@ -50,6 +50,10 @@ public synchronized void addRegistry(MetricRegistry registry) {
5050

5151
if (registry instanceof LookoutRegistry) {
5252
MetricConfig config = ((LookoutRegistry) registry).getConfig();
53+
if (!config.containsKey(APP_NAME)) {
54+
//HttpObserver needs
55+
config.setProperty(APP_NAME, getAppName());
56+
}
5357
if (!config.getBoolean(LOOKOUT_EXPORTER_ENABLE, false)) {
5458
return;
5559
}

client/lookout-common/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-ext-jvm/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-ext-os/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.alipay.sofa.lookout</groupId>
77
<artifactId>lookout-client-parent</artifactId>
8-
<version>1.5.0</version>
8+
<version>1.5.1</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

client/lookout-reg-dropwizard/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-reg-prometheus/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-reg-server/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/HttpObserver.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
public class HttpObserver implements MetricObserver<LookoutMeasurement> {
5252
private static final Logger logger = LookoutLoggerFactory
5353
.getLogger(HttpObserver.class);
54-
static final String APP_HEADER_NAME = "app";
5554
public static final String UTF_8 = "utf-8";
5655
static final String AGENT_URL_PATTERN = "http://%s:%d/datas";
5756
public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
@@ -77,7 +76,9 @@ public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService) {
7776
}
7877

7978
public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService, Registry registry) {
80-
this(lookoutConfig, addrService, registry, new DefaultHttpRequestProcessor(addrService));
79+
this(lookoutConfig, addrService, registry, new DefaultHttpRequestProcessor(addrService,
80+
lookoutConfig));
81+
8182
}
8283

8384
public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService, Registry registry,
@@ -91,11 +92,6 @@ public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService, Reg
9192
System.getProperty(LOOKOUT_AGENT_TEST_URL)));
9293
//inner port
9394
innerAgentPort = lookoutConfig.getInt(LOOKOUT_AGENT_SERVER_PORT, -1);
94-
//add common metadatas
95-
if (lookoutConfig.containsKey(LookoutConfig.APP_NAME)) {
96-
httpRequestProcessor.addCommonHeader(APP_HEADER_NAME,
97-
lookoutConfig.getString(LookoutConfig.APP_NAME));
98-
}
9995
this.reg = registry;
10096
}
10197

client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/support/http/DefaultHttpRequestProcessor.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import com.alipay.lookout.api.Registry;
2222
import com.alipay.lookout.common.log.LookoutLoggerFactory;
2323
import com.alipay.lookout.common.utils.NetworkUtil;
24+
import com.alipay.lookout.core.config.LookoutConfig;
25+
import com.alipay.lookout.core.config.MetricConfig;
2426
import com.alipay.lookout.remote.report.AddressService;
27+
import org.apache.commons.lang.StringUtils;
2528
import org.apache.http.Header;
2629
import org.apache.http.HttpResponse;
2730
import org.apache.http.client.ResponseHandler;
@@ -37,7 +40,6 @@
3740
import org.slf4j.Logger;
3841

3942
import java.io.IOException;
40-
import java.util.HashMap;
4143
import java.util.Map;
4244
import java.util.concurrent.TimeUnit;
4345
import java.util.concurrent.atomic.AtomicBoolean;
@@ -57,6 +59,8 @@ public final class DefaultHttpRequestProcessor extends ReportDecider {
5759
public static final String LOOKOUT_REPORT_FAIL_COUNT_ID = "lookout.report.fail";
5860
public static final String WAIT_MINUTES = "Wait-Minutes";
5961
static final String CLIENT_VERSION = "LOOKOUT-CLIENT-V1";
62+
static final String APP_HEADER_NAME = "app";
63+
6064
private final String clientIp = NetworkUtil.getLocalAddress()
6165
.getHostAddress();
6266

@@ -67,15 +71,8 @@ public final class DefaultHttpRequestProcessor extends ReportDecider {
6771

6872
private static final AtomicBoolean httpClientInitialized = new AtomicBoolean(false);
6973

70-
private final Map<String, String> commonMetadata = new HashMap<String, String>();
71-
72-
public DefaultHttpRequestProcessor(AddressService addressService) {
73-
super(addressService);
74-
}
75-
76-
@Override
77-
public void addCommonHeader(String headerName, String headerValue) {
78-
commonMetadata.put(headerName, headerValue);
74+
public DefaultHttpRequestProcessor(AddressService addressService, MetricConfig metricConfig) {
75+
super(addressService, metricConfig);
7976
}
8077

8178
@Override
@@ -140,14 +137,17 @@ public Boolean handleResponse(HttpResponse response) throws IOException {
140137

141138
}
142139

143-
private void addCommonHeaders(HttpRequestBase httpPost, Map<String, String> metadata) {
144-
httpPost.setHeader(CLIENT_IP_HEADER_NAME, clientIp);
140+
private void addCommonHeaders(HttpRequestBase httpMtd, Map<String, String> metadata) {
141+
httpMtd.setHeader(CLIENT_IP_HEADER_NAME, clientIp);
142+
String app = getMetricConfig().getString(LookoutConfig.APP_NAME);
143+
if (StringUtils.isNotEmpty(app)) {
144+
httpMtd.setHeader(APP_HEADER_NAME, app);
145+
}
145146
if (metadata == null) {
146-
metadata = new HashMap<String, String>();
147+
return;
147148
}
148-
metadata.putAll(commonMetadata);
149149
for (Map.Entry<String, String> entry : metadata.entrySet()) {
150-
httpPost.setHeader(entry.getKey(), entry.getValue());
150+
httpMtd.setHeader(entry.getKey(), entry.getValue());
151151
}
152152
}
153153

client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/support/http/HttpRequestProcessor.java

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
*/
2929
public interface HttpRequestProcessor {
3030

31-
void addCommonHeader(String headerName, String headerValue);
32-
3331
boolean sendPostRequest(HttpPost httpPost, Map<String, String> metadata) throws IOException;
3432

3533
boolean sendGetRequest(HttpGet httpGet, Map<String, String> metadata) throws IOException;

client/lookout-reg-server/src/main/java/com/alipay/lookout/remote/report/support/http/ReportDecider.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package com.alipay.lookout.remote.report.support.http;
1818

1919
import com.alipay.lookout.common.log.LookoutLoggerFactory;
20+
import com.alipay.lookout.core.config.MetricConfig;
2021
import com.alipay.lookout.remote.report.Address;
2122
import com.alipay.lookout.remote.report.AddressService;
23+
import com.google.common.base.Preconditions;
2224
import org.apache.http.client.methods.HttpGet;
2325
import org.slf4j.Logger;
2426

@@ -39,10 +41,18 @@ public abstract class ReportDecider implements HttpRequestProcessor {
3941
private long expiredTime = 65000; //65s
4042

4143
private AddressService addressService;
44+
private MetricConfig metricConfig;
4245

43-
public ReportDecider(AddressService addressService) {
46+
public ReportDecider(AddressService addressService, MetricConfig metricConfig) {
47+
Preconditions.checkNotNull(addressService, "An addressService is required!");
48+
Preconditions.checkNotNull(metricConfig, "A metricConfig is required!");
4449
this.addressService = addressService;
4550
this.addressLastModifiedTime = System.currentTimeMillis() - expiredTime;
51+
this.metricConfig = metricConfig;
52+
}
53+
54+
protected MetricConfig getMetricConfig() {
55+
return metricConfig;
4656
}
4757

4858
public boolean stillSilent() {
@@ -79,6 +89,7 @@ void refreshAddressCache() {
7989
Address oldOne = addressHolder.get();
8090
Address newOne = addressService.getAgentServerHost();
8191
if (newOne == null) {
92+
logger.debug("No gateway address found!");
8293
return;
8394
}
8495
//check new address
@@ -100,7 +111,8 @@ void refreshAddressCache() {
100111
return;
101112
} catch (Throwable e) {
102113
logger.debug("check gateway address {} fail:{}. old address:{}!", newOne.ip(),
103-
e.getMessage(), oldOne.ip());
114+
e.getMessage(), oldOne == null ? "" : oldOne.ip());
115+
104116
}
105117

106118
}

client/lookout-reg-server/src/test/java/com/alipay/lookout/MockHttpRequestProcessor.java

-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public boolean sendGetRequest(HttpGet httpGet, Map<String, String> metadata) thr
4242
return false;
4343
}
4444

45-
@Override
46-
public void addCommonHeader(String headerName, String headerValue) {
47-
48-
}
49-
5045
@Override
5146
public Address getAvailableAddress() {
5247
return new Address("localhost", 8080);

client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/HttpObserverTest.java

-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public void testGetBatches() {
6666
}
6767

6868
class TestHttpRequestProcessor implements HttpRequestProcessor {
69-
@Override
70-
public void addCommonHeader(String headerName, String headerValue) {
71-
72-
}
7369

7470
@Override
7571
public Address getAvailableAddress() {

client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/support/HttpRequestProcessorTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.alipay.lookout.remote.report.support;
1818

19+
import com.alipay.lookout.core.config.LookoutConfig;
1920
import com.alipay.lookout.remote.report.DefaultAddressService;
2021
import com.alipay.lookout.remote.report.support.http.DefaultHttpRequestProcessor;
2122
import org.apache.http.Header;
@@ -33,7 +34,8 @@
3334
*/
3435
public class HttpRequestProcessorTest {
3536
final DefaultHttpRequestProcessor httpRequestProcessor = new DefaultHttpRequestProcessor(
36-
new DefaultAddressService());
37+
new DefaultAddressService(),
38+
new LookoutConfig());
3739

3840
@Test
3941
public void testHandleErrorResponse401() {

client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/support/http/DefaultHttpRequestProcessorTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.alipay.lookout.remote.report.support.http;
1818

19+
import com.alipay.lookout.core.config.LookoutConfig;
1920
import com.alipay.lookout.remote.report.AddressService;
2021
import com.alipay.lookout.remote.report.DefaultAddressService;
2122
import org.apache.http.client.methods.HttpPost;
@@ -41,7 +42,8 @@ public void testGetHttpClient() {
4142
@Test(expected = IOException.class)
4243
public void testSendPostFail() throws IOException {
4344
AddressService addressService = new DefaultAddressService();
44-
DefaultHttpRequestProcessor p = new DefaultHttpRequestProcessor(addressService);
45+
DefaultHttpRequestProcessor p = new DefaultHttpRequestProcessor(addressService,
46+
new LookoutConfig());
4547
HttpPost httpPost = new HttpPost("http://localhost/ok");
4648
try {
4749
httpPost.setEntity(new StringEntity("a"));

client/lookout-reg-server/src/test/java/com/alipay/lookout/remote/report/support/http/ReportDeciderTest.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.alipay.lookout.remote.report.support.http;
1818

19+
import com.alipay.lookout.core.config.LookoutConfig;
1920
import com.alipay.lookout.remote.report.AddressService;
2021
import com.alipay.lookout.remote.report.DefaultAddressService;
2122
import org.apache.http.client.methods.HttpGet;
@@ -51,12 +52,7 @@ public void testRefreshAddressCache() {
5152
class ReportDecider2 extends ReportDecider {
5253

5354
public ReportDecider2(AddressService addressService) {
54-
super(addressService);
55-
}
56-
57-
@Override
58-
public void addCommonHeader(String headerName, String headerValue) {
59-
55+
super(addressService, new LookoutConfig());
6056
}
6157

6258
@Override

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.alipay.sofa.lookout</groupId>
77
<artifactId>lookout-client-parent</artifactId>
8-
<version>1.5.0</version>
8+
<version>1.5.1</version>
99
<packaging>pom</packaging>
1010
<name>${project.groupId}:${project.artifactId}</name>
1111
<description>It is a library which allows you to instrument your app with custom metrics</description>

client/samples/lookout-client-samples-boot/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2222
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2323
<java.version>1.8</java.version>
24-
<lookout.client.version>1.5.0</lookout.client.version>
24+
<lookout.client.version>1.5.1</lookout.client.version>
2525
</properties>
2626

2727
<dependencies>

0 commit comments

Comments
 (0)