@opentelemetry/metrics

OpenTelemetry metrics SDK

Package renamed to @opentelemetry/sdk-metrics-base
Last updated 3 years ago by dyladan .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @opentelemetry/metrics 
SYNC missed versions from official npm registry.

OpenTelemetry Metrics SDK

NPM Published Version dependencies devDependencies Apache License

OpenTelemetry metrics allow a user to collect data and export it to a metrics backend like Prometheus.

Installation

npm install --save @opentelemetry/metrics

Usage

Counter

Choose this kind of metric when the value is a quantity, the sum is of primary interest, and the event count and value distribution are not of primary interest. It is restricted to non-negative increments. Example uses for Counter:

  • count the number of bytes received
  • count the number of requests completed
  • count the number of accounts created
  • count the number of checkpoints run
  • count the number of 5xx errors.
const { MeterProvider } = require('@opentelemetry/metrics');

// Initialize the Meter to capture measurements in various ways.
const meter = new MeterProvider().getMeter('your-meter-name');

const counter = meter.createCounter('metric_name', {
  description: 'Example of a counter'
});

const labels = { pid: process.pid };

// Create a BoundInstrument associated with specified label values.
const boundCounter = counter.bind(labels);
boundCounter.add(10);

UpDownCounter

UpDownCounter is similar to Counter except that it supports negative increments. It is generally useful for capturing changes in an amount of resources used, or any quantity that rises and falls during a request.

Example uses for UpDownCounter:

  • count the number of active requests
  • count memory in use by instrumenting new and delete
  • count queue size by instrumenting enqueue and dequeue
  • count semaphore up and down operations
const { MeterProvider } = require('@opentelemetry/metrics');

// Initialize the Meter to capture measurements in various ways.
const meter = new MeterProvider().getMeter('your-meter-name');

const counter = meter.createUpDownCounter('metric_name', {
  description: 'Example of a UpDownCounter'
});

const labels = { pid: process.pid };

// Create a BoundInstrument associated with specified label values.
const boundCounter = counter.bind(labels);
boundCounter.add(Math.random() > 0.5 ? 1 : -1);

Value Observer

Choose this kind of metric when only last value is important without worry about aggregation. The callback can be sync or async.

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

const meter = new MeterProvider().getMeter('your-meter-name');


// async callback - for operation that needs to wait for value
meter.createValueObserver('your_metric_name', {
  description: 'Example of an async observer with callback',
}, async (observerResult) => {
  const value = await getAsyncValue();
  observerResult.observe(value, { label: '1' });
});

function getAsyncValue() {
  return new Promise((resolve) => {
    setTimeout(()=> {
      resolve(Math.random());
    }, 100);
  });
}

// sync callback in case you don't need to wait for value
meter.createValueObserver('your_metric_name', {
  description: 'Example of a sync observer with callback',
}, (observerResult) => {
  observerResult.observe(getRandomValue(), { label: '1' });
  observerResult.observe(getRandomValue(), { label: '2' });
});

function getRandomValue() {
  return Math.random();
}

UpDownSumObserver

Choose this kind of metric when sum is important and you want to capture any value that starts at zero and rises or falls throughout the process lifetime. The callback can be sync or async.

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

const meter = new MeterProvider().getMeter('your-meter-name');

// async callback - for operation that needs to wait for value
meter.createUpDownSumObserver('your_metric_name', {
  description: 'Example of an async observer with callback',
}, async (observerResult) => {
  const value = await getAsyncValue();
  observerResult.observe(value, { label: '1' });
});

function getAsyncValue() {
  return new Promise((resolve) => {
    setTimeout(()=> {
      resolve(Math.random());
    }, 100);
  });
}

// sync callback in case you don't need to wait for value
meter.createUpDownSumObserver('your_metric_name', {
  description: 'Example of a sync observer with callback',
}, (observerResult) => {
  observerResult.observe(getRandomValue(), { label: '1' });
});

function getRandomValue() {
  return Math.random();
}

Sum Observer

Choose this kind of metric when collecting a sum that never decreases. The callback can be sync or async.

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

const meter = new MeterProvider().getMeter('your-meter-name');

// async callback in case you need to wait for values
meter.createSumObserver('example_metric', {
  description: 'Example of an async sum observer with callback',
}, async (observerResult) => {
  const value = await getAsyncValue();
  observerResult.observe(value, { label: '1' });
});

function getAsyncValue() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Math.random());
    }, 100)
  });
}

