Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for request id logging #24

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b6a9e83
Add a simple logging service to log info
silwalanish Sep 20, 2019
0f65998
Refactor middlewares and services to use the logging service
silwalanish Sep 20, 2019
71323a0
Update doSomething to call logRequestContext after 2sec to show that …
silwalanish Sep 20, 2019
cf168dd
Update output of example in README.md
silwalanish Sep 20, 2019
99b7cdf
Add type for parameter in docblock
silwalanish Sep 23, 2019
5af9ad8
Add debug method to log debug and Use short request id if available
silwalanish Sep 23, 2019
848dd8e
Add middleware to capture request query params 'a' and 'b'
silwalanish Sep 23, 2019
387ea7a
Add middleware to add the params 'a' and 'b' with a simulated delay o…
silwalanish Sep 23, 2019
bfe2a3b
Implement the new addition functionality instead of previous x-id log…
silwalanish Sep 23, 2019
49f6047
Update README.md to reflect changes in output and test cmd
silwalanish Sep 23, 2019
5c6f18c
Remove delay from add middleware
silwalanish Sep 23, 2019
199a175
Add full stop at the end of docs
silwalanish Sep 24, 2019
72fbc7b
Add new line before relative imports
silwalanish Sep 24, 2019
b0c191e
Fix docs text to explain more about the method
silwalanish Sep 24, 2019
c6e6f78
Change parameter name
silwalanish Sep 24, 2019
c3b3a9a
Remove unnecessary function
silwalanish Sep 24, 2019
64eda1a
Make doSomething create a 2sec delay
silwalanish Sep 24, 2019
b7164ac
Remove unnecessary middlewares
silwalanish Sep 24, 2019
1e06678
Update README.md to show the outputs with delay
silwalanish Sep 24, 2019
bb85478
Remove unnecessary async from function defination
silwalanish Sep 24, 2019
eab65a7
Change textcase to maintain consistency
silwalanish Sep 24, 2019
ced6824
Add unparsed input to store
silwalanish Sep 24, 2019
5055a98
Remove full stop
silwalanish Sep 24, 2019
d9f65f1
Modify info logger to maintain alignment while logging
silwalanish Sep 24, 2019
2ae6e83
Update README.md to show the latest logs output
silwalanish Sep 24, 2019
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
17 changes: 13 additions & 4 deletions examples/express-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ $ yarn start
```
Now you can test it through `curl`. Trigger concurrent requests with different ids that are a part of request context.
```
$ curl localhost:3000 -H X-Id:1 && curl localhost:3000 -H X-Id:2 && curl localhost:3000 -H X-Id:3
$ curl 'http://localhost:3000?a=20&b=30' && curl 'http://localhost:3000?a=10&b=50' && curl 'http://localhost:3000?a=50&b=100'
```
**Output**
```
X-Id received in the middleware: 1
X-Id received in the middleware: 2
X-Id received in the middleware: 3
[ DEBUG ] [ 445ef6fb ] Received a: 20
[ DEBUG ] [ 445ef6fb ] Received b: 30
[ INFO ] [ 445ef6fb ] Simulating Delay
[ DEBUG ] [ 445ef6fb ] Calculated sum: 50
[ DEBUG ] [ b4c27a00 ] Received a: 10
[ DEBUG ] [ b4c27a00 ] Received b: 50
[ INFO ] [ b4c27a00 ] Simulating Delay
[ DEBUG ] [ b4c27a00 ] Calculated sum: 60
[ DEBUG ] [ a24d4281 ] Received a: 50
[ DEBUG ] [ a24d4281 ] Received b: 100
[ INFO ] [ a24d4281 ] Simulating Delay
[ DEBUG ] [ a24d4281 ] Calculated sum: 150

