UNPKG

chargebee

Version:

A library for integrating with Chargebee.

67 lines (66 loc) 2.66 kB
"use strict"; /* * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.OtelTelemetryAdapter = void 0; const api_1 = require("@opentelemetry/api"); const types_js_1 = require("./types.js"); const tracer = api_1.trace.getTracer(types_js_1.CHARGEBEE_SDK_NAME); function toRecordedException(error) { var _a; const exception = new Error(error.message); exception.name = (_a = error.chargebeeErrorCode) !== null && _a !== void 0 ? _a : 'ChargebeeAPIError'; return exception; } /** * Ready-to-use OpenTelemetry adapter for the Chargebee SDK. * * Each SDK request becomes a CLIENT span attached to the active OpenTelemetry * context (or a root span when none is active), and the W3C trace context is * propagated to Chargebee so the trace continues server-side. * * Exporting (endpoint, service name, credentials) is configured by your own * OpenTelemetry runtime via the standard `OTEL_*` environment variables — this * adapter only uses the globally registered tracer from `@opentelemetry/api`. * * `@opentelemetry/api` is an optional peer dependency; install it to use this * adapter. A default-exported, ready-to-use instance is provided. */ class OtelTelemetryAdapter { onRequestStart(ctx, requestHeaders) { // startSpan adopts the active context's span as parent (or starts a root span if none). const span = tracer.startSpan(ctx.spanName, { kind: api_1.SpanKind.CLIENT, attributes: ctx.startAttributes, }); // Propagate W3C trace context to Chargebee so the trace continues server-side. api_1.propagation.inject(api_1.trace.setSpan(api_1.context.active(), span), requestHeaders); return span; } onRequestEnd(span, result) { if (!span) { return; } for (const [key, value] of Object.entries(result.endAttributes)) { span.setAttribute(key, value); } if (result.error) { span.recordException(toRecordedException(result.error)); span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: result.error.message, }); } else { span.setStatus({ code: api_1.SpanStatusCode.OK }); } span.end(); } } exports.OtelTelemetryAdapter = OtelTelemetryAdapter; const otelDefaultAdapter = new OtelTelemetryAdapter(); exports.default = otelDefaultAdapter;