// sync callback in case you don't need to wait for values
meter.createSumObserver('example_metric', {
  description: 'Example of a sync sum observer with callback',
}, (observerResult) => {
  const value = getRandomValue();
  observerResult.observe(value, { label: '1' });
});

function getRandomValue() {
  return Math.random();
}

Batch Observer

Choose this kind of metric when you need to update multiple observers with the results of a single async calculation.

const { MeterProvider } = require('@opentelemetry/metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');

const exporter = new PrometheusExporter(
  {
    startServer: true,
  },
  () => {
    console.log('prometheus scrape endpoint: http://localhost:9464/metrics');
  },
);

const meter = new MeterProvider({
  exporter,
  interval: 3000,
}).getMeter('example-observer');

const cpuUsageMetric = meter.createValueObserver('cpu_usage_per_app', {
  description: 'CPU',
});

const MemUsageMetric = meter.createValueObserver('mem_usage_per_app', {
  description: 'Memory',
});

meter.createBatchObserver((observerBatchResult) => {
  getSomeAsyncMetrics().then(metrics => {
    observerBatchResult.observe({ app: 'myApp' }, [
      cpuUsageMetric.observation(metrics.value1),
      MemUsageMetric.observation(metrics.value2)
    ]);
  });
});

function getSomeAsyncMetrics() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        value1: Math.random(),
        value2: Math.random(),
      });
    }, 100)
  });
}

See examples/prometheus for a short example.

Value Recorder

ValueRecorder is a non-additive synchronous instrument useful for recording any non-additive number, positive or negative. Values captured by ValueRecorder.record(value) are treated as individual events belonging to a distribution that is being summarized. ValueRecorder should be chosen either when capturing measurements that do not contribute meaningfully to a sum, or when capturing numbers that are additive in nature, but where the distribution of individual increments is considered interesting.

Useful links

License

Apache 2.0 - See LICENSE for more information.

Current Tags

  • 0.24.1-alpha.1                                ...           canary (3 years ago)
  • 0.24.0                                ...           latest (3 years ago)
  • 0.24.0                                ...           next (3 years ago)

