Skip to content

feat(posthog): Added SDK data to header in requests, for Posthog filtering#44

Merged
Murike merged 9 commits intomainfrom
feature/posthog-monitoring
Mar 2, 2026
Merged

feat(posthog): Added SDK data to header in requests, for Posthog filtering#44
Murike merged 9 commits intomainfrom
feature/posthog-monitoring

Conversation

@Murike
Copy link
Contributor

@Murike Murike commented Feb 27, 2026

User description

Make sure TypeScript SDK usage is being captured on PostHog - [sc-54702]

Description

  • Added custom header to track SDK version on Posthog for requests made through galileo-generated.

Obs.: Ticket [sc-56960] was created to enable persistent edits on Speakeasy's configuration, that will allow customizing tsconfig.json, which is necessary to use import instead of require on this hook.


Generated description

Below is a concise technical summary of the changes proposed in this PR:

graph LR
SDKIdentifierHook_beforeRequest_("SDKIdentifierHook.beforeRequest"):::added
loadVersion_("loadVersion"):::added
getSdkIdentifier_("getSdkIdentifier"):::added
GALILEO_GENERATED_("GALILEO_GENERATED"):::added
initHooks_("initHooks"):::modified
SDKIdentifierHook_("SDKIdentifierHook"):::added
SDKIdentifierHook_beforeRequest_ -- "Fetches cached package version to set X-Galileo-SDK." --> loadVersion_
SDKIdentifierHook_beforeRequest_ -- "Builds 'galileo-generated/<version>' header value." --> getSdkIdentifier_
loadVersion_ -- "Reads galileo-generated/package.json version into cache." --> GALILEO_GENERATED_
initHooks_ -- "Registers SDKIdentifierHook to add SDK header before requests." --> SDKIdentifierHook_
classDef added stroke:#15AA7A
classDef removed stroke:#CD5270
classDef modified stroke:#EDAC4C
linkStyle default stroke:#CBD5E1,font-size:13px
Loading

Implements a new SDKIdentifierHook to inject the X-Galileo-SDK header into outgoing requests, enabling version tracking for PostHog filtering. Registers this hook within the global hook initialization flow to ensure all generated requests include the SDK version metadata.

TopicDetails
Testing Adds comprehensive unit tests for SDKIdentifierHook to verify header injection, version formatting, and error handling.
Modified files (1)
  • src/tests/hooks/sdk-identifier.test.ts
Latest Contributors(0)
UserCommitDate
SDK Identification Introduces the SDKIdentifierHook class and registers it in initHooks to append SDK version information to request headers.
Modified files (2)
  • src/hooks/registration.ts
  • src/hooks/sdk-identifier.ts
Latest Contributors(2)
UserCommitDate
Murikefeat-certificate-Suppo...February 27, 2026
ricardo.maurique@galil...chore-setup-Improved-m...January 28, 2026
This pull request is reviewed by Baz. Review like a pro on (Baz).

@Murike Murike requested a review from a team as a code owner February 27, 2026 20:15
Copy link

@fernandocorreia-galileo fernandocorreia-galileo left a comment

Choose a reason for hiding this comment

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

Approved but please look at the suggestions, if necessary create follow-up tickets.

): Promise<Request> {
const version = loadVersion();
const sdkIdentifier = getSdkIdentifier(version);
request.headers.set("X-Galileo-SDK", sdkIdentifier);

Choose a reason for hiding this comment

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

  • [Minor]: The SDKIdentifierHook mutates the incoming Request object's headers in place instead of cloning it first.
    • File: src/hooks/sdk-identifier.ts (lines 36-37)
    • Contrast with TokenManagementHook.beforeRequest(), which does const newRequest = request.clone() before mutating. In most practical cases the Fetch Request headers object is mutable in Node.js (undici), so this works. However, in certain environments or with certain Request implementations the headers can be immutable (e.g. a Request created with mode: 'cors' in some browser implementations). Cloning first is also the safer contract: it avoids mutating the caller's object, which could have surprising side effects if the same Request instance is reused or inspected after the hook runs.
    • Suggestion: Mirror the pattern used in token-management.ts:
      const newRequest = request.clone();
      newRequest.headers.set("X-Galileo-SDK", sdkIdentifier);
      return newRequest;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted accordingly.

Choose a reason for hiding this comment

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

  • [Minor]: No test for the loadVersion() fallback path ("unknown").
    • File: src/tests/hooks/sdk-identifier.test.ts
    • There is no test that exercises the catch branch of loadVersion() (e.g., by mocking require to throw). This means the graceful-degradation path is untested. Particularly relevant given that this path is triggered by the ESM runtime issue described above.
    • Suggestion: Add a test using vi.mock to simulate a require failure and assert that the header value becomes galileo-generated/unknown rather than throwing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added missing test.

@Murike Murike force-pushed the feature/posthog-monitoring branch from 1620f2b to 50a8c51 Compare March 2, 2026 16:33
@Murike Murike merged commit 6211709 into main Mar 2, 2026
7 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.

2 participants