# Middleware

MIddleware can be added between when the message is read from a transport and before it is dispatched to handlers.

### Uses in telemetry

Telemetry providers like AWS Xray, New Relic, Data Dog, etc can all be used to profile and report message processing times by using middleware.

This is useful to troubleshoot low message processing rates and identify message types that are taking the longest to process and would benefit from performance tuning.

The following shows how to integrate AWS Xray to profile how messages are handled.

```typescript
import AWSXRay from 'aws-xray-sdk'
import { Bus } from '@node-ts/bus-core'
import { Message } from '@node-ts/bus-messages'

const bus = await Bus.configure()
  .withMessageReadMiddleware(async (context, next) => {
    const messageName = (context.domainMessage as Message).$name
    const segment = new AWSXRay.Segment('my-service')
    const subSegment = segment.addNewSubSegment(messageName)
    
    await next()
    
    subSegment.close()
    segment.close()
  })
  .initialize()
```

### Uses in logging context

Middleware is also useful when adding context of the message handling scope to each log produced in the handling cycle.

An example of this is appending a message's `correlationId` to the metadata of each log. This can be done by using [async\_hooks](https://nodejs.org/docs/latest-v16.x/api/async_hooks.html) that attach data to the promise scope that can be accessed by any logging request inside of it.

```typescript
import { Bus } from '@node-ts/bus-core'
import * as asyncHooks from 'async_hooks'

type CorrelationId = string
// Requests to log() would look up the executionId in this map and attach the correlationId
export const handlingContext = new Map<number, CorrelationId>()

const bus = await Bus.configure()
  .withMessageReadMiddleware(async (context, next) => {
    const correlationId = context.attributes.correlationId
    const executionId = asyncHooks.executionAsyncId()
    
    handlingContext.set(executionId, correlationId)
    await next()
    handlingContext.delete(executionId)
  })
  .initialize()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bus.node-ts.com/guide/middleware.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
