From 0911fa60c8d6cf2ee521fbfe22a4707c905d9951 Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Sun, 5 May 2024 16:37:41 -0400 Subject: [PATCH 1/2] docs(nbs): plugin observability Signed-off-by: aramissennyeydd --- docs/plugins/observability.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/plugins/observability.md b/docs/plugins/observability.md index d8778f07f8..bb6f5abbe4 100644 --- a/docs/plugins/observability.md +++ b/docs/plugins/observability.md @@ -37,7 +37,39 @@ An example log line could look as follows: ## Health Checks -The example backend in the Backstage repository +### New Backend + +The new backend is moving towards health checks being plugin-based, as such there is no current plugin for providing a health check route. You can add this yourself easily though, + +```ts +import { + coreServices, + createBackendModule, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; + +const healthCheck = createBackendPlugin({ + pluginId: 'healthcheck', + + register(env) { + env.registerInit({ + deps: { + rootHttpRouter: coreServices.rootHttpRouter, + }, + init: async ({ rootHttpRouter }) => { + // You can adjust the route name and response as you need. + rootHttpRouter.use('/healthcheck', (req, res) => { + res.json({ status: 'ok' }); + }); + }, + }); + }, +}); +``` + +### Old Backend + +The example old backend in the Backstage repository [supplies](https://github.com/backstage/backstage/blob/bc18571b7a742863a770b2a54e785d6bbef7e184/packages/backend/src/index.ts#L99) a very basic health check endpoint on the `/healthcheck` route. You may add such a handler to your backend as well, and supply your own logic to it that fits From b17e7bea59faebfbe685e58e47b8decd3cb09367 Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Sun, 5 May 2024 16:52:44 -0400 Subject: [PATCH 2/2] add section about logging Signed-off-by: aramissennyeydd --- docs/plugins/observability.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/plugins/observability.md b/docs/plugins/observability.md index bb6f5abbe4..e13653706f 100644 --- a/docs/plugins/observability.md +++ b/docs/plugins/observability.md @@ -15,6 +15,32 @@ See how to install Datadog Events in your app ## Logging +### New Backend + +The backend supplies a central logging service, [`rootLogger`](../backend-system/core-services/root-logger.md), as well as a plugin based logger, [`logger`](../backend-system/core-services/logger.md) from `coreServices`. To add additional granularity to your logs, you can create children from the plugin based logger, using the `.child()` method and provide is with JSON data. For example, if you wanted to log items for a specific span in your plugin, you could do + +```ts +export function createRouter({ logger }) { + const router = Router(); + + router.post('/task/:taskId/queue', (req, res) => { + const { taskId } = req.params; + const taskLogger = logger.child({ task: taskId }); + taskLogger.log('Queueing this task.'); + }); + + router.get('/task/:taskId/results', (req, res) => { + const { taskId } = req.params; + const taskLogger = logger.child({ task: taskId }); + taskLogger.log('Getting the results of this task.'); + }); +} +``` + +You can also add additional metadata to all logs for your Backstage instance by overriding the `rootLogger` implementation, you can see an example in [the `logger` docs](../backend-system/core-services/logger.md#configuring-the-service). + +### Old Backend + The backend supplies a central [winston](https://github.com/winstonjs/winston) root logger that plugins are expected to use for their logging needs. In the default production setup, it emits structured JSON logs on stdout, with a field