UNPKG

solarwinds-apm

Version:

OpenTelemetry-based SolarWinds APM library

148 lines (145 loc) 5.99 kB
/* Copyright 2023-2025 SolarWinds Worldwide, LLC. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var _a; import { inspect } from "node:util"; import { SpanKind, SpanStatusCode, } from "@opentelemetry/api"; import { ExportResultCode, hrTimeToMicroseconds, } from "@opentelemetry/core"; import { ATTR_EXCEPTION_MESSAGE, ATTR_EXCEPTION_STACKTRACE, ATTR_EXCEPTION_TYPE, } from "@opentelemetry/semantic-conventions"; import { oboe } from "@solarwinds-apm/bindings"; import { TRANSACTION_NAME_ATTRIBUTE } from "../../processing/transaction-name.js"; import { traceParent } from "../../propagation/headers.js"; import { componentLogger } from "../../shared/logger.js"; export class AppopticsTraceExporter { #logger = componentLogger(_a); #reporter; #error = undefined; constructor(reporter) { this.#reporter = reporter; } export(spans, resultCallback) { for (const span of spans) { const context = span.spanContext(); const md = this.#metadata(context); let evt; if (span.parentSpanContext) { evt = oboe.Context.createEntry(md, hrTimeToMicroseconds(span.startTime), this.#metadata({ traceFlags: context.traceFlags, traceId: context.traceId, spanId: span.parentSpanContext.spanId, })); } else { evt = oboe.Context.createEntry(md, hrTimeToMicroseconds(span.startTime)); } const kind = SpanKind[span.kind]; const layer = `${kind}:${span.name}`; evt.addInfo("Layer", layer); evt.addInfo("sw.span_name", span.name); evt.addInfo("sw.span_kind", kind); evt.addInfo("Language", "Node.js"); evt.addInfo("otel.scope.name", span.instrumentationScope.name); evt.addInfo("otel.scope.version", span.instrumentationScope.version ?? null); if (span.status.code !== SpanStatusCode.UNSET) { evt.addInfo("otel.status_code", SpanStatusCode[span.status.code]); } if (span.status.message) { evt.addInfo("otel.status_description", span.status.message); } if (span.droppedAttributesCount > 0) { evt.addInfo("otel.dropped_attributes_count", span.droppedAttributesCount); } if (span.droppedEventsCount > 0) { evt.addInfo("otel.dropped_events_count", span.droppedEventsCount); } if (span.droppedLinksCount > 0) { evt.addInfo("otel.dropped_links_count", span.droppedLinksCount); } const rename = { [TRANSACTION_NAME_ATTRIBUTE]: "TransactionName", }; for (const [key, value] of Object.entries(span.attributes)) { const name = rename[key] ?? key; evt.addInfo(name, this.#attributeValue(value)); } this.#sendReport(evt); for (const event of span.events) { if (event.name === "exception") { this.#reportErrorEvent(event); } else { this.#reportInfoEvent(event); } } evt = oboe.Context.createExit(hrTimeToMicroseconds(span.endTime)); evt.addInfo("Layer", layer); this.#sendReport(evt); } const result = this.#error ? { code: ExportResultCode.FAILED, error: this.#error } : { code: ExportResultCode.SUCCESS }; this.#error = undefined; resultCallback(result); } forceFlush() { this.#reporter.flush(); return Promise.resolve(); } shutdown() { oboe.Context.shutdown(); return Promise.resolve(); } #metadata(spanContext) { return oboe.Metadata.fromString(traceParent(spanContext)); } #attributeValue(v) { if (Array.isArray(v)) { return inspect(v, { breakLength: Infinity, compact: true }); } else { return v ?? null; } } #reportErrorEvent(event) { const evt = oboe.Context.createEvent(hrTimeToMicroseconds(event.time)); evt.addInfo("Label", "error"); evt.addInfo("Spec", "error"); const rename = { [ATTR_EXCEPTION_TYPE]: "ErrorClass", [ATTR_EXCEPTION_MESSAGE]: "ErrorMsg", [ATTR_EXCEPTION_STACKTRACE]: "Backtrace", }; for (const [key, value] of Object.entries(event.attributes ?? {})) { const name = rename[key] ?? key; evt.addInfo(name, this.#attributeValue(value)); } this.#sendReport(evt); } #reportInfoEvent(event) { const evt = oboe.Context.createEvent(hrTimeToMicroseconds(event.time)); evt.addInfo("Label", "info"); for (const [key, value] of Object.entries(event.attributes ?? {})) { evt.addInfo(key, this.#attributeValue(value)); } this.#sendReport(evt); } #sendReport(evt) { const status = this.#reporter.sendReport(evt, false); if (status < 0) { this.#error = new Error(`Reporter::sendReport returned with error status ${status}`); this.#logger.warn("error sending report", this.#error); this.#logger.debug(evt.metadataString()); } } } _a = AppopticsTraceExporter; //# sourceMappingURL=traces.js.map