1.0.0 • Published 2 months ago

@bonniernews/gcp-trace-logging-correlator v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

gcp-trace-logging-correlator

Helps you correlate both request and application logs in an app running on GCP. You can read more about log correlation in GCP here https://cloud.google.com/run/docs/logging#correlate-logs

Installation

npm install @bonniernews/gcp-trace-logging-correlator

Usage

This package comes with an express middleware and a function that extracts the trace as an object that should be injected into your logs.

Install the middleware somewhere globally in your app. You have to provide the GCP project ID meaning you probably want to set this up with an environment variable.

import { middleware } from "@bonniernews/gcp-trace-logging-correlator";

// ...

app.use(middleware(config.get("gcp_project_id")));

You should then provide the object returned by the function getTraceObject to your logging library. If you're using pino, you should pass it to a mixin like this

import { getTraceObject } from "@bonniernews/gcp-trace-logging-correlator";

const logger = pino({
  // ...
  mixin = () => ({
    somethingElse: "foo",
    ...getTraceObject(),
  });
});