```
18 changes: 9 additions & 9 deletions examples/express-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { Request, Response } from 'express';

import * as store from '@leapfrogtechnology/async-store';

import * as service from './service';
import { requestContext, otherMiddleware } from './middlewares';
import * as logger from './logger';
import { requestParams, add } from './middlewares';

const app = express();
const port = 3000;

app.use(store.initializeMiddleware());
app.use(requestContext());
app.use(requestParams());

app.get('/', otherMiddleware, async (req: Request, res: Response) => {
await service.doSomething();
app.get('/', add(), async (req: Request, res: Response) => {
const a = store.get('a');
const b = store.get('b');
const sum = store.get('sum');

const requestId = store.get('x-id');

res.send(`Response to request: ${requestId}\n`);
res.send(`Sum of ${a}, ${b}: ${sum}\n`);
});

app.listen(port, () => {
process.stdout.write(`Express server listening on port ${port}!\n`);
logger.info(`Express server listening on port ${port}!\n`);
});
32 changes: 32 additions & 0 deletions examples/express-ts/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as store from '@leapfrogtechnology/async-store';

/**
* Get unique request id from store or return empty string
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
*
* @returns {string}
*/
function getRequestId() {
return (store.getId() || '').substring(0, 8);
}

/**
* A example service to log info with unique request id
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {string} infoText
*/
export function info(infoText: string) {
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
const requestId = getRequestId();

process.stdout.write(`[ INFO ] ${requestId ? `[ ${requestId} ]` : ''} ${infoText}\n`);
}

/**
* A example service to log debug with unique request id
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {string} debugText
*/
export function debug(debugText: string) {
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
const requestId = getRequestId();

process.stdout.write(`[ DEBUG ] ${requestId ? `[ ${requestId} ]` : ''} ${debugText}\n`);
}
46 changes: 45 additions & 1 deletion examples/express-ts/src/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import * as store from '@leapfrogtechnology/async-store';
import { Request, Response, NextFunction } from 'express';

import * as logger from './logger';

/**
* Middleware to set query params `a` and `b` on async-store
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
*
* @returns {(req, res, next) => void}
*/
export function requestParams() {
return async (req: Request, res: Response, next: NextFunction) => {
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
const a = req.query.a;
const b = req.query.b;

store.set({
a: parseFloat(a) || 0,
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
b: parseFloat(b) || 0
});

logger.debug(`Received a: ${a}`);
logger.debug(`Received b: ${b}`);

next();
};
}

/**
* Middleware to add the parameters `a` and `b` and set `sum` on the async store.
*
* @returns {(req, res, next) => void}
*/
export function add() {
return (req: Request, res: Response, next: NextFunction) => {
logger.info('Simulating Delay');
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
const a = store.get('a');
silwalanish marked this conversation as resolved.
Show resolved Hide resolved
const b = store.get('b');

const sum = a + b;

store.set({ sum });
logger.debug(`Calculated sum: ${sum}`);

next();
};
}

/**
* Middleware to set the request context `x-id` on the async store.
*
Expand All @@ -22,7 +66,7 @@ export function requestContext() {
export function otherMiddleware(req: Request, res: Response, next: NextFunction) {
const requestId = store.get('x-id');

process.stdout.write(`X-Id received in the middleware: ${requestId}\n`);
logger.info(`X-Id received in the middleware: ${requestId}\n`);

next();
}
7 changes: 5 additions & 2 deletions examples/express-ts/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as store from '@leapfrogtechnology/async-store';
import * as logger from './logger';
silwalanish marked this conversation as resolved.
Show resolved Hide resolved

/**
* An example function making use of the request context set in the store.
Expand All @@ -8,7 +9,9 @@ import * as store from '@leapfrogtechnology/async-store';
export async function doSomething() {
// Do something with the request.

await Promise.all([() => logRequestContext()]);
setTimeout(() => {
logRequestContext();
}, 2000);
}

/**
Expand All @@ -19,5 +22,5 @@ export async function doSomething() {
async function logRequestContext() {
const requestId = store.get('x-id');

process.stdout.write(`Request context: ${requestId}\n`);
logger.info(`Request context: ${requestId}\n`);
}