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 1 commit
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
51 changes: 51 additions & 0 deletions examples/gauge/derived-gauge-quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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 ==============================================]

setTimeout(() => {
console.log('Timeout executed.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you document what this timeout is for? I remember in another example the timeout kept the Node process alive while the metric was being collected.

}, 1000);

// 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);

gauge.createTimeSeries(labelValues, process._getActiveHandles());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you pass process._getActiveHandles() vs. passing the function itself process._getActiveHandles? I would have expected it to take a function that it calls to calculate the gauge value rather than the output of a function for a single value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createTimeSeries method accepts the object with AccessorInterface type.

AccessorInterface = LengthAttributeInterface | LengthMethodInterface | 
                    SizeAttributeInterface | SizeMethodInterface | ToValueInterface;

I have changed the example to show derived gauge example with queue depth. Do you think we should add support for function based extractor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense. Yes, I think supporting a function as well would be nice so that people can just pass in an anonymous arrow function easily.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sure, I will open an issue to include function based extractor in derived gauge API. Thanks

53 changes: 53 additions & 0 deletions examples/gauge/gauge-quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 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 ==============================================]

const nowInSeconds = Math.round(Date.now() / 1000 - process.uptime());

// 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: 'Start time of the process since unix epoch in seconds',
unit: MeasureUnit.SEC,
labelKeys: labelKeys
};
const gauge = metricRegistry.addInt64Gauge('process_start_time_seconds', metricOptions);

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