@foam-ai/node-cliengo 0.1.0-alpha.10 → 0.1.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -117,7 +117,12 @@ function createExpressMiddleware(tracer) {
117
117
  }
118
118
  };
119
119
  res.on('finish', onFinish);
120
- next();
120
+ // Set the shadow span as the active context for the duration of the
121
+ // request. This makes it discoverable via trace.getActiveSpan() so
122
+ // buildTraceparent() (used by injectJobData/injectSnsAttributes) returns
123
+ // the same traceId — even without NR.
124
+ const activeCtx = api_1.trace.setSpan(parentCtx ?? api_1.ROOT_CONTEXT, span);
125
+ api_1.context.with(activeCtx, () => next());
121
126
  };
122
127
  middleware._markErrorHandler = markErrorHandlerRegistered;
123
128
  return middleware;
@@ -88,6 +88,18 @@ function createFastifyPlugin(tracer) {
88
88
  }
89
89
  done();
90
90
  });
91
+ // Set the shadow span as the active context for route handlers so
92
+ // buildTraceparent() returns the same traceId — even without NR.
93
+ fastify.addHook('preHandler', (request, _reply, done) => {
94
+ const span = request[SPAN_KEY];
95
+ if (span) {
96
+ const activeCtx = api_1.trace.setSpan(api_1.ROOT_CONTEXT, span);
97
+ api_1.context.with(activeCtx, () => done());
98
+ }
99
+ else {
100
+ done();
101
+ }
102
+ });
91
103
  fastify.addHook('onError', (request, _reply, error, done) => {
92
104
  try {
93
105
  const span = request[SPAN_KEY];
@@ -29,6 +29,7 @@
29
29
  */
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
31
  exports.createWinstonTransport = createWinstonTransport;
32
+ const api_1 = require("@opentelemetry/api");
32
33
  const api_logs_1 = require("@opentelemetry/api-logs");
33
34
  const node_stream_1 = require("node:stream");
34
35
  const trace_bridge_1 = require("../trace-bridge");
@@ -69,11 +70,21 @@ function createWinstonTransport(loggerProvider, serviceName) {
69
70
  }
70
71
  }
71
72
  }
73
+ let logContext;
74
+ if (ctx.traceId && ctx.spanId) {
75
+ logContext = api_1.trace.setSpanContext(api_1.ROOT_CONTEXT, {
76
+ traceId: ctx.traceId,
77
+ spanId: ctx.spanId,
78
+ traceFlags: 1,
79
+ isRemote: true,
80
+ });
81
+ }
72
82
  otelLogger.emit({
73
83
  body: message,
74
84
  severityNumber: severity,
75
85
  severityText: level.toUpperCase(),
76
86
  attributes,
87
+ ...(logContext && { context: logContext }),
77
88
  });
78
89
  }
79
90
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foam-ai/node-cliengo",
3
- "version": "0.1.0-alpha.10",
3
+ "version": "0.1.0-alpha.11",
4
4
  "description": "Unified observability (traces, logs, metrics) for Cliengo Node.js services, connecting New Relic APM with Foam's OTel collector.",
5
5
  "main": "dist/node-cliengo/src/index.js",
6
6
  "types": "dist/node-cliengo/src/index.d.ts",