Skip to content

Commit 54518a3

Browse files
khotynluyiisme
authored andcommitted
Add app name to prom registry (#44)
Add app tag to prometheus registry
1 parent 5852af0 commit 54518a3

File tree

29 files changed

+110
-59
lines changed

29 files changed

+110
-59
lines changed

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.1</version>
8+
<version>1.5.2-SNAPSHOT</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.1</version>
9+
<version>1.5.2-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

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.1</version>
9+
<version>1.5.2-SNAPSHOT</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.1</version>
9+
<version>1.5.2-SNAPSHOT</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.1</version>
9+
<version>1.5.2-SNAPSHOT</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.1</version>
8+
<version>1.5.2-SNAPSHOT</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.1</version>
9+
<version>1.5.2-SNAPSHOT</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.1</version>
9+
<version>1.5.2-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

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

+42-6
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,37 @@
2020
import com.alipay.lookout.api.Metric;
2121
import com.alipay.lookout.api.info.Info;
2222
import com.alipay.lookout.common.log.LookoutLoggerFactory;
23+
import com.alipay.lookout.core.CommonTagsAccessor;
2324
import com.alipay.lookout.core.DefaultRegistry;
2425
import com.alipay.lookout.core.config.MetricConfig;
2526
import com.alipay.lookout.reg.prometheus.common.PromWriter;
2627
import com.alipay.lookout.reg.prometheus.exporter.ExporterServer;
28+
import com.alipay.lookout.remote.model.LookoutMeasurement;
2729
import com.sun.net.httpserver.HttpExchange;
2830
import com.sun.net.httpserver.HttpHandler;
2931

32+
import org.apache.commons.lang3.StringUtils;
3033
import org.slf4j.Logger;
3134

3235
import java.io.Closeable;
3336
import java.io.IOException;
3437
import java.util.Iterator;
38+
import java.util.Map;
39+
import java.util.concurrent.ConcurrentHashMap;
3540

41+
import static com.alipay.lookout.core.config.LookoutConfig.APP_NAME;
3642
import static com.alipay.lookout.core.config.LookoutConfig.DEFAULT_PROMETHEUS_EXPORTER_SERVER_PORT;
3743
import static com.alipay.lookout.core.config.LookoutConfig.LOOKOUT_PROMETHEUS_EXPORTER_SERVER_PORT;
3844

3945
/**
4046
* Created by [email protected] on 2018/5/10.
4147
*/
42-
public class PrometheusRegistry extends DefaultRegistry implements Closeable {
43-
private static final Logger logger = LookoutLoggerFactory
44-
.getLogger(PrometheusRegistry.class);
45-
private final ExporterServer exporterServer;
46-
private final PromWriter promWriter = new PromWriter();
48+
public class PrometheusRegistry extends DefaultRegistry implements Closeable, CommonTagsAccessor {
49+
private static final Logger logger = LookoutLoggerFactory
50+
.getLogger(PrometheusRegistry.class);
51+
private final ExporterServer exporterServer;
52+
private final PromWriter promWriter = new PromWriter();
53+
private final Map<String, String> commonTags = new ConcurrentHashMap<String, String>();
4754

4855
public PrometheusRegistry(MetricConfig config) {
4956
this(Clock.SYSTEM, config);
@@ -53,6 +60,10 @@ public PrometheusRegistry(Clock clock, MetricConfig config) {
5360
super(clock, config);
5461
int serverPort = config.getInt(LOOKOUT_PROMETHEUS_EXPORTER_SERVER_PORT,
5562
DEFAULT_PROMETHEUS_EXPORTER_SERVER_PORT);
63+
String appName = config.getString(APP_NAME);
64+
if (StringUtils.isNotEmpty(appName)) {
65+
setCommonTag("app", appName);
66+
}
5667
exporterServer = new ExporterServer(serverPort);
5768
exporterServer.addMetricsQueryHandler(new HttpHandler() {
5869
@Override
@@ -87,7 +98,8 @@ private String getMetricsSnapshot(Iterator<Metric> metricIterator) {
8798
if (metric instanceof Info) {
8899
continue;
89100
}
90-
sb.append(promWriter.printFromIndicator(metric.measure()));
101+
LookoutMeasurement measurement = LookoutMeasurement.from(metric, this);
102+
sb.append(promWriter.printFromLookoutMeasurement(measurement));
91103
}
92104
return sb.toString();
93105
}
@@ -98,4 +110,28 @@ public void close() throws IOException {
98110
logger.info("lookout client exporter is stopped.");
99111

100112
}
113+
114+
@Override
115+
public String getCommonTagValue(String name) {
116+
return commonTags.get(name);
117+
}
118+
119+
@Override
120+
public void setCommonTag(String name, String value) {
121+
if (value == null) {
122+
commonTags.remove(name);
123+
} else {
124+
commonTags.put(name, value);
125+
}
126+
}
127+
128+
@Override
129+
public void removeCommonTag(String name) {
130+
commonTags.remove(name);
131+
}
132+
133+
@Override
134+
public Map<String, String> commonTags() {
135+
return commonTags;
136+
}
101137
}

client/lookout-reg-prometheus/src/main/java/com/alipay/lookout/reg/prometheus/common/PromWriter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String printFromLookoutMeasurement(LookoutMeasurement measurement) {
5757
}
5858
StringBuilder sb = new StringBuilder();
5959
for (Map.Entry<String, Object> entry : measurement.getValues().entrySet()) {
60-
sb.append(measurement.metricId().name()).append("_").append(entry.getKey())
60+
sb.append(formatMetricName(measurement.metricId().name() + "_" + entry.getKey()))
6161
.append(tagStr).append(" ").append(entry.getValue().toString()).append("\n");
6262
}
6363
return sb.toString();

client/lookout-reg-prometheus/src/test/java/com/alipay/lookout/reg/prometheus/PromWriterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void testPrintPromText() {
4848

4949
String str = promWriter.printFromLookoutMeasurement(measurement);
5050
System.out.println(str);
51-
Assert.assertTrue(str.contains("aa.bb.cc_xxx3{tagk3=\"tagv3\"} 3"));
51+
Assert.assertTrue(str.contains("aa_bb_cc_xxx3{tagk3=\"tagv3\"} 3"));
5252
}
5353

5454
@Test

client/lookout-reg-prometheus/src/test/java/com/alipay/lookout/reg/prometheus/PrometheusRegistryTest.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.alipay.lookout.core.config.LookoutConfig;
2020
import org.junit.Assert;
21+
import org.junit.BeforeClass;
2122
import org.junit.Test;
2223

2324
import java.io.IOException;
@@ -29,14 +30,28 @@
2930
* Created by [email protected] on 2018/5/11.
3031
*/
3132
public class PrometheusRegistryTest {
33+
private static PrometheusRegistry r;
34+
35+
public PrometheusRegistryTest() {
36+
}
37+
38+
@BeforeClass
39+
public static void init() {
40+
LookoutConfig lookoutConfig = new LookoutConfig();
41+
lookoutConfig.setProperty(LookoutConfig.APP_NAME, "appNameTest");
42+
r = new PrometheusRegistry(lookoutConfig);
43+
r.registerExtendedMetrics();
44+
}
3245

3346
@Test
3447
public void testPromReg() throws IOException {
35-
PrometheusRegistry r = new PrometheusRegistry(new LookoutConfig());
36-
r.registerExtendedMetrics();
3748
String result = sendHttpRequest(new URL("http://localhost:9494/metrics"));
38-
System.out.println(result);
3949
Assert.assertTrue(result.contains("lookout_reg_max_size"));
40-
r.close();
50+
}
51+
52+
@Test
53+
public void testPromRegAppTag() throws IOException {
54+
String result = sendHttpRequest(new URL("http://localhost:9494/metrics"));
55+
Assert.assertTrue(result.contains("appNameTest"));
4156
}
4257
}

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.1</version>
9+
<version>1.5.2-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* 用于监听 poller 激活 和 空闲 的事件
2121
*
2222
* @author xiangfeng.xzc
23-
* @date 2018/7/19
23+
* @since 2018/7/19
2424
*/
2525
public interface Listener {
2626
/**

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* @author xiangfeng.xzc
28-
* @date 2018/7/12
28+
* @since 2018/7/12
2929
*/
3030
public class MetricCache {
3131
private final long rate;
@@ -48,9 +48,9 @@ public MetricCache(Clock clock, long rate, int slotCount) {
4848
/**
4949
* 如果存在原始的cache, 那么尽量保留原始origin里的数据
5050
*
51-
* @param origin
52-
* @param rate
53-
* @param slotCount
51+
* @param origin origin
52+
* @param rate rate
53+
* @param slotCount slotCount
5454
*/
5555
public MetricCache(MetricCache origin, long rate, int slotCount) {
5656
Preconditions.checkNotNull(origin);
@@ -97,7 +97,7 @@ public List<Slot> getNextData(Set<Long> successCursors) {
9797
/**
9898
* 找出一个可用的位置, 第一个available=true 或者 所有slot里, cursor最小的那个
9999
*
100-
* @return
100+
* @return available slot
101101
*/
102102
private int findAvailableSlot() {
103103
int oldestSlotIndex = -1;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/**
2222
* @author xiangfeng.xzc
23-
* @date 2018/7/18
23+
* @since 2018/7/18
2424
*/
2525
public class MetricDto {
2626
private String name;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
/**
4747
* @author xiangfeng.xzc
48-
* @date 2018/7/17
48+
* @since 2018/7/17
4949
*/
5050
public class MetricsHttpExporter {
5151
private static final Charset UTF8 = Charset.forName("UTF-8");
@@ -68,7 +68,7 @@ public MetricsHttpExporter(PollerController controller, int port, int backlog) {
6868
/**
6969
* 启动exporter, 暴露底层的http端口
7070
*
71-
* @throws IOException
71+
* @throws IOException IOException
7272
*/
7373
public void start() throws IOException {
7474
final ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1, 0L,
@@ -219,7 +219,7 @@ private static Set<Long> parseCursors(String str) {
219219
* 解析参数
220220
*
221221
* @param exchange
222-
* @return
222+
* @return the params
223223
*/
224224
private static List<NameValuePair> parseParams(HttpExchange exchange) {
225225
return new URIBuilder(exchange.getRequestURI()).getQueryParams();

0 commit comments

Comments
 (0)