https://github.com/blumamir/bull-otel-context-propagation
https://github.com/blumamir/bull-otel-context-propagation
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/blumamir/bull-otel-context-propagation
- Owner: blumamir
- License: mit
- Created: 2023-01-02T11:29:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-02T11:42:13.000Z (over 2 years ago)
- Last Synced: 2025-02-16T04:20:42.805Z (4 months ago)
- Language: TypeScript
- Size: 43 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bull-otel-context-propagation
This repo demonstrate how to propagate OpenTelemetry context from producer (`queue.add`) to consumer (`queue.process`). It requires adding few lines of code to inject the active context to the job before adding it to queue, and then extracting the context and wrapping the process logic with the propagated context.
## Install
```
yarn install
```## Run
To run the demo application, you need to set environment variable `ASPECTO_AUTH` to [your aspecto token](https://app.aspecto.io/7fe312f8/integration/tokens) and execute `yarn start` or `npm run start`. Example:
```
ASPECTO_AUTH=00000000-0000-0000-0000-000000000000 yarn start
```This will start an http server on port 4000. Then invoke it with `http://localhost:4000` and observe the trace in [`trace search`](https://app.aspecto.io/search) page on Aspecto App.
## Usage
For OpenTelemetry propagation to work accross bulljs, you need to modify job opts before adding it to the queue:This
```js
queue.add(data);
```Turns into this
```js
import { context, propagation } from '@opentelemetry/api';const otelSerializedContext = {};
propagation.inject(context.active(), otelSerializedContext);
queue.add(data, otelSerializedContext);
```Also when you process the message, you need to wrap your process logic with:
This
```js
queue.process(async (job: Job) => {
// process the job
});
```Turns into this
```js
import { context, propagation } from '@opentelemetry/api';queue.process(async (job: Job) => {
const propagatedContext = propagation.extract(context.active(), job.opts);
await context.with(propagatedContext, async () => {
// process the job
})
});
```