Research
Security News
Malicious PyPI Package ‘pycord-self’ Targets Discord Developers with Token Theft and Backdoor Exploit
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
@opentelemetry/sdk-metrics-base
Advanced tools
@opentelemetry/sdk-metrics-base is a package that provides the core functionalities for collecting and reporting metrics in applications using OpenTelemetry. It allows developers to create and manage various types of metrics, such as counters, gauges, and histograms, and export them to different backends for monitoring and analysis.
Creating a Meter
This code demonstrates how to create a MeterProvider and obtain a Meter instance. A Meter is used to create and manage metrics.
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const meterProvider = new MeterProvider();
const meter = meterProvider.getMeter('example-meter');
Creating a Counter
This code shows how to create a Counter metric and increment its value. Counters are used to count occurrences of events.
const counter = meter.createCounter('example_counter', {
description: 'An example counter'
});
counter.add(10);
Creating a Histogram
This code demonstrates how to create a Histogram metric and record a value. Histograms are used to measure the distribution of values.
const histogram = meter.createHistogram('example_histogram', {
description: 'An example histogram'
});
histogram.record(100);
Creating an UpDownCounter
This code shows how to create an UpDownCounter metric and adjust its value up or down. UpDownCounters are used to track values that can increase or decrease.
const upDownCounter = meter.createUpDownCounter('example_updowncounter', {
description: 'An example updown counter'
});
upDownCounter.add(5);
upDownCounter.add(-3);
Exporting Metrics
This code demonstrates how to export metrics using a ConsoleMetricExporter. Metrics can be exported to various backends for monitoring and analysis.
const { ConsoleMetricExporter, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base');
const exporter = new ConsoleMetricExporter();
meterProvider.addMetricReader(new PeriodicExportingMetricReader({ exporter }));
prom-client is a Prometheus client for Node.js that allows you to create and manage metrics, such as counters, gauges, histograms, and summaries. It is similar to @opentelemetry/sdk-metrics-base in that it provides a way to collect and export metrics, but it is specifically designed for use with Prometheus.
statsd is a simple, lightweight daemon that listens for statistics, like counters and timers, sent over UDP or TCP and sends aggregates to one or more pluggable backend services (e.g., Graphite). It is similar to @opentelemetry/sdk-metrics-base in that it collects and reports metrics, but it is more focused on network-based metric collection.
newrelic is a Node.js agent for New Relic, which provides performance monitoring and management for applications. It is similar to @opentelemetry/sdk-metrics-base in that it collects and reports metrics, but it is specifically designed to work with the New Relic platform.
OpenTelemetry metrics allow a user to collect data and export it to a metrics backend like Prometheus.
npm install --save @opentelemetry/sdk-metrics-base
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:
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
// 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 attributes = { pid: process.pid };
counter.add(10, attributes);
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:
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
// 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 attributes = { pid: process.pid };
counter.add(Math.random() > 0.5 ? 1 : -1, attributes);
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/sdk-metrics-base');
const meter = new MeterProvider().getMeter('your-meter-name');
// async callback - for operation that needs to wait for value
meter.createObservableGauge('your_metric_name', {
description: 'Example of an async observable gauge with callback',
}, async (observableResult) => {
const value = await getAsyncValue();
observableResult.observe(value, { attribute: '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.createObservableGauge('your_metric_name', {
description: 'Example of a sync observable gauge with callback',
}, (observableResult) => {
observableResult.observe(getRandomValue(), { attribute: '1' });
observableResult.observe(getRandomValue(), { attribute: '2' });
});
function getRandomValue() {
return Math.random();
}
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/sdk-metrics-base');
const meter = new MeterProvider().getMeter('your-meter-name');
// async callback - for operation that needs to wait for value
meter.createObservableUpDownCounter('your_metric_name', {
description: 'Example of an async observable up down counter with callback',
}, async (observableResult) => {
const value = await getAsyncValue();
observableResult.observe(value, { attribute: '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.createObservableUpDownCounter('your_metric_name', {
description: 'Example of a sync observable up down counter with callback',
}, (observableResult) => {
observableResult.observe(getRandomValue(), { attribute: '1' });
});
function getRandomValue() {
return Math.random();
}
Choose this kind of metric when collecting a sum that never decreases. The callback can be sync or async.
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const meter = new MeterProvider().getMeter('your-meter-name');
// async callback in case you need to wait for values
meter.createObservableCounter('example_metric', {
description: 'Example of an async observable counter with callback',
}, async (observableResult) => {
const value = await getAsyncValue();
observableResult.observe(value, { attribute: '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.createObservableCounter('example_metric', {
description: 'Example of a sync observable counter with callback',
}, (observableResult) => {
const value = getRandomValue();
observableResult.observe(value, { attribute: '1' });
});
function getRandomValue() {
return Math.random();
}
Histogram
is a non-additive synchronous instrument useful for recording any non-additive number, positive or negative.
Values captured by Histogram.record(value)
are treated as individual events belonging to a distribution that is being summarized.
Histogram
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.
Apache 2.0 - See LICENSE for more information.
1.0.1 / Experimental 0.27.0
opentelemetry-core
opentelemetry-core
opentelemetry-semantic-conventions
opentelemetry-core
, opentelemetry-sdk-trace-base
opentelemetry-core
opentelemetry-exporter-zipkin
opentelemetry-core
opentelemetry-sdk-trace-base
, opentelemetry-sdk-trace-node
, opentelemetry-sdk-trace-web
opentelemetry-context-async-hooks
, opentelemetry-context-zone-peer-dep
, opentelemetry-core
, opentelemetry-exporter-jaeger
, opentelemetry-exporter-zipkin
, opentelemetry-propagator-b3
, opentelemetry-propagator-jaeger
, opentelemetry-resources
, opentelemetry-sdk-trace-base
, opentelemetry-sdk-trace-node
, opentelemetry-sdk-trace-web
, opentelemetry-shim-opentracing
opentelemetry-core
FAQs
Work in progress OpenTelemetry metrics SDK
We found that @opentelemetry/sdk-metrics-base demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.