Skip to content

Commit

Permalink
Add baggage span processor package
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotorres97 committed Nov 9, 2023
1 parent bd57b26 commit 2bb1845
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
description: 'The package you wish to release'
type: choice
options:
- opentelemetry-baggage-span-processor
- opentelemetry-mutable-baggage
required: true
VERSION_BUMP:
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/tests.opentelemetry-baggage-span-processor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Tests - opentelemetry-baggage-span-processor

on:
push:
paths:
- '*'
- 'packages/opentelemetry-baggage-span-processor/**'

jobs:
unit:
runs-on: ubuntu-latest
env:
NPM_WORKSPACE: packages/opentelemetry-baggage-span-processor
strategy:
matrix:
node: [18, 20]
name: Node v${{ matrix.node }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js version
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install dependencies
run: npm ci

- name: Run lint
run: npm run lint --workspace $NPM_WORKSPACE

- name: Run tests
env:
VITEST_MAX_THREADS: 2
VITEST_MIN_THREADS: 1
run: npm run test --workspace $NPM_WORKSPACE
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This repository is setup as a mono-repository and all packages live under [`pack

| Name | Description |
|------|-------------|
| [`@uphold/opentelemetry-baggage-span-processor`](./packages/opentelemetry-baggage-span-processor/) | Package that sets all baggage entries as span attributes |
| [`@uphold/opentelemetry-mutable-baggage`](./packages/opentelemetry-mutable-baggage/) | Package that allows an OpenTelemetry baggage to be mutable |

## Tests
Expand Down
128 changes: 115 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
21 changes: 21 additions & 0 deletions packages/opentelemetry-baggage-span-processor/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Uphold

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions packages/opentelemetry-baggage-span-processor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# @uphold/opentelemetry-baggage-span-processor

Package that sets all baggage entries as span attributes.

## Installation

```sh
npm install @uphold/opentelemetry-baggage-span-processor
```

## Why?

Setting all baggage entries as span attributes using OpenTelemetry ensures end-to-end traceability, simplifies analysis, and enhances observability in distributed systems. This approach provides consistent contextual information across spans, facilitating correlation, context propagation, and streamlined troubleshooting. It also enables flexibility in using analysis tools, improving operational efficiency and accelerating issue resolution.

## Usage

Add `BaggageSpanProcessor` as a `spanProcessor` to the `tracerProvider`. Here's how it looks if you are using the NodeSDK:

```js
import { BaggageSpanProcessor } from '@uphold/opentelemetry-baggage-span-processor';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { ProxyTracerProvider, trace } from '@opentelemetry/api';

const sdk = new NodeSDK({});

// We can't rely on passing a custom `textMapPropagator` or `spanProcessor` when initializing the NodeSDK because it will be ignored unless `spanProcessor` or `traceExporter` are specified.
// However, when those are specified, the `OTEL_EXPORTER_*` variables won't be respected.
// To circumvent this issue, we wrap `start` and set those after.
sdk.start = wrap(sdk.start, start => {
start.apply(sdk);

const proxyTracerProvider = trace.getTracerProvider() as ProxyTracerProvider;
const tracerProvider = proxyTracerProvider.getDelegate() as NodeTracerProvider;

tracerProvider.addSpanProcessor(new BaggageSpanProcessor());
});
```

## Tests

```sh
npm test
```

## License

Licensed under MIT.
54 changes: 54 additions & 0 deletions packages/opentelemetry-baggage-span-processor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@uphold/opentelemetry-baggage-span-processor",
"version": "0.0.0",
"description": "Package that sets all baggage entries as span attributes.",
"main": "dist/index.js",
"files": [
"dist"
],
"author": "Uphold",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/uphold/opentelemetry-js-contrib.git"
},
"keywords": [
"opentelemetry",
"instrumentation",
"nodejs",
"tracing",
"attributes",
"baggage",
"processor",
"search",
"span"
],
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=18"
},
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc",
"lint": "eslint --ext .ts,.js .",
"postlint": "tsc --noEmit",
"release": "release-it",
"test": "vitest"
},
"lint-staged": {
"*.{ts,js}": [
"eslint --ext .ts,.js"
]
},
"dependencies": {
"@opentelemetry/sdk-trace-base": "^1.18.1"
},
"peerDependencies": {
"@opentelemetry/api": ">=1 <2"
},
"devDependencies": {
"@opentelemetry/sdk-trace-node": "^1.18.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as api from '@opentelemetry/api';
import { BaggageSpanProcessor } from './baggage-span-processor';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { Span } from '@opentelemetry/sdk-trace-base';
import { beforeAll, describe, expect, it } from 'vitest';

beforeAll(() => {
new NodeTracerProvider().register();
});

describe('.onStart()', () => {
it('should add all baggage attributes as span attributes', () => {
const baggage = api.propagation.createBaggage({
'request.id': { value: 'foo' },
'user.id': { value: 'bar' }
});
const context = api.propagation.setBaggage(api.context.active(), baggage);
const span = api.trace.getTracer('foo').startSpan('bar') as Span;
const baggageSpanProcessor = new BaggageSpanProcessor();

baggageSpanProcessor.onStart(span, context);

expect(span.attributes).toEqual({ 'request.id': 'foo', 'user.id': 'bar' });
});

it('should not add any span attribute if there is no active baggage', () => {
const context = api.context.active();
const span = api.trace.getTracer('foo').startSpan('bar') as Span;
const baggageSpanProcessor = new BaggageSpanProcessor();

baggageSpanProcessor.onStart(span, context);

expect(span.attributes).toEqual({});
});

it('should not add any span attribute if there active baggage is empty', () => {
const baggage = api.propagation.createBaggage();
const context = api.propagation.setBaggage(api.context.active(), baggage);
const span = api.trace.getTracer('foo').startSpan('bar') as Span;
const baggageSpanProcessor = new BaggageSpanProcessor();

baggageSpanProcessor.onStart(span, context);

expect(span.attributes).toEqual({});
});
});
Loading

0 comments on commit 2bb1845

Please sign in to comment.