Skip to content

Sdk telemetry header - #114

Draft
cb-karthikp wants to merge 8 commits into
v4from
sdk-telemetry-header
Draft

Sdk telemetry header#114
cb-karthikp wants to merge 8 commits into
v4from
sdk-telemetry-header

Conversation

@cb-karthikp

@cb-karthikp cb-karthikp commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

TBA

Added opt-out SDK telemetry with per-client state and RFC 9651 Prefer headers. Added telemetry emission for sync and async requests, including status, errors, request IDs, timing, and feature tokens. Separated SDK telemetry from customer adapters. Added documentation and comprehensive tests.

@snyk-io

snyk-io Bot commented Aug 10, 2026

Copy link
Copy Markdown

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

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues
Secrets 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The SDK now emits anonymous telemetry headers by default. It stores the last completed call per client, supports opt-out through the builder, serializes telemetry using structured fields, and keeps customer telemetry adapters independent.

Changes

SDK telemetry

Layer / File(s) Summary
Telemetry contracts and client configuration
src/main/java/com/chargebee/v4/client/ChargebeeClient.java, src/main/java/com/chargebee/v4/telemetry/SdkTelemetrySnapshot.java, src/main/java/com/chargebee/v4/telemetry/SdkTelemetryState.java, src/main/java/com/chargebee/v4/telemetry/SdkTelemetryHeader.java, README.md
The client stores per-client telemetry state and exposes SDK telemetry configuration. Telemetry snapshots are immutable and feature tokens are defensively copied.
Structured telemetry header encoding
src/main/java/com/chargebee/v4/telemetry/SdkTelemetryHeaderBuilder.java, src/test/java/com/chargebee/v4/telemetry/SdkTelemetryHeaderBuilderTest.java
Telemetry snapshots become validated RFC 9651 structured-field headers. String escaping, token filtering, and UTF-8 size limits are covered by tests.
SDK telemetry emission flow
src/main/java/com/chargebee/v4/telemetry/SdkTelemetryEmitter.java, src/test/java/com/chargebee/v4/telemetry/SdkTelemetryEmitterTest.java
Synchronous and asynchronous calls attach the previous completed snapshot and record success or failure details. Missing metadata and telemetry failures do not alter API behavior.
Customer telemetry adapter composition
src/main/java/com/chargebee/v4/telemetry/TelemetryExecutor.java, src/main/java/com/chargebee/v4/telemetry/TelemetryAdapterExecutor.java, src/main/java/com/chargebee/v4/telemetry/TelemetrySupport.java, src/test/java/com/chargebee/v4/telemetry/TelemetryExecutorTest.java
Customer adapter execution moves into TelemetryAdapterExecutor. TelemetryExecutor composes both telemetry layers, and adapter failure logging tests target the new executor.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client as ChargebeeClient
  participant Executor as TelemetryExecutor
  participant Emitter as SdkTelemetryEmitter
  participant Transport as API transport
  Client->>Executor: execute request
  Executor->>Emitter: emit SDK telemetry
  Emitter->>Transport: send request with prior-call header
  Transport-->>Emitter: response or API error
  Emitter->>Client: record completed snapshot
Loading

Possibly related PRs

🚥 Pre-merge checks | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 58.02% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/com/chargebee/v4/telemetry/SdkTelemetryHeaderBuilder.java`:
- Around line 110-141: Update appendBareParam to use the value unquoted only
when isSfToken returns true; otherwise serialize it with escapeSfString. Ensure
values containing characters outside the RFC 9651 sf-string ASCII range are
rejected, including validation in the path also covered by the related call
site, and update the test expectation so versions such as 4.14.0 are emitted as
escaped sf-strings.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 369497c2-82fe-419c-9cdc-afdea5fc43ea

📥 Commits

Reviewing files that changed from the base of the PR and between 26d271e and 1a0b26c.

📒 Files selected for processing (13)
  • README.md
  • src/main/java/com/chargebee/v4/client/ChargebeeClient.java
  • src/main/java/com/chargebee/v4/telemetry/SdkTelemetryEmitter.java
  • src/main/java/com/chargebee/v4/telemetry/SdkTelemetryHeader.java
  • src/main/java/com/chargebee/v4/telemetry/SdkTelemetryHeaderBuilder.java
  • src/main/java/com/chargebee/v4/telemetry/SdkTelemetrySnapshot.java
  • src/main/java/com/chargebee/v4/telemetry/SdkTelemetryState.java
  • src/main/java/com/chargebee/v4/telemetry/TelemetryAdapterExecutor.java
  • src/main/java/com/chargebee/v4/telemetry/TelemetryExecutor.java
  • src/main/java/com/chargebee/v4/telemetry/TelemetrySupport.java
  • src/test/java/com/chargebee/v4/telemetry/SdkTelemetryEmitterTest.java
  • src/test/java/com/chargebee/v4/telemetry/SdkTelemetryHeaderBuilderTest.java
  • src/test/java/com/chargebee/v4/telemetry/TelemetryExecutorTest.java

Comment on lines +110 to +141
/** Emits a bare {@code key=value}, or a quoted sf-string when the value needs escaping. */
private static boolean appendBareParam(StringBuilder segment, String key, String value) {
if (!isNotBlank(value)) {
return false;
}
if (containsInvalidSfStringChar(value)) {
return false;
}
String trimmed = value.trim();
String serialized = isBareSafe(trimmed) ? trimmed : escapeSfString(trimmed);
if (serialized == null) {
return false;
}
segment.append(';').append(key).append('=').append(serialized);
return true;
}

/** Whether {@code value} can be emitted unquoted without corrupting the sf-list. */
private static boolean isBareSafe(String value) {
for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
if (ch == '"'
|| ch == '\\'
|| ch == ','
|| ch == ';'
|| ch == '='
|| Character.isWhitespace(ch)) {
return false;
}
}
return !value.isEmpty();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Serialize only valid structured-field values.

appendBareParam emits version=4.14.0 for normal SDK versions. This is not a valid sf-token or sf-decimal. The telemetry server can reject the complete header.

Use an sf-token only when isSfToken returns true. Otherwise, emit an escaped sf-string. Reject all characters outside the RFC 9651 sf-string ASCII range. Update the test that expects the unquoted version.

Proposed fix
-    String serialized = isBareSafe(trimmed) ? trimmed : escapeSfString(trimmed);
+    String serialized = isSfToken(trimmed) ? trimmed : escapeSfString(trimmed);
-      if (ch == '\0' || ch == '\n' || ch == '\r') {
+      if (ch < 0x20 || ch > 0x7E) {
         return true;
       }

Also applies to: 186-195

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/com/chargebee/v4/telemetry/SdkTelemetryHeaderBuilder.java`
around lines 110 - 141, Update appendBareParam to use the value unquoted only
when isSfToken returns true; otherwise serialize it with escapeSfString. Ensure
values containing characters outside the RFC 9651 sf-string ASCII range are
rejected, including validation in the path also covered by the related call
site, and update the test expectation so versions such as 4.14.0 are emitted as
escaped sf-strings.

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.

1 participant