-
Notifications
You must be signed in to change notification settings - Fork 30
telemetry header handler #129
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
5
commits into
master
Choose a base branch
from
feat/chargebee-telemetry
base: master
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2221ed
telemetry header handler
cb-karthikp e0d8411
Default otel adapter getter method for runtime
cb-karthikp de9f197
add preferChargebeeTelemetry opt-in flag
cb-karthikp 9edf8d5
update type and tests
cb-karthikp dd49bc4
Prettier
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
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,244 @@ | ||
| /* | ||
| * This file is auto-generated by Chargebee. | ||
| * For more information on how to make changes to this file, please see the README. | ||
| * Reach out to dx@chargebee.com for any questions. | ||
| * Copyright 2026 Chargebee Inc. | ||
| */ | ||
|
|
||
| const CHARGEBEE_TELEMETRY_PREFIX = 'chargebee.telemetry.'; | ||
| const CHARGEBEE_TELEMETRY_CB_SEGMENT = 'cb'; | ||
| const CHARGEBEE_TELEMETRY_CB_PREFIX = `${CHARGEBEE_TELEMETRY_PREFIX}cb.`; | ||
| const CHARGEBEE_TELEMETRY_TP_PREFIX = 'tp-'; | ||
| const CHARGEBEE_TELEMETRY_TP_ATTRIBUTE_PREFIX = `${CHARGEBEE_TELEMETRY_PREFIX}tp.`; | ||
| const CHARGEBEE_TELEMETRY_FT_PREFIX = 'ft-'; | ||
| const CHARGEBEE_TELEMETRY_FEATURES = `${CHARGEBEE_TELEMETRY_PREFIX}features`; | ||
|
|
||
| const SF_DATE_PREFIX = '@'; | ||
| const SF_BOOLEAN_TRUE = '?1'; | ||
| const SF_BOOLEAN_FALSE = '?0'; | ||
| const INTEGER_PATTERN = /^-?\d+$/; | ||
| const DECIMAL_PATTERN = /^-?\d+\.\d+$/; | ||
|
|
||
| export type ChargebeeTelemetrySpanAttributes = Record< | ||
| string, | ||
| string | number | boolean | string[] | ||
| >; | ||
|
|
||
| /** Parses a raw {@code X-Chargebee-Telemetry} header into typed span attributes. */ | ||
| export function parseChargebeeTelemetryHeaderToSpanAttributes( | ||
| headerValue: string | null | undefined, | ||
| ): ChargebeeTelemetrySpanAttributes { | ||
| if (headerValue == null || headerValue.trim() === '') { | ||
| return {}; | ||
| } | ||
|
|
||
| try { | ||
| const attributes: ChargebeeTelemetrySpanAttributes = {}; | ||
| const features: string[] = []; | ||
|
|
||
| for (const item of splitListItems(headerValue)) { | ||
| parseListItem(item, attributes, features); | ||
| } | ||
|
|
||
| if (features.length > 0) { | ||
| attributes[CHARGEBEE_TELEMETRY_FEATURES] = features; | ||
| } | ||
|
|
||
| return Object.keys(attributes).length === 0 ? {} : attributes; | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| function parseListItem( | ||
| item: string, | ||
| attributes: ChargebeeTelemetrySpanAttributes, | ||
| features: string[], | ||
| ): void { | ||
| const trimmed = item.trim(); | ||
| if (trimmed === '') { | ||
| return; | ||
| } | ||
|
|
||
| const separator = indexOfParameterSeparator(trimmed); | ||
| const token = | ||
| separator < 0 ? trimmed : trimmed.substring(0, separator).trim(); | ||
| if (token === '') { | ||
| throw new Error('missing sf-item token'); | ||
| } | ||
|
|
||
| if (token.startsWith(CHARGEBEE_TELEMETRY_FT_PREFIX)) { | ||
| features.push(token.substring(CHARGEBEE_TELEMETRY_FT_PREFIX.length)); | ||
| return; | ||
| } | ||
|
|
||
| const attributePrefix = segmentAttributePrefix(token); | ||
| if (separator >= 0) { | ||
| parseParameters(trimmed.substring(separator + 1), attributePrefix, attributes); | ||
| } | ||
| } | ||
|
|
||
| function segmentAttributePrefix(token: string): string { | ||
| if (token === CHARGEBEE_TELEMETRY_CB_SEGMENT) { | ||
| return CHARGEBEE_TELEMETRY_CB_PREFIX; | ||
| } | ||
| if (token.startsWith(CHARGEBEE_TELEMETRY_TP_PREFIX)) { | ||
| return ( | ||
| CHARGEBEE_TELEMETRY_TP_ATTRIBUTE_PREFIX + | ||
| token.substring(CHARGEBEE_TELEMETRY_TP_PREFIX.length) + | ||
| '.' | ||
| ); | ||
| } | ||
| return `${CHARGEBEE_TELEMETRY_PREFIX}${token}.`; | ||
| } | ||
|
|
||
| function parseParameters( | ||
| parametersSection: string, | ||
| attributePrefix: string, | ||
| attributes: ChargebeeTelemetrySpanAttributes, | ||
| ): void { | ||
| for (const parameter of splitParameters(parametersSection)) { | ||
| parseParameter(parameter, attributePrefix, attributes); | ||
| } | ||
| } | ||
|
|
||
| function parseParameter( | ||
| parameter: string, | ||
| attributePrefix: string, | ||
| attributes: ChargebeeTelemetrySpanAttributes, | ||
| ): void { | ||
| const trimmed = parameter.trim(); | ||
| if (trimmed === '') { | ||
| return; | ||
| } | ||
|
|
||
| const equalsIndex = indexOfEquals(trimmed); | ||
| if (equalsIndex <= 0) { | ||
| throw new Error(`invalid parameter: ${trimmed}`); | ||
| } | ||
|
|
||
| const key = trimmed.substring(0, equalsIndex).trim(); | ||
| const rawValue = trimmed.substring(equalsIndex + 1).trim(); | ||
| if (key === '') { | ||
| throw new Error('missing parameter key'); | ||
| } | ||
|
|
||
| attributes[attributePrefix + key] = parseScalarValue(rawValue); | ||
| } | ||
|
|
||
| export function parseScalarValue( | ||
| rawValue: string, | ||
| ): string | number | boolean { | ||
| if (rawValue == null || rawValue === '') { | ||
| throw new Error('missing scalar value'); | ||
| } | ||
|
|
||
| if (rawValue.startsWith(SF_DATE_PREFIX)) { | ||
| return Number.parseInt(rawValue.substring(SF_DATE_PREFIX.length), 10); | ||
| } | ||
| if (rawValue === SF_BOOLEAN_TRUE) { | ||
| return true; | ||
| } | ||
| if (rawValue === SF_BOOLEAN_FALSE) { | ||
| return 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.test(rawValue)) { | ||
| return Number.parseInt(rawValue, 10); | ||
| } | ||
| if (DECIMAL_PATTERN.test(rawValue)) { | ||
| return Number.parseFloat(rawValue); | ||
| } | ||
| return rawValue; | ||
| } | ||
|
|
||
| function parseStringValue(rawValue: string): string { | ||
| if (rawValue.length < 2 || rawValue.charAt(rawValue.length - 1) !== '"') { | ||
| throw new Error('invalid sf-string value'); | ||
| } | ||
|
|
||
| let decoded = ''; | ||
| for (let i = 1; i < rawValue.length - 1; i++) { | ||
| const current = rawValue.charAt(i); | ||
| if (current === '\\') { | ||
| if (i + 1 >= rawValue.length - 1) { | ||
| throw new Error('invalid sf-string escape'); | ||
| } | ||
| decoded += rawValue.charAt(++i); | ||
| } else { | ||
| decoded += current; | ||
| } | ||
| } | ||
| return decoded; | ||
| } | ||
|
|
||
| function splitListItems(input: string): string[] { | ||
| return splitOnDelimiter(input, ','); | ||
| } | ||
|
|
||
| function splitParameters(input: string): string[] { | ||
| return splitOnDelimiter(input, ';'); | ||
| } | ||
|
|
||
| function splitOnDelimiter(input: string, delimiter: string): string[] { | ||
| const parts: string[] = []; | ||
| let current = ''; | ||
| let inQuotes = false; | ||
|
|
||
| for (let i = 0; i < input.length; i++) { | ||
| const currentChar = input.charAt(i); | ||
| if (currentChar === '"') { | ||
| inQuotes = !inQuotes; | ||
| current += currentChar; | ||
| } else if (currentChar === delimiter && !inQuotes) { | ||
| addIfNotBlank(parts, current); | ||
| current = ''; | ||
| } else { | ||
| current += currentChar; | ||
| } | ||
| } | ||
|
|
||
| addIfNotBlank(parts, current); | ||
| return parts; | ||
| } | ||
|
|
||
| function indexOfParameterSeparator(item: string): number { | ||
| let inQuotes = false; | ||
| for (let i = 0; i < item.length; i++) { | ||
| const current = item.charAt(i); | ||
| if (current === '"') { | ||
| inQuotes = !inQuotes; | ||
| } else if (current === ';' && !inQuotes) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| function indexOfEquals(parameter: string): number { | ||
| let inQuotes = false; | ||
| for (let i = 0; i < parameter.length; i++) { | ||
| const current = parameter.charAt(i); | ||
| if (current === '"') { | ||
| inQuotes = !inQuotes; | ||
| } else if (current === '=' && !inQuotes) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| function addIfNotBlank(parts: string[], current: string): void { | ||
| if (current.length === 0) { | ||
| return; | ||
| } | ||
| const value = current.trim(); | ||
| if (value !== '') { | ||
| parts.push(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.