Skip to content

Commit

Permalink
Migrate to UUID V1
Browse files Browse the repository at this point in the history
  • Loading branch information
puzpuzpuz committed Dec 30, 2019
1 parent ff3d735 commit 14bd102
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Request Tracer - Express, Fastify and Koa middlewares, and Hapi plugin for CLS-based request id generation, batteries included. An out-of-the-box solution for adding request ids into your logs. Check out [this Medium post](https://medium.com/@apechkurov/request-id-tracing-in-node-js-applications-c517c7dab62d) that describes the rationale behind `cls-rtracer`.

Automatically generates a UUID value as the id for each request and stores it in Continuation-Local Storage (CLS, see [cls-hooked](https://github.com/jeff-lewis/cls-hooked)). Optionally, if the request contains `X-Request-Id` header, uses its value instead. Allows to obtain the generated request id anywhere in your routes later and use it for logging or any other purposes.
Automatically generates a UUID V1 value as the id for each request and stores it in Continuation-Local Storage (CLS, see [cls-hooked](https://github.com/jeff-lewis/cls-hooked)). Optionally, if the request contains `X-Request-Id` header, uses its value instead. Allows to obtain the generated request id anywhere in your routes later and use it for logging or any other purposes.

Tested and works fine with Express v4, Fastify v2, Koa (both v1 and v2), and Hapi v18.

Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const cls = require('cls-hooked')
const uuidv4 = require('uuid/v4')
const uuidv1 = require('uuid/v1')

// generate a unique value for namespace
const nsid = `rtracer:${uuidv4()}`
const nsid = `rtracer:${uuidv1()}`
const ns = cls.createNamespace(nsid)

/**
Expand All @@ -27,7 +27,7 @@ const expressMiddleware = ({
if (useHeader) {
requestId = req.headers[headerName.toLowerCase()]
}
requestId = requestId || uuidv4()
requestId = requestId || uuidv1()

ns.run(() => {
ns.set('requestId', requestId)
Expand Down Expand Up @@ -56,7 +56,7 @@ const koaMiddleware = ({
if (useHeader) {
requestId = ctx.request.headers[headerName.toLowerCase()]
}
requestId = requestId || uuidv4()
requestId = requestId || uuidv1()

return new Promise(ns.bind((resolve, reject) => {
ns.set('requestId', requestId)
Expand Down Expand Up @@ -88,7 +88,7 @@ const koaV1Middleware = ({
if (useHeader) {
requestId = this.request.headers[headerName.toLowerCase()]
}
requestId = requestId || uuidv4()
requestId = requestId || uuidv1()
ns.set('requestId', requestId)

yield next
Expand Down Expand Up @@ -128,7 +128,7 @@ const hapiPlugin = ({
if (useHeader) {
requestId = request.headers[headerName.toLowerCase()]
}
requestId = requestId || uuidv4()
requestId = requestId || uuidv1()
ns.set('requestId', requestId)

return h.continue
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"logging"
],
"dependencies": {
"uuid": "3.3.2"
"uuid": "3.3.3"
},
"devDependencies": {
"@hapi/hapi": "^18.4.0",
Expand Down

0 comments on commit 14bd102

Please sign in to comment.