Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 227fb39

Browse files
authored
Add gauge example (#460)
* Add basic gauge example * Record process uptime directly * Use Queue related example for derived gauge
1 parent 27de0b1 commit 227fb39

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This is an example shows how to create a Derived Gauge metric. This gauge is
19+
* self sufficient once created
20+
*/
21+
22+
const { Metrics, MeasureUnit } = require('@opencensus/core');
23+
24+
// [UNCOMMENT THIS BLOCK to visualize the data =======================]
25+
// // Enable OpenCensus exporters to export gauges to Stackdriver Monitoring.
26+
// const { StackdriverStatsExporter } = require('@opencensus/exporter-stackdriver');
27+
// const exporter = new StackdriverStatsExporter({ projectId: 'projectId' });
28+
// const { globalStats } = require('@opencensus/core');
29+
// globalStats.registerExporter(exporter);
30+
// [END setup_exporter ==============================================]
31+
32+
// To instrument a queue's depth.
33+
class QueueManager {
34+
constructor () { this.depth = 0; }
35+
getValue () { return this.depth; }
36+
addJob () { this.depth++; }
37+
}
38+
39+
// a registry is a collection of metric objects.
40+
const metricRegistry = Metrics.getMetricRegistry();
41+
42+
// application labels - applied to each metric / gauge.
43+
const labelKeys = [{ key: 'VM', description: 'VM Description' }];
44+
const labelValues = [{ value: 'localhost' }];
45+
46+
// a new gauge instance - builds a new Int64 gauge to be added to the registry.
47+
const metricOptions = {
48+
description: 'Number of active handles',
49+
unit: MeasureUnit.UNIT,
50+
labelKeys: labelKeys
51+
};
52+
const gauge = metricRegistry.addDerivedInt64Gauge('active_handles_total', metricOptions);
53+
54+
const queue = new QueueManager();
55+
queue.addJob();
56+
57+
// The value of the gauge is observed from the obj whenever metrics are
58+
// collected. In this case it will be 1.
59+
gauge.createTimeSeries(labelValues, queue);

examples/gauge/gauge-quickstart.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This is an example shows how to create a Gauge metric and manually set or
19+
* add value of the gauge. Gauge metric API is to report instantaneous
20+
* measurement of an int64/double value. Gauges can go both up and down. The
21+
* gauges values can be negative.
22+
*/
23+
24+
const { Metrics, MeasureUnit } = require('@opencensus/core');
25+
26+
// [UNCOMMENT THIS BLOCK to visualize the data =======================]
27+
// // Enable OpenCensus exporters to export gauges to Stackdriver Monitoring.
28+
// const { StackdriverStatsExporter } = require('@opencensus/exporter-stackdriver');
29+
// const exporter = new StackdriverStatsExporter({ projectId: 'projectId' });
30+
// const { globalStats } = require('@opencensus/core');
31+
// globalStats.registerExporter(exporter);
32+
// [END setup_exporter ==============================================]
33+
34+
// a registry is a collection of metric objects.
35+
const metricRegistry = Metrics.getMetricRegistry();
36+
37+
// application labels - applied to each metric / gauge.
38+
const labelKeys = [{ key: 'VM', description: 'VM Description' }];
39+
const labelValues = [{ value: 'localhost' }];
40+
41+
// a new gauge instance - builds a new Int64 gauge to be added to the registry.
42+
const metricOptions = {
43+
description: 'The number of seconds the current Node.js process has been running',
44+
unit: MeasureUnit.SEC,
45+
labelKeys: labelKeys
46+
};
47+
const gauge = metricRegistry.addDoubleGauge('process_uptime', metricOptions);
48+
49+
// It is recommended to keep a reference of a point for manual operations.
50+
const point = gauge.getOrCreateTimeSeries(labelValues);
51+
point.set(process.uptime());
52+
53+
for (let i = 0; i < 1000000000; i++) {
54+
// do some work here
55+
}
56+
57+
point.set(process.uptime());

0 commit comments

Comments
 (0)