@atrim/instrument-web 0.5.0 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atrim/instrument-web",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "OpenTelemetry instrumentation for browsers with centralized YAML configuration",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -82,10 +82,10 @@
82
82
  "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
83
83
  "format:check": "prettier --check \"src/**/*.ts\" \"test/**/*.ts\"",
84
84
  "clean": "rm -rf target",
85
- "publish:dev:version": "npm version $(git describe --tags --abbrev=0 | sed 's/^.*@//' | sed 's/^v//')-$(git rev-parse --short HEAD)-$(date -u +%Y%m%d%H%M%S) --no-git-tag-version",
85
+ "publish:dev:version": "pnpm version $(node -p \"require('./package.json').version\")-$(git rev-parse --short HEAD)-$(date -u +%Y%m%d%H%M%S) --no-git-tag-version",
86
86
  "publish:dev:save": "node -p \"require('./package.json').version\" > .version",
87
87
  "publish:dev:publish": "pnpm build && pnpm publish --tag dev --access public --no-git-checks",
88
- "publish:dev:reset": "npm version 0.1.0 --no-git-tag-version",
88
+ "publish:dev:reset": "pnpm version 0.5.0 --no-git-tag-version",
89
89
  "publish:dev": "pnpm publish:dev:version && pnpm publish:dev:save && pnpm publish:dev:publish && pnpm publish:dev:reset"
90
90
  }
91
91
  }
@@ -70,6 +70,31 @@ interface SdkInitializationOptions {
70
70
  * @default true
71
71
  */
72
72
  enableXhr?: boolean;
73
+ /**
74
+ * Control trace context propagation for cross-origin requests
75
+ *
76
+ * Determines which cross-origin requests receive W3C Trace Context headers
77
+ * (traceparent, tracestate). Note: Backends must allow these headers in CORS
78
+ * configuration or requests will fail with CORS errors.
79
+ *
80
+ * - 'all': Propagate to all cross-origin requests (may cause CORS errors)
81
+ * - 'none': Never propagate trace headers
82
+ * - 'same-origin': Only propagate to same-origin requests (default)
83
+ * - Array of URL patterns: Custom propagation patterns (regex strings)
84
+ *
85
+ * @default 'same-origin'
86
+ *
87
+ * @example Propagate to specific API domains
88
+ * ```typescript
89
+ * propagateTraceContext: ['^https://api\\.myapp\\.com', '^http://localhost:300[0-9]']
90
+ * ```
91
+ *
92
+ * @example Disable propagation (for debugging CORS issues)
93
+ * ```typescript
94
+ * propagateTraceContext: 'none'
95
+ * ```
96
+ */
97
+ propagateTraceContext?: 'all' | 'none' | 'same-origin' | string[];
73
98
  }
