@grafana/faro-transport-otlp-http
Version:
Faro transport which converts the Faro data model to the Otlp data model.
180 lines • 11.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLogTransforms = getLogTransforms;
exports.getTraceTransforms = getTraceTransforms;
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
const faro_core_1 = require("@grafana/faro-core");
const semconv_1 = require("../../semconv");
const attribute_1 = require("../attribute");
function getLogTransforms(internalLogger, customOtlpTransform) {
function toResourceLog(transportItem) {
const resource = toResource(transportItem);
return {
resource,
scopeLogs: [toScopeLog(transportItem)],
};
}
function toScopeLog(transportItem) {
return {
scope: {
name: '@grafana/faro-web-sdk',
version: faro_core_1.VERSION,
},
logRecords: [toLogRecord(transportItem)],
};
}
function toLogRecord(transportItem) {
const { type } = transportItem;
switch (type) {
case faro_core_1.TransportItemType.LOG:
return toLogLogRecord(transportItem);
case faro_core_1.TransportItemType.EXCEPTION:
return toErrorLogRecord(transportItem);
case faro_core_1.TransportItemType.EVENT:
return toEventLogRecord(transportItem);
case faro_core_1.TransportItemType.MEASUREMENT:
return toMeasurementLogRecord(transportItem);
default:
internalLogger === null || internalLogger === void 0 ? void 0 : internalLogger.error(`Unknown TransportItemType: ${type}`);
return {};
}
}
function toLogLogRecord(transportItem) {
var _a, _b;
const { meta, payload } = transportItem;
const timeUnixNano = toTimeUnixNano(payload.timestamp);
const body = (0, attribute_1.toAttributeValue)(payload.message);
function getSeverityProperties(logLevel) {
switch (logLevel) {
case faro_core_1.LogLevel.TRACE:
return { severityNumber: 1, severityText: 'TRACE' };
case faro_core_1.LogLevel.DEBUG:
return { severityNumber: 5, severityText: 'DEBUG' };
case faro_core_1.LogLevel.INFO:
return { severityNumber: 9, severityText: 'INFO' };
case faro_core_1.LogLevel.LOG:
return { severityNumber: 10, severityText: 'INFO2' };
case faro_core_1.LogLevel.WARN:
return { severityNumber: 13, severityText: 'WARN' };
case faro_core_1.LogLevel.ERROR:
return { severityNumber: 17, severityText: 'ERROR' };
}
}
return Object.assign(Object.assign({ timeUnixNano }, getSeverityProperties(payload.level)), { body, attributes: [...getCommonLogAttributes(meta), (0, attribute_1.toAttribute)('faro.log.context', payload.context)].filter(attribute_1.isAttribute), traceId: (_a = payload.trace) === null || _a === void 0 ? void 0 : _a.trace_id, spanId: (_b = payload.trace) === null || _b === void 0 ? void 0 : _b.span_id });
}
function toEventLogRecord(transportItem) {
var _a, _b;
const { meta, payload } = transportItem;
const timeUnixNano = toTimeUnixNano(payload.timestamp);
const body = (0, attribute_1.toAttributeValue)(payload.name);
return {
timeUnixNano,
body,
attributes: [
...getCommonLogAttributes(meta),
(0, attribute_1.toAttribute)('event.name', payload.name), // This is a semantic attribute. But event.name constant is currently missing in sematic-conventions npm package
(0, attribute_1.toAttribute)('event.domain', payload.domain), // This is a semantic attribute. But event.domain constant is currently missing in sematic-conventions npm package
(0, attribute_1.toAttribute)('event.attributes', payload.attributes),
].filter(attribute_1.isAttribute),
traceId: (_a = payload.trace) === null || _a === void 0 ? void 0 : _a.trace_id,
spanId: (_b = payload.trace) === null || _b === void 0 ? void 0 : _b.span_id,
};
}
function toErrorLogRecord(transportItem) {
var _a, _b;
const { meta, payload } = transportItem;
const timeUnixNano = toTimeUnixNano(payload.timestamp);
const body = getCustomLogBody(transportItem, customOtlpTransform === null || customOtlpTransform === void 0 ? void 0 : customOtlpTransform.createErrorLogBody);
return Object.assign(Object.assign({ timeUnixNano }, (body ? { body } : {})), { attributes: [
...getCommonLogAttributes(meta),
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_EXCEPTION_TYPE, payload.type),
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_EXCEPTION_MESSAGE, payload.value),
// toAttribute(SemanticAttributes.EXCEPTION_STACKTRACE, undefined),
(0, attribute_1.toAttribute)('faro.error.stacktrace', payload.stacktrace),
(0, attribute_1.toAttribute)('faro.error.context', payload.context),
].filter(attribute_1.isAttribute), traceId: (_a = payload.trace) === null || _a === void 0 ? void 0 : _a.trace_id, spanId: (_b = payload.trace) === null || _b === void 0 ? void 0 : _b.span_id });
}
function toMeasurementLogRecord(transportItem) {
var _a, _b;
const { meta, payload } = transportItem;
const timeUnixNano = toTimeUnixNano(payload.timestamp);
const [measurementName, measurementValue] = Object.entries(payload.values).flat();
const body = getCustomLogBody(transportItem, customOtlpTransform === null || customOtlpTransform === void 0 ? void 0 : customOtlpTransform.createMeasurementLogBody);
return Object.assign(Object.assign({ timeUnixNano }, (body ? { body } : {})), { attributes: [
...getCommonLogAttributes(meta),
(0, attribute_1.toAttribute)('measurement.type', payload.type),
(0, attribute_1.toAttribute)('measurement.name', measurementName),
(0, attribute_1.toAttribute)('measurement.value', measurementValue),
(0, attribute_1.toAttribute)('faro.measurement.context', payload.context),
].filter(attribute_1.isAttribute), traceId: (_a = payload.trace) === null || _a === void 0 ? void 0 : _a.trace_id, spanId: (_b = payload.trace) === null || _b === void 0 ? void 0 : _b.span_id });
}
function getCommonLogAttributes(meta) {
const { view, page, session, user } = meta;
return [
(0, attribute_1.toAttribute)('view.name', view === null || view === void 0 ? void 0 : view.name),
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_URL_FULL, page === null || page === void 0 ? void 0 : page.url),
(0, attribute_1.toAttribute)('page.id', page === null || page === void 0 ? void 0 : page.id),
(0, attribute_1.toAttribute)('page.attributes', page === null || page === void 0 ? void 0 : page.attributes),
(0, attribute_1.toAttribute)(semconv_1.ATTR_SESSION_ID, session === null || session === void 0 ? void 0 : session.id),
(0, attribute_1.toAttribute)('session.attributes', session === null || session === void 0 ? void 0 : session.attributes),
(0, attribute_1.toAttribute)(semconv_1.ATTR_USER_ID, user === null || user === void 0 ? void 0 : user.id),
(0, attribute_1.toAttribute)(semconv_1.ATTR_USER_NAME, user === null || user === void 0 ? void 0 : user.username),
(0, attribute_1.toAttribute)(semconv_1.ATTR_USER_EMAIL, user === null || user === void 0 ? void 0 : user.email),
(0, attribute_1.toAttribute)(semconv_1.ATTR_USER_FULL_NAME, user === null || user === void 0 ? void 0 : user.fullName),
(0, attribute_1.toAttribute)(semconv_1.ATTR_USER_ROLES, user === null || user === void 0 ? void 0 : user.roles),
(0, attribute_1.toAttribute)(semconv_1.ATTR_USER_HASH, user === null || user === void 0 ? void 0 : user.hash),
(0, attribute_1.toAttribute)(semconv_1.ATTR_USER_ATTRIBUTES, user === null || user === void 0 ? void 0 : user.attributes),
].filter(attribute_1.isAttribute);
}
function toTimeUnixNano(timestamp) {
return Date.parse(timestamp) * 1e6;
}
return {
toResourceLog,
toScopeLog,
toLogRecord,
};
}
function getTraceTransforms(_internalLogger) {
function toResourceSpan(transportItem) {
var _a, _b;
const resource = toResource(transportItem);
const scopeSpans = (_b = (_a = transportItem.payload.resourceSpans) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.scopeSpans;
return {
resource,
scopeSpans: scopeSpans !== null && scopeSpans !== void 0 ? scopeSpans : [],
};
}
return {
toResourceSpan,
};
}
function toResource(transportItem) {
const { browser, sdk, app } = transportItem.meta;
return {
attributes: [
(0, attribute_1.toAttribute)(semconv_1.ATTR_BROWSER_MOBILE, browser === null || browser === void 0 ? void 0 : browser.mobile),
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_USER_AGENT_ORIGINAL, browser === null || browser === void 0 ? void 0 : browser.userAgent),
(0, attribute_1.toAttribute)(semconv_1.ATTR_BROWSER_LANGUAGE, browser === null || browser === void 0 ? void 0 : browser.language),
(0, attribute_1.toAttribute)(semconv_1.ATTR_BROWSER_BRANDS, browser === null || browser === void 0 ? void 0 : browser.brands),
(0, attribute_1.toAttribute)(semconv_1.ATTR_BROWSER_PLATFORM, browser === null || browser === void 0 ? void 0 : browser.os),
(0, attribute_1.toAttribute)('browser.name', browser === null || browser === void 0 ? void 0 : browser.name),
(0, attribute_1.toAttribute)('browser.version', browser === null || browser === void 0 ? void 0 : browser.version),
(0, attribute_1.toAttribute)('browser.screen_width', browser === null || browser === void 0 ? void 0 : browser.viewportWidth),
(0, attribute_1.toAttribute)('browser.screen_height', browser === null || browser === void 0 ? void 0 : browser.viewportHeight),
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_TELEMETRY_SDK_NAME, sdk === null || sdk === void 0 ? void 0 : sdk.name),
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_TELEMETRY_SDK_VERSION, sdk === null || sdk === void 0 ? void 0 : sdk.version),
sdk ? (0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_TELEMETRY_SDK_LANGUAGE, semantic_conventions_1.TELEMETRY_SDK_LANGUAGE_VALUE_WEBJS) : undefined,
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_SERVICE_NAME, app === null || app === void 0 ? void 0 : app.name),
(0, attribute_1.toAttribute)(semantic_conventions_1.ATTR_SERVICE_VERSION, app === null || app === void 0 ? void 0 : app.version),
(0, attribute_1.toAttribute)(semconv_1.ATTR_SERVICE_NAMESPACE, app === null || app === void 0 ? void 0 : app.namespace),
(0, attribute_1.toAttribute)(semconv_1.ATTR_DEPLOYMENT_ENVIRONMENT_NAME, app === null || app === void 0 ? void 0 : app.environment),
].filter(attribute_1.isAttribute),
};
}
function getCustomLogBody(transportItem, createCustomLogBody) {
return typeof createCustomLogBody === 'function'
? (0, attribute_1.toAttributeValue)(createCustomLogBody(transportItem))
: undefined;
}
//# sourceMappingURL=transform.js.map