-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Telemetry: Fix preview-first-load event #32859
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
Conversation
📝 WalkthroughWalkthroughExtracted payload construction into exported makePayload; added telemetry types ( Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Client
participant PreviewChannel as PreviewChannel
participant EventCache as EventCache
participant makePayload as makePayload
participant Telemetry as telemetry()
Client->>PreviewChannel: PREVIEW_INITIALIZED (userAgent, sessionId)
PreviewChannel->>EventCache: getLastEvents()
EventCache-->>PreviewChannel: lastInit (CacheEntry | undefined)
PreviewChannel->>makePayload: makePayload(userAgent, lastInit, sessionId)
makePayload-->>PreviewChannel: payload { userAgent, isNewUser, timeSinceInit? }
PreviewChannel->>Telemetry: telemetry("preview-first-load", payload)
Telemetry-->>PreviewChannel: ack
PreviewChannel-->>Client: done
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (5)
🚧 Files skipped from review as they are similar to previous changes (2)
🧰 Additional context used📓 Path-based instructions (7)code/**/*.{test,spec}.{ts,tsx}📄 CodeRabbit inference engine (.cursorrules)
Files:
**/*.test.{ts,tsx,js,jsx}📄 CodeRabbit inference engine (.cursor/rules/spy-mocking.mdc)
Files:
**/*.{js,jsx,json,html,ts,tsx,mjs}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
**/*.@(test|spec).{ts,tsx,js,jsx}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
**/*.{ts,tsx,js,jsx,mjs}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
code/**/*.{ts,tsx,js,jsx,mjs}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
{code/**,scripts/**}/**/*.{ts,tsx,js,jsx,mjs}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
🧠 Learnings (2)📚 Learning: 2025-10-13T13:33:14.659ZApplied to files:
📚 Learning: 2025-09-17T08:11:47.196ZApplied to files:
🧬 Code graph analysis (2)code/core/src/core-server/server-channel/preview-initialized-channel.test.ts (1)
code/core/src/telemetry/event-cache.ts (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
🔇 Additional comments (8)
Comment |
|
Failed to publish canary version of this pull request, triggered by @shilman. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/18868318743 |
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.
Actionable comments posted: 2
🧹 Nitpick comments (3)
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts (1)
16-35: Consider improving type safety in test mocks.The test uses
as anycasting (line 28) because the mocklastInitobject doesn't fully conform to theCacheEntrytype. While this is functional, you could improve type safety by creating a test helper that constructs partialCacheEntrymocks with proper typing.Example approach:
type PartialCacheEntry = Partial<CacheEntry> & { body?: Partial<TelemetryData>; }; const createMockCacheEntry = (partial: PartialCacheEntry): CacheEntry => ({ timestamp: partial.timestamp ?? Date.now(), body: { eventId: 'test-id', sessionId: partial.body?.sessionId ?? 'test-session', eventType: 'init', payload: partial.body?.payload ?? {}, context: {}, ...partial.body, } as TelemetryData, });code/core/src/telemetry/types.ts (1)
92-94: Consider strengthening Context type definition.The
Contextinterface usesanyfor values, which reduces type safety. Consider restricting to specific types:export interface Context { - [key: string]: any; + [key: string]: string | number | boolean | undefined | null | Context; }This allows nested contexts while maintaining type safety for primitive values.
code/core/src/core-server/server-channel/preview-initialized-channel.ts (1)
9-25: Consider simplifying type annotation.The explicit type annotation on lines 14-18 is redundant since TypeScript can infer the type from the initialization. The logic correctly handles edge cases with optional chaining and defaults.
- const payload = { + const payload: { + userAgent: string; + isNewUser: boolean; + timeSinceInit: number | undefined; + } = { userAgent, isNewUser: false, timeSinceInit: undefined, - } as { - userAgent: string; - isNewUser: boolean; - timeSinceInit: number | undefined; - }; + };Or simply remove the annotation entirely:
const payload = { userAgent, isNewUser: false, - timeSinceInit: undefined, - } as { - userAgent: string; - isNewUser: boolean; - timeSinceInit: number | undefined, - }; + timeSinceInit: undefined as number | undefined, + };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts(1 hunks)code/core/src/core-server/server-channel/preview-initialized-channel.ts(2 hunks)code/core/src/telemetry/event-cache.ts(3 hunks)code/core/src/telemetry/types.ts(2 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
**/*.{js,jsx,json,html,ts,tsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.{js,jsx,json,html,ts,tsx,mjs}: Run Prettier formatting on changed files before committing
Run ESLint on changed files and fix all errors/warnings before committing (useyarn lint:js:cmd <file>)
Files:
code/core/src/telemetry/event-cache.tscode/core/src/core-server/server-channel/preview-initialized-channel.tscode/core/src/core-server/server-channel/preview-initialized-channel.test.tscode/core/src/telemetry/types.ts
**/*.{ts,tsx,js,jsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Export functions from modules when they need to be unit-tested
Files:
code/core/src/telemetry/event-cache.tscode/core/src/core-server/server-channel/preview-initialized-channel.tscode/core/src/core-server/server-channel/preview-initialized-channel.test.tscode/core/src/telemetry/types.ts
code/**/*.{ts,tsx,js,jsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
In application code, use Storybook loggers instead of
console.*(client code:storybook/internal/client-logger; server code:storybook/internal/node-logger)
Files:
code/core/src/telemetry/event-cache.tscode/core/src/core-server/server-channel/preview-initialized-channel.tscode/core/src/core-server/server-channel/preview-initialized-channel.test.tscode/core/src/telemetry/types.ts
{code/**,scripts/**}/**/*.{ts,tsx,js,jsx,mjs}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Do not use
console.log,console.warn, orconsole.errordirectly unless in isolated files where importing loggers would significantly increase bundle size
Files:
code/core/src/telemetry/event-cache.tscode/core/src/core-server/server-channel/preview-initialized-channel.tscode/core/src/core-server/server-channel/preview-initialized-channel.test.tscode/core/src/telemetry/types.ts
code/**/*.{test,spec}.{ts,tsx}
📄 CodeRabbit inference engine (.cursorrules)
code/**/*.{test,spec}.{ts,tsx}: Place all test files under the code/ directory
Name test files as *.test.ts, *.test.tsx, *.spec.ts, or *.spec.tsx
Files:
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts
**/*.test.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/spy-mocking.mdc)
**/*.test.{ts,tsx,js,jsx}: Use vi.mock() with the spy: true option for all package and file mocks in Vitest tests
Place all mocks at the top of the test file before any test cases
Use vi.mocked() to type and access mocked functions
Implement mock behaviors in beforeEach blocks
Mock all required dependencies that the test subject uses
Mock implementations should be placed in beforeEach blocks
Each mock implementation should return a Promise for async functions
Mock implementations should match the expected return type of the original function
Use vi.mocked() to access and implement mock behaviors
Mock all required properties and methods that the test subject uses
Avoid direct function mocking without vi.mocked()
Avoid mock implementations outside of beforeEach blocks
Avoid mocking without the spy: true option
Avoid inline mock implementations within test cases
Avoid mocking only a subset of required dependencies
Mock at the highest level of abstraction needed
Keep mock implementations simple and focused
Use type-safe mocking with vi.mocked()
Document complex mock behaviors
Group related mocks together
Files:
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts
**/*.@(test|spec).{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.@(test|spec).{ts,tsx,js,jsx}: Unit tests should import and execute the functions under test rather than only asserting on syntax patterns
Mock external dependencies in tests usingvi.mock()(e.g., filesystem, loggers)
Files:
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts
🧬 Code graph analysis (3)
code/core/src/telemetry/event-cache.ts (1)
code/core/src/telemetry/types.ts (2)
TelemetryData(105-112)EventType(7-36)
code/core/src/core-server/server-channel/preview-initialized-channel.ts (2)
code/core/src/telemetry/event-cache.ts (1)
CacheEntry(12-15)code/core/src/telemetry/types.ts (1)
InitPayload(114-120)
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts (1)
code/core/src/core-server/server-channel/preview-initialized-channel.ts (1)
makePayload(9-25)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Core Unit Tests, windows-latest
🔇 Additional comments (8)
code/core/src/telemetry/event-cache.ts (3)
3-3: LGTM! Type safety improvements are well-structured.The addition of
TelemetryDataimport and the new exportedCacheEntryinterface properly type the cached event structure, replacing previousanytypes.Also applies to: 12-15
19-19: LGTM! Parameter type strengthened.Replacing
anywithTelemetryDataimproves type safety and aligns with theCacheEntryinterface.
31-31: LGTM! Return types properly updated.The return type updates for
get,getLastEvents, and theupgradeFieldsparameter type all correctly use the newCacheEntryinterface, ensuring consistency and type safety.Also applies to: 36-36, 40-40
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts (1)
58-70: LGTM! Edge case properly tested.This test correctly validates the behavior when no init session exists, ensuring
isNewUserdefaults tofalseandtimeSinceInitremainsundefined.code/core/src/telemetry/types.ts (2)
105-112: LGTM! TelemetryData properly extended.The additions of
eventId,sessionId, andcontextfields appropriately extend the telemetry data structure to support richer event tracking, aligning with the PR objectives.
114-120: LGTM! InitPayload interface is well-structured.The interface properly types the initialization payload with appropriate required and optional fields, supporting the telemetry changes for tracking new users.
code/core/src/core-server/server-channel/preview-initialized-channel.ts (2)
3-3: LGTM! Imports properly updated.The addition of
InitPayloadandCacheEntrytypes, along withgetLastEventsfunction, supports the refactored payload construction logic.Also applies to: 6-6
40-41: LGTM! Clean refactoring to use makePayload helper.Extracting payload construction into the
makePayloadfunction improves testability and separation of concerns. The handler now cleanly orchestrates the telemetry call.
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts
Outdated
Show resolved
Hide resolved
code/core/src/core-server/server-channel/preview-initialized-channel.test.ts
Show resolved
Hide resolved
fc4134c to
c0922e3
Compare
|
View your CI Pipeline Execution ↗ for commit 0ff3fd8
☁️ Nx Cloud last updated this comment at |
c89961d to
d598433
Compare
d598433 to
ac21b0a
Compare
Telemetry: Fix preview-first-load event (cherry picked from commit 239794e)
Closes N/A
What I did
Follow-up to #32770
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
See #32770
🦋 Canary release
This pull request has been released as version
0.0.0-pr-32859-sha-0ff3fd82. Try it out in a new sandbox by runningnpx [email protected] sandboxor in an existing project withnpx [email protected] upgrade.More information
0.0.0-pr-32859-sha-0ff3fd82shilman/first-load-new-user0ff3fd821761641532)To request a new release of this pull request, mention the
@storybookjs/coreteam.core team members can create a new canary release here or locally with
gh workflow run --repo storybookjs/storybook publish.yml --field pr=32859Summary by CodeRabbit
Tests
Refactor