74
99
  /**
75
100
  * Get the current SDK instance
@@ -4095,7 +4095,20 @@ var HttpFilteringConfigSchema = external_exports.object({
4095
4095
  // Patterns to ignore for incoming HTTP requests (string patterns only in YAML)
4096
4096
  ignore_incoming_paths: external_exports.array(external_exports.string()).optional(),
4097
4097
  // Require parent span for outgoing requests (prevents root spans for HTTP calls)
4098
- require_parent_for_outgoing_spans: external_exports.boolean().optional()
4098
+ require_parent_for_outgoing_spans: external_exports.boolean().optional(),
4099
+ // Trace context propagation configuration
4100
+ // Controls which cross-origin requests receive W3C Trace Context headers (traceparent, tracestate)
4101
+ propagate_trace_context: external_exports.object({
4102
+ // Strategy for trace propagation
4103
+ // - "all": Propagate to all cross-origin requests (may cause CORS errors)
4104
+ // - "none": Never propagate trace headers
4105
+ // - "same-origin": Only propagate to same-origin requests (default, safe)
4106
+ // - "patterns": Propagate based on include_urls patterns
4107
+ strategy: external_exports.enum(["all", "none", "same-origin", "patterns"]).default("same-origin"),
4108
+ // URL patterns to include when strategy is "patterns"
4109
+ // Supports regex patterns (e.g., "^https://api\\.myapp\\.com")
4110
+ include_urls: external_exports.array(external_exports.string()).optional()
4111
+ }).optional()
4099
4112
  });
4100
4113
  var InstrumentationConfigSchema = external_exports.object({
4101
4114
  version: external_exports.string(),
@@ -4526,6 +4539,54 @@ var PatternSpanProcessor = class {
4526
4539
 
4527
4540
  // src/core/sdk-initializer.ts
4528
4541
  var sdkInstance = null;
4542
+ function buildPropagateTraceUrls(options, config) {
4543
+ const apiOption = options.propagateTraceContext;
4544
+ const yamlStrategy = config?.http?.propagate_trace_context?.strategy;
4545
+ const yamlIncludeUrls = config?.http?.propagate_trace_context?.include_urls;
4546
+ let effectiveStrategy;
4547
+ let patterns = [];
4548
+ if (apiOption !== void 0) {
4549
+ if (typeof apiOption === "string") {
4550
+ effectiveStrategy = apiOption;
4551
+ } else {
4552
+ effectiveStrategy = "patterns";
4553
+ patterns = apiOption;
4554
+ }
4555
+ } else if (yamlStrategy) {
4556
+ effectiveStrategy = yamlStrategy;
4557
+ if (effectiveStrategy === "patterns" && yamlIncludeUrls) {
4558
+ patterns = yamlIncludeUrls;
4559
+ }
4560
+ } else {
4561
+ effectiveStrategy = "same-origin";
4562
+ }
4563
+ switch (effectiveStrategy) {
4564
+ case "all":
4565
+ return [/.*/];
4566
+ case "none":
4567
+ case "same-origin":
4568
+ return [];
4569
+ case "patterns": {
4570
+ const regexes = [];
4571
+ for (const pattern of patterns) {
4572
+ try {
4573
+ regexes.push(new RegExp(pattern));
4574
+ } catch (error) {
4575
+ console.warn(
4576
+ `[@atrim/instrument-web] Invalid trace propagation pattern: "${pattern}"`,
4577
+ error
4578
+ );
4579
+ }
4580
+ }
4581
+ return regexes;
4582
+ }
4583
+ default:
4584
+ console.warn(
4585
+ `[@atrim/instrument-web] Unknown trace propagation strategy: "${effectiveStrategy}". Defaulting to same-origin only.`
4586
+ );
4587
+ return [];
4588
+ }
4589
+ }
4529
4590
  async function initializeSdk(options) {
4530
4591
  if (sdkInstance) {
4531
4592
  return sdkInstance;
@@ -4563,6 +4624,7 @@ async function initializeSdk(options) {
4563
4624
  "[@atrim/instrument-web] Missing http.ignore_outgoing_urls in instrumentation.yaml. Using default OTLP endpoint patterns only. Consider adding http filtering to your config for better control."
4564
4625
  );
4565
4626
  }
4627
+ const propagateTraceUrls = buildPropagateTraceUrls(options, config);
4566
4628
  const exporterOptions = {};
4567
4629
  if (options.otlpEndpoint) {
4568
4630
  exporterOptions.endpoint = options.otlpEndpoint;
@@ -4594,15 +4656,16 @@ async function initializeSdk(options) {
4594
4656
  },
4595
4657
  "@opentelemetry/instrumentation-fetch": {
4596
4658
  enabled: options.enableFetch ?? true,
4597
- propagateTraceHeaderCorsUrls: [/.*/],
4598
- // Propagate to all origins
4659
+ propagateTraceHeaderCorsUrls: propagateTraceUrls,
4660
+ // Controlled by config/API
4599
4661
  clearTimingResources: true,
4600
4662
  ignoreUrls
4601
4663
  // Prevent self-instrumentation of OTLP exports
4602
4664
  },
4603
4665
  "@opentelemetry/instrumentation-xml-http-request": {
4604
4666
  enabled: options.enableXhr ?? true,
4605
- propagateTraceHeaderCorsUrls: [/.*/]
4667
+ propagateTraceHeaderCorsUrls: propagateTraceUrls
4668
+ // Controlled by config/API
4606
4669
  }
4607
4670
  })
4608
4671
  ]