Skip to content

Commit

Permalink
upgrade to 1.5.1 and do some refactors (#41)
Browse files Browse the repository at this point in the history
* fix log debug npe bug
* optimize the usage of lookoutclient
* change wiki link to doc site in readme
  • Loading branch information
luyiisme authored Sep 21, 2018
1 parent 44c68b1 commit 5852af0
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 66 deletions.
4 changes: 2 additions & 2 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SOFALookout Client 项目支持 Maven 3.2.5+,JDK 6+ 进行编译。

## 2. API 埋点需知

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

## 3. 扩展能力

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

## 6.如何使用

参考 [WIKI 文档](http://www.sofastack.tech/sofa-lookout/docs/Home)的快速开始和用户手册。
参考 [官方文档](http://www.sofastack.tech/sofa-lookout/docs/Home)的快速开始和用户手册。
2 changes: 1 addition & 1 deletion client/lookout-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public AbstractLookoutClient(String appName) {
Assert.checkArg(StringUtils.isNotEmpty(appName), "appName is required!");
}

protected String getAppName() {
return appName;
}

protected void addRegistry(MetricRegistry registry) {
Preconditions.checkArgument(!(registry instanceof CompositeRegistry),
"The registry can not be compositeRegistry!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.alipay.lookout.api.MetricRegistry;
import com.alipay.lookout.core.config.MetricConfig;
import com.alipay.lookout.remote.step.LookoutRegistry;

import static com.alipay.lookout.core.config.LookoutConfig.APP_NAME;
import static com.alipay.lookout.core.config.LookoutConfig.LOOKOUT_EXPORTER_ENABLE;

/**
Expand Down Expand Up @@ -50,6 +50,10 @@ public synchronized void addRegistry(MetricRegistry registry) {

if (registry instanceof LookoutRegistry) {
MetricConfig config = ((LookoutRegistry) registry).getConfig();
if (!config.containsKey(APP_NAME)) {
//HttpObserver needs
config.setProperty(APP_NAME, getAppName());
}
if (!config.getBoolean(LOOKOUT_EXPORTER_ENABLE, false)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion client/lookout-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-ext-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-ext-os/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-reg-dropwizard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-reg-prometheus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-reg-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
public class HttpObserver implements MetricObserver<LookoutMeasurement> {
private static final Logger logger = LookoutLoggerFactory
.getLogger(HttpObserver.class);
static final String APP_HEADER_NAME = "app";
public static final String UTF_8 = "utf-8";
static final String AGENT_URL_PATTERN = "http://%s:%d/datas";
public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
Expand All @@ -77,7 +76,9 @@ public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService) {
}

public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService, Registry registry) {
this(lookoutConfig, addrService, registry, new DefaultHttpRequestProcessor(addrService));
this(lookoutConfig, addrService, registry, new DefaultHttpRequestProcessor(addrService,
lookoutConfig));

}

public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService, Registry registry,
Expand All @@ -91,11 +92,6 @@ public HttpObserver(LookoutConfig lookoutConfig, AddressService addrService, Reg
System.getProperty(LOOKOUT_AGENT_TEST_URL)));
//inner port
innerAgentPort = lookoutConfig.getInt(LOOKOUT_AGENT_SERVER_PORT, -1);
//add common metadatas
if (lookoutConfig.containsKey(LookoutConfig.APP_NAME)) {
httpRequestProcessor.addCommonHeader(APP_HEADER_NAME,
lookoutConfig.getString(LookoutConfig.APP_NAME));
}
this.reg = registry;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import com.alipay.lookout.api.Registry;
import com.alipay.lookout.common.log.LookoutLoggerFactory;
import com.alipay.lookout.common.utils.NetworkUtil;
import com.alipay.lookout.core.config.LookoutConfig;
import com.alipay.lookout.core.config.MetricConfig;
import com.alipay.lookout.remote.report.AddressService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
Expand All @@ -37,7 +40,6 @@
import org.slf4j.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -57,6 +59,8 @@ public final class DefaultHttpRequestProcessor extends ReportDecider {
public static final String LOOKOUT_REPORT_FAIL_COUNT_ID = "lookout.report.fail";
public static final String WAIT_MINUTES = "Wait-Minutes";
static final String CLIENT_VERSION = "LOOKOUT-CLIENT-V1";
static final String APP_HEADER_NAME = "app";

private final String clientIp = NetworkUtil.getLocalAddress()
.getHostAddress();

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

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

private final Map<String, String> commonMetadata = new HashMap<String, String>();

public DefaultHttpRequestProcessor(AddressService addressService) {
super(addressService);
}

@Override
public void addCommonHeader(String headerName, String headerValue) {
commonMetadata.put(headerName, headerValue);
public DefaultHttpRequestProcessor(AddressService addressService, MetricConfig metricConfig) {
super(addressService, metricConfig);
}

@Override
Expand Down Expand Up @@ -140,14 +137,17 @@ public Boolean handleResponse(HttpResponse response) throws IOException {

}

private void addCommonHeaders(HttpRequestBase httpPost, Map<String, String> metadata) {
httpPost.setHeader(CLIENT_IP_HEADER_NAME, clientIp);
private void addCommonHeaders(HttpRequestBase httpMtd, Map<String, String> metadata) {
httpMtd.setHeader(CLIENT_IP_HEADER_NAME, clientIp);
String app = getMetricConfig().getString(LookoutConfig.APP_NAME);
if (StringUtils.isNotEmpty(app)) {
httpMtd.setHeader(APP_HEADER_NAME, app);
}
if (metadata == null) {
metadata = new HashMap<String, String>();
return;
}
metadata.putAll(commonMetadata);
for (Map.Entry<String, String> entry : metadata.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
httpMtd.setHeader(entry.getKey(), entry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
*/
public interface HttpRequestProcessor {

void addCommonHeader(String headerName, String headerValue);

boolean sendPostRequest(HttpPost httpPost, Map<String, String> metadata) throws IOException;

boolean sendGetRequest(HttpGet httpGet, Map<String, String> metadata) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.alipay.lookout.remote.report.support.http;

import com.alipay.lookout.common.log.LookoutLoggerFactory;
import com.alipay.lookout.core.config.MetricConfig;
import com.alipay.lookout.remote.report.Address;
import com.alipay.lookout.remote.report.AddressService;
import com.google.common.base.Preconditions;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;

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

private AddressService addressService;
private MetricConfig metricConfig;

public ReportDecider(AddressService addressService) {
public ReportDecider(AddressService addressService, MetricConfig metricConfig) {
Preconditions.checkNotNull(addressService, "An addressService is required!");
Preconditions.checkNotNull(metricConfig, "A metricConfig is required!");
this.addressService = addressService;
this.addressLastModifiedTime = System.currentTimeMillis() - expiredTime;
this.metricConfig = metricConfig;
}

protected MetricConfig getMetricConfig() {
return metricConfig;
}

public boolean stillSilent() {
Expand Down Expand Up @@ -79,6 +89,7 @@ void refreshAddressCache() {
Address oldOne = addressHolder.get();
Address newOne = addressService.getAgentServerHost();
if (newOne == null) {
logger.debug("No gateway address found!");
return;
}
//check new address
Expand All @@ -100,7 +111,8 @@ void refreshAddressCache() {
return;
} catch (Throwable e) {
logger.debug("check gateway address {} fail:{}. old address:{}!", newOne.ip(),
e.getMessage(), oldOne.ip());
e.getMessage(), oldOne == null ? "" : oldOne.ip());

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public boolean sendGetRequest(HttpGet httpGet, Map<String, String> metadata) thr
return false;
}

@Override
public void addCommonHeader(String headerName, String headerValue) {

}

@Override
public Address getAvailableAddress() {
return new Address("localhost", 8080);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public void testGetBatches() {
}

class TestHttpRequestProcessor implements HttpRequestProcessor {
@Override
public void addCommonHeader(String headerName, String headerValue) {

}

@Override
public Address getAvailableAddress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.lookout.remote.report.support;

import com.alipay.lookout.core.config.LookoutConfig;
import com.alipay.lookout.remote.report.DefaultAddressService;
import com.alipay.lookout.remote.report.support.http.DefaultHttpRequestProcessor;
import org.apache.http.Header;
Expand All @@ -33,7 +34,8 @@
*/
public class HttpRequestProcessorTest {
final DefaultHttpRequestProcessor httpRequestProcessor = new DefaultHttpRequestProcessor(
new DefaultAddressService());
new DefaultAddressService(),
new LookoutConfig());

@Test
public void testHandleErrorResponse401() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.lookout.remote.report.support.http;

import com.alipay.lookout.core.config.LookoutConfig;
import com.alipay.lookout.remote.report.AddressService;
import com.alipay.lookout.remote.report.DefaultAddressService;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -41,7 +42,8 @@ public void testGetHttpClient() {
@Test(expected = IOException.class)
public void testSendPostFail() throws IOException {
AddressService addressService = new DefaultAddressService();
DefaultHttpRequestProcessor p = new DefaultHttpRequestProcessor(addressService);
DefaultHttpRequestProcessor p = new DefaultHttpRequestProcessor(addressService,
new LookoutConfig());
HttpPost httpPost = new HttpPost("http://localhost/ok");
try {
httpPost.setEntity(new StringEntity("a"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.lookout.remote.report.support.http;

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

public ReportDecider2(AddressService addressService) {
super(addressService);
}

@Override
public void addCommonHeader(String headerName, String headerValue) {

super(addressService, new LookoutConfig());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion client/lookout-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
<packaging>pom</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>It is a library which allows you to instrument your app with custom metrics</description>
Expand Down
2 changes: 1 addition & 1 deletion client/samples/lookout-client-samples-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<lookout.client.version>1.5.0</lookout.client.version>
<lookout.client.version>1.5.1</lookout.client.version>
</properties>

<dependencies>
Expand Down
Loading

0 comments on commit 5852af0

Please sign in to comment.