-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into mike-ap-566-implement-a-pubsub-backed-queue-…
…for-fedify
- Loading branch information
Showing
9 changed files
with
736 additions
and
125 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
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
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,52 +1,89 @@ | ||
import { CloudPropagator } from '@google-cloud/opentelemetry-cloud-trace-propagator'; | ||
import { trace } from '@opentelemetry/api'; | ||
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; | ||
import { NodeSDK } from '@opentelemetry/sdk-node'; | ||
import { | ||
BatchSpanProcessor, | ||
SimpleSpanProcessor, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
const sdk = new NodeSDK({ | ||
instrumentations: getNodeAutoInstrumentations({ | ||
'@opentelemetry/instrumentation-mysql2': { | ||
addSqlCommenterCommentToQueries: true, | ||
}, | ||
}), | ||
}); | ||
|
||
const provider = new NodeTracerProvider(); | ||
let propagator: CloudPropagator | undefined; | ||
|
||
if (process.env.K_SERVICE) { | ||
const { TraceExporter } = await import( | ||
'@google-cloud/opentelemetry-cloud-trace-exporter' | ||
); | ||
provider.addSpanProcessor( | ||
new BatchSpanProcessor( | ||
new TraceExporter({ | ||
resourceFilter: /.*/, // TODO: filter by our service name? | ||
}), | ||
), | ||
); | ||
|
||
propagator = new CloudPropagator(); | ||
} | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
const { OTLPTraceExporter } = await import( | ||
'@opentelemetry/exporter-trace-otlp-proto' | ||
); | ||
|
||
provider.addSpanProcessor( | ||
new SimpleSpanProcessor( | ||
new OTLPTraceExporter({ | ||
url: 'http://jaeger:4318/v1/traces', | ||
}), | ||
), | ||
); | ||
} | ||
|
||
provider.register({ | ||
propagator, | ||
}); | ||
|
||
export const tracer = trace.getTracer('activitypub'); | ||
|
||
try { | ||
sdk.start(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
if (process.env.SENTRY_DSN) { | ||
const client = Sentry.init({ | ||
Sentry.init({ | ||
dsn: process.env.SENTRY_DSN, | ||
environment: process.env.NODE_ENV || 'unknown', | ||
release: process.env.K_REVISION, | ||
tracesSampleRate: 1.0, | ||
}); | ||
|
||
if (process.env.K_SERVICE) { | ||
const { TraceExporter } = await import( | ||
'@google-cloud/opentelemetry-cloud-trace-exporter' | ||
); | ||
|
||
client?.traceProvider?.addSpanProcessor( | ||
new BatchSpanProcessor(new TraceExporter({})), | ||
); | ||
} | ||
|
||
if (process.env.NODE_ENV === 'testing') { | ||
const { OTLPTraceExporter } = await import( | ||
'@opentelemetry/exporter-trace-otlp-proto' | ||
); | ||
|
||
client?.traceProvider?.addSpanProcessor( | ||
new SimpleSpanProcessor( | ||
new OTLPTraceExporter({ | ||
url: 'http://jaeger:4318/v1/traces', | ||
}), | ||
), | ||
); | ||
} | ||
} | ||
|
||
export function spanWrapper<TArgs extends unknown[], TReturn>( | ||
fn: (...args: TArgs) => TReturn, | ||
fn: (...args: TArgs) => TReturn | Promise<TReturn>, | ||
) { | ||
return (...args: TArgs) => { | ||
return Sentry.startSpan( | ||
{ | ||
op: 'fn', | ||
name: fn.name, | ||
}, | ||
() => fn(...args), | ||
); | ||
return tracer.startActiveSpan(fn.name || 'anonymous', async (span) => { | ||
try { | ||
const result = await Promise.resolve(fn(...args)); | ||
span.end(); | ||
return result; | ||
} catch (error) { | ||
span.recordException(error as Error); | ||
span.setStatus({ code: 2 }); // OpenTelemetry ERROR status | ||
span.end(); | ||
throw error; | ||
} | ||
}); | ||
}; | ||
} |
Oops, something went wrong.