Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OpenCensus Libraries for Node.js
[![Gitter chat][gitter-image]][gitter-url] ![Node Version][node-img] [![NPM Published Version][npm-img]][npm-url] ![Apache License][license-image]
[![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]

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.

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

[codecov-image]: https://codecov.io/gh/census-instrumentation/opencensus-node/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/census-instrumentation/opencensus-node
[gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg
[gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
[npm-url]: https://www.npmjs.com/package/@opencensus/exporter-prometheus
Expand Down
53 changes: 28 additions & 25 deletions examples/stats/exporter/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@
* OpenCensus to Prometheus.
*/

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

const fs = require("fs");
const readline = require("readline");

// Create the Stats manager
const stats = new Stats();

// [START setup_exporter]
// Enable OpenCensus exporters to export metrics to Prometheus Monitoring.
const exporter = new PrometheusStatsExporter({
Expand All @@ -36,19 +33,19 @@ const exporter = new PrometheusStatsExporter({
startServer: true
});

// Pass the created exporter to Stats
stats.registerExporter(exporter);
// Pass the created exporter to global Stats
globalStats.registerExporter(exporter);
// [END setup_exporter]

// The latency in milliseconds
const mLatencyMs = stats.createMeasureDouble(
const mLatencyMs = globalStats.createMeasureDouble(
"repl/latency",
MeasureUnit.MS,
"The latency in milliseconds per REPL loop"
);

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

const tagKeys = ["method", "status"];
const methodKey = { name: "method" };
const statusKey = { name: "status" };
const tagKeys = [methodKey, statusKey];

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

// Create & Register the view.
/*const lineCountView = */stats.createView(
const lineCountView = globalStats.createView(
"demo/lines_in",
mLineLengths,
AggregationType.COUNT,
tagKeys,
"The number of lines from standard input"
);
//stats.registerView(lineCountView);
globalStats.registerView(lineCountView);

// Create & Register the view.
/*const lineLengthView = */stats.createView(
const lineLengthView = globalStats.createView(
"demo/line_lengths",
mLineLengths,
AggregationType.DISTRIBUTION,
Expand All @@ -96,7 +95,7 @@ const tagKeys = ["method", "status"];
// [>=0B, >=5B, >=10B, >=15B, >=20B, >=40B, >=60B, >=80, >=100B, >=200B, >=400, >=600, >=800, >=1000]
[0, 5, 10, 15, 20, 40, 60, 80, 100, 200, 400, 600, 800, 1000]
);
//stats.registerView(lineLengthView);
globalStats.registerView(lineLengthView);

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

const tags = { method: "repl", status: "OK" };
const tags = new TagMap();
tags.set(methodKey, { value: "REPL" });
tags.set(statusKey, { value: "OK" });

stats.record({
globalStats.record([{
measure: mLineLengths,
tags,
value: processedLine.length
});
}], tags);

stats.record({
globalStats.record([{
measure: mLatencyMs,
tags,
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
});
}], tags);
} catch (err) {
const errTags = { method: "repl", status: "ERROR", error: err.message };
stats.record({
console.log(err);

const errTags = new TagMap();
errTags.set(methodKey, { value: "repl" });
errTags.set(statusKey, { value: "ERROR" });
globalStats.record([{
measure: mLatencyMs,
errTags,
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
});
}], errTags);
}

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

63 changes: 34 additions & 29 deletions examples/stats/exporter/stackdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@
* OpenCensus to Stackdriver.
*/

const { Stats, MeasureUnit, AggregationType } = require("@opencensus/core");
const {
StackdriverStatsExporter
} = require("@opencensus/exporter-stackdriver");
const { globalStats, MeasureUnit, AggregationType, TagMap } = require("@opencensus/core");
const { StackdriverStatsExporter } =
require("@opencensus/exporter-stackdriver");

const fs = require("fs");
const readline = require("readline");

// Create the Stats manager
const stats = new Stats();

// [START setup_exporter]
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
// Exporters use Application Default Credentials (ADCs) to authenticate.
Expand All @@ -47,19 +43,19 @@ if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
}
const exporter = new StackdriverStatsExporter({ projectId: projectId });

// Pass the created exporter to Stats
stats.registerExporter(exporter);
// Pass the created exporter to global Stats
globalStats.registerExporter(exporter);
// [END setup_exporter]

// The latency in milliseconds
const mLatencyMs = stats.createMeasureDouble(
const mLatencyMs = globalStats.createMeasureDouble(
"repl/latency",
MeasureUnit.MS,
"The latency in milliseconds per REPL loop"
);

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

const tagKeys = ["method", "status"];
const methodKey = { name: "method" };
const statusKey = { name: "status" };
const tagKeys = [methodKey, statusKey];

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

// Create the view.
stats.createView(
// Create & Register the view.
const lineCountView = globalStats.createView(
"demo/lines_in",
mLineLengths,
AggregationType.COUNT,
tagKeys,
"The number of lines from standard input"
);
globalStats.registerView(lineCountView);

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

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

const tags = { method: "repl", status: "OK" };
const tags = new TagMap();
tags.set(methodKey, { value: "REPL" });
tags.set(statusKey, { value: "OK" });

stats.record({
globalStats.record([{
measure: mLineLengths,
tags,
value: processedLine.length
});
}], tags);

stats.record({
globalStats.record([{
measure: mLatencyMs,
tags,
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
});
}], tags);

} catch (err) {
const errTags = { method: "repl", status: "ERROR", error: err.message };
stats.record({
console.log(err);

const errTags = new TagMap();
errTags.set(methodKey, { value: "repl" });
errTags.set(statusKey, { value: "ERROR" });
globalStats.record([{
measure: mLatencyMs,
errTags,
value: sinceInMilliseconds(endNanoseconds, startNanoseconds)
});
}], errTags);
}

// Restarts the start time for the REPL
Expand Down
64 changes: 35 additions & 29 deletions examples/stats/web_client_monitoring/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const assert = require('assert');
const process = require("process");
const bodyParser = require('body-parser');
// [START web_client_monitoring_imports]
const { Stats, MeasureUnit, AggregationType } = require('@opencensus/core');
const { globalStats, MeasureUnit, AggregationType, TagMap } = require('@opencensus/core');
const { StackdriverStatsExporter } = require('@opencensus/exporter-stackdriver');
// [END web_client_monitoring_imports]

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

// OpenCensus setup
// [START web_client_monitoring_ocsetup]
const stats = new Stats();
const exporter = new StackdriverStatsExporter({projectId: project});
stats.registerExporter(exporter);
const mLatencyMs = stats.createMeasureDouble("webmetrics/latency",
globalStats.registerExporter(exporter);
const mLatencyMs = globalStats.createMeasureDouble("webmetrics/latency",
MeasureUnit.MS,
"Latency related to page loading");
const mClickCount = stats.createMeasureInt64("webmetrics/click_count",
const mClickCount = globalStats.createMeasureInt64("webmetrics/click_count",
MeasureUnit.UNIT,
"Number of clicks");
const buckets = [0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80,
100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000,
5000, 10000, 20000, 50000, 100000];
const tagPhase = "phase";
const tagClient = "client";
const latencyView = stats.createView(

const tagPhase = { name: "phase" };
const tagClient = { name: "client" };

const latencyView = globalStats.createView(
"webmetrics/latency",
mLatencyMs,
AggregationType.DISTRIBUTION,
[tagPhase, tagClient],
"Distribution of latencies",
buckets
);
const clickCountView = stats.createView(
globalStats.registerView(latencyView);
const clickCountView = globalStats.createView(
"webmetrics/click_count",
mClickCount,
AggregationType.COUNT,
[tagClient],
"The number of button clicks"
);
globalStats.registerView(clickCountView);
// [END web_client_monitoring_ocsetup]

// Process the metrics data posted to the server
Expand All @@ -86,32 +89,35 @@ app.post("/metrics", (req, res) => {
const valueDNSLookup = "dns_lookup";
const valueLoad = "load";
const valueWeb = "web";
let tags = { phase: valueDNSLookup, client: valueWeb };

const tags = new TagMap();
tags.set(tagPhase, { value: valueDNSLookup });
tags.set(tagClient, { value: valueWeb });
// [START web_client_monitoring_record]
try {
stats.record({
globalStats.record([{
measure: mLatencyMs,
tags,
value: dnsTime
});
tags = { phase: valueTLSNegotiation, client: valueWeb };
stats.record({
value: 1
}], tags);

tags.set(tagPhase, { value: valueTLSNegotiation });
globalStats.record([{
measure: mLatencyMs,
tags,
value: connectTime
});
tags = { phase: valueLoad, client: valueWeb };
stats.record({
value: 1
}], tags);

tags.set(tagPhase, { value: valueLoad });
globalStats.record([{
measure: mLatencyMs,
tags,
value: totalTime
});
tags = { client: valueWeb };
stats.record({
value: 1
}], tags);

const tags1 = new TagMap();
tags1.set(tagClient, { value: valueWeb });
globalStats.record([{
measure: mClickCount,
tags,
value: clickCount
});
value: 1
}], tags1);
res.status(200).send("Received").end();
console.log('Competed recording metrics');
} catch (err) {
Expand Down
Loading