sasai-common-utils 1.0.44 → 1.0.45

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sasai-common-utils",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "description": "Reusable utility library for common logging and other shared features.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -107,23 +107,36 @@ function sendLog(level, data) {
107
107
  const activeSpan = trace.getSpan(context.active());
108
108
  const spanContext = activeSpan?.spanContext();
109
109
 
110
+ // Use trace/span from data if available, otherwise try to get from active span
111
+ let traceId = data.trace || spanContext?.traceId || "";
112
+ let spanId = data.span || spanContext?.spanId || "";
113
+
110
114
  const logRecord = {
111
115
  severityNumber: getSeverityNumber(level),
112
116
  severityText: level.toUpperCase(),
113
117
  body: typeof data === "string" ? data : JSON.stringify(data),
114
118
  attributes: {
115
119
  "log.level": level,
116
- ...(data.trace && { "trace.id": data.trace }),
117
- ...(data.span && { "span.id": data.span }),
120
+ "service.name": globalConfig.SERVICE_NAME,
121
+ ...(traceId && { "trace.id": traceId }),
122
+ ...(spanId && { "span.id": spanId }),
118
123
  ...data,
119
124
  },
120
- ...(spanContext?.traceId && { traceId: spanContext.traceId }),
121
- ...(spanContext?.spanId && { spanId: spanContext.spanId }),
122
- ...(spanContext?.traceFlags !== undefined && {
123
- traceFlags: spanContext.traceFlags,
124
- }),
125
125
  };
126
126
 
127
+ // Add trace context to log record if available
128
+ if (traceId) {
129
+ logRecord.traceId = traceId;
130
+ }
131
+ if (spanId) {
132
+ logRecord.spanId = spanId;
133
+ }
134
+ if (spanContext?.traceFlags !== undefined) {
135
+ logRecord.traceFlags = spanContext.traceFlags;
136
+ } else if (traceId) {
137
+ logRecord.traceFlags = 1; // Default to SAMPLED if we have a traceId
138
+ }
139
+
127
140
  otelLogger.emit(logRecord);
128
141
  } catch (error) {
129
142
  console.error("[Logger] Failed to send OTLP log:", error);