This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
feat: support prometheus #110
Open
foreverneverer
wants to merge
9
commits into
XiaoMi:thrift-0.11.0-inlined
Choose a base branch
from
foreverneverer:prometheus-support
base: thrift-0.11.0-inlined
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
64ac0ff
init
foreverneverer 21ef387
init but not update test code
foreverneverer 2421673
update test code
foreverneverer 677d045
update test code
foreverneverer a0abfd6
update test code
foreverneverer 12df7dd
refactor
foreverneverer 04f3e06
refactor
foreverneverer 6c0c371
update log and client options
foreverneverer ad42d21
update test
foreverneverer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/com/xiaomi/infra/pegasus/metrics/FalconCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.xiaomi.infra.pegasus.metrics; | ||
|
||
import static com.xiaomi.infra.pegasus.metrics.MetricsPool.getMetricName; | ||
import static com.xiaomi.infra.pegasus.metrics.MetricsPool.getTableTag; | ||
|
||
import com.codahale.metrics.Histogram; | ||
import com.codahale.metrics.Meter; | ||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.Snapshot; | ||
import com.xiaomi.infra.pegasus.tools.Tools; | ||
import java.util.Map; | ||
import java.util.SortedMap; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public class FalconCollector { | ||
private FalconMetric falconMetric = new FalconMetric(); | ||
private final MetricRegistry registry; | ||
public final String defaultTags; | ||
|
||
public FalconCollector(String host, String tags, int reportStepSec, MetricRegistry registry) { | ||
this.defaultTags = tags; | ||
this.registry = registry; | ||
falconMetric.endpoint = host; | ||
falconMetric.step = reportStepSec; | ||
} | ||
|
||
public String metricsToJson() { | ||
falconMetric.timestamp = Tools.unixEpochMills() / 1000; | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
builder.append('['); | ||
SortedMap<String, Meter> meters = registry.getMeters(); | ||
for (Map.Entry<String, Meter> entry : meters.entrySet()) { | ||
genJsonsFromMeter(entry.getKey(), entry.getValue(), builder); | ||
builder.append(','); | ||
} | ||
|
||
for (Map.Entry<String, Histogram> entry : registry.getHistograms().entrySet()) { | ||
genJsonsFromHistogram(entry.getKey(), entry.getValue(), builder); | ||
builder.append(','); | ||
} | ||
|
||
if (builder.charAt(builder.length() - 1) == ',') { | ||
builder.deleteCharAt(builder.length() - 1); | ||
} | ||
|
||
builder.append("]"); | ||
|
||
return builder.toString(); | ||
} | ||
|
||
public void genJsonsFromMeter(String name, Meter meter, StringBuilder output) | ||
throws JSONException { | ||
falconMetric.counterType = "GAUGE"; | ||
falconMetric.metric = getMetricName(name, ""); | ||
falconMetric.tags = getTableTag(name, defaultTags); | ||
falconMetric.value = meter.getMeanRate(); | ||
oneMetricToJson(falconMetric, output); | ||
} | ||
|
||
public void genJsonsFromHistogram(String name, Histogram hist, StringBuilder output) | ||
throws JSONException { | ||
falconMetric.counterType = "GAUGE"; | ||
Snapshot s = hist.getSnapshot(); | ||
|
||
falconMetric.metric = getMetricName(name, "_p99"); | ||
falconMetric.tags = getTableTag(name, defaultTags); | ||
falconMetric.value = s.get99thPercentile(); | ||
oneMetricToJson(falconMetric, output); | ||
output.append(','); | ||
|
||
falconMetric.metric = getMetricName(name, "_p999"); | ||
falconMetric.tags = getTableTag(name, defaultTags); | ||
falconMetric.value = s.get999thPercentile(); | ||
oneMetricToJson(falconMetric, output); | ||
} | ||
|
||
public static void oneMetricToJson(FalconMetric metric, StringBuilder output) | ||
throws JSONException { | ||
JSONObject obj = new JSONObject(); | ||
obj.put("endpoint", metric.endpoint); | ||
obj.put("metric", metric.metric); | ||
obj.put("timestamp", metric.timestamp); | ||
obj.put("step", metric.step); | ||
obj.put("value", metric.value); | ||
obj.put("counterType", metric.counterType); | ||
obj.put("tags", metric.tags); | ||
output.append(obj.toString()); | ||
} | ||
|
||
static final class FalconMetric { | ||
public String endpoint; // metric host | ||
public String metric; // metric name | ||
public long timestamp; // report time in unix seconds | ||
public int step; // report interval in seconds; | ||
public double value; // metric value | ||
public String counterType; // GAUGE or COUNTER | ||
public String tags; // metrics description | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about define a enum to represent the type of perf counter?