Skip to content

Commit

Permalink
fix(otel): Fix OTel sdk loading (#9777)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Walker-GM authored and Tobbe committed Jan 1, 2024
1 parent dd684f1 commit 1323755
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
33 changes: 19 additions & 14 deletions packages/api-server/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:<PORT>/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
Expand All @@ -51,7 +48,7 @@ registerInstrumentations({
new FastifyInstrumentation(),
new PrismaInstrumentation({
middleware: true,
})
}),
],
})

Expand Down
1 change: 0 additions & 1 deletion packages/project-config/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe('getConfig', () => {
],
},
"opentelemetry": {
"apiSdk": undefined,
"enabled": false,
"wrapApi": true,
},
Expand Down
1 change: 0 additions & 1 deletion packages/project-config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ const DEFAULT_CONFIG: Config = {
opentelemetry: {
enabled: false,
wrapApi: true,
apiSdk: undefined,
},
studio: {
basePort: 4318,
Expand Down

0 comments on commit 1323755

Please sign in to comment.