traceparent
Version:
Context management helper for the w3c traceparent header format
89 lines (50 loc) • 3.26 kB
Markdown
//img.shields.io/npm/v/traceparent.svg)](https://www.npmjs.com/package/traceparent)
[](https://travis-ci.org/elastic/node-traceparent)
[](https://codecov.io/gh/elastic/node-traceparent)
[](https://github.com/feross/standard)
This is a basic implementation of the traceparent header part of the W3C trace context spec.
```
npm install traceparent
```
```js
const crypto = require('crypto')
const TraceParent = require('traceparent')
const version = Buffer.alloc(1).toString('hex')
const traceId = crypto.randomBytes(16).toString('hex')
const id = crypto.randomBytes(8).toString('hex')
const flags = '01'
const header = `${version}-${traceId}-${id}-${flags}`
const parent = TraceParent.fromString(header)
```
Construct a new `TraceParent` instance from an existing buffer. The contents are binary data that corresponds to the structure of the [W3C traceparent header][traceparent] format, with separators removed.
Reconstruct a `TraceParent` instance from a formatted [W3C traceparent header][traceparent] string.
Resume from a parent context, if given, or start a new context. Accepts another `TraceParent` instance, a [W3C traceparent header][traceparent] string, or a `Span` or `Transaction` instance from [elastic-apm-node](http://npmjs.org/package/elastic-apm-node).
Requires a `settings` object with a `transactionSampleRate` value from 0.0 to 1.0 to generate a sampling decision for the context. This will only be applied when starting a _new_ context. When continuing an existing context, the sampling decision will be propagated into all child contexts.
Returns `true` if this `TraceParent` is sampled.
The `traceId` property will propagate through all children in the tree to link them all together.
The `id` property is used to uniquely identify a given `TraceParent` instance within the tree.
The `parentId` property links this context to its direct parent in the tree.
The `flags` property is used to store metadata such as the sampling decision.
The `version` property corresponds to the version segment of the [W3C traceparent header][traceparent].
Create a new `TraceParent` instance that is a child of this one.
Formats the `TraceParent` instance as a [W3C traceparent header][traceparent].
Return the parent ID, if there is none, generate one. This is useful in browser instrumentation to produce a starting span around a browser request which was not instrumented prior to page load.
[ ](LICENSE)
[ ]: https://github.com/w3c/trace-context/blob/master/spec/20-http_header_format.md
[![npm](https: