Add spans for AP signature checkers

pull/5190/head
Chocobozzz 2022-08-11 14:10:18 +02:00
parent ab08ab4e28
commit 2a95b88477
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 86 additions and 70 deletions

View File

@ -1,5 +1,5 @@
import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize'
import { diag, DiagLogLevel, trace } from '@opentelemetry/api'
import { context, diag, DiagLogLevel, trace } from '@opentelemetry/api'
import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
import { registerInstrumentations } from '@opentelemetry/instrumentation'
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'
@ -15,6 +15,8 @@ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { logger } from '@server/helpers/logger'
import { CONFIG } from '@server/initializers/config'
const tracer = trace.getTracer('peertube')
function registerOpentelemetryTracing () {
if (CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true) return
@ -75,9 +77,18 @@ function registerOpentelemetryTracing () {
tracerProvider.register()
}
const tracer = trace.getTracer('peertube')
async function wrapWithSpanAndContext <T> (spanName: string, cb: () => Promise<T>) {
const span = tracer.startSpan(spanName)
const activeContext = trace.setSpan(context.active(), span)
const result = await context.with(activeContext, () => cb())
span.end()
return result
}
export {
registerOpentelemetryTracing,
tracer
tracer,
wrapWithSpanAndContext
}

View File

@ -1,6 +1,7 @@
import { NextFunction, Request, Response } from 'express'
import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor'
import { getAPId } from '@server/lib/activitypub/activity'
import { wrapWithSpanAndContext } from '@server/lib/opentelemetry/tracing'
import { ActivityDelete, ActivityPubSignature, HttpStatusCode } from '@shared/models'
import { logger } from '../helpers/logger'
import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
@ -61,6 +62,7 @@ export {
// ---------------------------------------------------------------------------
async function checkHttpSignature (req: Request, res: Response) {
return wrapWithSpanAndContext('peertube.activitypub.checkHTTPSignature', async () => {
// FIXME: compatibility with http-signature < v1.3
const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '')
@ -116,9 +118,11 @@ async function checkHttpSignature (req: Request, res: Response) {
res.locals.signature = { actor }
return true
})
}
async function checkJsonLDSignature (req: Request, res: Response) {
return wrapWithSpanAndContext('peertube.activitypub.JSONLDSignature', async () => {
const signatureObject: ActivityPubSignature = req.body.signature
if (!signatureObject || !signatureObject.creator) {
@ -148,4 +152,5 @@ async function checkJsonLDSignature (req: Request, res: Response) {
res.locals.signature = { actor }
return true
})
}