|
| 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); |
0 commit comments