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

Add gauge example #460

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions examples/gauge/derived-gauge-quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This is an example shows how to create a Derived Gauge metric. This gauge is
* self sufficient once created
*/

const { Metrics, MeasureUnit } = require('@opencensus/core');

// [UNCOMMENT THIS BLOCK to visualize the data =======================]
// // Enable OpenCensus exporters to export gauges to Stackdriver Monitoring.
// const { StackdriverStatsExporter } = require('@opencensus/exporter-stackdriver');
// const exporter = new StackdriverStatsExporter({ projectId: 'projectId' });
// const { globalStats } = require('@opencensus/core');
// globalStats.registerExporter(exporter);
// [END setup_exporter ==============================================]

// To instrument a queue's depth.
class QueueManager {
constructor () { this.depth = 0; }
getValue () { return this.depth; }
addJob () { this.depth++; }
}

// a registry is a collection of metric objects.
const metricRegistry = Metrics.getMetricRegistry();

// application labels - applied to each metric / gauge.
const labelKeys = [{ key: 'VM', description: 'VM Description' }];
const labelValues = [{ value: 'localhost' }];

// a new gauge instance - builds a new Int64 gauge to be added to the registry.
const metricOptions = {
description: 'Number of active handles',
unit: MeasureUnit.UNIT,
labelKeys: labelKeys
};
const gauge = metricRegistry.addDerivedInt64Gauge('active_handles_total', metricOptions);

const queue = new QueueManager();
queue.addJob();

// The value of the gauge is observed from the obj whenever metrics are
// collected. In this case it will be 1.
gauge.createTimeSeries(labelValues, queue);
57 changes: 57 additions & 0 deletions examples/gauge/gauge-quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This is an example shows how to create a Gauge metric and manually set or
* add value of the gauge. Gauge metric API is to report instantaneous
* measurement of an int64/double value. Gauges can go both up and down. The
* gauges values can be negative.
*/

const { Metrics, MeasureUnit } = require('@opencensus/core');

// [UNCOMMENT THIS BLOCK to visualize the data =======================]
// // Enable OpenCensus exporters to export gauges to Stackdriver Monitoring.
// const { StackdriverStatsExporter } = require('@opencensus/exporter-stackdriver');
// const exporter = new StackdriverStatsExporter({ projectId: 'projectId' });
// const { globalStats } = require('@opencensus/core');
// globalStats.registerExporter(exporter);
// [END setup_exporter ==============================================]

// a registry is a collection of metric objects.
const metricRegistry = Metrics.getMetricRegistry();

// application labels - applied to each metric / gauge.
const labelKeys = [{ key: 'VM', description: 'VM Description' }];
const labelValues = [{ value: 'localhost' }];

// a new gauge instance - builds a new Int64 gauge to be added to the registry.
const metricOptions = {
description: 'The number of seconds the current Node.js process has been running',
unit: MeasureUnit.SEC,
labelKeys: labelKeys
};
const gauge = metricRegistry.addDoubleGauge('process_uptime', metricOptions);

// It is recommended to keep a reference of a point for manual operations.
const point = gauge.getOrCreateTimeSeries(labelValues);
point.set(process.uptime());

for (let i = 0; i < 1000000000; i++) {
// do some work here
}

point.set(process.uptime());