# @opencensus/core

> OpenCensus is a toolkit for collecting application performance and behavior data.

Latest version **0.1.0** (published 2021-07-27) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @opencensus/core
pnpm add @opencensus/core
yarn add @opencensus/core
bun add @opencensus/core
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2021-07-27 |
| First published | 2018-08-02 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 5 |
| Unpacked size | 376 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 273 |
| Author | Google Inc. |
| Maintainers | kjin, ofrobots, google-admin, isaikevych, google-wombot, mayurkale22 |
| Keywords | opencensus, nodejs, tracing, profiling, metrics, stats |

## Links

- npm: https://www.npmjs.com/package/@opencensus/core
- Repository: https://github.com/census-instrumentation/opencensus-node
- Homepage: https://github.com/census-instrumentation/opencensus-node#readme
- Issues: https://github.com/census-instrumentation/opencensus-node/issues
- npm.io page: https://npm.io/package/@opencensus/core

## Dependencies (5)

- [uuid](https://npm.io/package/uuid.md) ^8.0.0
- [semver](https://npm.io/package/semver.md) ^7.0.0
- [shimmer](https://npm.io/package/shimmer.md) ^1.2.0
- [log-driver](https://npm.io/package/log-driver.md) ^1.2.7
- [continuation-local-storage](https://npm.io/package/continuation-local-storage.md) ^3.2.1

## Recent versions

- 0.1.0 (latest) — 2021-07-27
- 0.0.22 — 2020-06-05
- 0.0.21 — 2020-03-30
- 0.0.20 — 2020-02-19
- 0.0.19 — 2019-11-22
- 0.0.18 — 2019-10-09
- 0.0.17 — 2019-09-03
- 0.0.16 — 2019-07-20
- 0.0.15 — 2019-07-09
- 0.0.14 — 2019-06-04
- 0.0.13 — 2019-05-20
- 0.0.12 — 2019-05-13
- 0.0.11 — 2019-04-09
- 0.0.10 — 2019-04-03
- 0.0.9 — 2019-02-13
- … 6 more at https://npm.io/package/@opencensus/core/versions

## README

# OpenCensus Core Node.js
[![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]

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.

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

## Installation

Install the opencensus-core package with NPM:
```bash
npm install @opencensus/core
```

## Usage

#### Get the global Stats manager instance.

To enable metrics, we’ll import a few items from OpenCensus Core package.

```javascript
const { globalStats, MeasureUnit, AggregationType, TagMap } = require('@opencensus/core');

// The latency in milliseconds
const mLatencyMs = globalStats.createMeasureDouble(
  "repl/latency",
  MeasureUnit.MS,
  "The latency in milliseconds"
);
```

#### Create Views and Tags:

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```.

```javascript
const methodTagKey = { name: "method" };
const statusTagKey = { name: "status" };
const errorTagKey = { name: "error" };

// Create & Register the view.
const latencyView = globalStats.createView(
  "demo/latency",
  mLatencyMs,
  AggregationType.DISTRIBUTION,
  [methodTagKey, statusTagKey, errorTagKey],
  "The distribution of the latencies",
  // Bucket Boundaries:
  // [>=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);
```

#### Recording Metrics:

Now we will record the desired metrics. To do so, we will use ```globalStats.record()``` and pass in measurements.

```javascript
const [_, startNanoseconds] = process.hrtime();
const tags = new TagMap();
tags.set(methodTagKey, { value: "REPL" });
tags.set(statusTagKey, { value: "OK" });

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

function sinceInMilliseconds(startNanoseconds) {
  const [_, endNanoseconds] = process.hrtime();
  return (endNanoseconds - startNanoseconds) / 1e6;
}
```

Measures can be of type `Int64` or `DOUBLE`, created by calling `createMeasureInt64` and `createMeasureDouble` respectively. Its units can be:

| MeasureUnit | Usage |
| ----------- | ----- |
| `UNIT` | for general counts |
| `BYTE` | bytes |
| `KBYTE` | Kbytes |
| `SEC` | seconds |
| `MS` | millisecond |
| `NS` | nanosecond |

Views can have agregations of type `SUM`, `LAST_VALUE`, `COUNT` and `DISTRIBUTION`. To know more about Stats core concepts, please visit: [https://opencensus.io/core-concepts/metrics/](https://opencensus.io/core-concepts/metrics/)

See [Quickstart/Metrics](https://opencensus.io/quickstart/nodejs/metrics/) for a full example of registering and collecting metrics.

## Useful links
- For more information on OpenCensus, visit: <https://opencensus.io/>
- To checkout the OpenCensus for Node.js, visit: <https://github.com/census-instrumentation/opencensus-node>
- For help or feedback on this project, join us on [gitter](https://gitter.im/census-instrumentation/Lobby)

[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/core
[npm-img]: https://badge.fury.io/js/%40opencensus%2Fcore.svg
[node-img]: https://img.shields.io/node/v/@opencensus/core.svg
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-status]: https://david-dm.org/census-instrumentation/opencensus-node/status.svg?path=packages/opencensus-core
[devdependencies-status]:
https://david-dm.org/census-instrumentation/opencensus-node/dev-status.svg?path=packages/opencensus-core

## LICENSE

Apache License 2.0

---
_Source: https://npm.io/package/@opencensus/core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
