@obsrviq/tracker 0.7.0 → 0.7.1

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/dist/index.js CHANGED
@@ -837,16 +837,36 @@ function hostAllowed(hostname, hosts) {
837
837
  }
838
838
  return false;
839
839
  }
840
- function injectFetchTrace(input, init, header) {
840
+ function traceIdOf(header) {
841
+ if (!header) return null;
842
+ const parts = header.split("-");
843
+ return parts.length >= 2 && /^[0-9a-f]{32}$/.test(parts[1]) ? parts[1] : null;
844
+ }
845
+ function existingFetchTraceparent(input, init) {
846
+ const hasInitHeaders = !!init && "headers" in init && init.headers != null;
847
+ const src = hasInitHeaders ? init.headers : input instanceof Request ? input.headers : void 0;
848
+ if (!src) return null;
849
+ try {
850
+ return new Headers(src).get("traceparent");
851
+ } catch {
852
+ return null;
853
+ }
854
+ }
855
+ function applyFetchTrace(input, init) {
856
+ const existing = existingFetchTraceparent(input, init);
857
+ if (existing) {
858
+ return { input, init, traceId: traceIdOf(existing) ?? "" };
859
+ }
860
+ const tr = newTrace();
841
861
  const hasInitHeaders = !!init && "headers" in init && init.headers != null;
842
862
  if (input instanceof Request && !hasInitHeaders) {
843
863
  const h2 = new Headers(input.headers);
844
- if (!h2.has("traceparent")) h2.set("traceparent", header);
845
- return { input: new Request(input, { headers: h2 }), init };
864
+ h2.set("traceparent", tr.header);
865
+ return { input: new Request(input, { headers: h2 }), init, traceId: tr.traceId };
846
866
  }
847
867
  const h = new Headers(init?.headers ?? void 0);
848
- if (!h.has("traceparent")) h.set("traceparent", header);
849
- return { input, init: { ...init || {}, headers: h } };
868
+ h.set("traceparent", tr.header);
869
+ return { input, init: { ...init || {}, headers: h }, traceId: tr.traceId };
850
870
  }
851
871
 
852
872
  // src/network.ts
@@ -1102,11 +1122,10 @@ function wrapFetch(ctx, tr, emitNet) {
1102
1122
  let traceId;
1103
1123
  if (traceTarget(rawUrl, ctx.config)) {
1104
1124
  try {
1105
- const tr2 = newTrace();
1106
- const inj = injectFetchTrace(input, init, tr2.header);
1125
+ const inj = applyFetchTrace(input, init);
1107
1126
  input = inj.input;
1108
1127
  init = inj.init;
1109
- traceId = tr2.traceId;
1128
+ traceId = inj.traceId || void 0;
1110
1129
  } catch {
1111
1130
  }
1112
1131
  }
@@ -1216,9 +1235,14 @@ function wrapXhr(ctx, tr, emitNet) {
1216
1235
  if (ctx.config.captureNetworkBodies) meta.body = stringifyBody(body);
1217
1236
  if (traceTarget(meta.rawUrl, ctx.config)) {
1218
1237
  try {
1219
- const tr2 = newTrace();
1220
- this.setRequestHeader("traceparent", tr2.header);
1221
- meta.traceId = tr2.traceId;
1238
+ const appSet = meta.reqHeaders["traceparent"];
1239
+ if (appSet) {
1240
+ meta.traceId = traceIdOf(appSet) ?? void 0;
1241
+ } else {
1242
+ const tr2 = newTrace();
1243
+ this.setRequestHeader("traceparent", tr2.header);
1244
+ meta.traceId = tr2.traceId;
1245
+ }
1222
1246
  } catch {
1223
1247
  }
1224
1248
  }