1919 * OpenCensus to Stackdriver.
2020 */
2121
22- const { globalStats, MeasureUnit, AggregationType, TagMap } = require ( " @opencensus/core" ) ;
22+ const { globalStats, MeasureUnit, AggregationType, TagMap } = require ( ' @opencensus/core' ) ;
2323const { StackdriverStatsExporter } =
24- require ( " @opencensus/exporter-stackdriver" ) ;
24+ require ( ' @opencensus/exporter-stackdriver' ) ;
2525
26- const fs = require ( "fs" ) ;
27- const readline = require ( " readline" ) ;
26+ const fs = require ( 'fs' ) ;
27+ const readline = require ( ' readline' ) ;
2828
2929// [START setup_exporter]
3030// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
@@ -38,8 +38,7 @@ const projectId = process.env.GOOGLE_PROJECT_ID;
3838// GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
3939// Not this code itself. Checking for existence here but not retaining (as not needed)
4040if ( ! 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' ) ;
4342}
4443const exporter = new StackdriverStatsExporter ( { projectId : projectId } ) ;
4544
@@ -49,61 +48,61 @@ globalStats.registerExporter(exporter);
4948
5049// The latency in milliseconds
5150const mLatencyMs = globalStats . createMeasureDouble (
52- " repl/latency" ,
51+ ' repl/latency' ,
5352 MeasureUnit . MS ,
54- " The latency in milliseconds per REPL loop"
53+ ' The latency in milliseconds per REPL loop'
5554) ;
5655
5756// Counts/groups the lengths of lines read in.
5857const mLineLengths = globalStats . createMeasureInt64 (
59- " repl/line_lengths" ,
58+ ' repl/line_lengths' ,
6059 MeasureUnit . BYTE ,
61- " The distribution of line lengths"
60+ ' The distribution of line lengths'
6261) ;
6362
6463// Create a stream to read our file
65- const stream = fs . createReadStream ( " ./test.txt" ) ;
64+ const stream = fs . createReadStream ( ' ./test.txt' ) ;
6665
6766// Create an interface to read and process our file line by line
6867const lineReader = readline . createInterface ( { input : stream } ) ;
6968
70- const methodKey = { name : " method" } ;
71- const statusKey = { name : " status" } ;
69+ const methodKey = { name : ' method' } ;
70+ const statusKey = { name : ' status' } ;
7271const tagKeys = [ methodKey , statusKey ] ;
7372
7473// Create & Register the view.
7574const latencyView = globalStats . createView (
76- " demo/latency" ,
75+ ' demo/latency' ,
7776 mLatencyMs ,
7877 AggregationType . DISTRIBUTION ,
7978 tagKeys ,
80- " The distribution of the repl latencies" ,
79+ ' The distribution of the repl latencies' ,
8180 // 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 ]
8483) ;
8584globalStats . registerView ( latencyView ) ;
8685
8786// Create & Register the view.
8887const lineCountView = globalStats . createView (
89- " demo/lines_in" ,
88+ ' demo/lines_in' ,
9089 mLineLengths ,
9190 AggregationType . COUNT ,
9291 tagKeys ,
93- " The number of lines from standard input"
92+ ' The number of lines from standard input'
9493) ;
9594globalStats . registerView ( lineCountView ) ;
9695
9796// Create & Register the view.
9897const lineLengthView = globalStats . createView (
99- " demo/line_lengths" ,
98+ ' demo/line_lengths' ,
10099 mLineLengths ,
101100 AggregationType . DISTRIBUTION ,
102101 tagKeys ,
103- " Groups the lengths of keys in buckets" ,
102+ ' Groups the lengths of keys in buckets' ,
104103 // 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 ]
107106) ;
108107globalStats . registerView ( lineLengthView ) ;
109108
@@ -112,7 +111,7 @@ let [_, startNanoseconds] = process.hrtime();
112111let endNanoseconds ;
113112
114113// REPL is the read, evaluate, print and loop
115- lineReader . on ( " line" , function ( line ) {
114+ lineReader . on ( ' line' , function ( line ) {
116115 // Read
117116 try {
118117 const processedLine = processLine ( line ) ; // Evaluate
@@ -122,8 +121,8 @@ lineReader.on("line", function(line) {
122121 [ _ , endNanoseconds ] = process . hrtime ( ) ;
123122
124123 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' } ) ;
127126
128127 globalStats . record ( [ {
129128 measure : mLineLengths ,
@@ -134,13 +133,12 @@ lineReader.on("line", function(line) {
134133 measure : mLatencyMs ,
135134 value : sinceInMilliseconds ( endNanoseconds , startNanoseconds )
136135 } ] , tags ) ;
137-
138136 } catch ( err ) {
139137 console . log ( err ) ;
140138
141139 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' } ) ;
144142 globalStats . record ( [ {
145143 measure : mLatencyMs ,
146144 value : sinceInMilliseconds ( endNanoseconds , startNanoseconds )
@@ -157,15 +155,15 @@ lineReader.on("line", function(line) {
157155 * metrics that must be collected, or some risk being lost if they are recorded
158156 * after the last export.
159157 */
160- setTimeout ( function ( ) {
161- console . log ( " Completed." ) ;
158+ setTimeout ( function ( ) {
159+ console . log ( ' Completed.' ) ;
162160} , 60 * 1000 ) ;
163161
164162/**
165163 * Takes a line and process it.
166164 * @param {string } line The line to process
167165 */
168- function processLine ( line ) {
166+ function processLine ( line ) {
169167 // Currently, it just capitalizes it.
170168 return line . toUpperCase ( ) ;
171169}
@@ -175,6 +173,6 @@ function processLine(line) {
175173 * @param {number } endNanoseconds The end time of REPL.
176174 * @param {number } startNanoseconds The start time of REPL.
177175 */
178- function sinceInMilliseconds ( endNanoseconds , startNanoseconds ) {
176+ function sinceInMilliseconds ( endNanoseconds , startNanoseconds ) {
179177 return ( endNanoseconds - startNanoseconds ) / 1e6 ;
180178}
0 commit comments