Skip to content

Commit

Permalink
feat: support before and after fn tracer
Browse files Browse the repository at this point in the history
perf: remove unsed var
perf: add lint
optimize
  • Loading branch information
orangemi committed Jun 30, 2023
1 parent fa23093 commit d16d5ac
Show file tree
Hide file tree
Showing 8 changed files with 726 additions and 74 deletions.
2 changes: 2 additions & 0 deletions example/src/als.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { AsyncLocalStorage } from 'async_hooks'
export const als = new AsyncLocalStorage<Record<string, any>>()
14 changes: 13 additions & 1 deletion example/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { controller, get, post, state, before, validateState, after } from '../../src'
import { controller, get, post, state, before, validateState, after, middleware } from '../../src'
import { als } from './als'

// Home
@controller('/')
Expand All @@ -21,12 +22,23 @@ export class HomeController {

// user
@controller('/user')
@middleware(async (ctx, next) => {
// get trace id by als in any function
ctx.set('trace-id', als.getStore().traceId)
setTimeout(() => {
console.log('async job after api in 1 second in trace:', als.getStore().traceId)
}, 1000)
return next()
})
export class UserController {

// http://localhost:3000/123?name=John
// { id: 123, name: 'John' }
@get('/:id')
@state()
@after(async (ctx) => {
ctx.set('x-user-id', (ctx.body as any).id)
})
@validateState({
type: 'object',
properties: {
Expand Down
4 changes: 2 additions & 2 deletions example/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as Koa from 'koa'
import * as c from '../../src'
import { AsyncLocalStorage } from 'async_hooks'
import { initTracer, Reporter } from 'jaeger-client'
import { Span } from 'opentracing'
import { als } from './als'


// support async local storage
const als = new AsyncLocalStorage<any>()
const consoleReporter: Reporter = {
report(span: Span): void {
const spanAny: any = span
Expand All @@ -16,6 +15,7 @@ const consoleReporter: Reporter = {
callback && callback()
},
setProcess(serviceName: string, tags: any): void {
// ignore this method
}
}
// support opentracing tracer like jaeger or zipkin
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@tng/koa-controller",
"version": "0.1.1",
"version": "1.0.0-alpha.1",
"description": "Write koa http server with koa-controller by decorator feature in typescript.",
"main": "lib/index.js",
"scripts": {
"build": "tsc",
"lint": "eslint src",
"prepare": "npm run build",
"test": "mocha",
"test:coverage": "nyc mocha"
Expand All @@ -26,7 +27,10 @@
"@types/koa-router": "^7.4.4",
"@types/lodash": "^4.14.191",
"@types/mocha": "^10.0.1",
"@types/node": "^18.13.0",
"@types/node": "^18.14.0",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"eslint": "^8.34.0",
"koa": "^2.14.1",
"koa-bodyparser": "^4.3.0",
"mocha": "^10.2.0",
Expand Down
Loading

0 comments on commit d16d5ac

Please sign in to comment.