elastic / elastic/apm-agent-nodejs
Allow mutation of tracestate for current transaction/span
- Dominant language
- JavaScript
- Stars
- 594
- Forks
- 244
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 16
Description
We use tracestate header to pass information (to connected microservices) like the vehicle id that caused the request.
We then parse the tracestate header with your TraceState class (elastic-apm-node/lib/tracecontext/tracestate.js) and add state entries to each log row to filter them with vehicle id.
We used this header because the agent is already taking care of passing it in HTTP requests and it's a W3C standard.
The issue that we get is that we didn't find a way to change tracestate of current transaction, we use as a workaround the start of a new span passing the new tracestate created with the help of your TraceState class and saved also in custom context of the current transaction.
```
// set initial tracestate from header value
const tracestate = TraceState.fromStringFormatString(traceStateHeader, this.TSNS);
// add any custom context key already available
for (const [key, value] of Object.entries(this.getCustomContext())) {
tracestate.setValue(key, value);
}
// start a new child transaction with new state
const span = apm.startSpan(eventName, {
childOf: apm.currentTraceparent,
tracestate: tracestate.toW3cString(),
});
// add trace state coming from header (and that was already
// in custom context) to custom context
const context = tracestate.toObject();
if (context[this.TSNS]) {
const parsedValues = this.parseTraceStatePart(context[this.TSNS]);
apm.setCustomContext(parsedValues);
}
```
It would be of great help to be able to add additional values to tracestate of current transaction / span to be able to pass this new tracestate to subsequent HTTP request.
Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.