-
Notifications
You must be signed in to change notification settings - Fork 28
Chargebee telemetry handling #109
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
Draft
cb-karthikp
wants to merge
8
commits into
v4
Choose a base branch
from
feat/chargebee-telemetry
base: v4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2742928
Chargebee telemetry handling
cb-karthikp 81a8a4e
Update BaseResponse.java
cb-karthikp 272eb27
add testcase
cb-karthikp e100e46
add preferChargebeeTelemetry flag
cb-karthikp a1ab9cb
Merge branch 'v4' into feat/chargebee-telemetry
cb-karthikp ae99e67
add telemetry perference
cb-karthikp 5fdb8c8
Update BaseResponseTest.java
cb-karthikp 969c9d3
Update README.md
cb-karthikp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
src/main/java/com/chargebee/v4/telemetry/ChargebeeTelemetryHeaderParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| /* | ||
| * Copyright 2026 Chargebee Inc. | ||
| */ | ||
|
|
||
| package com.chargebee.v4.telemetry; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** Parses the {@code X-Chargebee-Telemetry} response header into OpenTelemetry span attributes. */ | ||
| public final class ChargebeeTelemetryHeaderParser { | ||
|
|
||
| static final String SF_DATE_PREFIX = "@"; | ||
| static final String SF_BOOLEAN_TRUE = "?1"; | ||
| static final String SF_BOOLEAN_FALSE = "?0"; | ||
|
|
||
| private static final Pattern INTEGER_PATTERN = Pattern.compile("-?\\d+"); | ||
| private static final Pattern DECIMAL_PATTERN = Pattern.compile("-?\\d+\\.\\d+"); | ||
|
|
||
| private ChargebeeTelemetryHeaderParser() { | ||
| // utility class | ||
| } | ||
|
|
||
| /** | ||
| * Parses a raw {@code X-Chargebee-Telemetry} header value into typed span attributes. | ||
| * | ||
| * @param headerValue raw header string | ||
| * @return parsed attributes, or empty map when input is null/blank or structurally invalid | ||
| */ | ||
| public static Map<String, Object> parseToSpanAttributes(String headerValue) { | ||
| if (headerValue == null || headerValue.trim().isEmpty()) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| try { | ||
| Map<String, Object> attributes = new HashMap<>(); | ||
| List<String> features = new ArrayList<>(); | ||
|
|
||
| for (String item : splitListItems(headerValue)) { | ||
| parseListItem(item, attributes, features); | ||
| } | ||
|
|
||
| if (!features.isEmpty()) { | ||
| attributes.put(TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_FEATURES, features); | ||
| } | ||
|
|
||
| return attributes.isEmpty() ? Collections.emptyMap() : attributes; | ||
| } catch (RuntimeException ex) { | ||
| return Collections.emptyMap(); | ||
| } | ||
| } | ||
|
|
||
| private static void parseListItem( | ||
| String item, Map<String, Object> attributes, List<String> features) { | ||
| String trimmed = item.trim(); | ||
| if (trimmed.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| int separator = indexOfParameterSeparator(trimmed); | ||
| String token = separator < 0 ? trimmed : trimmed.substring(0, separator).trim(); | ||
| if (token.isEmpty()) { | ||
| throw new IllegalArgumentException("missing sf-item token"); | ||
| } | ||
|
|
||
| if (token.startsWith(TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_FT_PREFIX)) { | ||
| features.add(token.substring(TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_FT_PREFIX.length())); | ||
| return; | ||
| } | ||
|
|
||
| String attributePrefix = segmentAttributePrefix(token); | ||
| if (separator >= 0) { | ||
| parseParameters(trimmed.substring(separator + 1), attributePrefix, attributes); | ||
| } | ||
| } | ||
|
|
||
| private static String segmentAttributePrefix(String token) { | ||
| if (TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_CB_SEGMENT.equals(token)) { | ||
| return TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_CB_PREFIX; | ||
| } | ||
| if (token.startsWith(TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_TP_PREFIX)) { | ||
| return TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_TP_ATTRIBUTE_PREFIX | ||
| + token.substring(TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_TP_PREFIX.length()) | ||
| + "."; | ||
| } | ||
| return TelemetryAttributeKeys.CHARGEBEE_TELEMETRY_PREFIX + token + "."; | ||
| } | ||
|
|
||
| private static void parseParameters( | ||
| String parametersSection, String attributePrefix, Map<String, Object> attributes) { | ||
| for (String parameter : splitParameters(parametersSection)) { | ||
| parseParameter(parameter, attributePrefix, attributes); | ||
| } | ||
| } | ||
|
|
||
| private static void parseParameter( | ||
| String parameter, String attributePrefix, Map<String, Object> attributes) { | ||
| String trimmed = parameter.trim(); | ||
| if (trimmed.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| int equalsIndex = indexOfEquals(trimmed); | ||
| if (equalsIndex <= 0) { | ||
| throw new IllegalArgumentException("invalid parameter: " + trimmed); | ||
| } | ||
|
|
||
| String key = trimmed.substring(0, equalsIndex).trim(); | ||
| String rawValue = trimmed.substring(equalsIndex + 1).trim(); | ||
| if (key.isEmpty()) { | ||
| throw new IllegalArgumentException("missing parameter key"); | ||
| } | ||
|
|
||
| attributes.put(attributePrefix + key, parseScalarValue(rawValue)); | ||
| } | ||
|
|
||
| static Object parseScalarValue(String rawValue) { | ||
| if (rawValue == null || rawValue.isEmpty()) { | ||
| throw new IllegalArgumentException("missing scalar value"); | ||
| } | ||
|
|
||
| if (rawValue.startsWith(SF_DATE_PREFIX)) { | ||
| return Long.parseLong(rawValue.substring(SF_DATE_PREFIX.length())); | ||
| } | ||
| if (SF_BOOLEAN_TRUE.equals(rawValue)) { | ||
| return Boolean.TRUE; | ||
| } | ||
| if (SF_BOOLEAN_FALSE.equals(rawValue)) { | ||
| return Boolean.FALSE; | ||
| } | ||
| if (rawValue.startsWith(":") && rawValue.endsWith(":") && rawValue.length() >= 2) { | ||
| return rawValue.substring(1, rawValue.length() - 1); | ||
| } | ||
| if (rawValue.startsWith("\"")) { | ||
| return parseStringValue(rawValue); | ||
| } | ||
| if (INTEGER_PATTERN.matcher(rawValue).matches()) { | ||
| return Long.parseLong(rawValue); | ||
| } | ||
| if (DECIMAL_PATTERN.matcher(rawValue).matches()) { | ||
| return Double.parseDouble(rawValue); | ||
| } | ||
| return rawValue; | ||
| } | ||
|
|
||
| private static String parseStringValue(String rawValue) { | ||
| if (rawValue.length() < 2 || rawValue.charAt(rawValue.length() - 1) != '"') { | ||
| throw new IllegalArgumentException("invalid sf-string value"); | ||
| } | ||
|
|
||
| StringBuilder decoded = new StringBuilder(); | ||
| for (int i = 1; i < rawValue.length() - 1; i++) { | ||
| char current = rawValue.charAt(i); | ||
| if (current == '\\') { | ||
| if (i + 1 >= rawValue.length() - 1) { | ||
| throw new IllegalArgumentException("invalid sf-string escape"); | ||
| } | ||
| decoded.append(rawValue.charAt(++i)); | ||
| } else { | ||
| decoded.append(current); | ||
| } | ||
| } | ||
| return decoded.toString(); | ||
| } | ||
|
|
||
| private static List<String> splitListItems(String input) { | ||
| return splitOnDelimiter(input, ','); | ||
| } | ||
|
|
||
| private static List<String> splitParameters(String input) { | ||
| return splitOnDelimiter(input, ';'); | ||
| } | ||
|
|
||
| private static List<String> splitOnDelimiter(String input, char delimiter) { | ||
| List<String> parts = new ArrayList<>(); | ||
| StringBuilder current = new StringBuilder(); | ||
| boolean inQuotes = false; | ||
|
|
||
| for (int i = 0; i < input.length(); i++) { | ||
| char currentChar = input.charAt(i); | ||
| if (currentChar == '"') { | ||
| if (!isEscapedQuote(input, i)) { | ||
| inQuotes = !inQuotes; | ||
| } | ||
| current.append(currentChar); | ||
| } else if (currentChar == delimiter && !inQuotes) { | ||
| addIfNotBlank(parts, current); | ||
| current = new StringBuilder(); | ||
| } else { | ||
| current.append(currentChar); | ||
| } | ||
| } | ||
|
|
||
| addIfNotBlank(parts, current); | ||
| return parts; | ||
| } | ||
|
|
||
| private static int indexOfParameterSeparator(String item) { | ||
| boolean inQuotes = false; | ||
| for (int i = 0; i < item.length(); i++) { | ||
| char current = item.charAt(i); | ||
| if (current == '"') { | ||
| if (!isEscapedQuote(item, i)) { | ||
| inQuotes = !inQuotes; | ||
| } | ||
| } else if (current == ';' && !inQuotes) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| private static int indexOfEquals(String parameter) { | ||
| boolean inQuotes = false; | ||
| for (int i = 0; i < parameter.length(); i++) { | ||
| char current = parameter.charAt(i); | ||
| if (current == '"') { | ||
| if (!isEscapedQuote(parameter, i)) { | ||
| inQuotes = !inQuotes; | ||
| } | ||
| } else if (current == '=' && !inQuotes) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /** Returns true when {@code input[index]} is a quote escaped by a preceding backslash. */ | ||
| private static boolean isEscapedQuote(String input, int index) { | ||
| int backslashes = 0; | ||
| for (int i = index - 1; i >= 0 && input.charAt(i) == '\\'; i--) { | ||
| backslashes++; | ||
| } | ||
| return backslashes % 2 == 1; | ||
| } | ||
|
|
||
| private static void addIfNotBlank(List<String> parts, StringBuilder current) { | ||
| if (current.length() == 0) { | ||
| return; | ||
| } | ||
| String value = current.toString().trim(); | ||
| if (!value.isEmpty()) { | ||
| parts.add(value); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.