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

Commit cb97d3f

Browse files
authored
Update examples to work with global stats instance (#339)
* update stackdriver and prometheus example * update web client example * update core ReadMe * update prometheus example * update stackdriver example * fix review comments * add keywords, badge and MeasureUnits
1 parent 95be16e commit cb97d3f

File tree

10 files changed

+208
-131
lines changed

10 files changed

+208
-131
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# OpenCensus Libraries for Node.js
2-
[![Gitter chat][gitter-image]][gitter-url] ![Node Version][node-img] [![NPM Published Version][npm-img]][npm-url] ![Apache License][license-image]
2+
[![Gitter chat][gitter-image]][gitter-url] ![Node Version][node-img] [![NPM Published Version][npm-img]][npm-url] [![codecov][codecov-image]][codecov-url] ![Apache License][license-image]
33

44
OpenCensus Node.js is an implementation of OpenCensus, a toolkit for collecting application performance and behavior monitoring data. Right now OpenCensus for Node.js supports custom tracing and automatic tracing for HTTP and HTTPS. Please visit the [OpenCensus Node.js package](https://github.com/census-instrumentation/opencensus-node/tree/master/packages/opencensus-nodejs) for usage.
55

@@ -73,6 +73,8 @@ months before removing it, if possible.
7373
- For more information on OpenCensus, visit: <https://opencensus.io/>
7474
- For help or feedback on this project, join us on [gitter](https://gitter.im/census-instrumentation/Lobby)
7575

76+
[codecov-image]: https://codecov.io/gh/census-instrumentation/opencensus-node/branch/master/graph/badge.svg
77+
[codecov-url]: https://codecov.io/gh/census-instrumentation/opencensus-node
7678
[gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg
7779
[gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
7880
[npm-url]: https://www.npmjs.com/package/@opencensus/exporter-prometheus

examples/stats/exporter/prometheus.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919
* OpenCensus to Prometheus.
2020
*/
2121

22-
const { Stats, MeasureUnit, AggregationType } = require("@opencensus/core");
22+
const { globalStats, MeasureUnit, AggregationType, TagMap } = require("@opencensus/core");
2323
const { PrometheusStatsExporter } = require("@opencensus/exporter-prometheus");
2424

2525
const fs = require("fs");
2626
const readline = require("readline");
2727

28-
// Create the Stats manager
29-
const stats = new Stats();
30-
3128
// [START setup_exporter]
3229
// Enable OpenCensus exporters to export metrics to Prometheus Monitoring.
3330
const exporter = new PrometheusStatsExporter({
@@ -36,19 +33,19 @@ const exporter = new PrometheusStatsExporter({
3633
startServer: true
3734
});
3835

39-
// Pass the created exporter to Stats
40-
stats.registerExporter(exporter);
36+
// Pass the created exporter to global Stats
37+
globalStats.registerExporter(exporter);
4138
// [END setup_exporter]
4239

4340
// The latency in milliseconds
44-
const mLatencyMs = stats.createMeasureDouble(
41+
const mLatencyMs = globalStats.createMeasureDouble(
4542
"repl/latency",
4643
MeasureUnit.MS,
4744
"The latency in milliseconds per REPL loop"
4845
);
4946

5047
// Counts/groups the lengths of lines read in.
51-
const mLineLengths = stats.createMeasureInt64(
48+
const mLineLengths = globalStats.createMeasureInt64(
5249
"repl/line_lengths",
5350
MeasureUnit.BYTE,
5451
"The distribution of line lengths"
@@ -60,10 +57,12 @@ const stream = fs.createReadStream("./test.txt");
6057
// Create an interface to read and process our file line by line
6158
const lineReader = readline.createInterface({ input: stream });
6259

63-
const tagKeys = ["method", "status"];
60+
const methodKey = { name: "method" };
61+
const statusKey = { name: "status" };
62+
const tagKeys = [methodKey, statusKey];
6463

6564
// Create & Register the view.
66-
/*const latencyView = */stats.createView(
65+
const latencyView = globalStats.createView(
6766
"demo/latency",
6867
mLatencyMs,
6968
AggregationType.DISTRIBUTION,
@@ -73,20 +72,20 @@ const tagKeys = ["method", "status"];
7372
// [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
7473
[0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000, 6000]
7574
);
76-
//stats.registerView(latencyView);
75+
globalStats.registerView(latencyView);
7776

7877
// Create & Register the view.
79-
/*const lineCountView = */stats.createView(
78+
const lineCountView = globalStats.createView(
8079
"demo/lines_in",
8180
mLineLengths,
8281
AggregationType.COUNT,
8382
tagKeys,
8483
"The number of lines from standard input"
8584
);
86-
//stats.registerView(lineCountView);
85+
globalStats.registerView(lineCountView);
8786

8887
// Create & Register the view.
89-
/*const lineLengthView = */stats.createView(
88+
const lineLengthView = globalStats.createView(
9089
"demo/line_lengths",
9190
mLineLengths,
9291
AggregationType.DISTRIBUTION,
@@ -96,7 +95,7 @@ const tagKeys = ["method", "status"];
9695
// [>=0B, >=5B, >=10B, >=15B, >=20B, >=40B, >=60B, >=80, >=100B, >=200B, >=400, >=600, >=800, >=1000]
9796
[0, 5, 10, 15, 20, 40, 60, 80, 100, 200, 400, 600, 800, 1000]
9897
);
99-
//stats.registerView(lineLengthView);
98+
globalStats.registerView(lineLengthView);
10099

101100
// The begining of our REPL loop
102101
let [_, startNanoseconds] = process.hrtime();
@@ -112,26 +111,31 @@ lineReader.on("line", function(line) {
112111
// Registers the end of our REPL
113112
[_, endNanoseconds] = process.hrtime();
114113

115-
const tags = { method: "repl", status: "OK" };
114+
const tags = new TagMap();
115+
tags.set(methodKey, { value: "REPL" });
116+
tags.set(statusKey, { value: "OK" });
116117

117-
stats.record({
118+
globalStats.record([{
118119
measure: mLineLengths,
119120
tags,
120121
value: processedLine.length
121-
});
122+
}], tags);
122123

123-
stats.record({
124+
globalStats.record([{
124125
measure: mLatencyMs,
125126
tags,
126127
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
127-
});
128+
}], tags);
128129
} catch (err) {
129-
const errTags = { method: "repl", status: "ERROR", error: err.message };
130-
stats.record({
130+
console.log(err);
131+
132+
const errTags = new TagMap();
133+
errTags.set(methodKey, { value: "repl" });
134+
errTags.set(statusKey, { value: "ERROR" });
135+
globalStats.record([{
131136
measure: mLatencyMs,
132-
errTags,
133137
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
134-
});
138+
}], errTags);
135139
}
136140

137141
// Restarts the start time for the REPL
@@ -155,4 +159,3 @@ function processLine(line) {
155159
function sinceInMilliseconds(endNanoseconds, startNanoseconds) {
156160
return (endNanoseconds - startNanoseconds) / 1e6;
157161
}
158-

examples/stats/exporter/stackdriver.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@
1919
* OpenCensus to Stackdriver.
2020
*/
2121

22-
const { Stats, MeasureUnit, AggregationType } = require("@opencensus/core");
23-
const {
24-
StackdriverStatsExporter
25-
} = require("@opencensus/exporter-stackdriver");
22+
const { globalStats, MeasureUnit, AggregationType, TagMap } = require("@opencensus/core");
23+
const { StackdriverStatsExporter } =
24+
require("@opencensus/exporter-stackdriver");
2625

2726
const fs = require("fs");
2827
const readline = require("readline");
2928

30-
// Create the Stats manager
31-
const stats = new Stats();
32-
3329
// [START setup_exporter]
3430
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
3531
// Exporters use Application Default Credentials (ADCs) to authenticate.
@@ -47,19 +43,19 @@ if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
4743
}
4844
const exporter = new StackdriverStatsExporter({ projectId: projectId });
4945

50-
// Pass the created exporter to Stats
51-
stats.registerExporter(exporter);
46+
// Pass the created exporter to global Stats
47+
globalStats.registerExporter(exporter);
5248
// [END setup_exporter]
5349

5450
// The latency in milliseconds
55-
const mLatencyMs = stats.createMeasureDouble(
51+
const mLatencyMs = globalStats.createMeasureDouble(
5652
"repl/latency",
5753
MeasureUnit.MS,
5854
"The latency in milliseconds per REPL loop"
5955
);
6056

6157
// Counts/groups the lengths of lines read in.
62-
const mLineLengths = stats.createMeasureInt64(
58+
const mLineLengths = globalStats.createMeasureInt64(
6359
"repl/line_lengths",
6460
MeasureUnit.BYTE,
6561
"The distribution of line lengths"
@@ -71,10 +67,12 @@ const stream = fs.createReadStream("./test.txt");
7167
// Create an interface to read and process our file line by line
7268
const lineReader = readline.createInterface({ input: stream });
7369

74-
const tagKeys = ["method", "status"];
70+
const methodKey = { name: "method" };
71+
const statusKey = { name: "status" };
72+
const tagKeys = [methodKey, statusKey];
7573

76-
// Create the view.
77-
stats.createView(
74+
// Create & Register the view.
75+
const latencyView = globalStats.createView(
7876
"demo/latency",
7977
mLatencyMs,
8078
AggregationType.DISTRIBUTION,
@@ -84,18 +82,20 @@ stats.createView(
8482
// [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
8583
[0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000, 6000]
8684
);
85+
globalStats.registerView(latencyView);
8786

88-
// Create the view.
89-
stats.createView(
87+
// Create & Register the view.
88+
const lineCountView = globalStats.createView(
9089
"demo/lines_in",
9190
mLineLengths,
9291
AggregationType.COUNT,
9392
tagKeys,
9493
"The number of lines from standard input"
9594
);
95+
globalStats.registerView(lineCountView);
9696

97-
// Create the view.
98-
stats.createView(
97+
// Create & Register the view.
98+
const lineLengthView = globalStats.createView(
9999
"demo/line_lengths",
100100
mLineLengths,
101101
AggregationType.DISTRIBUTION,
@@ -105,6 +105,7 @@ stats.createView(
105105
// [>=0B, >=5B, >=10B, >=15B, >=20B, >=40B, >=60B, >=80, >=100B, >=200B, >=400, >=600, >=800, >=1000]
106106
[0, 5, 10, 15, 20, 40, 60, 80, 100, 200, 400, 600, 800, 1000]
107107
);
108+
globalStats.registerView(lineLengthView);
108109

109110
// The begining of our REPL loop
110111
let [_, startNanoseconds] = process.hrtime();
@@ -120,26 +121,30 @@ lineReader.on("line", function(line) {
120121
// Registers the end of our REPL
121122
[_, endNanoseconds] = process.hrtime();
122123

123-
const tags = { method: "repl", status: "OK" };
124+
const tags = new TagMap();
125+
tags.set(methodKey, { value: "REPL" });
126+
tags.set(statusKey, { value: "OK" });
124127

125-
stats.record({
128+
globalStats.record([{
126129
measure: mLineLengths,
127-
tags,
128130
value: processedLine.length
129-
});
131+
}], tags);
130132

131-
stats.record({
133+
globalStats.record([{
132134
measure: mLatencyMs,
133-
tags,
134135
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
135-
});
136+
}], tags);
137+
136138
} catch (err) {
137-
const errTags = { method: "repl", status: "ERROR", error: err.message };
138-
stats.record({
139+
console.log(err);
140+
141+
const errTags = new TagMap();
142+
errTags.set(methodKey, { value: "repl" });
143+
errTags.set(statusKey, { value: "ERROR" });
144+
globalStats.record([{
139145
measure: mLatencyMs,
140-
errTags,
141146
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
142-
});
147+
}], errTags);
143148
}
144149

145150
// Restarts the start time for the REPL

examples/stats/web_client_monitoring/app.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const assert = require('assert');
2525
const process = require("process");
2626
const bodyParser = require('body-parser');
2727
// [START web_client_monitoring_imports]
28-
const { Stats, MeasureUnit, AggregationType } = require('@opencensus/core');
28+
const { globalStats, MeasureUnit, AggregationType, TagMap } = require('@opencensus/core');
2929
const { StackdriverStatsExporter } = require('@opencensus/exporter-stackdriver');
3030
// [END web_client_monitoring_imports]
3131

@@ -41,35 +41,38 @@ console.log(`Sending metrics data to project: ${project}`);
4141

4242
// OpenCensus setup
4343
// [START web_client_monitoring_ocsetup]
44-
const stats = new Stats();
4544
const exporter = new StackdriverStatsExporter({projectId: project});
46-
stats.registerExporter(exporter);
47-
const mLatencyMs = stats.createMeasureDouble("webmetrics/latency",
45+
globalStats.registerExporter(exporter);
46+
const mLatencyMs = globalStats.createMeasureDouble("webmetrics/latency",
4847
MeasureUnit.MS,
4948
"Latency related to page loading");
50-
const mClickCount = stats.createMeasureInt64("webmetrics/click_count",
49+
const mClickCount = globalStats.createMeasureInt64("webmetrics/click_count",
5150
MeasureUnit.UNIT,
5251
"Number of clicks");
5352
const buckets = [0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80,
5453
100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000,
5554
5000, 10000, 20000, 50000, 100000];
56-
const tagPhase = "phase";
57-
const tagClient = "client";
58-
const latencyView = stats.createView(
55+
56+
const tagPhase = { name: "phase" };
57+
const tagClient = { name: "client" };
58+
59+
const latencyView = globalStats.createView(
5960
"webmetrics/latency",
6061
mLatencyMs,
6162
AggregationType.DISTRIBUTION,
6263
[tagPhase, tagClient],
6364
"Distribution of latencies",
6465
buckets
6566
);
66-
const clickCountView = stats.createView(
67+
globalStats.registerView(latencyView);
68+
const clickCountView = globalStats.createView(
6769
"webmetrics/click_count",
6870
mClickCount,
6971
AggregationType.COUNT,
7072
[tagClient],
7173
"The number of button clicks"
7274
);
75+
globalStats.registerView(clickCountView);
7376
// [END web_client_monitoring_ocsetup]
7477

7578
// Process the metrics data posted to the server
@@ -86,32 +89,35 @@ app.post("/metrics", (req, res) => {
8689
const valueDNSLookup = "dns_lookup";
8790
const valueLoad = "load";
8891
const valueWeb = "web";
89-
let tags = { phase: valueDNSLookup, client: valueWeb };
92+
93+
const tags = new TagMap();
94+
tags.set(tagPhase, { value: valueDNSLookup });
95+
tags.set(tagClient, { value: valueWeb });
9096
// [START web_client_monitoring_record]
9197
try {
92-
stats.record({
98+
globalStats.record([{
9399
measure: mLatencyMs,
94-
tags,
95-
value: dnsTime
96-
});
97-
tags = { phase: valueTLSNegotiation, client: valueWeb };
98-
stats.record({
100+
value: 1
101+
}], tags);
102+
103+
tags.set(tagPhase, { value: valueTLSNegotiation });
104+
globalStats.record([{
99105
measure: mLatencyMs,
100-
tags,
101-
value: connectTime
102-
});
103-
tags = { phase: valueLoad, client: valueWeb };
104-
stats.record({
106+
value: 1
107+
}], tags);
108+
109+
tags.set(tagPhase, { value: valueLoad });
110+
globalStats.record([{
105111
measure: mLatencyMs,
106-
tags,
107-
value: totalTime
108-
});
109-
tags = { client: valueWeb };
110-
stats.record({
112+
value: 1
113+
}], tags);
114+
115+
const tags1 = new TagMap();
116+
tags1.set(tagClient, { value: valueWeb });
117+
globalStats.record([{
111118
measure: mClickCount,
112-
tags,
113-
value: clickCount
114-
});
119+
value: 1
120+
}], tags1);
115121
res.status(200).send("Received").end();
116122
console.log('Competed recording metrics');
117123
} catch (err) {

0 commit comments

Comments
 (0)