How to add custom attribute with aws-otel-nodejs layer configured for nodejs lambda? #2042
|
Hi we are using the aws-otel-nodejs layer for auto instrumentation of our nodejs lambda app and traces are sent to aws XRay. We would like to add custom attributes to the traces. Is that possible while continuing to use the layer? More specifically, will the following code work? |
Replies: 1 comment 1 reply
|
Yes, that should generally be possible. Using the So the basic idea you showed is the right one: const { trace } = require("@opentelemetry/api");
function handleUser(user) {
const activeSpan = trace.getActiveSpan();
if (activeSpan) {
activeSpan.setAttribute("user.id", user.getId());
}
}The main thing I’d add is the null check, because So the short answer is:
If you find that So in practice, your approach is fine — just make it a bit more defensive with |
Yes, that should generally be possible.
Using the
aws-otel-nodejslayer for auto-instrumentation does not stop you from also using the OpenTelemetry API in your own code to enrich the current span with custom attributes.So the basic idea you showed is the right one:
The main thing I’d add is the null check, because
getActiveSpan()can return nothing if that code runs outside an active traced context.So the short answer is: