Skip to content

Commit 4e61459

Browse files
mayurkale22dyladan
andauthored
chore: update metrics example with UpDownCounter (open-telemetry#1239)
* chore: update metrics example with UpDownCounter * chore: update getting-started * chore: update README * fix: lint * Update packages/opentelemetry-metrics/README.md Co-authored-by: Daniel Dyla <[email protected]> * Update packages/opentelemetry-metrics/README.md Co-authored-by: Daniel Dyla <[email protected]> Co-authored-by: Daniel Dyla <[email protected]>
1 parent d738a82 commit 4e61459

File tree

10 files changed

+46
-19
lines changed

10 files changed

+46
-19
lines changed

examples/prometheus/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ prometheus --config.file=prometheus.yml
4848

4949
If you are using the default configurations, the prometheus client will be available at <http://localhost:9090>
5050

51-
<p align="center"><img src="images/prom-ui.png?raw=true"/></p>
51+
<p align="center"><img src="images/prom-counter.png?raw=true"/></p>
52+
<p align="center"><img src="images/prom-updowncounter.png?raw=true"/></p>
5253

5354
## Useful links
5455

246 KB
Loading
-327 KB
Binary file not shown.
Loading

examples/prometheus/index.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,17 @@ const meter = new MeterProvider({
1919
interval: 1000,
2020
}).getMeter('example-prometheus');
2121

22-
// Monotonic counters can only be increased.
23-
const monotonicCounter = meter.createCounter('monotonic_counter', {
24-
monotonic: true,
25-
description: 'Example of a monotonic counter',
22+
const requestCounter = meter.createCounter('requests', {
23+
description: 'Example of a Counter',
2624
});
2725

28-
// Non-monotonic counters can be increased or decreased.
29-
const nonMonotonicCounter = meter.createCounter('non_monotonic_counter', {
30-
monotonic: false,
31-
description: 'Example of a non-monotonic counter',
26+
const upDownCounter = meter.createUpDownCounter('test_up_down_counter', {
27+
description: 'Example of a UpDownCounter',
3228
});
3329

34-
const labels = { pid: process.pid };
30+
const labels = { pid: process.pid, environment: 'staging' };
3531

3632
setInterval(() => {
37-
monotonicCounter.bind(labels).add(1);
38-
nonMonotonicCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1);
33+
requestCounter.bind(labels).add(1);
34+
upDownCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1);
3935
}, 1000);

getting-started/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ const { MeterProvider } = require('@opentelemetry/metrics');
253253
const meter = new MeterProvider().getMeter('your-meter-name');
254254
255255
const requestCount = meter.createCounter("requests", {
256-
monotonic: true,
257256
description: "Count all incoming requests"
258257
});
259258
@@ -323,7 +322,6 @@ const meter = new MeterProvider({
323322
}).getMeter('your-meter-name');
324323

325324
const requestCount = meter.createCounter("requests", {
326-
monotonic: true,
327325
description: "Count all incoming requests"
328326
});
329327

getting-started/monitored-example/monitoring.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const meter = new MeterProvider({
2020
}).getMeter('example-monitored');
2121

2222
const requestCount = meter.createCounter("requests", {
23-
monotonic: true,
2423
description: "Count all incoming requests"
2524
});
2625

getting-started/ts-example/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ import { Metric, BoundCounter } from '@opentelemetry/api';
252252
const meter = new MeterProvider().getMeter('your-meter-name');
253253
254254
const requestCount: Metric<BoundCounter> = meter.createCounter("requests", {
255-
monotonic: true,
256255
description: "Count all incoming requests"
257256
});
258257
@@ -321,7 +320,6 @@ const meter = new MeterProvider({
321320
}).getMeter('your-meter-name');
322321

323322
const requestCount: Metric<BoundCounter> = meter.createCounter("requests", {
324-
monotonic: true,
325323
description: "Count all incoming requests"
326324
});
327325

getting-started/ts-example/monitoring.ts

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const meter = new MeterProvider({
1919
}).getMeter('example-ts');
2020

2121
const requestCount: Metric<BoundCounter> = meter.createCounter("requests", {
22-
monotonic: true,
2322
description: "Count all incoming requests"
2423
});
2524

packages/opentelemetry-metrics/README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ npm install --save @opentelemetry/metrics
1818

1919
### Counter
2020

21-
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. Counters are defined as `Monotonic = true` by default, meaning that positive values are expected.
21+
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.
22+
Example uses for Counter:
23+
24+
- count the number of bytes received
25+
- count the number of requests completed
26+
- count the number of accounts created
27+
- count the number of checkpoints run
28+
- count the number of 5xx errors.
2229

2330
```js
2431
const { MeterProvider } = require('@opentelemetry/metrics');
@@ -38,6 +45,35 @@ boundCounter.add(10);
3845

3946
```
4047

48+
### UpDownCounter
49+
50+
`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.
51+
52+
Example uses for UpDownCounter:
53+
54+
- count the number of active requests
55+
- count memory in use by instrumenting new and delete
56+
- count queue size by instrumenting enqueue and dequeue
57+
- count semaphore up and down operations
58+
59+
```js
60+
const { MeterProvider } = require('@opentelemetry/metrics');
61+
62+
// Initialize the Meter to capture measurements in various ways.
63+
const meter = new MeterProvider().getMeter('your-meter-name');
64+
65+
const counter = meter.createUpDownCounter('metric_name', {
66+
description: 'Example of a UpDownCounter'
67+
});
68+
69+
const labels = { pid: process.pid };
70+
71+
// Create a BoundInstrument associated with specified label values.
72+
const boundCounter = counter.bind(labels);
73+
boundCounter.add(Math.random() > 0.5 ? 1 : -1);
74+
75+
```
76+
4177
### Observable
4278

4379
Choose this kind of metric when only last value is important without worry about aggregation

0 commit comments

Comments
 (0)