Gadget Trace

Gadget Trace Node/Express Guide

Node/Express Middleware

Install npm package:
yarn add gadget-trace-middleware
Add middleware to express
const express = require('express')
const gadgetTraceMiddleware = require('gadget-trace-middleware')   // require middleware
const serviceApiCall = require('./service-api-call')
const models = require('./models')

const app = express()

...

app.use(gadgetTraceMiddleware({                                    // pass config into middleware
  host: 'localhost',                                               // gadget trace hostname that is reachable from your server
  publicHost: 'localhost',                                         // gadget trace hostname that is reachable your browser (only needed if different than host)
  secret: '123'                                                    // your application secret, can be generated with the extension
})
app.use((req, res, next) => {
  req.serviceApiCall = req.gadgetTrace.wrap(serviceApiCall)        // wrap a specific function
  req.models = req.gadgetTrace.wrap(models, 'models')              // wrap all functions found within an object
  next()
})
Example route:
app.get('/service', (req, res, next) => {
  let response = await req.serviceApiCall(req.query.service, req.query.id)                    // serviceApiCall is wrapped, so this is recorded in Gadget Trace
  req.models.logs.saveEvent('service call', { service: req.query.service, id: req.query.id }) // also wrapped, and therefore recorded in gadget trace
  res.end(response.data)                                                                      // the middleware wil also record the http response
})