Skip to content

Commit 6c5430d

Browse files
committed
Update config
1 parent 4072966 commit 6c5430d

File tree

5 files changed

+151
-29
lines changed

5 files changed

+151
-29
lines changed

packages/pwa-kit-react-sdk/src/ssr/server/opentelemetry-server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {B3Propagator} from '@opentelemetry/propagator-b3'
1111
import {Resource} from '@opentelemetry/resources'
1212
import {propagation} from '@opentelemetry/api'
1313
import logger from '../../utils/logger-instance'
14-
import {getServiceName, OTEL_CONFIG} from '../../utils/opentelemetry'
14+
import {getServiceName, getOTELConfig} from '../../utils/opentelemetry-config'
1515

1616
let provider = null
1717

@@ -27,7 +27,7 @@ export const initializeServerTracing = (options = {}) => {
2727
const {
2828
serviceName = options.serviceName || getServiceName(),
2929
serviceVersion,
30-
enabled = OTEL_CONFIG.enabled
30+
enabled = getOTELConfig().enabled
3131
} = options
3232

3333
// If tracing is disabled, return null without initializing
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025, Salesforce, Inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
const DEFAULT_SERVICE_NAME = 'pwa-kit-react-sdk'
9+
10+
// Only call this function in the server context
11+
// This wrapper function is necessary because if the config is in the top-level code
12+
// process will be undefined as it gets executed in the browser context and will throw an uncaught error.
13+
export const getOTELConfig = () => {
14+
return {
15+
serviceName: process.env.OTEL_SERVICE_NAME || DEFAULT_SERVICE_NAME,
16+
enabled: process.env.OTEL_SDK_ENABLED === 'true',
17+
b3TracingEnabled: process.env.OTEL_B3_TRACING_ENABLED === 'true'
18+
}
19+
}
20+
21+
export const getServiceName = () => getOTELConfig().serviceName
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2025, Salesforce, Inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
import {getOTELConfig, getServiceName} from './opentelemetry-config'
9+
10+
// Mock the module to reset cache between tests
11+
const mockModule = () => {
12+
jest.resetModules()
13+
return require('./opentelemetry-config')
14+
}
15+
16+
describe('OpenTelemetry Config', () => {
17+
const originalEnv = process.env
18+
19+
beforeEach(() => {
20+
// Reset environment variables
21+
process.env = {...originalEnv}
22+
23+
// Clear module cache to reset _cachedConfig
24+
jest.resetModules()
25+
})
26+
27+
afterEach(() => {
28+
// Restore original environment
29+
process.env = originalEnv
30+
})
31+
32+
describe('getOTELConfig', () => {
33+
test('should return default config when no environment variables are set', () => {
34+
delete process.env.OTEL_SERVICE_NAME
35+
delete process.env.OTEL_SDK_ENABLED
36+
delete process.env.OTEL_B3_TRACING_ENABLED
37+
38+
const {getOTELConfig} = mockModule()
39+
const config = getOTELConfig()
40+
41+
expect(config).toEqual({
42+
serviceName: 'pwa-kit-react-sdk',
43+
enabled: false,
44+
b3TracingEnabled: false
45+
})
46+
})
47+
48+
test('should use environment variables when provided', () => {
49+
process.env.OTEL_SERVICE_NAME = 'test-service'
50+
process.env.OTEL_SDK_ENABLED = 'true'
51+
process.env.OTEL_B3_TRACING_ENABLED = 'true'
52+
53+
const {getOTELConfig} = mockModule()
54+
const config = getOTELConfig()
55+
56+
expect(config).toEqual({
57+
serviceName: 'test-service',
58+
enabled: true,
59+
b3TracingEnabled: true
60+
})
61+
})
62+
63+
test('should handle partial environment variables', () => {
64+
process.env.OTEL_SERVICE_NAME = 'custom-service'
65+
process.env.OTEL_SDK_ENABLED = 'false'
66+
delete process.env.OTEL_B3_TRACING_ENABLED
67+
68+
const {getOTELConfig} = mockModule()
69+
const config = getOTELConfig()
70+
71+
expect(config).toEqual({
72+
serviceName: 'custom-service',
73+
enabled: false,
74+
b3TracingEnabled: false
75+
})
76+
})
77+
78+
test('should treat non-"true" values as false for boolean flags', () => {
79+
process.env.OTEL_SDK_ENABLED = 'false'
80+
process.env.OTEL_B3_TRACING_ENABLED = 'yes'
81+
82+
const {getOTELConfig} = mockModule()
83+
const config = getOTELConfig()
84+
85+
expect(config.enabled).toBe(false)
86+
expect(config.b3TracingEnabled).toBe(false)
87+
})
88+
89+
test('should handle empty string environment variables', () => {
90+
process.env.OTEL_SERVICE_NAME = ''
91+
process.env.OTEL_SDK_ENABLED = ''
92+
process.env.OTEL_B3_TRACING_ENABLED = ''
93+
94+
const {getOTELConfig} = mockModule()
95+
const config = getOTELConfig()
96+
97+
expect(config).toEqual({
98+
serviceName: 'pwa-kit-react-sdk', // Falls back to default
99+
enabled: false,
100+
b3TracingEnabled: false
101+
})
102+
})
103+
})
104+
105+
describe('getServiceName', () => {
106+
test('should return service name from config', () => {
107+
process.env.OTEL_SERVICE_NAME = 'test-service-name'
108+
109+
const {getServiceName} = mockModule()
110+
const serviceName = getServiceName()
111+
112+
expect(serviceName).toBe('test-service-name')
113+
})
114+
115+
test('should return default service name when no env var set', () => {
116+
delete process.env.OTEL_SERVICE_NAME
117+
118+
const {getServiceName} = mockModule()
119+
const serviceName = getServiceName()
120+
121+
expect(serviceName).toBe('pwa-kit-react-sdk')
122+
})
123+
})
124+
})

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,7 @@
88
import {trace, context, SpanStatusCode} from '@opentelemetry/api'
99
import {hrTimeToMilliseconds, hrTimeToTimeStamp} from '@opentelemetry/core'
1010
import logger from './logger-instance'
11-
12-
const DEFAULT_SERVICE_NAME = 'pwa-kit-react-sdk'
13-
14-
export const OTEL_CONFIG = {
15-
serviceName:
16-
(typeof process !== 'undefined' && process.env.OTEL_SERVICE_NAME) || DEFAULT_SERVICE_NAME,
17-
enabled: typeof process !== 'undefined' && process.env.OTEL_SDK_ENABLED === 'true',
18-
b3TracingEnabled:
19-
typeof process !== 'undefined' && process.env.OTEL_B3_TRACING_ENABLED === 'true'
20-
}
21-
22-
export const getServiceName = () => OTEL_CONFIG.serviceName
11+
import {getOTELConfig, getServiceName} from './opentelemetry-config'
2312

2413
const logSpanData = (span, event = 'start', res = null) => {
2514
const spanContext = span.spanContext()
@@ -47,16 +36,11 @@ const logSpanData = (span, event = 'start', res = null) => {
4736
links: [],
4837
start_time: startTime,
4938
end_time: endTime,
50-
forwardTrace: OTEL_CONFIG.b3TracingEnabled
39+
forwardTrace: getOTELConfig().b3TracingEnabled
5140
}
5241

53-
// Inject B3 headers into response if available (server-side only)
54-
if (
55-
res &&
56-
typeof process !== 'undefined' &&
57-
process.env.DISABLE_B3_TRACING !== 'true' &&
58-
event === 'start'
59-
) {
42+
// Inject B3 headers into response if available
43+
if (res && process.env.DISABLE_B3_TRACING !== 'true' && event === 'start') {
6044
res.setHeader('x-b3-traceid', spanContext.traceId)
6145
res.setHeader('x-b3-spanid', spanContext.spanId)
6246
res.setHeader('x-b3-sampled', '1')

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ describe('OpenTelemetry Utilities', () => {
103103
opentelemetryUtils = require('./opentelemetry')
104104
})
105105

106-
describe('getServiceName', () => {
107-
test('should return the service name from config', () => {
108-
const serviceName = opentelemetryUtils.getServiceName()
109-
expect(serviceName).toBe('pwa-kit-react-sdk')
110-
})
111-
})
112-
113106
describe('createSpan', () => {
114107
test('should create a span successfully', () => {
115108
const result = opentelemetryUtils.createSpan('test-span', {

0 commit comments

Comments
 (0)