@grafana/faro-transport-otlp-http
Version:
Faro transport which converts the Faro data model to the Otlp data model.
65 lines • 3.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OtelPayload = void 0;
const faro_core_1 = require("@grafana/faro-core");
const transform_1 = require("./transform");
class OtelPayload {
constructor({ internalLogger, customOtlpTransform, transportItem }) {
this.resourceSpans = [];
this.internalLogger = internalLogger;
this.resourceLogs = [];
this.getLogTransforms = (0, transform_1.getLogTransforms)(this.internalLogger, customOtlpTransform);
this.getTraceTransforms = (0, transform_1.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 faro_core_1.TransportItemType.LOG:
case faro_core_1.TransportItemType.EXCEPTION:
case faro_core_1.TransportItemType.EVENT:
case faro_core_1.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 faro_core_1.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);
}
}
}
exports.OtelPayload = OtelPayload;
//# sourceMappingURL=OtelPayload.js.map