-
Notifications
You must be signed in to change notification settings - Fork 214
@W-18759755 - Creating tests and a server for open telemetry within the pwa-kit-rea… #2617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
larnelleankunda
merged 14 commits into
feature/opentelemetry
from
W-18759755-opentelemetry-server.js
Jul 1, 2025
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ae56f79
Creating tests and a server for open telemetry within the pwa-kit-rea…
larnelleankunda b442012
Creating tests and a server for open telemetry within the pwa-kit-rea…
larnelleankunda 6f50178
Updating my package lock.json file for the pwa-kit-react-sdk folder w…
larnelleankunda c6c1d89
Updated the open telemetry tests
larnelleankunda f03ca23
Linted open telemetry files
larnelleankunda 245f60d
Linted open telemetry files
larnelleankunda a6d0bb7
Linted open telemetry files
larnelleankunda dc48667
updated open telemetry files
larnelleankunda e246905
Merge branch 'feature/opentelemetry' into W-18759755-opentelemetry-se…
larnelleankunda 40c3da9
made changes to server and test files
larnelleankunda 8095da0
made changes to server and test files
larnelleankunda 6cc769a
made changes to server and test files
larnelleankunda c2f1b91
made changes to test files
larnelleankunda 9130b9a
made changes to test and server files
larnelleankunda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/pwa-kit-react-sdk/src/ssr/server/opentelemetry-server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
|
|
||
| import {NodeTracerProvider} from '@opentelemetry/sdk-trace-node' | ||
| import {SimpleSpanProcessor} from '@opentelemetry/sdk-trace-base' | ||
| import {B3Propagator} from '@opentelemetry/propagator-b3' | ||
| import {Resource} from '@opentelemetry/resources' | ||
| import {propagation} from '@opentelemetry/api' | ||
| import logger from '../../utils/logger-instance' | ||
|
|
||
| const DEFAULT_SERVICE_NAME = 'pwa-kit-react-sdk' | ||
|
|
||
| let provider = null | ||
|
|
||
| /** | ||
| * Initialize OpenTelemetry tracing for server-side rendering | ||
| * @param {Object} options | ||
| * @param {string} [options.serviceName] | ||
| * @param {string} [options.serviceVersion] | ||
| * @param {boolean} [options.enabled] | ||
| * @returns {NodeTracerProvider|null} | ||
| */ | ||
| export const initializeServerTracing = (options = {}) => { | ||
| const { | ||
| serviceName = process.env.OTEL_SERVICE_NAME || DEFAULT_SERVICE_NAME, | ||
| serviceVersion = process.env.npm_package_version, | ||
| enabled = process.env.OTEL_SDK_ENABLED === 'true' | ||
| } = options | ||
|
|
||
| // If tracing is disabled, return null without initializing | ||
| if (!enabled) { | ||
| return null | ||
| } | ||
|
|
||
| try { | ||
| // Build resource attributes | ||
| const resourceAttributes = { | ||
| 'service.name': serviceName | ||
| } | ||
|
|
||
| // Add service version if provided | ||
| if (serviceVersion) { | ||
| resourceAttributes['service.version'] = serviceVersion | ||
| } | ||
|
|
||
| // Initialize the tracer provider | ||
| provider = new NodeTracerProvider({ | ||
| resource: new Resource(resourceAttributes), | ||
| spanProcessor: new SimpleSpanProcessor() | ||
| }) | ||
|
|
||
| // Add B3 propagator | ||
| propagation.setGlobalPropagator(new B3Propagator()) | ||
| provider.register() | ||
|
|
||
| return provider | ||
| } catch (error) { | ||
| // Log errors from OpenTelemetry initialization | ||
| logger.error('Failed to initialize OpenTelemetry provider', { | ||
| namespace: 'opentelemetry-server.initializeServerTracing', | ||
| additionalProperties: {error: error.message} | ||
| }) | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Shutdown OpenTelemetry tracing and clean up resources | ||
| * @returns {Promise<void>} | ||
| */ | ||
| export const shutdownServerTracing = async () => { | ||
| if (provider) { | ||
| try { | ||
| await provider.shutdown() | ||
| provider = null // Clean up after successful shutdown | ||
| } catch (error) { | ||
| logger.warn('Failed to shutdown OpenTelemetry provider', { | ||
| namespace: 'opentelemetry-server.shutdownServerTracing', | ||
| additionalProperties: {error: error.message} | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the current OpenTelemetry provider instance | ||
| * @returns {NodeTracerProvider|null} The current provider or null if not initialized | ||
| */ | ||
| export const getServerTracingProvider = () => { | ||
| return provider | ||
| } | ||
|
|
||
| /** | ||
| * Check if OpenTelemetry tracing is currently initialized | ||
| * @returns {boolean} True if tracing is initialized, false otherwise | ||
| */ | ||
| export const isServerTracingInitialized = () => { | ||
| return provider !== null | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@larnelleankunda Thanks for adding the telemetry options! It was my bad for not clarifying the code I shared was just an example of possible configuration options. Since
process.env.npm_package_versionwill always be undefined, let’s remove it for now. We can explore a better way to include it later if needed.