Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 8d5e708

Browse files
authored
Add stats examples (#210)
* Add stats examples * Use high-resolution real time (process.hrtime) to record latency value
1 parent e5281f4 commit 8d5e708

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed
220 KB
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright 2018, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// TODO(mayurkale): Add example.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* Copyright 2018, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This is an example of exporting a custom metric from
19+
* OpenCensus to Stackdriver.
20+
*/
21+
22+
const { Stats, MeasureUnit, AggregationType } = require("@opencensus/core");
23+
const {
24+
StackdriverStatsExporter
25+
} = require("@opencensus/exporter-stackdriver");
26+
27+
const fs = require("fs");
28+
const readline = require("readline");
29+
30+
// Create the Stats manager
31+
const stats = new Stats();
32+
33+
// [START setup_exporter]
34+
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
35+
// Exporters use Application Default Credentials to authenticate.
36+
// See https://developers.google.com/identity/protocols/application-default-credentials
37+
// for more details.
38+
// Add your project id to the Stackdriver options
39+
const exporter = new StackdriverStatsExporter({ projectId: "your-project-id" });
40+
41+
// Pass the created exporter to Stats
42+
stats.registerExporter(exporter);
43+
// [END setup_exporter]
44+
45+
// The latency in milliseconds
46+
const mLatencyMs = stats.createMeasureDouble(
47+
"repl/latency",
48+
MeasureUnit.MS,
49+
"The latency in milliseconds per REPL loop"
50+
);
51+
52+
// Counts/groups the lengths of lines read in.
53+
const mLineLengths = stats.createMeasureInt64(
54+
"repl/line_lengths",
55+
MeasureUnit.BYTE,
56+
"The distribution of line lengths"
57+
);
58+
59+
// Create a stream to read our file
60+
const stream = fs.createReadStream("./test.txt");
61+
62+
// Create an interface to read and process our file line by line
63+
const lineReader = readline.createInterface({ input: stream });
64+
65+
const tagKey = "method";
66+
67+
// Register the view.
68+
const latencyView = stats.createView(
69+
"demo/latency",
70+
mLatencyMs,
71+
AggregationType.DISTRIBUTION,
72+
[tagKey],
73+
"The distribution of the repl latencies",
74+
// Latency in buckets:
75+
// [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
76+
[0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000, 6000]
77+
);
78+
79+
// Register the view.
80+
const lineCountView = stats.createView(
81+
"demo/lines_in",
82+
mLineLengths,
83+
AggregationType.COUNT,
84+
[tagKey],
85+
"The number of lines from standard input"
86+
);
87+
88+
// Register the view.
89+
const lineLengthView = stats.createView(
90+
"demo/line_lengths",
91+
mLineLengths,
92+
AggregationType.DISTRIBUTION,
93+
[tagKey],
94+
"Groups the lengths of keys in buckets",
95+
// Bucket Boudaries:
96+
// [>=0B, >=5B, >=10B, >=15B, >=20B, >=40B, >=60B, >=80, >=100B, >=200B, >=400, >=600, >=800, >=1000]
97+
[0, 5, 10, 15, 20, 40, 60, 80, 100, 200, 400, 600, 800, 1000]
98+
);
99+
100+
// The begining of our REPL loop
101+
let [_, startNanoseconds] = process.hrtime();
102+
let endNanoseconds;
103+
104+
// REPL is the read, evaluate, print and loop
105+
lineReader.on("line", function(line) {
106+
// Read
107+
try {
108+
const processedLine = processLine(line); // Evaluate
109+
console.log(processedLine); // Print
110+
111+
// Registers the end of our REPL
112+
[_, endNanoseconds] = process.hrtime();
113+
114+
const tags = { method: "repl", status: "OK" };
115+
116+
stats.record({
117+
measure: mLineLengths,
118+
tags,
119+
value: processedLine.length
120+
});
121+
122+
stats.record({
123+
measure: mLatencyMs,
124+
tags,
125+
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
126+
});
127+
} catch (err) {
128+
const errTags = { method: "repl", status: "ERROR", error: err.message };
129+
stats.record({
130+
measure: mLatencyMs,
131+
errTags,
132+
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
133+
});
134+
}
135+
136+
// Restarts the start time for the REPL
137+
startNanoseconds = endNanoseconds;
138+
});
139+
140+
/**
141+
* The default export interval is 60 seconds. The thread with the
142+
* StackdriverStatsExporter must live for at least the interval past any
143+
* metrics that must be collected, or some risk being lost if they are recorded
144+
* after the last export.
145+
*/
146+
setTimeout(function() {
147+
console.log("Completed.");
148+
}, 60 * 1000);
149+
150+
/**
151+
* Takes a line and process it.
152+
* @param {string} line The line to process
153+
*/
154+
function processLine(line) {
155+
// Currently, it just capitalizes it.
156+
return line.toUpperCase();
157+
}
158+
159+
/**
160+
* Converts to milliseconds.
161+
* @param {number} endNanoseconds The end time of REPL.
162+
* @param {number} startNanoseconds The start time of REPL.
163+
*/
164+
function sinceInMilliseconds(endNanoseconds, startNanoseconds) {
165+
return (endNanoseconds - startNanoseconds) / 1e6;
166+
}

examples/stats/exporter/test.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
2+
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
3+
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
4+
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
5+
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
6+
culpa qui officia deserunt mollit anim id est laborum.

examples/stats/helloworld/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright 2018, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// TODO(mayurkale): Add basic helloworld example.

0 commit comments

Comments
 (0)