-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched to simplified version of OTel setup
ref https://linear.app/ghost/issue/AP-603/logging-and-debugging-improvements - changes from previous approaches include using the minimum setup needed to make things work, and disabling as much as I can in Sentry - this seems to work locally using Jaeger, so we'll have to see if it works in GCP
- Loading branch information
1 parent
410cd0f
commit 3a96ab1
Showing
4 changed files
with
656 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,65 @@ | ||
import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api'; | ||
import { TraceExporter } from '@google-cloud/opentelemetry-cloud-trace-exporter'; | ||
import { | ||
BatchSpanProcessor, | ||
SimpleSpanProcessor, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
DiagConsoleLogger, | ||
DiagLogLevel, | ||
diag, | ||
trace, | ||
} from '@opentelemetry/api'; | ||
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; | ||
import { NodeSDK } from '@opentelemetry/sdk-node'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
if (process.env.OTEL_DEBUG_LOGGING) { | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL); | ||
} else { | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO); | ||
} | ||
} | ||
|
||
const sdk = new NodeSDK({ | ||
instrumentations: getNodeAutoInstrumentations(), | ||
serviceName: 'activitypub', | ||
traceExporter: process.env.K_SERVICE | ||
? new TraceExporter() | ||
: new OTLPTraceExporter({ | ||
url: 'http://jaeger:4318/v1/traces', | ||
}), | ||
}); | ||
|
||
try { | ||
sdk.start(); | ||
} catch (error) { | ||
console.error('Failed to start OpenTelemetry SDK:', error); | ||
} | ||
|
||
const tracer = trace.getTracer('activitypub'); | ||
|
||
if (process.env.SENTRY_DSN) { | ||
const sentryClient = Sentry.init({ | ||
Sentry.init({ | ||
dsn: process.env.SENTRY_DSN, | ||
environment: process.env.NODE_ENV || 'unknown', | ||
release: process.env.K_REVISION, | ||
tracesSampleRate: 1.0, | ||
tracesSampleRate: 0, | ||
skipOpenTelemetrySetup: true, | ||
defaultIntegrations: false, | ||
}); | ||
|
||
if (process.env.K_SERVICE) { | ||
const { TraceExporter } = await import( | ||
'@google-cloud/opentelemetry-cloud-trace-exporter' | ||
); | ||
sentryClient?.traceProvider?.addSpanProcessor( | ||
new BatchSpanProcessor(new TraceExporter({})), | ||
); | ||
} | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
const { OTLPTraceExporter } = await import( | ||
'@opentelemetry/exporter-trace-otlp-proto' | ||
); | ||
sentryClient?.traceProvider?.addSpanProcessor( | ||
new SimpleSpanProcessor( | ||
new OTLPTraceExporter({ | ||
url: 'http://jaeger:4318/v1/traces', | ||
}), | ||
), | ||
); | ||
} | ||
} | ||
|
||
export function spanWrapper<TArgs extends unknown[], TReturn>( | ||
fn: (...args: TArgs) => TReturn, | ||
) { | ||
return (...args: TArgs) => { | ||
return Sentry.startSpan( | ||
{ | ||
op: 'fn', | ||
name: fn.name || 'anonymous', | ||
}, | ||
() => fn(...args), | ||
); | ||
return tracer.startActiveSpan(fn.name || 'anonymous', (span) => { | ||
try { | ||
return fn(...args); | ||
} catch (error) { | ||
span.recordException(error as Error); | ||
throw error; | ||
} finally { | ||
span.end(); | ||
} | ||
}); | ||
}; | ||
} |
Oops, something went wrong.