19
19
* OpenCensus to Stackdriver.
20
20
*/
21
21
22
- const { globalStats, MeasureUnit, AggregationType, TagMap } = require ( " @opencensus/core" ) ;
22
+ const { globalStats, MeasureUnit, AggregationType, TagMap } = require ( ' @opencensus/core' ) ;
23
23
const { StackdriverStatsExporter } =
24
- require ( " @opencensus/exporter-stackdriver" ) ;
24
+ require ( ' @opencensus/exporter-stackdriver' ) ;
25
25
26
- const fs = require ( "fs" ) ;
27
- const readline = require ( " readline" ) ;
26
+ const fs = require ( 'fs' ) ;
27
+ const readline = require ( ' readline' ) ;
28
28
29
29
// [START setup_exporter]
30
30
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
@@ -38,8 +38,7 @@ const projectId = process.env.GOOGLE_PROJECT_ID;
38
38
// GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
39
39
// Not this code itself. Checking for existence here but not retaining (as not needed)
40
40
if ( ! projectId || ! process . env . GOOGLE_APPLICATION_CREDENTIALS ) {
41
- // Unable to proceed without a Project ID
42
- process . exit ( 1 ) ;
41
+ throw Error ( 'Unable to proceed without a Project ID' ) ;
43
42
}
44
43
const exporter = new StackdriverStatsExporter ( { projectId : projectId } ) ;
45
44
@@ -49,61 +48,61 @@ globalStats.registerExporter(exporter);
49
48
50
49
// The latency in milliseconds
51
50
const mLatencyMs = globalStats . createMeasureDouble (
52
- " repl/latency" ,
51
+ ' repl/latency' ,
53
52
MeasureUnit . MS ,
54
- " The latency in milliseconds per REPL loop"
53
+ ' The latency in milliseconds per REPL loop'
55
54
) ;
56
55
57
56
// Counts/groups the lengths of lines read in.
58
57
const mLineLengths = globalStats . createMeasureInt64 (
59
- " repl/line_lengths" ,
58
+ ' repl/line_lengths' ,
60
59
MeasureUnit . BYTE ,
61
- " The distribution of line lengths"
60
+ ' The distribution of line lengths'
62
61
) ;
63
62
64
63
// Create a stream to read our file
65
- const stream = fs . createReadStream ( " ./test.txt" ) ;
64
+ const stream = fs . createReadStream ( ' ./test.txt' ) ;
66
65
67
66
// Create an interface to read and process our file line by line
68
67
const lineReader = readline . createInterface ( { input : stream } ) ;
69
68
70
- const methodKey = { name : " method" } ;
71
- const statusKey = { name : " status" } ;
69
+ const methodKey = { name : ' method' } ;
70
+ const statusKey = { name : ' status' } ;
72
71
const tagKeys = [ methodKey , statusKey ] ;
73
72
74
73
// Create & Register the view.
75
74
const latencyView = globalStats . createView (
76
- " demo/latency" ,
75
+ ' demo/latency' ,
77
76
mLatencyMs ,
78
77
AggregationType . DISTRIBUTION ,
79
78
tagKeys ,
80
- " The distribution of the repl latencies" ,
79
+ ' The distribution of the repl latencies' ,
81
80
// Latency in buckets:
82
- // [>=0ms, >= 25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
83
- [ 0 , 25 , 50 , 75 , 100 , 200 , 400 , 600 , 800 , 1000 , 2000 , 4000 , 6000 ]
81
+ // [>=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
82
+ [ 25 , 50 , 75 , 100 , 200 , 400 , 600 , 800 , 1000 , 2000 , 4000 , 6000 ]
84
83
) ;
85
84
globalStats . registerView ( latencyView ) ;
86
85
87
86
// Create & Register the view.
88
87
const lineCountView = globalStats . createView (
89
- " demo/lines_in" ,
88
+ ' demo/lines_in' ,
90
89
mLineLengths ,
91
90
AggregationType . COUNT ,
92
91
tagKeys ,
93
- " The number of lines from standard input"
92
+ ' The number of lines from standard input'
94
93
) ;
95
94
globalStats . registerView ( lineCountView ) ;
96
95
97
96
// Create & Register the view.
98
97
const lineLengthView = globalStats . createView (
99
- " demo/line_lengths" ,
98
+ ' demo/line_lengths' ,
100
99
mLineLengths ,
101
100
AggregationType . DISTRIBUTION ,
102
101
tagKeys ,
103
- " Groups the lengths of keys in buckets" ,
102
+ ' Groups the lengths of keys in buckets' ,
104
103
// Bucket Boudaries:
105
- // [>=0B, >= 5B, >=10B, >=15B, >=20B, >=40B, >=60B, >=80, >=100B, >=200B, >=400, >=600, >=800, >=1000]
106
- [ 0 , 5 , 10 , 15 , 20 , 40 , 60 , 80 , 100 , 200 , 400 , 600 , 800 , 1000 ]
104
+ // [>=5B, >=10B, >=15B, >=20B, >=40B, >=60B, >=80, >=100B, >=200B, >=400, >=600, >=800, >=1000]
105
+ [ 5 , 10 , 15 , 20 , 40 , 60 , 80 , 100 , 200 , 400 , 600 , 800 , 1000 ]
107
106
) ;
108
107
globalStats . registerView ( lineLengthView ) ;
109
108
@@ -112,7 +111,7 @@ let [_, startNanoseconds] = process.hrtime();
112
111
let endNanoseconds ;
113
112
114
113
// REPL is the read, evaluate, print and loop
115
- lineReader . on ( " line" , function ( line ) {
114
+ lineReader . on ( ' line' , function ( line ) {
116
115
// Read
117
116
try {
118
117
const processedLine = processLine ( line ) ; // Evaluate
@@ -122,8 +121,8 @@ lineReader.on("line", function(line) {
122
121
[ _ , endNanoseconds ] = process . hrtime ( ) ;
123
122
124
123
const tags = new TagMap ( ) ;
125
- tags . set ( methodKey , { value : " REPL" } ) ;
126
- tags . set ( statusKey , { value : "OK" } ) ;
124
+ tags . set ( methodKey , { value : ' REPL' } ) ;
125
+ tags . set ( statusKey , { value : 'OK' } ) ;
127
126
128
127
globalStats . record ( [ {
129
128
measure : mLineLengths ,
@@ -134,13 +133,12 @@ lineReader.on("line", function(line) {
134
133
measure : mLatencyMs ,
135
134
value : sinceInMilliseconds ( endNanoseconds , startNanoseconds )
136
135
} ] , tags ) ;
137
-
138
136
} catch ( err ) {
139
137
console . log ( err ) ;
140
138
141
139
const errTags = new TagMap ( ) ;
142
- errTags . set ( methodKey , { value : " repl" } ) ;
143
- errTags . set ( statusKey , { value : " ERROR" } ) ;
140
+ errTags . set ( methodKey , { value : ' repl' } ) ;
141
+ errTags . set ( statusKey , { value : ' ERROR' } ) ;
144
142
globalStats . record ( [ {
145
143
measure : mLatencyMs ,
146
144
value : sinceInMilliseconds ( endNanoseconds , startNanoseconds )
@@ -157,15 +155,15 @@ lineReader.on("line", function(line) {
157
155
* metrics that must be collected, or some risk being lost if they are recorded
158
156
* after the last export.
159
157
*/
160
- setTimeout ( function ( ) {
161
- console . log ( " Completed." ) ;
158
+ setTimeout ( function ( ) {
159
+ console . log ( ' Completed.' ) ;
162
160
} , 60 * 1000 ) ;
163
161
164
162
/**
165
163
* Takes a line and process it.
166
164
* @param {string } line The line to process
167
165
*/
168
- function processLine ( line ) {
166
+ function processLine ( line ) {
169
167
// Currently, it just capitalizes it.
170
168
return line . toUpperCase ( ) ;
171
169
}
@@ -175,6 +173,6 @@ function processLine(line) {
175
173
* @param {number } endNanoseconds The end time of REPL.
176
174
* @param {number } startNanoseconds The start time of REPL.
177
175
*/
178
- function sinceInMilliseconds ( endNanoseconds , startNanoseconds ) {
176
+ function sinceInMilliseconds ( endNanoseconds , startNanoseconds ) {
179
177
return ( endNanoseconds - startNanoseconds ) / 1e6 ;
180
178
}
0 commit comments