Giter Club home page Giter Club logo

express-request-correlator's Introduction

Logo

๐Ÿงท Express middleware to help tracing correlation IDs โš™๏ธ

CI Status Made with Coffee

โฏ Why another middleware for express to work with correlation ids?

This packages is a combination of observations and experiences we have had with other middlewares upon whose ideas we build:

  1. ๐ŸŽจ Customized correlation ids: sometimes correlation ids need customization. For instance adding prefix based on the incoming request's origin.
  2. ๐Ÿ• Forwarding correlation ids: oftentimes an incoming correlation id needs to be forwarded as a header to another request (e.g. a fetch call).
  3. ๐Ÿ„๐Ÿป Opting out of correlation ids: cases exist where a correlation id can or should not be generated while the fact should be logged
  4. ๐Ÿ‘Œ๐Ÿผ Inspecting correlation ids: the correlation id of a request should be easy to extract without knowing the specific header it is passed under

โฏ Installation

Depending on the preferred package manager:

yarn add @commercetools/express-request-correlator

or

npm i @commercetools/express-request-correlator --save

โฏ Concepts

A correlation id, can be referred to as a unique identifier that is attached to requests that allow grouping them as a transaction or event chain. In the case of multiple microservices it is used to correlate an incoming request to other resulting requests to other services. As a result a correlation ids should be passed on when found on an incoming request. If not found they should be generated early to have a correlatable request/transaction chain as soon as possible. Ultimately, this helps enabling a concept called distributed tracing in a distributed systems by for instance graphing a sequence diagram of multiple requests across various services.

Usually correlation ids are passed as a header. The specific name of that header differs. Often X-Correlation-Id is used. Generally multiple services should agree on a shared name and location (e.g. header) of the id. However, it is possible that service A makes a request with a correlation id named (X-Request-Id) to service B which forwards it as X-Correlation-Id.

โฏ Documentation

The express-request-correlator comes with a couple of options it can be configured with:

  • headerName: the name of the header on which a new correlation id will be set on. Defaults to X-Correlation-Id.
  • generateId: a function called whenever an incoming request does not have a correlation id. Receives the request within its options argument.

An example configuration would be:

import { createRequestCorrelatorMiddleware } from '@commercetools/express-request-correlator';

app.use(createRequestCorrelatorMiddleware());
  1. The middleware adds a correlation id to each response as a header for debugging
    • The previously received id is forwarded/used or a new id is generated
  2. The middleware itself exposes a property called correlator (more below) onto each incoming request
    • This object allows receiving the correlation id in other parts of the request chain

Configuring the middleware on your express application exposes a correlator property on each request. See type TRequestCorrelator.

  1. If for instance you need to retrieve the current request's correlation id invoke request.correlator.getCorrelationId()
  2. If you want to forward the correlation id to another request's headers run request.correlator.forwardCorrelationId({ headers })
    • This will mutate the passed in headers to contain the previous request's correlation id

Note, that getCorrelationId and forwardCorrelationId both accept a object with configuration:

{
  ifNotDefined: () => TCorrelationId;
}

Whenever ifNotDefined is passed it can be used as a last resort to generate a new correlation id given the request does not already have one. Another use case is logging. Whenever ifNotDefined is not passed the "globally" configured generate function will be used if no correlation id is present on the request.

โฏ Testing

In case you want to create a test correlator you can use the createRequestCorrelatorMock function on the testUtils object. It will create a mocked correlator with the same API as the actual correlator.

import { createRequestCorrelatorMock } from '@commercetools/express-request-correlator/test-utils';

createRequestCorrelatorMock();

express-request-correlator's People

Contributors

dogayuksel avatar emmenko avatar merchant-center-automation avatar renovate-bot avatar renovate[bot] avatar tdeekens avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

express-request-correlator's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

Repo update

Let's also do some maintenance of the repo (tools, config, etc.), as well as update dependencies and automate releases.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/main.yml
  • actions/checkout v4
  • pnpm/action-setup v2.4.0
  • actions/setup-node v3
.github/workflows/release.yml
  • tibdex/github-app-token v2.1.0
  • actions/checkout v4
  • pnpm/action-setup v2.4.0
  • actions/setup-node v3
  • changesets/action v1.4.5
npm
package.json
  • @babel/runtime ^7.22.5
  • @babel/runtime-corejs3 ^7.22.5
  • @types/express 4.17.19
  • @types/uuid 9.0.5
  • uuid 9.0.1
  • @changesets/changelog-github 0.4.8
  • @changesets/cli 2.26.2
  • @commercetools-backend/eslint-config-node 22.9.1
  • @commercetools-frontend/babel-preset-mc-app 22.9.1
  • @commitlint/cli 17.8.0
  • @commitlint/config-conventional 17.8.0
  • @manypkg/cli 0.21.0
  • @preconstruct/cli 2.8.1
  • @tsconfig/node18 2.0.1
  • @types/jest 29.5.5
  • @types/node 18.18.5
  • babel-jest 29.7.0
  • cross-env 7.0.3
  • eslint 8.51.0
  • eslint-formatter-pretty 5.0.0
  • express 4.18.2
  • husky 8.0.3
  • jest 29.7.0
  • jest-mock 29.7.0
  • jest-runner-eslint 2.1.2
  • jest-silent-reporter 0.5.0
  • jest-watch-typeahead 2.2.2
  • lint-staged 13.3.0
  • prettier 2.8.8
  • ts-jest 29.1.1
  • typescript 5.2.2
  • express >=4
  • node >=16
  • pnpm 8.9.2
nvm
.nvmrc
  • node 18

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.