Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,25 @@ npm run check-types # TypeScript
npm run build-types # generate type definitions from JSON schemas
npm -w ui run build # build the Vue SPA
```

## Logging

Server code (`api/`, `worker/`, `shared/`) logs through `import log from '@data-fair/lib-node/log.js'`
(pino: JSON on stdout in production, pretty in dev); the full API is in the "Logging" section of the
`@data-fair/lib-node` README. eslint `no-console` applies to `api/src/**`, `worker/src/**`, `shared/**` and the
two `index.ts`; `api/scripts/` keep `console`.

- `log.info|warn|alert|error(msg, { code?, err?, ...fields })`: `code` is a **static string**, ids go in their
own fields, the error in `err`, never in the message.
- `error` is for internal failures only (counted in `df_logs`, alerting). Client errors, upstream failures
(a remote catalog, a plugin removed from the registry) and situations reported to the owner on the
task (lost permissions, missing publication site) are `warn`.
- The actor (ip, host, userId, accountKey...) is never passed by hand: `session.middleware()` opens the
request log context. The owner of a catalog, import or publication uses the `owner*` names
(`ownerLogFields` in `shared/log-fields.ts`).
- The worker runs each task in `withLogContext(taskLogFields(task, type))` (`catalogId`, `taskType`,
`importId` / `publicationId`, owner), so every line below it, plugin code included, carries the task.
The plugin is loaded before entering that context, its lines pass the fields explicitly.
- `debug` call sites are unchanged; their output is redirected into the logger.
- Task logs shown in the UI (`worker/src/lib/logs.ts`, the `log` given to plugins) are a separate thing,
stored on the import / publication document.
9 changes: 5 additions & 4 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import log from '@data-fair/lib-node/log.js'
import { start, stop } from './src/server.ts'

start().then(() => {}, err => {
console.error('Failure while starting service', err)
log.error('failure while starting the service', { code: 'start-process', err })
process.exit(1)
})

process.on('SIGTERM', function onSigterm () {
console.info('Received SIGTERM signal, shutdown gracefully...')
log.info('received SIGTERM signal, shutting down gracefully...')
stop().then(() => {
console.log('shutting down now')
log.info('shutting down now')
process.exit()
}, err => {
console.error('Failure while stopping service', err)
log.error('failure while stopping the service', { code: 'stop-process', err })
process.exit(1)
})
})
4 changes: 2 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"dependencies": {
"@data-fair/catalogs-shared": "*",
"@data-fair/lib-express": "^1.31.0",
"@data-fair/lib-node": "^2.16.1",
"@data-fair/lib-express": "^2.0.0",
"@data-fair/lib-node": "^3.0.0",
"@data-fair/lib-node-registry": "^0.7.2",
"@data-fair/lib-utils": "^1.10.1",
"config": "^5.0.1",
Expand Down
7 changes: 4 additions & 3 deletions api/src/misc/routers/datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Debug from 'debug'
import { Router } from 'express'
import { assertReqInternalSecret } from '@data-fair/lib-express'
import { internalError } from '@data-fair/lib-node/observer.js'
import log from '@data-fair/lib-node/log.js'
import mongo from '#mongo'
import config from '#config'

Expand All @@ -16,8 +16,9 @@ router.post('/', async (req, res) => {
assertReqInternalSecret(req, config.secretKeys.catalogs)
debug('Incoming data fair webhook for datasets update/delete', req.body)

if (!req.body.update || !req.body.delete) { // Non blocking error
internalError('catalogs-data-fair-webhook', 'Missing datasets in request body')
// non blocking, a malformed call of the caller is not an internal failure
if (!req.body.update || !req.body.delete) {
log.warn('data-fair webhook without update or delete datasets lists', { code: 'catalogs-data-fair-webhook' })
}

await mongo.publications.updateMany(
Expand Down
3 changes: 2 additions & 1 deletion api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as wsServer from '@data-fair/lib-express/ws-server.js'
import { init as wsEmitterInit } from '@data-fair/lib-node/ws-emitter.js'
import eventsQueue from '@data-fair/lib-node/events-queue.js'
import { startObserver, stopObserver } from '@data-fair/lib-node/observer.js'
import log from '@data-fair/lib-node/log.js'
import { createHttpTerminator } from 'http-terminator'
import http from 'http'
import config from '#config'
Expand Down Expand Up @@ -58,7 +59,7 @@ export const start = async () => {
server.listen(config.port)
await new Promise(resolve => server.once('listening', resolve))

console.log(`API server listening on port ${config.port}`)
log.info(`API server listening on port ${config.port}`)
}

export const stop = async () => {
Expand Down
7 changes: 6 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ import dfLibRecommended from '@data-fair/lib-utils/eslint/recommended.js'
export default [
{ ignores: ['ui/', '**/.type/', 'data/'] },
...dfLibRecommended,
...neostandard({ ts: true })
...neostandard({ ts: true }),
{
// server code logs through @data-fair/lib-node/log.js (its README, "Logging"), api/scripts/ keep console
files: ['api/src/**/*.ts', 'api/index.ts', 'worker/src/**/*.ts', 'worker/index.ts', 'shared/**/*.ts'],
rules: { 'no-console': 'error' }
}
]
Loading
Loading