Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#40] fix unit scaling for Prometheus exporter, add test #48

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,13 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ private void writeTimerValues(StringBuffer sb, MetricRegistry.Type scope, TimerI

writeMeterRateValues(sb, scope, timer.getMeter(), md);
Snapshot snapshot = timer.getSnapshot();
writeSnapshotBasics(sb, scope, md, snapshot, theUnit);
writeSnapshotBasics(sb, scope, md, snapshot, theUnit, true);

String suffix = USCORE + PrometheusUnit.getBaseUnitAsPrometheusString(md.getUnit());
writeHelpLine(sb, scope, md.getName(), md, suffix);
writeTypeLine(sb,scope,md.getName(),md, suffix,SUMMARY);
writeValueLine(sb,scope,suffix + "_count",timer.getCount(),md, null, false);

writeSnapshotQuantiles(sb, scope, md, snapshot, theUnit);
writeSnapshotQuantiles(sb, scope, md, snapshot, theUnit, true);
}

private void writeHistogramValues(StringBuffer sb, MetricRegistry.Type scope, HistogramImpl histogram, Metadata md) {
Expand All @@ -195,47 +195,47 @@ private void writeHistogramValues(StringBuffer sb, MetricRegistry.Type scope, Hi
String theUnit = unit.equals("none") ? "" : USCORE + unit;

writeHelpLine(sb, scope, md.getName(), md, SUMMARY);
writeSnapshotBasics(sb, scope, md, snapshot, theUnit);
writeSnapshotBasics(sb, scope, md, snapshot, theUnit, true);
writeTypeLine(sb,scope,md.getName(),md, theUnit,SUMMARY);
writeValueLine(sb,scope,theUnit + "_count",histogram.getCount(),md, null, false);
writeSnapshotQuantiles(sb, scope, md, snapshot, theUnit);
writeSnapshotQuantiles(sb, scope, md, snapshot, theUnit, true);
}


private void writeSnapshotBasics(StringBuffer sb, MetricRegistry.Type scope, Metadata md, Snapshot snapshot, String unit) {
private void writeSnapshotBasics(StringBuffer sb, MetricRegistry.Type scope, Metadata md, Snapshot snapshot, String unit, boolean performScaling) {

writeTypeAndValue(sb, scope, "_min" + unit, snapshot.getMin(), GAUGE, md);
writeTypeAndValue(sb, scope, "_max" + unit, snapshot.getMax(), GAUGE, md);
writeTypeAndValue(sb, scope, "_mean" + unit, snapshot.getMean(), GAUGE, md);
writeTypeAndValue(sb, scope, "_stddev" + unit, snapshot.getStdDev(), GAUGE, md);
writeTypeAndValue(sb, scope, "_min" + unit, snapshot.getMin(), GAUGE, md, performScaling);
writeTypeAndValue(sb, scope, "_max" + unit, snapshot.getMax(), GAUGE, md, performScaling);
writeTypeAndValue(sb, scope, "_mean" + unit, snapshot.getMean(), GAUGE, md, performScaling);
writeTypeAndValue(sb, scope, "_stddev" + unit, snapshot.getStdDev(), GAUGE, md, performScaling);
}

private void writeSnapshotQuantiles(StringBuffer sb, MetricRegistry.Type scope, Metadata md, Snapshot snapshot, String unit) {
writeValueLine(sb, scope, unit, snapshot.getMedian(), md, new Tag(QUANTILE, "0.5"));
writeValueLine(sb, scope, unit, snapshot.get75thPercentile(), md, new Tag(QUANTILE, "0.75"));
writeValueLine(sb, scope, unit, snapshot.get95thPercentile(), md, new Tag(QUANTILE, "0.95"));
writeValueLine(sb, scope, unit, snapshot.get98thPercentile(), md, new Tag(QUANTILE, "0.98"));
writeValueLine(sb, scope, unit, snapshot.get99thPercentile(), md, new Tag(QUANTILE, "0.99"));
writeValueLine(sb, scope, unit, snapshot.get999thPercentile(), md, new Tag(QUANTILE, "0.999"));
private void writeSnapshotQuantiles(StringBuffer sb, MetricRegistry.Type scope, Metadata md, Snapshot snapshot, String unit, boolean performScaling) {
writeValueLine(sb, scope, unit, snapshot.getMedian(), md, new Tag(QUANTILE, "0.5"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get75thPercentile(), md, new Tag(QUANTILE, "0.75"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get95thPercentile(), md, new Tag(QUANTILE, "0.95"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get98thPercentile(), md, new Tag(QUANTILE, "0.98"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get99thPercentile(), md, new Tag(QUANTILE, "0.99"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get999thPercentile(), md, new Tag(QUANTILE, "0.999"), performScaling);
}

private void writeMeterValues(StringBuffer sb, MetricRegistry.Type scope, Metered metric, Metadata md) {
writeHelpLine(sb, scope, md.getName(), md, "_total");
writeTypeAndValue(sb, scope, "_total", metric.getCount(), COUNTER, md);
writeTypeAndValue(sb, scope, "_total", metric.getCount(), COUNTER, md, false);
writeMeterRateValues(sb, scope, metric, md);
}

private void writeMeterRateValues(StringBuffer sb, MetricRegistry.Type scope, Metered metric, Metadata md) {
writeTypeAndValue(sb, scope, "_rate_per_second", metric.getMeanRate(), GAUGE, md);
writeTypeAndValue(sb, scope, "_one_min_rate_per_second", metric.getOneMinuteRate(), GAUGE, md);
writeTypeAndValue(sb, scope, "_five_min_rate_per_second", metric.getFiveMinuteRate(), GAUGE, md);
writeTypeAndValue(sb, scope, "_fifteen_min_rate_per_second", metric.getFifteenMinuteRate(), GAUGE, md);
writeTypeAndValue(sb, scope, "_rate_per_second", metric.getMeanRate(), GAUGE, md, false);
writeTypeAndValue(sb, scope, "_one_min_rate_per_second", metric.getOneMinuteRate(), GAUGE, md, false);
writeTypeAndValue(sb, scope, "_five_min_rate_per_second", metric.getFiveMinuteRate(), GAUGE, md, false);
writeTypeAndValue(sb, scope, "_fifteen_min_rate_per_second", metric.getFifteenMinuteRate(), GAUGE, md, false);
}

private void writeTypeAndValue(StringBuffer sb, MetricRegistry.Type scope, String suffix, double valueRaw, String type, Metadata md) {
private void writeTypeAndValue(StringBuffer sb, MetricRegistry.Type scope, String suffix, double valueRaw, String type, Metadata md, boolean performScaling) {
String key = md.getName();
writeTypeLine(sb, scope, key, md, suffix, type);
writeValueLine(sb, scope, suffix, valueRaw, md);
writeValueLine(sb, scope, suffix, valueRaw, md, null, performScaling);
}

private void writeValueLine(StringBuffer sb, MetricRegistry.Type scope, String suffix, double valueRaw, Metadata md) {
Expand All @@ -252,7 +252,7 @@ private void writeValueLine(StringBuffer sb,
double valueRaw,
Metadata md,
Tag extraTag,
boolean scaled) {
boolean performScaling) {
String name = md.getName();
name = getPrometheusMetricName(name);
fillBaseName(sb, scope, name);
Expand All @@ -270,9 +270,18 @@ private void writeValueLine(StringBuffer sb,
}

sb.append(SPACE);
Double value = scaled ? PrometheusUnit.scaleToBase(md.getUnit(), valueRaw) : valueRaw;
sb.append(value).append(LF);

Double value;
if(performScaling) {
String scaleFrom = "nanoseconds";
if(md.getTypeRaw() == MetricType.HISTOGRAM)
// for histograms, internally the data is stored using the metric's unit
scaleFrom = md.getUnit();
value = PrometheusUnit.scaleToBase(scaleFrom, valueRaw);
} else {
value = valueRaw;
}
sb.append(value).append(LF);
}

private void addTags(StringBuffer sb, Map<String, String> tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ private PrometheusUnit() {
}


/**
* Determines the basic unit to be used by Prometheus exporter based on the input unit from parameter.
* That is:
* - for memory size units, returns "bytes"
* - for time units, returns "seconds"
* - for any other unit, returns the input unit itself
*/
public static String getBaseUnitAsPrometheusString(String unit) {

String out;
Expand Down Expand Up @@ -88,11 +95,18 @@ public static String getBaseUnitAsPrometheusString(String unit) {
return out;
}

public static Double scaleToBase(String unit, Double value) {
/**
* Scales the value (time or memory size) interpreted using inputUnit to the base unit for Prometheus exporter
* That means:
* - values for memory size units are scaled to bytes
* - values for time units are scaled to seconds
* - values for other units are returned unchanged
*/
public static Double scaleToBase(String inputUnit, Double value) {

Double out;

switch (unit) {
switch (inputUnit) {

case MetricUnits.BITS:
out = value / 8;
Expand Down
Loading