|
| 1 | +/** |
| 2 | + * Copyright 2020 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 | +// Import Core OpenCensus and our Azure Exporter module. |
| 18 | +const OpenCensus = require('@opencensus/core'); |
| 19 | +const AzureStats = require('../build/src/azure-stats'); |
| 20 | + |
| 21 | +function exportSingleMetrics() { |
| 22 | + // Construct and register an AzureStatsExporter with the OpenCensus library. |
| 23 | + const exporter = new AzureStats.AzureStatsExporter({ |
| 24 | + instrumentationKey: 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', |
| 25 | + logger: new OpenCensus.logger.ConsoleLogger(process.argv[2] || 'info') |
| 26 | + }); |
| 27 | + OpenCensus.globalStats.registerExporter(exporter); |
| 28 | + |
| 29 | + // Create a dummy metric. |
| 30 | + const mDummy = OpenCensus.globalStats.createMeasureInt64('test/dummy', OpenCensus.MeasureUnit.UNIT, 'This variable has absolutely no meaning.'); |
| 31 | + |
| 32 | + // Create some dummy tags. |
| 33 | + const dumbTagKey = { name: 'dumb' }; |
| 34 | + const dumberTagKey = { name: 'dumber' }; |
| 35 | + const evenDumberTagKey = { name: 'evenDumber' }; |
| 36 | + const dumbestTagKey = { name: 'dumbest' }; |
| 37 | + |
| 38 | + // Create some dummy views. |
| 39 | + const sumView = OpenCensus.globalStats.createView( |
| 40 | + 'dummy/sumView', |
| 41 | + mDummy, |
| 42 | + OpenCensus.AggregationType.SUM, |
| 43 | + [dumbTagKey], |
| 44 | + 'A sum of the dummy measure.' |
| 45 | + ); |
| 46 | + OpenCensus.globalStats.registerView(sumView); |
| 47 | + |
| 48 | + const distributionView = OpenCensus.globalStats.createView( |
| 49 | + 'dummy/distView', |
| 50 | + mDummy, |
| 51 | + OpenCensus.AggregationType.DISTRIBUTION, |
| 52 | + [dumbTagKey, dumberTagKey], |
| 53 | + 'A distribution of the dummy measure.', |
| 54 | + [0, 5, 10, 15, 20, 25, 30, 35, 40, 45] |
| 55 | + ); |
| 56 | + OpenCensus.globalStats.registerView(distributionView); |
| 57 | + |
| 58 | + const countView = OpenCensus.globalStats.createView( |
| 59 | + 'dummy/countView', |
| 60 | + mDummy, |
| 61 | + OpenCensus.AggregationType.COUNT, |
| 62 | + [dumbTagKey, dumberTagKey, evenDumberTagKey], |
| 63 | + 'A count of the dummy measure.' |
| 64 | + ); |
| 65 | + OpenCensus.globalStats.registerView(countView); |
| 66 | + |
| 67 | + const lastValueView = OpenCensus.globalStats.createView( |
| 68 | + 'dummy/lastValueView', |
| 69 | + mDummy, |
| 70 | + OpenCensus.AggregationType.LAST_VALUE, |
| 71 | + [dumbTagKey, dumberTagKey, evenDumberTagKey, dumbestTagKey], |
| 72 | + 'The last value of the dummy measure.' |
| 73 | + ); |
| 74 | + OpenCensus.globalStats.registerView(lastValueView); |
| 75 | + |
| 76 | + // Loop through an arbitrary amount of numbers, and record them as 'metrics'. |
| 77 | + for (let i = 0; i < 42; i++) { |
| 78 | + // Create the tag map so we can set values for our dummy tags. |
| 79 | + const tags = new OpenCensus.TagMap(); |
| 80 | + |
| 81 | + // Set a value for each. |
| 82 | + tags.set(dumbTagKey, { value: 'dumb' }); |
| 83 | + tags.set(dumberTagKey, { value: 'dumber' }); |
| 84 | + tags.set(evenDumberTagKey, { value: 'evenDumber' }); |
| 85 | + tags.set(dumbestTagKey, { value: 'dumbest' }); |
| 86 | + |
| 87 | + OpenCensus.globalStats.record([{ |
| 88 | + measure: mDummy, |
| 89 | + value: i |
| 90 | + }], tags); |
| 91 | + |
| 92 | + // Do something special if i is greater than 30 so we have extra things to look for in |
| 93 | + // AppInsights. |
| 94 | + if (i > 30) { |
| 95 | + tags.set(dumbTagKey, { value: 'dumb but over 30' }); |
| 96 | + OpenCensus.globalStats.record([{ |
| 97 | + measure: mDummy, |
| 98 | + value: i |
| 99 | + }], tags); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +exportSingleMetrics(); |
0 commit comments