PeerTube/server/lib/opentelemetry/tracing.ts

95 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-07-28 10:56:05 +02:00
import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize'
2022-08-11 14:10:18 +02:00
import { context, diag, DiagLogLevel, trace } from '@opentelemetry/api'
2022-07-05 15:43:21 +02:00
import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
import { registerInstrumentations } from '@opentelemetry/instrumentation'
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
import FsInstrumentation from '@opentelemetry/instrumentation-fs'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg'
import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis-4'
import { Resource } from '@opentelemetry/resources'
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { logger } from '@server/helpers/logger'
import { CONFIG } from '@server/initializers/config'
2022-08-11 14:10:18 +02:00
const tracer = trace.getTracer('peertube')
2022-07-05 15:43:21 +02:00
function registerOpentelemetryTracing () {
if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
logger.info('Registering Open Telemetry tracing')
const customLogger = (level: string) => {
return (message: string, ...args: unknown[]) => {
let fullMessage = message
for (const arg of args) {
if (typeof arg === 'string') fullMessage += arg
else break
}
logger[level](fullMessage)
}
}
diag.setLogger({
error: customLogger('error'),
warn: customLogger('warn'),
info: customLogger('info'),
debug: customLogger('debug'),
verbose: customLogger('verbose')
}, DiagLogLevel.INFO)
const tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'peertube'
})
})
registerInstrumentations({
2022-07-13 11:58:01 +02:00
tracerProvider,
2022-07-05 15:43:21 +02:00
instrumentations: [
new PgInstrumentation({
enhancedDatabaseReporting: true
}),
new DnsInstrumentation(),
new HttpInstrumentation(),
new ExpressInstrumentation(),
new RedisInstrumentation({
dbStatementSerializer: function (cmdName, cmdArgs) {
return [ cmdName, ...cmdArgs ].join(' ')
}
}),
2022-07-28 10:56:05 +02:00
new FsInstrumentation(),
new SequelizeInstrumentation()
2022-07-05 15:43:21 +02:00
]
})
tracerProvider.addSpanProcessor(
new BatchSpanProcessor(
new JaegerExporter({ endpoint: CONFIG.OPEN_TELEMETRY.TRACING.JAEGER_EXPORTER.ENDPOINT })
)
)
tracerProvider.register()
}
2022-08-11 14:10:18 +02:00
async function wrapWithSpanAndContext <T> (spanName: string, cb: () => Promise<T>) {
const span = tracer.startSpan(spanName)
const activeContext = trace.setSpan(context.active(), span)
const result = await context.with(activeContext, () => cb())
span.end()
return result
}
2022-07-05 15:43:21 +02:00
export {
registerOpentelemetryTracing,
2022-08-11 14:10:18 +02:00
tracer,
wrapWithSpanAndContext
2022-07-05 15:43:21 +02:00
}