UNPKG

chargebee

Version:

A library for integrating with Chargebee.

133 lines (132 loc) 5.48 kB
/* * 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. */ import { CHARGEBEE_SDK_NAME, CHARGEBEE_TELEMETRY_HEADER_EXCLUDE_PREFIX, CHARGEBEE_TELEMETRY_HEADER_PREFIX, HTTP_REQUEST_HEADER_ATTRIBUTE_PREFIX, TELEMETRY_SPAN_NAME_PREFIX, TelemetryAttributeKeys, } from './types.js'; export class NoOpTelemetryAdapter { onRequestStart() { return; } onRequestEnd() { return; } } export const NO_OP_TELEMETRY_ADAPTER = new NoOpTelemetryAdapter(); export function buildSpanName(resource, operation) { return `${TELEMETRY_SPAN_NAME_PREFIX}.${resource}.${operation}`; } export function resolveChargebeeApiVersion(apiPath) { return apiPath === '/api/v1' ? 'v1' : 'v2'; } /** * Captures Chargebee custom request headers as OTel span attributes. * * Headers whose (lowercased) name starts with `chargebee-` are recorded as * `http.request.header.<name>` with string[] values, per the OpenTelemetry HTTP semantic * conventions. The `chargebee-request-origin-*` family (origin IP, email, device) is skipped * because it carries end-user PII. Matching by prefix means new `chargebee-*` headers are * captured automatically without an SDK upgrade. */ export function buildRequestHeaderSpanAttributes(requestHeaders) { const attributes = {}; if (!requestHeaders) { return attributes; } for (const [name, value] of Object.entries(requestHeaders)) { if (value === undefined || value === null) { continue; } const lowerName = name.toLowerCase(); if (!lowerName.startsWith(CHARGEBEE_TELEMETRY_HEADER_PREFIX) || lowerName.startsWith(CHARGEBEE_TELEMETRY_HEADER_EXCLUDE_PREFIX)) { continue; } attributes[`${HTTP_REQUEST_HEADER_ATTRIBUTE_PREFIX}${lowerName}`] = [ String(value), ]; } return attributes; } export function buildRequestStartSpanAttributes(input) { return Object.assign({ [TelemetryAttributeKeys.URL_FULL]: input.httpUrl, [TelemetryAttributeKeys.HTTP_REQUEST_METHOD]: input.httpMethod, [TelemetryAttributeKeys.SERVER_ADDRESS]: input.serverAddress, [TelemetryAttributeKeys.CHARGEBEE_SITE]: input.chargebeeSite, [TelemetryAttributeKeys.CHARGEBEE_API_VERSION]: input.chargebeeApiVersion, [TelemetryAttributeKeys.CHARGEBEE_RESOURCE]: input.resource, [TelemetryAttributeKeys.CHARGEBEE_OPERATION]: input.operation, [TelemetryAttributeKeys.CHARGEBEE_SDK_NAME]: CHARGEBEE_SDK_NAME, [TelemetryAttributeKeys.CHARGEBEE_SDK_VERSION]: input.sdkVersion }, buildRequestHeaderSpanAttributes(input.requestHeaders)); } export function buildRequestEndSpanAttributes(result) { const attributes = { [TelemetryAttributeKeys.HTTP_RESPONSE_STATUS_CODE]: result.httpStatusCode, }; if (result.error) { // error.type is the status code on failed requests attributes[TelemetryAttributeKeys.ERROR_TYPE] = String(result.httpStatusCode); if (result.error.chargebeeErrorCode) { attributes[TelemetryAttributeKeys.CHARGEBEE_ERROR_CODE] = result.error.chargebeeErrorCode; } if (result.error.chargebeeApiErrorType) { attributes[TelemetryAttributeKeys.CHARGEBEE_ERROR_TYPE] = result.error.chargebeeApiErrorType; } if (result.error.chargebeeErrorParam) { attributes[TelemetryAttributeKeys.CHARGEBEE_ERROR_PARAM] = result.error.chargebeeErrorParam; } } return attributes; } export function buildRequestTelemetryContext(input) { return { spanName: buildSpanName(input.resource, input.operation), resource: input.resource, operation: input.operation, httpMethod: input.httpMethod, httpUrl: input.httpUrl, serverAddress: input.serverAddress, chargebeeSite: input.chargebeeSite, chargebeeApiVersion: input.chargebeeApiVersion, sdkName: CHARGEBEE_SDK_NAME, sdkVersion: input.sdkVersion, startAttributes: buildRequestStartSpanAttributes(input), }; } export function buildRequestTelemetryResult(result) { return Object.assign(Object.assign({}, result), { endAttributes: buildRequestEndSpanAttributes(result) }); } export function extractRequestTelemetryError(err) { if (err == null || typeof err !== 'object') { return undefined; } const errorObj = err; const message = typeof errorObj.message === 'string' ? errorObj.message : 'Chargebee API request failed'; const result = { message }; if (typeof errorObj.api_error_code === 'string') { result.chargebeeErrorCode = errorObj.api_error_code; } if (typeof errorObj.type === 'string') { result.chargebeeApiErrorType = errorObj.type; } if (typeof errorObj.param === 'string') { result.chargebeeErrorParam = errorObj.param; } return result; } export function extractHttpStatusCode(err) { if (err == null || typeof err !== 'object') { return undefined; } const errorObj = err; for (const key of [ 'http_status_code', 'httpStatusCode', 'http_code', 'statusCode', ]) { const value = errorObj[key]; if (typeof value === 'number') { return value; } } return undefined; }