Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
update test code
Browse files Browse the repository at this point in the history
  • Loading branch information
foreverneverer committed Jun 8, 2020
1 parent 677d045 commit a0abfd6
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 313 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<packaging>jar</packaging>
<version>1.12-thrift-0.11.0-inlined-SNAPSHOT</version>
<name>Pegasus Java Client</name>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -80,6 +81,16 @@
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.4.0</version>
</dependency>
</dependencies>
<reporting>
<plugins>
Expand Down
81 changes: 56 additions & 25 deletions src/main/java/com/xiaomi/infra/pegasus/client/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* .operationTimeout(Duration.ofMillis(1000))
* .asyncWorkers(4)
* .enablePerfCounter(false)
* .falconPerfCounterTags("")
* .perfCounterTags("")
* .falconPushInterval(Duration.ofSeconds(10))
* .metaQueryTimeout(Duration.ofMillis(5000))
* .build();
Expand All @@ -36,7 +36,8 @@ public class ClientOptions {
public static final Duration DEFAULT_OPERATION_TIMEOUT = Duration.ofMillis(1000);
public static final int DEFAULT_ASYNC_WORKERS = Runtime.getRuntime().availableProcessors();
public static final boolean DEFAULT_ENABLE_PERF_COUNTER = true;
public static final String DEFAULT_FALCON_PERF_COUNTER_TAGS = "";
public static final String DEFAULT_PERF_COUNTER_TYPE = "falcon";
public static final String DEFAULT_PERF_COUNTER_TAGS = "";
public static final Duration DEFAULT_FALCON_PUSH_INTERVAL = Duration.ofSeconds(10);
public static final boolean DEFAULT_ENABLE_WRITE_LIMIT = true;
public static final Duration DEFAULT_META_QUERY_TIMEOUT = Duration.ofMillis(5000);
Expand All @@ -45,7 +46,8 @@ public class ClientOptions {
private final Duration operationTimeout;
private final int asyncWorkers;
private final boolean enablePerfCounter;
private final String falconPerfCounterTags;
private final String perfCounterType;
private final String perfCounterTags;
private final Duration falconPushInterval;
private final boolean enableWriteLimit;
private final Duration metaQueryTimeout;
Expand All @@ -55,7 +57,8 @@ protected ClientOptions(Builder builder) {
this.operationTimeout = builder.operationTimeout;
this.asyncWorkers = builder.asyncWorkers;
this.enablePerfCounter = builder.enablePerfCounter;
this.falconPerfCounterTags = builder.falconPerfCounterTags;
this.perfCounterType = builder.perfCounterType;
this.perfCounterTags = builder.perfCounterTags;
this.falconPushInterval = builder.falconPushInterval;
this.enableWriteLimit = builder.enableWriteLimit;
this.metaQueryTimeout = builder.metaQueryTimeout;
Expand All @@ -66,7 +69,8 @@ protected ClientOptions(ClientOptions original) {
this.operationTimeout = original.getOperationTimeout();
this.asyncWorkers = original.getAsyncWorkers();
this.enablePerfCounter = original.isEnablePerfCounter();
this.falconPerfCounterTags = original.getFalconPerfCounterTags();
this.perfCounterType = original.perfCounterType;
this.perfCounterTags = original.getPerfCounterTags();
this.falconPushInterval = original.getFalconPushInterval();
this.enableWriteLimit = original.isWriteLimitEnabled();
this.metaQueryTimeout = original.getMetaQueryTimeout();
Expand Down Expand Up @@ -111,7 +115,8 @@ public boolean equals(Object options) {
&& this.operationTimeout.toMillis() == clientOptions.operationTimeout.toMillis()
&& this.asyncWorkers == clientOptions.asyncWorkers
&& this.enablePerfCounter == clientOptions.enablePerfCounter
&& this.falconPerfCounterTags.equals(clientOptions.falconPerfCounterTags)
&& this.perfCounterType.equals(clientOptions.perfCounterType)
&& this.perfCounterTags.equals(clientOptions.perfCounterTags)
&& this.falconPushInterval.toMillis() == clientOptions.falconPushInterval.toMillis()
&& this.enableWriteLimit == clientOptions.enableWriteLimit
&& this.metaQueryTimeout.toMillis() == clientOptions.metaQueryTimeout.toMillis();
Expand All @@ -129,10 +134,12 @@ public String toString() {
+ operationTimeout.toMillis()
+ ", asyncWorkers="
+ asyncWorkers
+ ", enablePerfCounter="
+ ", perfCounterType="
+ enablePerfCounter
+ ", falconPerfCounterTags='"
+ falconPerfCounterTags
+ ", perfCounterTags='"
+ perfCounterType
+ ", enablePerfCounter="
+ perfCounterTags
+ '\''
+ ", falconPushInterval(s)="
+ falconPushInterval.getSeconds()
Expand All @@ -149,7 +156,8 @@ public static class Builder {
private Duration operationTimeout = DEFAULT_OPERATION_TIMEOUT;
private int asyncWorkers = DEFAULT_ASYNC_WORKERS;
private boolean enablePerfCounter = DEFAULT_ENABLE_PERF_COUNTER;
private String falconPerfCounterTags = DEFAULT_FALCON_PERF_COUNTER_TAGS;
private String perfCounterType = DEFAULT_PERF_COUNTER_TYPE;
private String perfCounterTags = DEFAULT_PERF_COUNTER_TAGS;
private Duration falconPushInterval = DEFAULT_FALCON_PUSH_INTERVAL;
private boolean enableWriteLimit = DEFAULT_ENABLE_WRITE_LIMIT;
private Duration metaQueryTimeout = DEFAULT_META_QUERY_TIMEOUT;
Expand Down Expand Up @@ -194,8 +202,8 @@ public Builder asyncWorkers(int asyncWorkers) {

/**
* Whether to enable performance statistics. If true, the client will periodically report
* metrics to local falcon agent (currently we only support falcon as monitoring system).
* Defaults to {@literal true}, see {@link #DEFAULT_ENABLE_PERF_COUNTER}.
* metrics to local falcon agent (if set falcon as monitoring system) or open prometheus
* collector http server. Defaults to {@literal true}, see {@link #DEFAULT_ENABLE_PERF_COUNTER}.
*
* @param enablePerfCounter enablePerfCounter
* @return {@code this}
Expand All @@ -205,22 +213,34 @@ public Builder enablePerfCounter(boolean enablePerfCounter) {
return this;
}

/**
* set the perf-counter type, now only support falcon and prometheus, Defaults to {@literal
* falcon}, see {@link #DEFAULT_PERF_COUNTER_TYPE}
*
* @param perfCounterType perfCounterType
* @return this
*/
public Builder perfCounterType(String perfCounterType) {
this.perfCounterType = perfCounterType;
return this;
}

/**
* Additional tags for falcon metrics. For example:
* "cluster=c3srv-ad,job=recommend-service-history". Defaults to empty string, see {@link
* #DEFAULT_FALCON_PERF_COUNTER_TAGS}.
* #DEFAULT_PERF_COUNTER_TAGS}.
*
* @param falconPerfCounterTags falconPerfCounterTags
* @param perfCounterTags perfCounterTags
* @return {@code this}
*/
public Builder falconPerfCounterTags(String falconPerfCounterTags) {
this.falconPerfCounterTags = falconPerfCounterTags;
public Builder perfCounterTags(String perfCounterTags) {
this.perfCounterTags = perfCounterTags;
return this;
}

/**
* The interval to report metrics to local falcon agent. Defaults to {@literal 10s}, see {@link
* #DEFAULT_FALCON_PUSH_INTERVAL}.
* The interval to report metrics to local falcon agent(if set falcon as monitor system).
* Defaults to {@literal 10s}, see {@link #DEFAULT_FALCON_PUSH_INTERVAL}.
*
* @param falconPushInterval falconPushInterval
* @return {@code this}
Expand Down Expand Up @@ -279,7 +299,8 @@ public ClientOptions.Builder mutate() {
.operationTimeout(getOperationTimeout())
.asyncWorkers(getAsyncWorkers())
.enablePerfCounter(isEnablePerfCounter())
.falconPerfCounterTags(getFalconPerfCounterTags())
.perfCounterType(getPerfCounterType())
.perfCounterTags(getPerfCounterTags())
.falconPushInterval(getFalconPushInterval())
.enableWriteLimit(isWriteLimitEnabled())
.metaQueryTimeout(getMetaQueryTimeout());
Expand Down Expand Up @@ -316,8 +337,8 @@ public int getAsyncWorkers() {

/**
* Whether to enable performance statistics. If true, the client will periodically report metrics
* to local falcon agent (currently we only support falcon as monitoring system). Defaults to
* {@literal true}.
* to local falcon agent (if set falcon as monitoring system) or open prometheus collector http
* server. Defaults to {@literal true}.
*
* @return whether to enable performance statistics.
*/
Expand All @@ -326,16 +347,26 @@ public boolean isEnablePerfCounter() {
}

/**
* Additional tags for falcon metrics. Defaults to empty string.
* get perf-counter type, now only support falcon and prometheus
*
* @return perf-counter type
*/
public String getPerfCounterType() {
return perfCounterType;
}

/**
* Additional tags for metrics. Defaults to empty string.
*
* @return additional tags for falcon metrics.
*/
public String getFalconPerfCounterTags() {
return falconPerfCounterTags;
public String getPerfCounterTags() {
return perfCounterTags;
}

/**
* The interval to report metrics to local falcon agent. Defaults to {@literal 10s}.
* The interval to report metrics to local falcon agent(if set falcon as monitor system). Defaults
* to {@literal 10s}.
*
* @return the interval to report metrics to local falcon agent.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
import org.json.JSONException;
import org.json.JSONObject;

public class Falcon implements PegasusCollector {

public class FalconCollector {
private FalconMetric falconMetric = new FalconMetric();
private final MetricRegistry registry;
public final String defaultTags;

public Falcon(String host, String tags, int reportStepSec, MetricRegistry registry) {
public FalconCollector(String host, String tags, int reportStepSec, MetricRegistry registry) {
this.defaultTags = tags;
this.registry = registry;
falconMetric.endpoint = host;
falconMetric.step = reportStepSec;
}

public String updateMetric() {
public String metricsToJson() {
falconMetric.timestamp = Tools.unixEpochMills() / 1000;

StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -54,8 +53,8 @@ public void genJsonsFromMeter(String name, Meter meter, StringBuilder output)
throws JSONException {
falconMetric.counterType = "GAUGE";

falconMetric.metric = name + ".cps-1sec";
falconMetric.tags = getTableTag(name, defaultTags, "@");
falconMetric.metric = name + "_cps_1sec";
falconMetric.tags = getTableTag(name, defaultTags);
falconMetric.value = meter.getMeanRate();
oneMetricToJson(falconMetric, output);
}
Expand All @@ -65,14 +64,14 @@ public void genJsonsFromHistogram(String name, Histogram hist, StringBuilder out
falconMetric.counterType = "GAUGE";
Snapshot s = hist.getSnapshot();

falconMetric.metric = name + ".p99";
falconMetric.tags = getTableTag(name, defaultTags, "@");
falconMetric.metric = name + "_p99";
falconMetric.tags = getTableTag(name, defaultTags);
falconMetric.value = s.get99thPercentile();
oneMetricToJson(falconMetric, output);
output.append(',');

falconMetric.metric = name + ".p999";
falconMetric.tags = getTableTag(name, defaultTags, "@");
falconMetric.metric = name + "_p999";
falconMetric.tags = getTableTag(name, defaultTags);
falconMetric.value = s.get999thPercentile();
oneMetricToJson(falconMetric, output);
}
Expand Down
Loading

0 comments on commit a0abfd6

Please sign in to comment.