# @opentelemetry/api-metrics

> Public metrics API for OpenTelemetry

Latest version **0.33.0** (published 2022-09-16) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @opentelemetry/api-metrics
pnpm add @opentelemetry/api-metrics
yarn add @opentelemetry/api-metrics
bun add @opentelemetry/api-metrics
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.33.0 |
| Published | 2022-09-16 |
| First published | 2021-01-21 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 1 |
| Unpacked size | 239.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3486 |
| Author | OpenTelemetry Authors |
| Maintainers | bogdandrutu, dyladan |
| Keywords | opentelemetry, nodejs, browser, profiling, metrics, stats, monitoring |

## Links

- npm: https://www.npmjs.com/package/@opentelemetry/api-metrics
- Repository: https://github.com/open-telemetry/opentelemetry-js
- Homepage: https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-api-metrics
- Issues: https://github.com/open-telemetry/opentelemetry-js/issues
- npm.io page: https://npm.io/package/@opentelemetry/api-metrics

## Dependencies (1)

- [@opentelemetry/api](https://npm.io/package/@opentelemetry/api.md) ^1.0.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.33.0 (latest) — 2022-09-16
- 1.0.0 (next) — 2022-10-27
- 0.25.1-alpha.13 (canary) — 2021-08-30
- 0.32.0 — 2022-08-24
- 0.31.0 — 2022-07-26
- 0.30.0 — 2022-07-06
- 0.29.2 — 2022-06-06
- 0.29.1 — 2022-06-01
- 0.29.0 — 2022-05-27
- 0.28.0 — 2022-04-25
- 0.27.0 — 2021-11-11
- 0.26.0 — 2021-10-05
- 0.25.1-alpha.4 — 2021-08-24
- 0.25.0 — 2021-08-18
- 0.25.1-alpha.21 — 2021-08-18
- … 54 more at https://npm.io/package/@opentelemetry/api-metrics/versions

## README

# OpenTelemetry API for JavaScript

[![NPM Published Version][npm-img]][npm-url]
[![Apache License][license-image]][license-image]

**Note: This is an experimental package under active development. New releases may include breaking changes.**

This package provides everything needed to interact with the unstable OpenTelemetry Metrics API, including all TypeScript interfaces, enums, and no-op implementations. It is intended for use both on the server and in the browser.

## Beta Software - Use at your own risk

The metrics API is considered alpha software and there is no guarantee of stability or long-term support. When the API is stabilized, it will be made available and supported long-term in the `@opentelemetry/api` package and this package will be deprecated.

## Quick Start

To get started you need to install the SDK and instrumentations, create a MeterProvider, and register it with the API.

### Install Dependencies

```sh
$ # Install metrics dependencies
$ npm install \
    @opentelemetry/api-metrics \
    @opentelemetry/sdk-metrics \
    @opentelemetry/exporter-prometheus # add exporters as needed
```

> Note: this example is for node.js. See [examples/tracer-web](https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/tracer-web) for a browser example.

### Initialize the SDK

Before any other module in your application is loaded, you must initialize the global tracer and meter providers. If you fail to initialize a provider, no-op implementations will be provided to any library which acquires them from the API.

To collect traces and metrics, you will have to tell the SDK where to export telemetry data to. This example uses Jaeger and Prometheus, but exporters exist for [other tracing backends][other-tracing-backends]. If you're not sure if there is an exporter for your tracing backend, contact your tracing provider.

#### Metrics

```javascript
const api = require("@opentelemetry/api-metrics");
const { MeterProvider } = require("@opentelemetry/sdk-metrics");
const { PrometheusExporter } = require("@opentelemetry/exporter-prometheus");

// The Prometheus exporter runs an HTTP server which the Prometheus backend
// scrapes to collect metrics.
const exporter = new PrometheusExporter({ startServer: true });
// Creates MeterProvider and installs the exporter as a MetricReader
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);

/**
 * Registering the provider with the API allows it to be discovered
 * and used by instrumentation libraries.
 */
api.metrics.setGlobalMeterProvider(meterProvider);
```

## Version Compatibility

Because the npm installer and node module resolution algorithm could potentially allow two or more copies of any given package to exist within the same `node_modules` structure, the OpenTelemetry API takes advantage of a variable on the `global` object to store the global API. When an API method in the API package is called, it checks if this `global` API exists and proxies calls to it if and only if it is a compatible API version. This means if a package has a dependency on an OpenTelemetry API version which is not compatible with the API used by the end user, the package will receive a no-op implementation of the API.

## Advanced Use

### API Methods

If you are writing an instrumentation library, or prefer to call the API methods directly rather than using the `register` method on the Tracer/Meter Provider, OpenTelemetry provides direct access to the underlying API methods through the `@opentelemetry/api-metrics` package. API entry points are defined as global singleton objects `trace`, `metrics`, `propagation`, and `context` which contain methods used to initialize SDK implementations and acquire resources from the API.

- [Metrics API Documentation][metrics-api-docs]

```javascript
const api = require("@opentelemetry/api-metrics");

/* Initialize MeterProvider */
api.metrics.setGlobalMeterProvider(meterProvider);
/* returns meterProvider (no-op if a working provider has not been initialized) */
api.metrics.getMeterProvider();
/* returns a meter from the registered global meter provider (no-op if a working provider has not been initialized) */
api.metrics.getMeter(name, version);
```

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>
- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]

## License

Apache 2.0 - See [LICENSE][license-url] for more information.

[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[npm-url]: https://www.npmjs.com/package/@opentelemetry/api-metrics
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fapi-metrics.svg

[metrics-api-docs]: https://open-telemetry.github.io/opentelemetry-js/modules/_opentelemetry_api_metrics.html

[other-tracing-backends]: https://github.com/open-telemetry/opentelemetry-js#trace-exporters

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