From 12cbb22b8512478c191dada145904bf0fc16a354 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Fri, 21 Jun 2024 11:11:00 +0100 Subject: [PATCH] Lazyloaded OpenTelemetry packages to avoid boot time regression - we don't want to load the Otel packages unless the instrumentation is enabled, because they dramatically increase the boot time (2x locally!) --- ghost/core/core/shared/instrumentation.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ghost/core/core/shared/instrumentation.js b/ghost/core/core/shared/instrumentation.js index 841029ffed..d4ab13a51b 100644 --- a/ghost/core/core/shared/instrumentation.js +++ b/ghost/core/core/shared/instrumentation.js @@ -1,7 +1,3 @@ -const {NodeSDK} = require('@opentelemetry/sdk-node'); -const {OTLPTraceExporter} = require('@opentelemetry/exporter-trace-otlp-http'); -const {getNodeAutoInstrumentations} = require('@opentelemetry/auto-instrumentations-node'); - async function initOpenTelemetry({config}) { // Always enable in development environment // In production, only enable if explicitly enabled via config `opentelemetry:enabled` @@ -16,6 +12,12 @@ async function initOpenTelemetry({config}) { headers: {}, concurrencyLimit: 10 }; + + // Lazyloaded to avoid boot time overhead when not enabled + const {NodeSDK} = require('@opentelemetry/sdk-node'); + const {OTLPTraceExporter} = require('@opentelemetry/exporter-trace-otlp-http'); + const {getNodeAutoInstrumentations} = require('@opentelemetry/auto-instrumentations-node'); + const sdk = new NodeSDK({ serviceName: 'ghost', traceExporter: new OTLPTraceExporter(collectorOptions), @@ -28,4 +30,4 @@ async function initOpenTelemetry({config}) { module.exports = { initOpenTelemetry -}; \ No newline at end of file +};