@grafana/faro-transport-otlp-http
Version:
Faro transport which converts the Faro data model to the Otlp data model.
59 lines • 3.12 kB
JavaScript
import { TransportItemType } from '@grafana/faro-core';
import { getLogTransforms, getTraceTransforms } from './transform';
export class OtelPayload {
constructor({ internalLogger, customOtlpTransform, transportItem }) {
this.resourceSpans = [];
this.internalLogger = internalLogger;
this.resourceLogs = [];
this.getLogTransforms = getLogTransforms(this.internalLogger, customOtlpTransform);
this.getTraceTransforms = getTraceTransforms(this.internalLogger);
if (transportItem) {
this.addResourceItem(transportItem);
}
}
getPayload() {
return {
resourceLogs: this.resourceLogs,
resourceSpans: this.resourceSpans,
};
}
addResourceItem(transportItem) {
var _a, _b, _c, _d;
const { type } = transportItem;
try {
switch (type) {
case TransportItemType.LOG:
case TransportItemType.EXCEPTION:
case TransportItemType.EVENT:
case TransportItemType.MEASUREMENT:
const { toLogRecord, toResourceLog } = this.getLogTransforms;
// Currently the scope is fixed to '@grafana/faro-web-sdk'.
// Once we are able to drive the scope by instrumentation this will change and we need to align this function
if (this.resourceLogs.length === 0) {
this.resourceLogs = [toResourceLog(transportItem)];
}
else {
// Faro takes care of the grouping with different metadata (or OTel attributes), so we can safely
// use the just the first element of the resource.
(_b = (_a = this.resourceLogs[0]) === null || _a === void 0 ? void 0 : _a.scopeLogs[0]) === null || _b === void 0 ? void 0 : _b.logRecords.push(toLogRecord(transportItem));
}
break;
case TransportItemType.TRACE:
const { toResourceSpan } = this.getTraceTransforms;
// We use the Otel Model as it is to avoid unnecessary resource consumption.
// This is because we don't need the same logic to add items as it is for logs.
// Also the Otel library already applies the respective protocol transforms so there is no need for additional transforms.
// We only transform the resource object to ensure that we are compliant with the respective Faro Metas which add a few more items to the resource object.
this.resourceSpans.push(toResourceSpan(transportItem));
break;
default:
(_c = this.internalLogger) === null || _c === void 0 ? void 0 : _c.error(`Unknown TransportItemType: ${type}`);
break;
}
}
catch (error) {
(_d = this.internalLogger) === null || _d === void 0 ? void 0 : _d.error(error);
}
}
}
//# sourceMappingURL=OtelPayload.js.map