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

Commit e272ccc

Browse files
authored
Addition of Stats Exporter for Microsoft Azure (#795)
1 parent 910d123 commit e272ccc

File tree

15 files changed

+3736
-0
lines changed

15 files changed

+3736
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"nyc": {
3+
"all": true,
4+
"include": [
5+
"test/**/*.ts"
6+
]
7+
}
8+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
const AS = require('../build/src/azure-stats');
18+
const OC = require('@opencensus/core');
19+
20+
function exportSingleMetric() {
21+
// Construct and register an AzureStatsExporter with the OpenCensus library.
22+
const exporter = new AS.AzureStatsExporter({
23+
instrumentationKey: 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
24+
logger: new OC.logger.ConsoleLogger(process.argv[2] || 'info'),
25+
periodInMillis: 15000,
26+
exportMode: AS.ExportMode.BATCH,
27+
aggregationMethod: AS.AggregationMethod.AVERAGE
28+
});
29+
OC.globalStats.registerExporter(exporter);
30+
31+
// Create a dummy metric.
32+
const mDummy2 = OC.globalStats.createMeasureInt64('test/dummy2', OC.MeasureUnit.UNIT, 'This variable has absolutely no meaning.');
33+
const dumbTagKey = { name: 'dumb' };
34+
35+
const view = OC.globalStats.createView(
36+
'dummy/view2',
37+
mDummy2,
38+
OC.AggregationType.SUM,
39+
[dumbTagKey],
40+
'An average of the dummy measure.'
41+
);
42+
OC.globalStats.registerView(view);
43+
44+
// Create the tag map so we can set values for our dummy tags.
45+
const tags = new OC.TagMap();
46+
47+
// Set a value for each.
48+
tags.set(dumbTagKey, { value: 'dumb' });
49+
50+
// Loop through an arbitrary amount of numbers, and record them as 'metrics'.
51+
setInterval(() => {
52+
let randomCap = Math.floor(Math.random() * 901) + 100;
53+
let randomFloor = Math.floor(Math.random() * randomCap);
54+
for (let i = 0; i < 100; i++) {
55+
let randomNum = Math.floor(Math.random() * (randomCap + 1)) + randomFloor;
56+
OC.globalStats.record([{
57+
measure: mDummy2,
58+
value: randomNum
59+
}], tags);
60+
}
61+
}, 6000);
62+
63+
}
64+
65+
exportSingleMetric();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 tracing = require('@opencensus/nodejs');
20+
const AzureTrace = require('../build/src/azure-trace');
21+
const ApplicationInsights = require('applicationinsights');
22+
23+
// Start the global tracing object
24+
const tracer = tracing.start({samplingRate: 1}).tracer;
25+
26+
function doWork() {
27+
for (let i = 0; i < 10; i++) {
28+
const span = tracer.startChildSpan('doWork');
29+
span.start();
30+
31+
for (let j = 0; j < 100000; j++);
32+
span.addAnnotation('Invoking doWork');
33+
for (let j = 0; j < 20000000; j++);
34+
span.end();
35+
}
36+
}
37+
38+
function delay(ms) {
39+
return new Promise(resolve => setTimeout(resolve, ms));
40+
}
41+
42+
async function exportSingleTrace() {
43+
// Construct and register an AzureStatsExporter with the OpenCensus library.
44+
45+
const exporterOptions = {
46+
instrumentationKey: 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
47+
logger: new OpenCensus.logger.ConsoleLogger(process.argv[2] || 'info'),
48+
maxBatchInterval: 1
49+
};
50+
51+
const exporter = new AzureTrace.AzureTraceExporter(exporterOptions);
52+
// OpenCensus.globalStats.registerExporter(exporter);
53+
54+
// Register the Azure trace exporter so that the global object will utilize
55+
// the Azure exporter to push traces
56+
tracer.registerSpanEventListener(exporter);
57+
58+
// Start an arbitrary root span
59+
tracer.startRootSpan({ name: 'root-s01'}, rootSpan => {
60+
// Do some arbitrary work, and create children spans while we are at it.
61+
for(let i = 0; i < 10; i++) {
62+
doWork();
63+
}
64+
// End the root span, triggering the publishing of it.
65+
rootSpan.end();
66+
});
67+
68+
// Pause execution of the script for 20 seconds, to allow the ExportBuffer that controls
69+
// the publish() method within the Trace Exporter time to complete logging
70+
await delay(20 * 1000);
71+
}
72+
73+
exportSingleTrace();
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)