Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: play with instrumenting Azure SDK usage #176

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
61 changes: 61 additions & 0 deletions examples/azure-instr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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.
*/

// Setup the Elastic OTel Node.js distro, along with extra Azure instrumentation.
// Usage: node -r ./azure-instr.js SCRIPT.js

const os = require('os');
const path = require('path');

const {
ElasticNodeSDK,
getInstrumentations,
} = require('@elastic/opentelemetry-node/sdk');

const {
createAzureSdkInstrumentation,
} = require('@azure/opentelemetry-instrumentation-azure-sdk');

const sdk = new ElasticNodeSDK({
serviceName: path.parse(process.argv[1]).name,
instrumentations: [
getInstrumentations(), // the default set of instrumentations
createAzureSdkInstrumentation(),
],
});

process.on('SIGTERM', async () => {
try {
await sdk.shutdown();
} catch (err) {
console.warn('warning: error shutting down OTel SDK', err);
}
process.exit(128 + os.constants.signals.SIGTERM);
});

process.once('beforeExit', async () => {
// Flush recent telemetry data if about the shutdown.
try {
await sdk.shutdown();
} catch (err) {
console.warn('warning: error shutting down OTel SDK', err);
}
});

sdk.start();
58 changes: 58 additions & 0 deletions examples/azure-storage-blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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.
*/

// Start an HTTP server and make a request to it.
// Usage:
// node -r @elastic/opentelemetry-node XXX simple-http-request.js

const otel = require('@opentelemetry/api');
const {DefaultAzureCredential} = require('@azure/identity');
const {BlobServiceClient} = require('@azure/storage-blob');

async function main() {
const account = 'trentmstorage1';
const defaultAzureCredential = new DefaultAzureCredential();
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
defaultAzureCredential
);

let i = 0;
for await (const container of blobServiceClient.listContainers()) {
console.log(`Container ${++i}: ${container.name}`);
}
if (i === 0) {
console.log('Account has no storage blob containers.');
}

// // Create a container
// const containerName = `newcontainer${new Date().getTime()}`;
// const containerClient = blobServiceClient.getContainerClient(containerName);
// const createContainerResponse = await containerClient.create();
// console.log(
// `Create container ${containerName} successfully`,
// createContainerResponse.requestId
// );
}

const tracer = otel.trace.getTracer('example');
tracer.startActiveSpan('manual-span', async (span) => {
await main();
span.end();
});
5 changes: 4 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"version": "0.1.0",
"scripts": {},
"devDependencies": {
"@elastic/opentelemetry-node": "*",
"@azure/identity": "^4.2.0",
"@azure/opentelemetry-instrumentation-azure-sdk": "^1.0.0-beta.5",
"@azure/storage-blob": "^12.17.0",
"@elastic/opentelemetry-node": "../packages/opentelemetry-node",
"bunyan": "^1.8.15",
"express": "^4.19.2"
}
Expand Down
25 changes: 1 addition & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "elastic-otel-node",
"version": "0.1.0",
"workspaces": [
"packages/*",
"examples"
"packages/*"
],
"license": "Apache-2.0",
"private": true,
Expand Down
22 changes: 12 additions & 10 deletions packages/mockotlpserver/lib/waterfall.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,27 @@ function renderSpan(span, prefix = '') {
BigInt(lastRenderedSpan.startTimeUnixNano)
) / 1e6;
let unit = 'ms';
if (startOffset >= 1000) {
if (Math.abs(startOffset) >= 1000) {
startOffset /= 1000;
unit = 's';
unit = 's ';
}
if (startOffset >= 1000) {
if (Math.abs(startOffset) >= 1000) {
startOffset /= 60;
unit = 'm'; // minutes
unit = 'm '; // minutes
}
if (startOffset >= 1000) {
if (Math.abs(startOffset) >= 1000) {
startOffset /= 60;
unit = 'h';
unit = 'h ';
}
if (startOffset >= 1000) {
if (Math.abs(startOffset) >= 1000) {
// Seems unlikely we'll receive a trace with *days* of offset. :)
startOffset /= 24;
unit = 'd';
unit = 'd ';
}
gutter = `${startOffset >= 0 ? '+' : '-'}${Math.floor(startOffset)}`;
gutter = `${' '.repeat(4 - gutter.length)}${gutter}${unit}`;
gutter = `${startOffset >= 0 ? '+' : ''}${Math.round(startOffset)}`;
gutter = `${' '.repeat(
Math.max(4 - gutter.length, 0)
)}${gutter}${unit}`;
} else {
gutter = ' '.repeat(6);
}
Expand Down
Loading