Elie Ghazal
01/26/2023, 9:52 PMimport { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { Resource } from '@opentelemetry/resources'
import process from 'process'
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core'
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'
import { NodeSDK } from '@opentelemetry/sdk-node'
import { ConfigService } from '@nestjs/config'
export async function initTrace(configService: ConfigService<Record<string, unknown>, false>) {
const exporterOptions = {
url: configService.get<string>('gateway-graphql.APM_URL') || '0.0.0.0',
}
const traceExporter = new OTLPTraceExporter(exporterOptions)
const tracer = new NodeSDK({
traceExporter,
instrumentations: [new GraphQLInstrumentation(),new HttpInstrumentation(),new NestInstrumentation()],
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'gateway-graphql',
}),
})
tracer
.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', () => {
tracer
.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.log('Error terminating tracing', error))
.finally(() => process.exit(0))
})
await tracer.start()
console.log('tracer.started')
}
Srikanth Chekuri
01/27/2023, 12:11 AMElie Ghazal
01/27/2023, 11:41 AMSrikanth Chekuri
01/27/2023, 12:07 PMElie Ghazal
01/27/2023, 12:12 PMSrikanth Chekuri
01/27/2023, 12:14 PMElie Ghazal
01/27/2023, 12:16 PM