Skip to content

Commit fb960df

Browse files
committed
feat: extract inbound W3C trace context in HTTP server
When an inbound request carries traceparent/tracestate headers, extract them via propagation.extract() and scope the entire request processing under that context. This connects cloudserver spans to upstream callers (e.g. NGINX, Beyla) as a single distributed trace. Issue: CLDSRV-884
1 parent 9d17764 commit fb960df

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

lib/server.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const { setServerHeader } = arsenal.s3routes.routesUtils;
77
const { RedisClient, StatsClient } = arsenal.metrics;
88
const monitoringClient = require('./utilities/monitoringHandler');
99

10+
const enableOtel = process.env.ENABLE_OTEL === 'true';
11+
const otelApi = enableOtel ? require('@opentelemetry/api') : null;
12+
1013
const logger = require('./utilities/logger');
1114
const { internalHandlers } = require('./utilities/internalHandlers');
1215
const { clientCheck, healthcheckHandler } = require('./utilities/healthcheckHandler');
@@ -206,7 +209,23 @@ class S3Server {
206209
vault,
207210
},
208211
};
209-
arsenal.s3routes.routes(req, res, params, logger, this.config);
212+
213+
// Extract inbound W3C trace context (traceparent/tracestate) and
214+
// run the entire request within it so that spans created downstream
215+
// are children of the caller's trace.
216+
if (enableOtel && req.headers.traceparent) {
217+
const traceHeaders = { traceparent: req.headers.traceparent };
218+
if (req.headers.tracestate) {
219+
traceHeaders.tracestate = req.headers.tracestate;
220+
}
221+
const remoteContext = otelApi.propagation.extract(
222+
otelApi.context.active(), traceHeaders);
223+
return otelApi.context.with(remoteContext, () => {
224+
arsenal.s3routes.routes(req, res, params, logger, this.config);
225+
});
226+
}
227+
228+
return arsenal.s3routes.routes(req, res, params, logger, this.config);
210229
}
211230

212231
/**

0 commit comments

Comments
 (0)