From 1323755258ed0530d7ae0c56569caaa97671056e Mon Sep 17 00:00:00 2001 From: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com> Date: Sat, 30 Dec 2023 09:54:16 +0000 Subject: [PATCH] fix(otel): Fix OTel sdk loading (#9777) --- packages/api-server/src/watch.ts | 33 +++++++++++-------- .../experimental/setupOpentelemetryHandler.js | 2 +- .../templates/opentelemetry.ts.template | 29 ++++++++-------- .../src/__tests__/config.test.ts | 1 - packages/project-config/src/config.ts | 1 - 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/api-server/src/watch.ts b/packages/api-server/src/watch.ts index 7c204a9a91b0..3de776455599 100644 --- a/packages/api-server/src/watch.ts +++ b/packages/api-server/src/watch.ts @@ -84,21 +84,26 @@ const rebuildApiServer = () => { // OpenTelemetry SDK Setup if (getConfig().experimental.opentelemetry.enabled) { - const opentelemetrySDKScriptPath = - getConfig().experimental.opentelemetry.apiSdk - if (opentelemetrySDKScriptPath) { - console.log( - `Setting up OpenTelemetry using the setup file: ${opentelemetrySDKScriptPath}` + // We expect the OpenTelemetry SDK setup file to be in a specific location + const opentelemetrySDKScriptPath = path.join( + getPaths().api.dist, + 'opentelemetry.js' + ) + const opentelemetrySDKScriptPathRelative = path.relative( + getPaths().base, + opentelemetrySDKScriptPath + ) + console.log( + `Setting up OpenTelemetry using the setup file: ${opentelemetrySDKScriptPathRelative}` + ) + if (fs.existsSync(opentelemetrySDKScriptPath)) { + forkOpts.execArgv = forkOpts.execArgv.concat([ + `--require=${opentelemetrySDKScriptPath}`, + ]) + } else { + console.error( + `OpenTelemetry setup file does not exist at ${opentelemetrySDKScriptPathRelative}` ) - if (fs.existsSync(opentelemetrySDKScriptPath)) { - forkOpts.execArgv = forkOpts.execArgv.concat([ - `--require=${opentelemetrySDKScriptPath}`, - ]) - } else { - console.error( - `OpenTelemetry setup file does not exist at ${opentelemetrySDKScriptPath}` - ) - } } } diff --git a/packages/cli/src/commands/experimental/setupOpentelemetryHandler.js b/packages/cli/src/commands/experimental/setupOpentelemetryHandler.js index eef9373fd67d..72478ed7d852 100644 --- a/packages/cli/src/commands/experimental/setupOpentelemetryHandler.js +++ b/packages/cli/src/commands/experimental/setupOpentelemetryHandler.js @@ -69,7 +69,7 @@ export const handler = async ({ force, verbose }) => { writeFile( redwoodTomlPath, configContent.concat( - `\n[experimental.opentelemetry]\n\tenabled = true\n\twrapApi = true\n\tapiSdk = "${opentelemetryScriptPath}"` + `\n[experimental.opentelemetry]\n\tenabled = true\n\twrapApi = true` ), { overwriteExisting: true, // redwood.toml always exists diff --git a/packages/cli/src/commands/experimental/templates/opentelemetry.ts.template b/packages/cli/src/commands/experimental/templates/opentelemetry.ts.template index d737f9c5f50a..a73897fac5cc 100644 --- a/packages/cli/src/commands/experimental/templates/opentelemetry.ts.template +++ b/packages/cli/src/commands/experimental/templates/opentelemetry.ts.template @@ -1,21 +1,17 @@ -const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api') -const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http') -const { registerInstrumentations } = require('@opentelemetry/instrumentation') -const { - FastifyInstrumentation, -} = require('@opentelemetry/instrumentation-fastify') -const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http') -const { Resource } = require('@opentelemetry/resources') -const { +import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api' +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http' +import { registerInstrumentations } from '@opentelemetry/instrumentation' +import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify' +import { HttpInstrumentation } from '@opentelemetry/instrumentation-http' +import { Resource } from '@opentelemetry/resources' +import { NodeTracerProvider, SimpleSpanProcessor, -} = require('@opentelemetry/sdk-trace-node') -const { - SemanticResourceAttributes, -} = require('@opentelemetry/semantic-conventions') -const { PrismaInstrumentation } = require ('@prisma/instrumentation') +} from '@opentelemetry/sdk-trace-node' +import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' +import { PrismaInstrumentation } from '@prisma/instrumentation' -const { getConfig } = require('@redwoodjs/project-config') +import { getConfig } from '@redwoodjs/project-config' // You may wish to set this to DiagLogLevel.DEBUG when you need to debug opentelemetry itself diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO) @@ -33,6 +29,7 @@ const exporter = new OTLPTraceExporter({ // The redwood development studio (`yarn rw exp studio`) can collect your // telemetry at `http://127.0.0.1:/v1/traces` (default PORT is 4318) url: `http://127.0.0.1:${studioPort}/v1/traces`, + concurrencyLimit: 64, }) // You may wish to switch to BatchSpanProcessor in production as it is the recommended choice for performance reasons @@ -51,7 +48,7 @@ registerInstrumentations({ new FastifyInstrumentation(), new PrismaInstrumentation({ middleware: true, - }) + }), ], }) diff --git a/packages/project-config/src/__tests__/config.test.ts b/packages/project-config/src/__tests__/config.test.ts index fe0ca7450fb7..2f739069ccb0 100644 --- a/packages/project-config/src/__tests__/config.test.ts +++ b/packages/project-config/src/__tests__/config.test.ts @@ -55,7 +55,6 @@ describe('getConfig', () => { ], }, "opentelemetry": { - "apiSdk": undefined, "enabled": false, "wrapApi": true, }, diff --git a/packages/project-config/src/config.ts b/packages/project-config/src/config.ts index b3a32c3b31cc..2c54c447a9e5 100644 --- a/packages/project-config/src/config.ts +++ b/packages/project-config/src/config.ts @@ -158,7 +158,6 @@ const DEFAULT_CONFIG: Config = { opentelemetry: { enabled: false, wrapApi: true, - apiSdk: undefined, }, studio: { basePort: 4318,