tctx
Version:
W3C Trace Contexts made simple
94 lines (65 loc) • 2.21 kB
Markdown
<div align="left">
<samp>
</samp>
**W3C [Trace Context](https://w3c.github.io/trace-context/)'s made simple**
<a href="https://npm-stat.com/charts.html?package=tctx">
<img src="https://badgen.net/npm/dm/tctx?labelColor=black&color=black&label=npm downloads" alt="js downloads"/>
</a>
<a href="https://licenses.dev/npm/tctx">
<img src="https://licenses.dev/b/npm/tctx?style=dark" alt="licenses" />
</a>
<a href="https://bundlephobia.com/result?p=tctx">
<img src="https://badgen.net/bundlephobia/minzip/tctx?labelColor=black&color=black" alt="size"/>
</a>
<br><br>
<sup>
This is free to use software, but if you do like it, consisder supporting me ❤️
[](https://github.com/sponsors/maraisr)
[](https://www.buymeacoffee.com/marais)
</sup>
</div>
`npm add tctx`
```ts
// producer
import * as traceparent from 'tctx';
import * as tracestate from 'tctx/tracestate';
fetch('/api', {
headers: {
traceparent: traceparent.make(),
tracestate: tracestate.make({ key: 'value' }),
},
});
// consumer
import * as traceparent from 'tctx';
import * as tracestate from 'tctx/tracestate';
const parent_key = traceparent.parse(request.headers.traceparent);
const parent_state = tracestate.parse(request.headers.tracestate);
parent_state.set("vendor", "value");
fetch('/downstream', {
headers: {
traceparent: parent.child(),
tracestate: parent_state,
},
});
```
> via the [`/bench`](/bench) directory with Node v21.7.1
```
✔ tctx ~ 532,641 ops/sec ± 0.17%
✔ traceparent ~ 119,325 ops/sec ± 0.16%
✔ trace-context ~ 231,614 ops/sec ± 0.34%
✔ tctx ~ 8,284,422 ops/sec ± 0.71%
✔ traceparent ~ 244,167 ops/sec ± 0.12%
✔ trace-context ~ 4,040,319 ops/sec ± 0.07%
✔ tctx ~ 611,854 ops/sec ± 0.62%
✔ traceparent ~ 264,412 ops/sec ± 0.15%
✔ trace-context ~ 498,430 ops/sec ± 0.37%
```
MIT © [Marais Rossouw](https://marais.io)