125 Versions

  • 0.24.1-alpha.1 [deprecated]           ...           3 years ago
  • 0.24.0 [deprecated]           ...           3 years ago
  • 0.24.1-alpha.31 [deprecated]           ...           3 years ago
  • 0.23.1-alpha.28 [deprecated]           ...           3 years ago
  • 0.23.1-alpha.24 [deprecated]           ...           3 years ago
  • 0.23.1-alpha.23 [deprecated]           ...           3 years ago
  • 0.23.1-alpha.15 [deprecated]           ...           3 years ago
  • 0.23.1-alpha.3 [deprecated]           ...           3 years ago
  • 0.23.1-alpha.18 [deprecated]           ...           3 years ago
  • 0.23.0 [deprecated]           ...           3 years ago
  • 0.23.1-alpha.17 [deprecated]           ...           3 years ago
  • 0.22.1-alpha.16 [deprecated]           ...           3 years ago
  • 0.22.1-alpha.15 [deprecated]           ...           3 years ago
  • 0.22.1-alpha.13 [deprecated]           ...           3 years ago
  • 0.22.1-alpha.3 [deprecated]           ...           3 years ago
  • 0.22.1-alpha.2 [deprecated]           ...           3 years ago
  • 0.22.0 [deprecated]           ...           3 years ago
  • 0.22.1-alpha.5 [deprecated]           ...           3 years ago
  • 0.21.1-alpha.2 [deprecated]           ...           3 years ago
  • 0.21.1-alpha.1 [deprecated]           ...           3 years ago
  • 0.21.0 [deprecated]           ...           3 years ago
  • 0.21.1-alpha.7 [deprecated]           ...           3 years ago
  • 0.20.1-alpha.4 [deprecated]           ...           3 years ago
  • 0.20.1-alpha.3 [deprecated]           ...           3 years ago
  • 0.20.0 [deprecated]           ...           3 years ago
  • 0.20.1-alpha.49 [deprecated]           ...           3 years ago
  • 0.19.1-alpha.43 [deprecated]           ...           3 years ago
  • 0.19.1-alpha.38 [deprecated]           ...           3 years ago
  • 0.19.1-alpha.33 [deprecated]           ...           3 years ago
  • 0.19.1-alpha.31 [deprecated]           ...           3 years ago
  • 0.19.1-alpha.26 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.25 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.21 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.19 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.12 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.11 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.9 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.7 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.42 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.39 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.37 [deprecated]           ...           4 years ago
  • 0.19.0 [deprecated]           ...           4 years ago
  • 0.19.1-alpha.36 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.31 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.28 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.27 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.25 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.23 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.20 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.18 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.16 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.14 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.11 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.10 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.7 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.5 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.4 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.3 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.1 [deprecated]           ...           4 years ago
  • 0.18.2 [deprecated]           ...           4 years ago
  • 0.18.3-alpha.2 [deprecated]           ...           4 years ago
  • 0.18.2-alpha.1 [deprecated]           ...           4 years ago
  • 0.18.1 [deprecated]           ...           4 years ago
  • 0.18.2-alpha.81 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.26 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.24 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.23 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.21 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.20 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.16 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.3 [deprecated]           ...           4 years ago
  • 0.18.0 [deprecated]           ...           4 years ago
  • 0.18.1-alpha.12 [deprecated]           ...           4 years ago
  • 0.17.1-alpha.10 [deprecated]           ...           4 years ago
  • 0.17.1-alpha.8 [deprecated]           ...           4 years ago
  • 0.17.1-alpha.5 [deprecated]           ...           4 years ago
  • 0.17.1-alpha.2 [deprecated]           ...           4 years ago
  • 0.17.1-alpha.21 [deprecated]           ...           4 years ago
  • 0.17.0 [deprecated]           ...           4 years ago
  • 0.16.1-alpha.20 [deprecated]           ...           4 years ago
  • 0.16.1-alpha.18 [deprecated]           ...           4 years ago
  • 0.16.1-alpha.15 [deprecated]           ...           4 years ago
  • 0.16.1-alpha.14 [deprecated]           ...           4 years ago
  • 0.16.1-alpha.11 [deprecated]           ...           4 years ago
  • 0.16.1-alpha.10 [deprecated]           ...           4 years ago
  • 0.16.0 [deprecated]           ...           4 years ago
  • 0.15.0 [deprecated]           ...           4 years ago
  • 0.14.0 [deprecated]           ...           4 years ago
  • 0.13.0 [deprecated]           ...           4 years ago
  • 0.12.1-alpha.7 [deprecated]           ...           4 years ago
  • 0.12.0 [deprecated]           ...           4 years ago
  • 0.12.1-alpha.54 [deprecated]           ...           4 years ago
  • 0.11.1-alpha.53 [deprecated]           ...           4 years ago
  • 0.11.1-alpha.44 [deprecated]           ...           4 years ago
  • 0.11.1-alpha.36 [deprecated]           ...           4 years ago
  • 0.11.1-alpha.35 [deprecated]           ...           4 years ago
  • 0.11.1-alpha.15 [deprecated]           ...           4 years ago
  • 0.11.0 [deprecated]           ...           4 years ago
  • 0.11.1-alpha.48 [deprecated]           ...           4 years ago
  • 0.10.3-alpha.41 [deprecated]           ...           4 years ago
  • 0.10.3-alpha.40 [deprecated]           ...           4 years ago
  • 0.10.3-alpha.35 [deprecated]           ...           4 years ago
  • 0.10.3-alpha.34 [deprecated]           ...           4 years ago
  • 0.10.3-alpha.28 [deprecated]           ...           4 years ago
  • 0.10.3-canary.0 [deprecated]           ...           4 years ago
  • 0.10.2 [deprecated]           ...           4 years ago
  • 0.10.1 [deprecated]           ...           4 years ago
  • 0.10.0 [deprecated]           ...           4 years ago
  • 0.9.0 [deprecated]           ...           4 years ago
  • 0.8.3 [deprecated]           ...           4 years ago
  • 0.8.2 [deprecated]           ...           4 years ago
  • 0.8.1 [deprecated]           ...           4 years ago
  • 0.8.0 [deprecated]           ...           5 years ago
  • 0.7.0 [deprecated]           ...           5 years ago
  • 0.6.1 [deprecated]           ...           5 years ago
  • 0.6.0 [deprecated]           ...           5 years ago
  • 0.5.2 [deprecated]           ...           5 years ago
  • 0.5.1 [deprecated]           ...           5 years ago
  • 0.5.0 [deprecated]           ...           5 years ago
  • 0.4.0 [deprecated]           ...           5 years ago
  • 0.3.3 [deprecated]           ...           5 years ago
  • 0.3.2 [deprecated]           ...           5 years ago
  • 0.3.1 [deprecated]           ...           5 years ago
  • 0.3.0 [deprecated]           ...           5 years ago
  • 0.2.0 [deprecated]           ...           5 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (12)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE