feat: add opt-in system attributes to publish payload#54
Conversation
Add includeSystemAttributes context option that, when enabled, automatically includes sdk_name, sdk_version, application, environment, and app_version (if > 0) as attributes in every publish payload.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughThis PR adds an exported Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
src/version.ts (1)
1-1: Consider automating version synchronisation withpackage.json.Manually maintaining the SDK version in this file creates a risk of drift from
package.json. Consider importing the version frompackage.jsonat build time or using a build script to inject it, ensuring consistency.That said, the current approach is functional and acceptable for now.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/version.ts` at line 1, The SDK_VERSION constant is manually maintained and can drift from package.json; change the implementation so SDK_VERSION is sourced from package.json at build/runtime (or injected by a build script) instead of a hardcoded string: replace the current SDK_VERSION export with logic that reads the "version" field from package.json (or uses an injected env var during build) and export that value so SDK_VERSION always matches package.json’s version.src/__tests__/context.test.js (1)
480-484: Remove unusedsystemAttributesconstant.This constant is defined but never used in the test file. The tests directly check individual attributes inline instead. Consider removing this dead code to avoid confusion.
🧹 Suggested fix
client.getAgent.mockReturnValue("absmartly-javascript-sdk"); client.getApplication.mockReturnValue({ name: "website", version: 0 }); client.getEnvironment.mockReturnValue("production"); - -const systemAttributes = [ - { name: "sdk_version", value: expect.any(String), setAt: expect.any(Number) }, - { name: "application", value: "website", setAt: expect.any(Number) }, - { name: "environment", value: "production", setAt: expect.any(Number) }, -];🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/__tests__/context.test.js` around lines 480 - 484, Remove the dead variable declaration systemAttributes from the test file (the const named systemAttributes in src/__tests__/context.test.js) since it is never referenced; simply delete the entire const systemAttributes = [...] block to clean up unused code and keep the tests focused on the inline expectations that already assert those attributes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@README.md`:
- Around line 140-141: Update the phrasing in the "Including system attributes"
paragraph: replace "You can opt-in to automatically include system
attributes..." with a grammatically correct alternative such as "You can opt in
to automatically include system attributes..." or "You can choose to
automatically include system attributes (SDK name, SDK version, application,
environment, and application version) in every publish payload." Ensure the new
sentence preserves the same meaning and mentions that these attributes are sent
as context attributes useful for debugging and filtering in the Web Console.
In `@src/__tests__/context.test.js`:
- Around line 4030-4055: The test mutates the shared mock client.getApplication
and only resets it inside the async callback, risking mock leakage; change the
test to use client.getApplication.mockReturnValueOnce({ name: "website",
version: 3 }) when constructing the Context (or move the reset into a
centralized beforeEach/afterEach), so the override is scoped to this single run
and won't affect other tests — update the call site in the "should include
app_version when application version is set" test that currently calls
client.getApplication.mockReturnValue and remove the inline reset at the end.
---
Nitpick comments:
In `@src/__tests__/context.test.js`:
- Around line 480-484: Remove the dead variable declaration systemAttributes
from the test file (the const named systemAttributes in
src/__tests__/context.test.js) since it is never referenced; simply delete the
entire const systemAttributes = [...] block to clean up unused code and keep the
tests focused on the inline expectations that already assert those attributes.
In `@src/version.ts`:
- Line 1: The SDK_VERSION constant is manually maintained and can drift from
package.json; change the implementation so SDK_VERSION is sourced from
package.json at build/runtime (or injected by a build script) instead of a
hardcoded string: replace the current SDK_VERSION export with logic that reads
the "version" field from package.json (or uses an injected env var during build)
and export that value so SDK_VERSION always matches package.json’s version.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 516af9c1-916e-4f24-aecd-35f80b2a4462
📒 Files selected for processing (5)
README.mdsrc/__tests__/context.test.jssrc/client.tssrc/context.tssrc/version.ts
Add sdkVersion as a top-level field in the publish payload body, sent with every publish request regardless of includeSystemAttributes.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
README.md (1)
160-160: Consider tightening wording in Line 160 for readability.“prepended to the publish payload” reads a bit awkwardly; “prepended to the published payload” (or “publish request payload”) is clearer.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@README.md` at line 160, The sentence "When enabled, the following attributes are automatically prepended to the publish payload:" is awkward; update that line (the string "When enabled, the following attributes are automatically prepended to the publish payload:") to clearer wording such as "When enabled, the following attributes are automatically prepended to the published payload:" or "When enabled, the following attributes are automatically prepended to the publish request payload:" to improve readability.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@README.md`:
- Line 160: The sentence "When enabled, the following attributes are
automatically prepended to the publish payload:" is awkward; update that line
(the string "When enabled, the following attributes are automatically prepended
to the publish payload:") to clearer wording such as "When enabled, the
following attributes are automatically prepended to the published payload:" or
"When enabled, the following attributes are automatically prepended to the
publish request payload:" to improve readability.
- Fix "opt-in" grammar to "opt in" (verb form) - Clarify "publish request payload" wording - Remove unused systemAttributes test constant - Use mockReturnValueOnce to prevent mock leakage - Source SDK_VERSION from package.json instead of hardcoding
- Make sdkVersion required in PublishParams (matches actual usage) - Add explicit return types to Client getters with post-construction types - Extract _buildAttributes() private method from _flush() - Wrap request construction in try-catch to prevent silent data loss - Fix sdkVersion indentation alignment in test assertions - Add client tests: sdkVersion in publish body, getter methods - Strictly assert sdk_version attribute value equals SDK_VERSION - Remove unused getApplicationName/getApplicationVersion imports
- Replace package.json import in version.ts with build-time generation via scripts/generate-version.js to avoid bundling package.json - Support string | number for application version (semver support) - Update ClientOptions, utils, Client getters, and context system attributes to handle string versions - Add tests for semver string version in both client and context - Update README and docs with semver version examples
- Introduce NormalizedClientOptions where application is always an object after constructor validation/normalization - Remove 'as' casts from Client getters since internal type is correct - Remove getApplicationName/getApplicationVersion usage from client (application is guaranteed to be an object) - Replace 1==1 getAgent test with meaningful custom agent test
Summary
includeSystemAttributescontext option (opt-in, defaults tofalse)sdk_name,sdk_version,application,environment, andapp_version(if > 0) as attributes in every publish payloadgetAgent(),getApplication(),getEnvironment()getters toClientsrc/version.tsexportingSDK_VERSIONTest plan
includeSystemAttributes: trueapp_versionincluded when application version > 0app_versionexcluded when application version is 0Summary by CodeRabbit
New Features
Documentation