Skip to content

@W-18759755 - Creating tests and a server for open telemetry within the pwa-kit-rea…#2617

Merged
larnelleankunda merged 14 commits intofeature/opentelemetryfrom
W-18759755-opentelemetry-server.js
Jul 1, 2025
Merged

@W-18759755 - Creating tests and a server for open telemetry within the pwa-kit-rea…#2617
larnelleankunda merged 14 commits intofeature/opentelemetryfrom
W-18759755-opentelemetry-server.js

Conversation

@larnelleankunda
Copy link
Contributor

@larnelleankunda larnelleankunda commented Jun 20, 2025

Creating tests and a server for open telemetry within the pwa-kit-react-sdk folder acting as a service provider

Description

<!
Implemented the functions to initialize and shutdown OTel tracing
Implemented initializeServerTracing and shutdownServerTracing functions into the open telemetry server
B3 propagation is properly set up for tracing
Error handling is implemented to catch and log failures
Wrote unit tests with 100% coverage of new code>

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • (step1)

How to Test-Drive This PR

  • Start your PWA Kit application
  • Check console logs for any OpenTelemetry initialization messages
  • No errors related to OpenTelemetry initialization should be received
    -To verify B3 Propagation you have make a request to the PWA Kit app locally.
  • Check response headers for B3 trace context headers:
    b3 header (single header format)
    Or X-B3-SpanId, X-B3-TracenId, X-B3-Sampled
    -The B3 headers should be present in responses
    -In Verifying its Shutdown you should stop the application and no OpenTelemetry-related errors during shutdown should be seen.

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@larnelleankunda larnelleankunda requested a review from a team as a code owner June 20, 2025 23:32
@cc-prodsec
Copy link
Collaborator

cc-prodsec commented Jun 20, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@larnelleankunda larnelleankunda changed the title Creating tests and a server for open telemetry within the pwa-kit-rea… W-18759755 - Creating tests and a server for open telemetry within the pwa-kit-rea… Jun 20, 2025
@larnelleankunda larnelleankunda changed the title W-18759755 - Creating tests and a server for open telemetry within the pwa-kit-rea… @W-18759755 - Creating tests and a server for open telemetry within the pwa-kit-rea… Jun 20, 2025
@yunakim714
Copy link
Collaborator

@larnelleankunda Hey Larnelle! I would create a feature branch that we can merge into, so that we are not merging an incomplete feature into the develop branch.

Could you also update the PR description - I see this at the top not sure if it cut something off! "…ct-sdk folder acting as a service provider"

@larnelleankunda larnelleankunda changed the base branch from develop to feature/opentelemetry-clean June 24, 2025 21:35
@larnelleankunda larnelleankunda changed the base branch from feature/opentelemetry-clean to feature/opentelemetry June 24, 2025 21:41
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2024, Salesforce, Inc.
* Copyright (c) 2025, Salesforce, Inc.

"@loadable/server": "^5.15.3",
"@loadable/webpack-plugin": "^5.15.2",
"@opentelemetry/api": "^1.4.1",
"@opentelemetry/core": "^1.15.1",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to include the @opentelemetry/core package here? We are not importing anything from this package in the new opentelemetry-server.js file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was migrating all the packages from the POC branch into this package.json but I can remove the core package.

* @jest-environment node
*/
/*
* Copyright (c) 2024, Salesforce, Inc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2024, Salesforce, Inc.
* Copyright (c) 2025, Salesforce, Inc.

* Initialize OpenTelemetry tracing for server-side rendering
* @returns {NodeTracerProvider|null} The initialized provider or null if initialization failed
*/
export const initializeServerTracing = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you consider making the telemetry configurable by passing options to the initializeServerTracing?

Something like this:

Suggested change
export const initializeServerTracing = () => {
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
...

return provider
} catch (error) {
// Log errors from OpenTelemetry initialization
console.warn('Failed to initialize OpenTelemetry provider:', error.message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's emit the log warnings using the PWAKitLogger to add a namespace.

E.g.

logger.error(cause, {namespace: 'react-rendering.render'})

await provider.shutdown()
provider = null // Clean up after successful shutdown
} catch (error) {
console.warn('Failed to shutdown OpenTelemetry provider:', error.message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, let's use logger instead of directly using console.

// Initialize the tracer provider
provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: SERVICE_NAME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you look into the deprecation warnings?

It looks like SemanticResourceAttributes is deprecated an we should instead SEMRESATTRS_SERVICE_NAME

export const initializeServerTracing = (options = {}) => {
const {
serviceName = process.env.OTEL_SERVICE_NAME || DEFAULT_SERVICE_NAME,
serviceVersion = process.env.npm_package_version,
Copy link
Contributor

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_version will always be undefined, let’s remove it for now. We can explore a better way to include it later if needed.

Suggested change
serviceVersion = process.env.npm_package_version,

@larnelleankunda larnelleankunda merged commit cb22899 into feature/opentelemetry Jul 1, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants