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

Commit

Permalink
Addition of Stats Exporter for Microsoft Azure (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-long authored May 12, 2020
1 parent 910d123 commit e272ccc
Show file tree
Hide file tree
Showing 15 changed files with 3,736 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/opencensus-exporter-azure/.nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"nyc": {
"all": true,
"include": [
"test/**/*.ts"
]
}
}
65 changes: 65 additions & 0 deletions packages/opencensus-exporter-azure/bbtest/batch_blackbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2020 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.
*/

const AS = require('../build/src/azure-stats');
const OC = require('@opencensus/core');

function exportSingleMetric() {
// Construct and register an AzureStatsExporter with the OpenCensus library.
const exporter = new AS.AzureStatsExporter({
instrumentationKey: 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
logger: new OC.logger.ConsoleLogger(process.argv[2] || 'info'),
periodInMillis: 15000,
exportMode: AS.ExportMode.BATCH,
aggregationMethod: AS.AggregationMethod.AVERAGE
});
OC.globalStats.registerExporter(exporter);

// Create a dummy metric.
const mDummy2 = OC.globalStats.createMeasureInt64('test/dummy2', OC.MeasureUnit.UNIT, 'This variable has absolutely no meaning.');
const dumbTagKey = { name: 'dumb' };

const view = OC.globalStats.createView(
'dummy/view2',
mDummy2,
OC.AggregationType.SUM,
[dumbTagKey],
'An average of the dummy measure.'
);
OC.globalStats.registerView(view);

// Create the tag map so we can set values for our dummy tags.
const tags = new OC.TagMap();

// Set a value for each.
tags.set(dumbTagKey, { value: 'dumb' });

// Loop through an arbitrary amount of numbers, and record them as 'metrics'.
setInterval(() => {
let randomCap = Math.floor(Math.random() * 901) + 100;
let randomFloor = Math.floor(Math.random() * randomCap);
for (let i = 0; i < 100; i++) {
let randomNum = Math.floor(Math.random() * (randomCap + 1)) + randomFloor;
OC.globalStats.record([{
measure: mDummy2,
value: randomNum
}], tags);
}
}, 6000);

}

exportSingleMetric();
73 changes: 73 additions & 0 deletions packages/opencensus-exporter-azure/bbtest/blackbox-trace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2020 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.
*/

// Import Core OpenCensus and our Azure Exporter module.
const OpenCensus = require('@opencensus/core');
const tracing = require('@opencensus/nodejs');
const AzureTrace = require('../build/src/azure-trace');
const ApplicationInsights = require('applicationinsights');

// Start the global tracing object
const tracer = tracing.start({samplingRate: 1}).tracer;

function doWork() {
for (let i = 0; i < 10; i++) {
const span = tracer.startChildSpan('doWork');
span.start();

for (let j = 0; j < 100000; j++);
span.addAnnotation('Invoking doWork');
for (let j = 0; j < 20000000; j++);
span.end();
}
}

function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function exportSingleTrace() {
// Construct and register an AzureStatsExporter with the OpenCensus library.

const exporterOptions = {
instrumentationKey: 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
logger: new OpenCensus.logger.ConsoleLogger(process.argv[2] || 'info'),
maxBatchInterval: 1
};

const exporter = new AzureTrace.AzureTraceExporter(exporterOptions);
// OpenCensus.globalStats.registerExporter(exporter);

// Register the Azure trace exporter so that the global object will utilize
// the Azure exporter to push traces
tracer.registerSpanEventListener(exporter);

// Start an arbitrary root span
tracer.startRootSpan({ name: 'root-s01'}, rootSpan => {
// Do some arbitrary work, and create children spans while we are at it.
for(let i = 0; i < 10; i++) {
doWork();
}
// End the root span, triggering the publishing of it.
rootSpan.end();
});

// Pause execution of the script for 20 seconds, to allow the ExportBuffer that controls
// the publish() method within the Trace Exporter time to complete logging
await delay(20 * 1000);
}

exportSingleTrace();
104 changes: 104 additions & 0 deletions packages/opencensus-exporter-azure/bbtest/blackbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright 2020 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.
*/

// Import Core OpenCensus and our Azure Exporter module.
const OpenCensus = require('@opencensus/core');
const AzureStats = require('../build/src/azure-stats');

function exportSingleMetrics() {
// Construct and register an AzureStatsExporter with the OpenCensus library.
const exporter = new AzureStats.AzureStatsExporter({
instrumentationKey: 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
logger: new OpenCensus.logger.ConsoleLogger(process.argv[2] || 'info')
});
OpenCensus.globalStats.registerExporter(exporter);

// Create a dummy metric.
const mDummy = OpenCensus.globalStats.createMeasureInt64('test/dummy', OpenCensus.MeasureUnit.UNIT, 'This variable has absolutely no meaning.');

// Create some dummy tags.
const dumbTagKey = { name: 'dumb' };
const dumberTagKey = { name: 'dumber' };
const evenDumberTagKey = { name: 'evenDumber' };
const dumbestTagKey = { name: 'dumbest' };

// Create some dummy views.
const sumView = OpenCensus.globalStats.createView(
'dummy/sumView',
mDummy,
OpenCensus.AggregationType.SUM,
[dumbTagKey],
'A sum of the dummy measure.'
);
OpenCensus.globalStats.registerView(sumView);

const distributionView = OpenCensus.globalStats.createView(
'dummy/distView',
mDummy,
OpenCensus.AggregationType.DISTRIBUTION,
[dumbTagKey, dumberTagKey],
'A distribution of the dummy measure.',
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]
);
OpenCensus.globalStats.registerView(distributionView);

const countView = OpenCensus.globalStats.createView(
'dummy/countView',
mDummy,
OpenCensus.AggregationType.COUNT,
[dumbTagKey, dumberTagKey, evenDumberTagKey],
'A count of the dummy measure.'
);
OpenCensus.globalStats.registerView(countView);

const lastValueView = OpenCensus.globalStats.createView(
'dummy/lastValueView',
mDummy,
OpenCensus.AggregationType.LAST_VALUE,
[dumbTagKey, dumberTagKey, evenDumberTagKey, dumbestTagKey],
'The last value of the dummy measure.'
);
OpenCensus.globalStats.registerView(lastValueView);

// Loop through an arbitrary amount of numbers, and record them as 'metrics'.
for (let i = 0; i < 42; i++) {
// Create the tag map so we can set values for our dummy tags.
const tags = new OpenCensus.TagMap();

// Set a value for each.
tags.set(dumbTagKey, { value: 'dumb' });
tags.set(dumberTagKey, { value: 'dumber' });
tags.set(evenDumberTagKey, { value: 'evenDumber' });
tags.set(dumbestTagKey, { value: 'dumbest' });

OpenCensus.globalStats.record([{
measure: mDummy,
value: i
}], tags);

// Do something special if i is greater than 30 so we have extra things to look for in
// AppInsights.
if (i > 30) {
tags.set(dumbTagKey, { value: 'dumb but over 30' });
OpenCensus.globalStats.record([{
measure: mDummy,
value: i
}], tags);
}
}
}

exportSingleMetrics();
Loading

0 comments on commit e272ccc

Please sign in to comment.