Skip to content

Commit f6ffadf

Browse files
edited open telemetry js and test file
1 parent 01da556 commit f6ffadf

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export const OTEL_CONFIG = {
1919

2020
export const getServiceName = () => OTEL_CONFIG.serviceName
2121

22-
const SERVICE_NAME = OTEL_CONFIG.serviceName
23-
2422
const logSpanData = (span, event = 'start', res = null) => {
2523
const spanContext = span.spanContext()
2624
const startTime = span.startTime
@@ -37,7 +35,7 @@ const logSpanData = (span, event = 'start', res = null) => {
3735
timestamp: hrTimeToTimeStamp(startTime),
3836
duration: duration,
3937
attributes: {
40-
'service.name': SERVICE_NAME,
38+
'service.name': getServiceName(),
4139
...span.attributes,
4240
event: event // Add event type to distinguish start/end
4341
},
@@ -77,10 +75,7 @@ const logSpanData = (span, event = 'start', res = null) => {
7775
*/
7876
export const createSpan = (name, options = {}) => {
7977
try {
80-
const tracer = trace.getTracer(SERVICE_NAME)
81-
// Get the current context and active span
82-
const ctx = context.active()
83-
// Note: currentSpan is not used in this implementation
78+
const tracer = trace.getTracer(getServiceName())
8479

8580
// Create a new span with the current context
8681
const span = tracer.startSpan(
@@ -89,15 +84,15 @@ export const createSpan = (name, options = {}) => {
8984
...options,
9085
attributes: {
9186
...options.attributes,
92-
'service.name': SERVICE_NAME
87+
'service.name': getServiceName()
9388
}
9489
},
95-
ctx
90+
context.active()
9691
)
9792

9893
// Set the new span as active
9994
logSpanData(span, 'start')
100-
return trace.setSpan(ctx, span)
95+
return trace.setSpan(context.active(), span)
10196
} catch (error) {
10297
logger.error('Failed to create span', {
10398
namespace: 'opentelemetry',
@@ -118,7 +113,7 @@ export const createSpan = (name, options = {}) => {
118113
*/
119114
export const createChildSpan = (name, attributes = {}) => {
120115
try {
121-
const tracer = trace.getTracer(SERVICE_NAME)
116+
const tracer = trace.getTracer(getServiceName())
122117
const ctx = context.active()
123118
const parentSpan = trace.getSpan(ctx)
124119

@@ -130,7 +125,7 @@ export const createChildSpan = (name, attributes = {}) => {
130125
const {performance_mark, performance_detail, ...otherAttributes} = attributes
131126

132127
const spanAttributes = {
133-
'service.name': SERVICE_NAME,
128+
'service.name': getServiceName(),
134129
...otherAttributes
135130
}
136131

@@ -176,9 +171,6 @@ export const endSpan = (span) => {
176171
}
177172

178173
try {
179-
const ctx = context.active()
180-
// Note: parentSpan is not used in this implementation
181-
182174
span.end()
183175

184176
// Log completion data
@@ -202,11 +194,11 @@ export const endSpan = (span) => {
202194
* @returns {Promise<any>} The result of the function
203195
*/
204196
export const tracePerformance = async (name, fn, res = null) => {
205-
const tracer = trace.getTracer(SERVICE_NAME)
197+
const tracer = trace.getTracer(getServiceName())
206198
// Create the root span
207199
const rootSpan = tracer.startSpan(name, {
208200
attributes: {
209-
'service.name': SERVICE_NAME
201+
'service.name': getServiceName()
210202
}
211203
})
212204

@@ -254,7 +246,7 @@ export const tracePerformance = async (name, fn, res = null) => {
254246
*/
255247
export const logPerformanceMetric = (name, duration, attributes = {}) => {
256248
try {
257-
const tracer = trace.getTracer(SERVICE_NAME)
249+
const tracer = trace.getTracer(getServiceName())
258250
const ctx = context.active()
259251
const parentSpan = trace.getSpan(ctx)
260252

@@ -271,7 +263,7 @@ export const logPerformanceMetric = (name, duration, attributes = {}) => {
271263

272264
// Build metric attributes
273265
const metricAttributes = {
274-
'service.name': SERVICE_NAME,
266+
'service.name': getServiceName(),
275267
'metric.duration': duration,
276268
...otherAttributes
277269
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
/*
8-
* Copyright (c) 2025, Salesforce, Inc.
9-
* All rights reserved.
10-
* SPDX-License-Identifier: BSD-3-Clause
11-
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
12-
*/
137

148
// Mock OpenTelemetry dependencies
159
jest.mock('@opentelemetry/api', () => ({

0 commit comments

Comments
 (0)