Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions e2e/nextjs/src/app/components/highlight-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ export function HighlightButtons() {
>
Track
</Button>
<Button
onClick={() => {
H.recordMetric({name: 'my-metric', value: Math.random()})
}}
>
H.recordMetric
</Button>
<Button
onClick={() => {
H.start()
}}
>
Start
</Button>
<Button
onClick={() => {
H.stop()
}}
>
Stop
</Button>
</div>
)
}
11 changes: 8 additions & 3 deletions sdk/highlight-run/src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ import {
NetworkPerformancePayload,
} from './listeners/network-listener/performance-listener'
import { Logger } from './logger'
import { BROWSER_METER_NAME, getTracer, setupBrowserTracing } from './otel'
import {
BROWSER_METER_NAME,
getTracer,
setupBrowserTracing,
shutdown,
} from './otel'
import {
HighlightIframeMessage,
HighlightIframeReponse,
Expand Down Expand Up @@ -119,8 +124,7 @@ import {
UpDownCounter,
} from '@opentelemetry/api'
import { IntegrationClient } from '../integrations'
import { LaunchDarklyIntegration } from '../integrations/launchdarkly'
import { LDClient } from '../integrations/launchdarkly'
import { LaunchDarklyIntegration, LDClient } from '../integrations/launchdarkly'
import { createLog, defaultLogOptions } from './listeners/console-listener'
import { CustomSampler } from './otel/sampling/CustomSampler'
import randomUuidV4 from './utils/randomUuidV4'
Expand Down Expand Up @@ -1367,6 +1371,7 @@ SessionSecureID: ${this.sessionData.sessionSecureID}`,
// stop all other event listeners, to be restarted on initialize()
this.listeners.forEach((stop) => stop())
this.listeners = []
void shutdown()
}

getCurrentSessionTimestamp() {
Expand Down
2 changes: 2 additions & 0 deletions sdk/highlight-run/src/client/otel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,14 @@ export const shutdown = async () => {
if (providers.tracerProvider) {
await providers.tracerProvider.forceFlush()
await providers.tracerProvider.shutdown()
providers.tracerProvider = undefined
} else {
console.warn('OTEL shutdown called without initialized tracerProvider.')
}
if (providers.meterProvider) {
await providers.meterProvider.forceFlush()
await providers.meterProvider.shutdown()
providers.meterProvider = undefined
Copy link

Choose a reason for hiding this comment

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

Bug: Race Condition in Async Shutdown Causes Provider Detection Issues

The void shutdown() call is fire-and-forget, leading to a race condition. Since OTEL providers are cleared after the async shutdown completes, a quick re-initialization can incorrectly detect an active provider, skip setup, and leave the system in a broken state. This also causes unhandled promise rejections if shutdown operations fail.

Additional Locations (1)

Fix in Cursor Fix in Web

} else {
console.warn('OTEL shutdown called without initialized meterProvider.')
}
Expand Down
Loading