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

Commit e5281f4

Browse files
authored
Update Core library README (#216)
* Update Core library README * fix startTime
1 parent 3f72f14 commit e5281f4

File tree

1 file changed

+83
-3
lines changed

1 file changed

+83
-3
lines changed

packages/opencensus-core/README.md

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,80 @@
11
# OpenCensus Core Node.js
2-
[![Gitter chat][gitter-image]][gitter-url]
2+
[![Gitter chat][gitter-image]][gitter-url] ![Node Version][node-img] [![NPM Published Version][npm-img]][npm-url] ![dependencies Status][dependencies-status] ![devDependencies Status][devdependencies-status] ![Apache License][license-image]
33

4-
OpenCensus for Node.js is an implementation of OpenCensus, a toolkit for collecting application performance and behavior monitoring data.
4+
OpenCensus for Node.js is an implementation of OpenCensus, a toolkit for collecting application performance and behavior monitoring data. It currently includes 3 apis: stats, tracing and tags.
55

66
The library is in alpha stage and the API is subject to change.
77

8-
Please join [gitter](https://gitter.im/census-instrumentation/Lobby) for help or feedback on this project.
8+
## Installation
9+
10+
Install the opencensus-core package with NPM:
11+
```bash
12+
npm install @opencensus/core
13+
```
14+
15+
## Usage
16+
17+
#### Set up a new Stats manager instance.
18+
19+
To enable metrics, we’ll import a few items from OpenCensus Core package.
20+
21+
```javascript
22+
const { Stats, MeasureUnit, AggregationType } = require('@opencensus/core');
23+
24+
// Create the Stats manager
25+
const stats = new Stats();
26+
27+
// The latency in milliseconds
28+
const mLatencyMs = stats.createMeasureDouble(
29+
"repl/latency",
30+
MeasureUnit.MS,
31+
"The latency in milliseconds"
32+
);
33+
```
34+
35+
#### Create Views and Tags:
36+
37+
We now determine how our metrics will be organized by creating ```Views```. We will also create the variable needed to add extra text meta-data to our metrics – ```methodTagKey```, ```statusTagKey```, and ```errorTagKey```.
38+
39+
```javascript
40+
const methodTagKey = "method";
41+
const statusTagKey = "status";
42+
const errorTagKey = "error";
43+
44+
const latencyView = stats.createView(
45+
"demo/latency",
46+
mLatencyMs,
47+
AggregationType.DISTRIBUTION,
48+
[methodTagKey, statusTagKey, errorTagKey],
49+
"The distribution of the latencies",
50+
// Bucket Boundaries:
51+
// [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
52+
[0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000, 6000]
53+
);
54+
```
55+
56+
#### Recording Metrics:
57+
58+
Now we will record the desired metrics. To do so, we will use ```stats.record()``` and pass in measurements.
59+
60+
```javascript
61+
const [_, startNanoseconds] = process.hrtime();
62+
const tags = {method: "repl", status: "OK"};
63+
64+
stats.record({
65+
measure: mLatencyMs,
66+
tags,
67+
value: sinceInMilliseconds(startNanoseconds)
68+
});
69+
70+
71+
function sinceInMilliseconds(startNanoseconds) {
72+
const [_, endNanoseconds] = process.hrtime();
73+
return (endNanoseconds - startNanoseconds) / 1e6;
74+
}
75+
```
76+
77+
See [Quickstart/Metrics](https://opencensus.io/quickstart/nodejs/metrics/) for a full example of registering and collecting metrics.
978

1079
## Useful links
1180
- For more information on OpenCensus, visit: <https://opencensus.io/>
@@ -14,3 +83,14 @@ Please join [gitter](https://gitter.im/census-instrumentation/Lobby) for help or
1483

1584
[gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg
1685
[gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
86+
[npm-url]: https://www.npmjs.com/package/@opencensus/core
87+
[npm-img]: https://badge.fury.io/js/%40opencensus%2Fcore.svg
88+
[node-img]: https://img.shields.io/node/v/@opencensus/core.svg
89+
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
90+
[dependencies-status]: https://david-dm.org/census-instrumentation/opencensus-node/status.svg?path=packages/opencensus-core
91+
[devdependencies-status]:
92+
https://david-dm.org/census-instrumentation/opencensus-node/dev-status.svg?path=packages/opencensus-core
93+
94+
## LICENSE
95+
96+
Apache License 2.0

0 commit comments

Comments
 (0)