chargebee
Version:
A library for integrating with Chargebee.
63 lines (62 loc) • 2.5 kB
JavaScript
/*
* 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 { context, propagation, SpanKind, SpanStatusCode, trace, } from '@opentelemetry/api';
import { CHARGEBEE_SDK_NAME, } from './types.js';
const tracer = trace.getTracer(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.
*/
export 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: SpanKind.CLIENT,
attributes: ctx.startAttributes,
});
// Propagate W3C trace context to Chargebee so the trace continues server-side.
propagation.inject(trace.setSpan(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: SpanStatusCode.ERROR,
message: result.error.message,
});
}
else {
span.setStatus({ code: SpanStatusCode.OK });
}
span.end();
}
}
const otelDefaultAdapter = new OtelTelemetryAdapter();
export default otelDefaultAdapter;