Skip to content

Commit 8137759

Browse files
authored
Merge pull request #4073 from SalesforceCommerceCloud/feat/mrt-distributed-tracing
@WI-24614336: Enable tracing with MRT distributed tracing flag
2 parents 7865a2a + 75d7000 commit 8137759

6 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎docs/distributed-tracing.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ DT env var**.
3333
| Env var | Effect |
3434
|---------|--------|
3535
| `OTEL_TRACING_ENABLED=true` | Enables DT (and `server_timing`'s OTel path). Resolved via `getOTELConfig().enabled`. |
36+
| `MRT_DISTRIBUTED_TRACING_ENABLED=true` | Alternative MRT-managed switch that enables the same shared tracing paths. |
3637
| `OTEL_SERVICE_NAME` | Affects `server_timing` only. DT's `service.name` is a hardcoded constant (`pwa-kit-react-sdk`) and ignores this var. |
3738

38-
When `OTEL_TRACING_ENABLED` is unset (or anything other than `"true"`), DT is a
39-
complete no-op: no spans, no header injection, zero behavior change.
39+
When neither tracing flag is set to `"true"`, DT is a complete no-op: no spans,
40+
no header injection, zero behavior change.
4041

4142
Set the env var via MRT environment configuration. No code change or redeploy of
4243
application code is required to toggle it.
@@ -101,7 +102,7 @@ packages/pwa-kit-react-sdk/src/
101102
mrt-console-span-exporter.js ← stdout exporter (MRT scrape contract)
102103
react-rendering.js ← wires server + child spans into the render path
103104
utils/
104-
opentelemetry-config.js ← getOTELConfig(): the OTEL_TRACING_ENABLED master switch
105+
opentelemetry-config.js ← getOTELConfig(): the shared tracing master switch
105106
ssr/universal/components/with-react-query/
106107
index.js ← creates per-query scapi:* child spans (OTel-free; uses res.locals helper)
107108

‎packages/pwa-kit-react-sdk/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## v3.22.0-dev (Oct 09, 2026)
2+
- [Feature] Enable OpenTelemetry and distributed tracing when either `OTEL_TRACING_ENABLED=true` or MRT's `MRT_DISTRIBUTED_TRACING_ENABLED=true` is set.
23
## v3.21.0 (Oct 08, 2026)
34
- Version alignment release; no functional changes to `pwa-kit-react-sdk` since v3.20.0.
45

‎packages/pwa-kit-react-sdk/src/ssr/server/mrt-console-span-exporter.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* exporter's only job is to print the JSON shape MRT expects. No OTLP endpoint
1313
* is involved.
1414
*
15-
* @env OTEL_TRACING_ENABLED — resolved via getOTELConfig().enabled
15+
* @env OTEL_TRACING_ENABLED / MRT_DISTRIBUTED_TRACING_ENABLED — resolved via
16+
* getOTELConfig().enabled
1617
*/
1718
import {ConsoleSpanExporter} from '@opentelemetry/sdk-trace-base'
1819
import {ExportResultCode, hrTimeToTimeStamp} from '@opentelemetry/core'

‎packages/pwa-kit-react-sdk/src/utils/opentelemetry-config.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const DEFAULT_SERVICE_NAME = 'pwa-kit-react-sdk'
1313
export const getOTELConfig = () => {
1414
return {
1515
serviceName: process.env.OTEL_SERVICE_NAME || DEFAULT_SERVICE_NAME,
16-
enabled: process.env.OTEL_TRACING_ENABLED === 'true'
16+
enabled:
17+
process.env.OTEL_TRACING_ENABLED === 'true' ||
18+
process.env.MRT_DISTRIBUTED_TRACING_ENABLED === 'true'
1719
}
1820
}
1921

‎packages/pwa-kit-react-sdk/src/utils/opentelemetry-config.test.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('OpenTelemetry Config', () => {
1717
beforeEach(() => {
1818
// Reset environment variables
1919
process.env = {...originalEnv}
20+
delete process.env.OTEL_TRACING_ENABLED
21+
delete process.env.MRT_DISTRIBUTED_TRACING_ENABLED
2022

2123
// Clear module cache to reset _cachedConfig
2224
jest.resetModules()
@@ -67,6 +69,15 @@ describe('OpenTelemetry Config', () => {
6769
expect(config.enabled).toBe(true)
6870
})
6971

72+
test('should return enabled as true when MRT_DISTRIBUTED_TRACING_ENABLED is "true"', () => {
73+
process.env.MRT_DISTRIBUTED_TRACING_ENABLED = 'true'
74+
75+
const {getOTELConfig} = mockModule()
76+
const config = getOTELConfig()
77+
78+
expect(config.enabled).toBe(true)
79+
})
80+
7081
test('should return enabled as false when OTEL_TRACING_ENABLED is not set', () => {
7182
delete process.env.OTEL_TRACING_ENABLED
7283

@@ -97,6 +108,16 @@ describe('OpenTelemetry Config', () => {
97108
expect(config.enabled).toBe(false)
98109
})
99110
})
111+
112+
test('should return enabled as false when neither tracing flag is "true"', () => {
113+
process.env.OTEL_TRACING_ENABLED = 'false'
114+
process.env.MRT_DISTRIBUTED_TRACING_ENABLED = '1'
115+
116+
const {getOTELConfig} = mockModule()
117+
const config = getOTELConfig()
118+
119+
expect(config.enabled).toBe(false)
120+
})
100121
})
101122
})
102123
})

‎packages/pwa-kit-react-sdk/src/utils/opentelemetry.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export const createChildSpan = (name, attributes = {}) => {
123123
span_name: name,
124124
otel_enabled: otelConfig.enabled,
125125
otel_service_name: otelConfig.serviceName,
126-
suggestion: 'Set OTEL_TRACING_ENABLED=true to enable proper timing'
126+
suggestion:
127+
'Set OTEL_TRACING_ENABLED=true or MRT_DISTRIBUTED_TRACING_ENABLED=true to enable proper timing'
127128
}
128129
})
129130
}

0 commit comments

Comments
 (0)