-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd57b26
commit 2bb1845
Showing
13 changed files
with
360 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
.github/workflows/tests.opentelemetry-baggage-span-processor.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
packages/opentelemetry-baggage-span-processor/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/opentelemetry-baggage-span-processor/src/baggage-span-processor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); | ||
}); | ||
}); |
Oops, something went wrong.