forked from Joystream/joystream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
50 lines (44 loc) · 1.89 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api'
import { NodeSDK } from '@opentelemetry/sdk-node'
import 'dotenv/config'
import { DefaultInstrumentation, DistributorNodeInstrumentation, StorageNodeInstrumentation } from './instrumentations'
// For troubleshooting, set the log level to DiagLogLevel.DEBUG
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO)
async function addInstrumentation() {
const applicationName = process.env.OTEL_APPLICATION
let instrumentation: NodeSDK
if (applicationName) {
if (applicationName === 'distributor-node') {
instrumentation = DistributorNodeInstrumentation
diag.info(`Loaded Application Instrumentation: "Distributor Node"`)
} else if (applicationName === 'storage-node') {
instrumentation = StorageNodeInstrumentation
diag.info(`Loaded Application Instrumentation: "Storage Node"`)
} else if (applicationName === 'query-node') {
instrumentation = DefaultInstrumentation
diag.info(`Loaded Application Instrumentation: "Query Node"`)
} else {
instrumentation = DefaultInstrumentation
diag.warn(
`Opentelemetry instrumentation for package/application "${applicationName}"` +
` does not exist. Falling back to running "Default" instrumentation.`
)
}
} else {
instrumentation = DefaultInstrumentation
diag.info(`Loaded Application Instrumentation: "Default"`)
}
// Start Opentelemetry NodeJS Instrumentation
diag.info('Starting tracing...')
instrumentation.start()
// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
instrumentation
.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.log('Error terminating tracing', error))
.finally(() => process.exit(0))
})
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
addInstrumentation()