UNPKG

@ogcio/o11y-sdk-node

Version:

Opentelemetry standard instrumentation SDK for NodeJS based project

105 lines (104 loc) 3.61 kB
import { OTLPExporterBase } from "@opentelemetry/otlp-exporter-base"; import { _cleanLogBodyPII, _cleanObjectPII, _cleanStringPII, } from "../internals/pii-detection.js"; export class PIIExporterDecorator extends OTLPExporterBase { _exporter; _config; constructor(exporter, config) { super(exporter["_delegate"]); this._exporter = exporter; this._config = config; } forceFlush() { return this._exporter.forceFlush(); } shutdown() { return this._exporter.shutdown(); } export(items, resultCallback) { if (!this._config.detection?.email) { this._exporter.export(items, resultCallback); return; } if (Array.isArray(items)) { const redactedItem = items.map((item) => { if (this._isReadableSpan(item)) { this._redactSpan(item); } else if (this._isReadableLogRecord(item)) { item = this._redactLogRecord(item); } return item; }); this._exporter.export(redactedItem, resultCallback); return; } if (this._isResourceMetrics(items)) { this._redactResourceMetrics(items); this._exporter.export(items, resultCallback); } } _isReadableSpan(span) { return (typeof span === "object" && span !== null && "name" in span && "kind" in span && "spanContext" in span && "attributes" in span); } _isReadableLogRecord(span) { return (typeof span === "object" && span !== null && "body" in span && "attributes" in span && "severityText" in span && "severityNumber" in span); } _isResourceMetrics(obj) { return (typeof obj === "object" && obj !== null && !Array.isArray(obj) && "resource" in obj && "scopeMetrics" in obj); } _redactSpan(span) { Object.assign(span, { name: _cleanStringPII(span.name, "trace"), attributes: span.attributes && _cleanObjectPII(span.attributes, "trace"), resource: { attributes: span?.resource?.attributes && _cleanObjectPII(span.resource.attributes, "trace"), }, links: span?.links?.map((link) => { Object.assign(link, { attributes: link?.attributes && _cleanObjectPII(link.attributes, "trace"), }); }), events: span?.events?.map((event) => { Object.assign(event, { name: _cleanStringPII(event.name, "trace"), attributes: event?.attributes && _cleanObjectPII(event.attributes, "trace"), }); return event; }), }); } _redactLogRecord(log) { return { ...log, body: _cleanLogBodyPII(log.body), attributes: log.attributes && _cleanObjectPII(log.attributes, "log"), resource: log.resource && { ...log.resource, attributes: _cleanObjectPII(log.resource.attributes, "log"), }, }; } _redactResourceMetrics(metric) { Object.assign(metric, { resource: { attributes: metric?.resource?.attributes && _cleanObjectPII(metric.resource.attributes, "metric"), }, }); } }