Files
speckle-server/packages/server/logging/apolloPlugin.js
T
Iain Sproat 444d2ca7dd Structured logging (attempt 2) (#1234)
* Revert "Revert "feat(structured logging): implements structured logging for backend (#1217)" (#1227)"

This reverts commit 63e6581162.

* Use pino-http instead of express pino logger
* Use correct reference to knex and do not instantiate HttpLogger prematurely
* Adds missing dependency for pino to webhook-service
* Do not instantiate middleware when passed to express
* Refactor to move logging into shared
* Copy shared packages into dockerfiles
* Build shared workspace in docker build-stage for fileimport & webhook
2022-12-05 14:49:52 +00:00

70 lines
2.1 KiB
JavaScript

/* istanbul ignore file */
// const { logger } = require('@/logging/logging')
const Sentry = require('@sentry/node')
const { ApolloError } = require('apollo-server-express')
const prometheusClient = require('prom-client')
const metricCallCount = new prometheusClient.Counter({
name: 'speckle_server_apollo_calls',
help: 'Number of calls',
labelNames: ['actionName']
})
/** @type {import('apollo-server-core').PluginDefinition} */
module.exports = {
// eslint-disable-next-line no-unused-vars
requestDidStart(ctx) {
return {
didResolveOperation(ctx) {
if (!ctx.operation) {
return
}
const transaction = Sentry.startTransaction({
op: `GQL ${ctx.operation.operation} ${ctx.operation.selectionSet.selections[0].name.value}`,
name: `GQL ${ctx.operation.selectionSet.selections[0].name.value}`
})
try {
const actionName = `${ctx.operation.operation} ${ctx.operation.selectionSet.selections[0].name.value}`
metricCallCount.labels(actionName).inc()
// logger.debug(actionName)
} catch (e) {
Sentry.captureException(e)
}
Sentry.configureScope((scope) => scope.setSpan(transaction))
ctx.request.transaction = transaction
},
didEncounterErrors(ctx) {
if (!ctx.operation) return
for (const err of ctx.errors) {
if (err instanceof ApolloError) {
continue
}
Sentry.withScope((scope) => {
scope.setTag('kind', ctx.operation.operation)
scope.setExtra('query', ctx.request.query)
scope.setExtra('variables', ctx.request.variables)
if (err.path) {
// We can also add the path as breadcrumb
scope.addBreadcrumb({
category: 'query-path',
message: err.path.join(' > '),
level: Sentry.Severity.Debug
})
}
Sentry.captureException(err)
})
}
},
willSendResponse(ctx) {
if (ctx.request.transaction) {
ctx.request.transaction.finish()
}
}
}
}
}