-
Notifications
You must be signed in to change notification settings - Fork 96
Add gauge example #460
Add gauge example #460
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.'); | ||
}, 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
I have changed the example to show derived gauge example with queue depth. Do you think we should add support for function based extractor? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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()); | ||
draffensperger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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); |
There was a problem hiding this comment.
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.