opentelemetry-js/packages/sdk-metrics
Daniel Dyla 0f8aa7924f
Update `next` branch (#4294)
* chore: track package-lock.json (#4238)

* chore: track package-lock.json

* Pin to old versions for node 14

* Use version range

* Remove unused cached directories

* Temporarily disable other tests

* Temporarily enable only api test

* Enable only some packages

* Test only api packages

* Test trace exporters

* Fix line ordering

* Test all packages except otlp exporters

* Add trace http exporter

* Add trace proto exporter

* Test all but grpc exporters

* chore: use npm workspaces and degrade lerna to v6

* chore: get rid of lerna bootstrap

* chore: use npx

* chore: allow install scripts to setup buf

* chore: fix w3c-integration-test cache key

* chore: fix cache key

* chore: disable resource compat test

* chore: fix node_modules assumptions

* chore: fix hoisted karma issue

* chore: fix markdown linter complaints

* chore: lock @grpc/grpc-js to v1.8.21

* Break caches

* chore: remove cache

* chore: fixup inline commands

---------

Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>

* docs: fixed link to benchmark results (#4233)

Co-authored-by: Chengzhong Wu <legendecas@gmail.com>

* chore(deps): update all patch versions (#4215)

* fix: otlp json encoding (#4220)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* fix: remove duplicate export star from version.ts (#4225)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* docs: fix sdk-node config instructions (#4249)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* feat(api): publish api esnext target (#4231)

* chore: release API 1.7.0/Core 1.18.0/Experimental 0.45.0 (#4254)

* fix(sdk-metrics): hand-roll MetricAdvice type as older API versions do not include it (#4260)

* chore: prepare release 1.18.1/0.45.1 (#4261)

* chore: no need for 'packages' in "lerna.json" (#4264)

* Benchmark tests for trace OTLP transform and BatchSpanProcessor (#4218)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* chore: type reference on zone.js (#4257)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* docs: add docker-compose to run prometheus for the experimental example (#4268)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* fix(sdk-logs): avoid map attribute set when count limit exceeded (#4195)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* chore(deps): update dependency chromedriver to v119 [security] (#4280)

* chore(deps): update actions/setup-node action to v4 (#4236)

* fix(sdk-trace-base): processor onStart called with a span having empty attributes (#4277)

Co-authored-by: artahmetaj <artahmetaj@yahoo.com>

* Update fetch instrumentation to be runtime agnostic (#4063)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

---------

Co-authored-by: Chengzhong Wu <legendecas@gmail.com>
Co-authored-by: Martin Kuba <martin@martinkuba.com>
Co-authored-by: Mend Renovate <bot@renovateapp.com>
Co-authored-by: Siim Kallas <siimkallas@gmail.com>
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
Co-authored-by: David Luna <david.luna@elastic.co>
Co-authored-by: Dinko Osrecki <dinko.osrecki@emarsys.com>
Co-authored-by: Trent Mick <trentm@gmail.com>
Co-authored-by: François <32224751+Lp-Francois@users.noreply.github.com>
Co-authored-by: Hyun Oh <hyunnoh01@gmail.com>
Co-authored-by: André Cruz <andremiguelcruz@msn.com>
Co-authored-by: artahmetaj <artahmetaj@yahoo.com>
Co-authored-by: drewcorlin1 <82601620+drewcorlin1@users.noreply.github.com>
2023-11-15 11:15:51 -05:00
..
src Update `next` branch (#4294) 2023-11-15 11:15:51 -05:00
test feat(metrics): prototype experimental advice support (#3876) 2023-10-11 10:05:03 +02:00
.eslintignore chore: release SDK 1.8.0 / Experimental 0.34.0 with metrics GA (#3340) 2022-11-09 14:21:50 -05:00
.eslintrc.js chore(deps): update dependency eslint to v8.43.0 (#3929) 2023-07-06 15:14:56 +02:00
.npmignore chore: release SDK 1.8.0 / Experimental 0.34.0 with metrics GA (#3340) 2022-11-09 14:21:50 -05:00
LICENSE chore: release SDK 1.8.0 / Experimental 0.34.0 with metrics GA (#3340) 2022-11-09 14:21:50 -05:00
README.md docs(sdk-metrics): fix badge and reword introductory sentence (#3484) 2023-01-04 23:41:00 +08:00
karma.conf.js chore: release SDK 1.8.0 / Experimental 0.34.0 with metrics GA (#3340) 2022-11-09 14:21:50 -05:00
package.json Update `next` branch (#4294) 2023-11-15 11:15:51 -05:00
tsconfig.esm.json chore: automatically generate tsconfigs (#3432) 2022-11-23 23:34:41 +08:00
tsconfig.esnext.json chore: automatically generate tsconfigs (#3432) 2022-11-23 23:34:41 +08:00
tsconfig.json chore: automatically generate tsconfigs (#3432) 2022-11-23 23:34:41 +08:00

README.md

OpenTelemetry Metrics SDK

NPM Published Version Apache License

This module contains the Metrics SDK of opentelemetry-js.

Used standalone, this module provides methods for manual instrumentation of code, offering full control over recording metrics for client-side JavaScript (browser) and Node.js.

It does not provide automated instrumentation of known libraries or host environment metrics out-of-the-box.

Installation

npm install --save @opentelemetry/api
npm install --save @opentelemetry/sdk-metrics

Usage

The basic setup of the SDK can be seen as followings:

const opentelemetry = require('@opentelemetry/api');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');

// To create an instrument, you first need to initialize the Meter provider.
// NOTE: The default OpenTelemetry meter provider does not record any metric instruments.
//       Registering a working meter provider allows the API methods to record instruments.
opentelemetry.setGlobalMeterProvider(new MeterProvider());

// To record a metric event, we used the global singleton meter to create an instrument.
const counter = opentelemetry.getMeter('default').createCounter('foo');

// record a metric event.
counter.add(1, { attributeKey: 'attribute-value' });

In conditions, we may need to setup an async instrument to observe costly events:

// Creating an async instrument, similar to synchronous instruments
const observableCounter = opentelemetry.getMeter('default')
  .createObservableCounter('observable-counter');

// Register a single-instrument callback to the async instrument.
observableCounter.addCallback(async (observableResult) => {
  // ... do async stuff
  observableResult.observe(1, { attributeKey: 'attribute-value' });
});

// Register a multi-instrument callback and associate it with a set of async instruments.
opentelemetry.getMeter('default')
  .addBatchObservableCallback(batchObservableCallback, [ observableCounter ]);
async function batchObservableCallback(batchObservableResult) {
  // ... do async stuff
  batchObservableResult.observe(observableCounter, 1, { attributeKey: 'attribute-value' });
}

Views can be registered when instantiating a MeterProvider:

const meterProvider = new MeterProvider({
  views: [
    // override the bucket boundaries on `my.histogram` to [0, 50, 100]
    new View({ aggregation: new ExplicitBucketHistogramAggregation([0, 50, 100]), instrumentName: 'my.histogram'}),
    // rename 'my.counter' to 'my.renamed.counter'
    new View({ name: 'my.renamed.counter', instrumentName: 'my.counter'})
  ]
})

Example

See examples/prometheus for an end-to-end example, including exporting metrics.

License

Apache 2.0 - See LICENSE for more information.