const express = require('express')
const { Tracer, RouterProxy } = require('opentracing-connect')
const app = new express()
const serviceName = 'One'
//create and register global tracer
Tracer.createGlobalTracer(serviceName, { logger: console })
//proxy express router
const Router = RouterProxy.create({ router: express.Router(), proxy: { type: "express" } })
Router.get('/one', async (request, response, next) => {
//request has trace prop: traceCtx
const result = await doSomething(request.traceCtx.span)
console.log('service one result:')
console.dir(result)
response.json(result)
})
const Koa = require('koa')
const KoaRouter = require('koa-router')
const app = new Koa()
const serviceName = 'Two'
const tracer = Tracer.createGlobalTracer(serviceName, { logger: console })
const Router = RouterProxy.create({ router: KoaRouter(), proxy: { type: "koa2" } })
Router.get('/two', async (ctx, next) => {
try {
const userIds = [1, 2]
//ctx has trace prop: traceCtx
const result = await getUserInfo(userIds, { span: ctx.traceCtx.span })
ctx.traceCtx.span.log({'event': 'request_end'})
ctx.body = result
} catch(err) {
console.log(err)
}
return
})
{
//router instance
router: expressRouter,
//proxy type
proxy: {
type: 'express'
},
//add cusmomize tags
customizeTags: function ({ span, tracer}, ...args) {
}
}