Compare commits

...

1 Commits

Author SHA1 Message Date
unlikelyzero
fedb8fb908 tracing.js 2022-03-01 11:58:07 -08:00
2 changed files with 59 additions and 1 deletions

View File

@@ -109,5 +109,11 @@
},
"author": "",
"license": "Apache-2.0",
"private": true
"private": true,
"dependencies": {
"@opentelemetry/api": "^1.0.4",
"@opentelemetry/auto-instrumentations-node": "^0.27.3",
"@opentelemetry/instrumentation-long-task": "^0.27.0",
"@opentelemetry/sdk-node": "^0.27.0"
}
}

52
tracing.js Normal file
View File

@@ -0,0 +1,52 @@
'use strict';
const process = require('process');
const opentelemetry = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { WebTracerProvider } = require('@opentelemetry/sdk-trace-web');
const { LongTaskInstrumentation } = require('@opentelemetry/instrumentation-long-task');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
//Long Task instrumentation
//https://github.com/t2t2/opentelemetry-js-contrib/blob/62ef9b42478dee5bdbc522ab9bb7254966cb3b8f/plugins/web/opentelemetry-instrumentation-long-task/README.md#usage
const provider = new WebTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
new LongTaskInstrumentation()
]
});
//End Long Task instrumentation
// configure the SDK to export telemetry data to the console
// enable all auto-instrumentations from the meta package
const traceExporter = new ConsoleSpanExporter();
const sdk = new opentelemetry.NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'my-service'
}),
traceExporter,
instrumentations: [getNodeAutoInstrumentations()]
});
// initialize the SDK and register with the OpenTelemetry API
// this enables the API to record telemetry
sdk.start()
.then(() => console.log('Tracing initialized'))
.catch((error) => console.log('Error initializing tracing', error));
// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
sdk.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.log('Error terminating tracing', error))
.finally(() => process.exit(0));
});