Created
August 4, 2018 08:30
-
-
Save skyrocknroll/73f02bc52cf1b14fc9e4a69e0e7963a7 to your computer and use it in GitHub Desktop.
opencensus prometheus java stats exporter
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
package io.opencensus.quickstart; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import io.opencensus.common.Scope; | |
import io.opencensus.exporter.stats.prometheus.PrometheusStatsCollector; | |
import io.opencensus.stats.Aggregation; | |
import io.opencensus.stats.Aggregation.Distribution; | |
import io.opencensus.stats.BucketBoundaries; | |
import io.opencensus.stats.Stats; | |
import io.opencensus.stats.Measure; | |
import io.opencensus.stats.Measure.MeasureLong; | |
import io.opencensus.stats.Measure.MeasureDouble; | |
import io.opencensus.stats.Stats; | |
import io.opencensus.stats.StatsRecorder; | |
import io.opencensus.stats.View; | |
import io.opencensus.tags.Tags; | |
import io.opencensus.tags.Tagger; | |
import io.opencensus.tags.TagContext; | |
import io.opencensus.tags.TagContextBuilder; | |
import io.opencensus.tags.TagKey; | |
import io.opencensus.tags.TagValue; | |
import io.opencensus.stats.View; | |
import io.opencensus.stats.View.Name; | |
import io.opencensus.stats.ViewManager; | |
import io.opencensus.stats.View.AggregationWindow.Cumulative; | |
import io.prometheus.client.exporter.HTTPServer; | |
public class Repl { | |
// The latency in milliseconds | |
private static final MeasureDouble M_LATENCY_MS = MeasureDouble.create("repl/latency", "The latency in milliseconds per REPL loop", "ms"); | |
// Counts the number of lines read in from standard input. | |
private static final MeasureLong M_LINES_COUNT = MeasureLong.create("repl/lines_in", "The number of lines read in", "1"); | |
// Counts the number of non EOF(end-of-file) errors. | |
private static final MeasureLong M_ERRORS = MeasureLong.create("repl/errors", "The number of errors encountered", "1"); | |
// Counts/groups the lengths of lines read in. | |
private static final MeasureLong M_LINE_LENGTHS = MeasureLong.create("repl/line_lengths", "The distribution of line lengths", "By"); | |
// The tag "method" | |
private static final TagKey KEY_METHOD = TagKey.create("method"); | |
private static final Tagger tagger = Tags.getTagger(); | |
private static final StatsRecorder statsRecorder = Stats.getStatsRecorder(); | |
public static void main(String ...args) { | |
registerAllViews(); | |
// Step 1. Enable OpenCensus Metrics. | |
try { | |
PrometheusStatsCollector.createAndRegister(); | |
// Run the server as a daemon on address "localhost:8888" | |
HTTPServer server = new HTTPServer("localhost", 8888, true); | |
} catch (IOException e) { | |
System.err.println("Failed to create and register OpenCensus Stackdriver Trace exporter "+ e); | |
return; | |
} | |
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); | |
while (true) { | |
try { | |
readEvaluateProcess(stdin); | |
} catch (IOException e) { | |
System.err.println("EOF bye "+ e); | |
return; | |
} catch (Exception e) { | |
recordTaggedStat(KEY_METHOD, "repl", M_ERRORS, new Long(1)); | |
} | |
} | |
} | |
private static void recordStat(MeasureLong ml, Long n) { | |
TagContext tctx = tagger.emptyBuilder().build(); | |
try (Scope ss = tagger.withTagContext(tctx)) { | |
statsRecorder.newMeasureMap().put(ml, n).record(); | |
} | |
} | |
private static void recordTaggedStat(TagKey key, String value, MeasureLong ml, Long n) { | |
TagContext tctx = tagger.emptyBuilder().put(key, TagValue.create(value)).build(); | |
try (Scope ss = tagger.withTagContext(tctx)) { | |
statsRecorder.newMeasureMap().put(ml, n).record(); | |
} | |
} | |
private static void recordTaggedStat(TagKey key, String value, MeasureDouble md, Double d) { | |
TagContext tctx = tagger.emptyBuilder().put(key, TagValue.create(value)).build(); | |
try (Scope ss = tagger.withTagContext(tctx)) { | |
statsRecorder.newMeasureMap().put(md, d).record(); | |
} | |
} | |
private static String processLine(String line) { | |
long startTimeNs = System.nanoTime(); | |
try { | |
return line.toUpperCase(); | |
} catch (Exception e) { | |
recordTaggedStat(KEY_METHOD, "processLine", M_ERRORS, new Long(1)); | |
return ""; | |
} finally { | |
long totalTimeNs = System.nanoTime() - startTimeNs; | |
double timespentMs = (new Double(totalTimeNs))/1e6; | |
recordTaggedStat(KEY_METHOD, "processLine", M_LATENCY_MS, timespentMs); | |
} | |
} | |
private static void readEvaluateProcess(BufferedReader in) throws IOException { | |
System.out.print("> "); | |
System.out.flush(); | |
String line = in.readLine(); | |
String processed = processLine(line); | |
System.out.println("< " + processed + "\n"); | |
if (line != null && line.length() > 0) { | |
recordStat(M_LINES_COUNT, new Long(1)); | |
recordStat(M_LINE_LENGTHS, new Long(line.length())); | |
} | |
} | |
private static void registerAllViews() { | |
// Defining the distribution aggregations | |
Aggregation latencyDistribution = Distribution.create(BucketBoundaries.create( | |
Arrays.asList( | |
// [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s] | |
0.0, 25.0, 50.0, 75.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0, 2000.0, 4000.0, 6000.0) | |
)); | |
Aggregation lengthsDistribution = Distribution.create(BucketBoundaries.create( | |
Arrays.asList( | |
// [>=0B, >=5B, >=10B, >=20B, >=40B, >=60B, >=80B, >=100B, >=200B, >=400B, >=600B, >=800B, >=1000B] | |
0.0, 5.0, 10.0, 20.0, 40.0, 60.0, 80.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0) | |
)); | |
// Define the count aggregation | |
Aggregation countAggregation = Aggregation.Count.create(); | |
// So tagKeys | |
List<TagKey> noKeys = new ArrayList<TagKey>(); | |
// Define the views | |
View[] views = new View[]{ | |
View.create(Name.create("demo/latency"), "The distribution of latencies", M_LATENCY_MS, latencyDistribution, Collections.singletonList(KEY_METHOD)), | |
View.create(Name.create("demo/lines_in"), "The number of lines read in from standard input", M_LINES_COUNT, countAggregation, noKeys), | |
View.create(Name.create("demo/errors"), "The number of errors encountered", M_ERRORS, countAggregation, Collections.singletonList(KEY_METHOD)), | |
View.create(Name.create("demo/line_lengths"), "The distribution of line lengths", M_LINE_LENGTHS, lengthsDistribution, noKeys) | |
}; | |
// Create the view manager | |
ViewManager vmgr = Stats.getViewManager(); | |
// Then finally register the views | |
for (View view : views) | |
vmgr.registerView(view); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment