2022-07-14 10:08:50 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
|
|
|
hrTimeToMilliseconds,
|
|
|
|
//hrTimeToMicroseconds
|
|
|
|
} = require('@opentelemetry/core');
|
2023-05-01 23:39:01 -06:00
|
|
|
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
|
|
|
|
|
|
|
|
const SAFE_ATTRIBUTES = [
|
|
|
|
SemanticAttributes.HTTP_METHOD,
|
|
|
|
SemanticAttributes.HTTP_URL,
|
|
|
|
SemanticAttributes.HTTP_TARGET,
|
|
|
|
SemanticAttributes.HTTP_STATUS_CODE,
|
|
|
|
];
|
2022-07-14 10:08:50 -06:00
|
|
|
|
|
|
|
// Convert a `Span` object to a plain old JavaScript object with only the info
|
|
|
|
// we care about and that is safe to share. We want something we can JSON
|
|
|
|
// serialize.
|
|
|
|
function serializeSpan(span) {
|
|
|
|
const spanContext = span.spanContext();
|
|
|
|
|
|
|
|
const safeAttributes = {};
|
|
|
|
SAFE_ATTRIBUTES.forEach((safeAttribute) => {
|
|
|
|
safeAttributes[safeAttribute] = span.attributes[safeAttribute];
|
|
|
|
});
|
|
|
|
|
|
|
|
const startTimeInMs = hrTimeToMilliseconds(span.startTime);
|
|
|
|
const endTimeInMs = hrTimeToMilliseconds(span.endTime);
|
|
|
|
|
|
|
|
let durationInMs = null;
|
|
|
|
if (startTimeInMs && endTimeInMs) {
|
|
|
|
durationInMs = endTimeInMs - startTimeInMs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: span.name,
|
|
|
|
spanContext: {
|
|
|
|
traceId: spanContext.traceId,
|
|
|
|
spanId: spanContext.spanId,
|
|
|
|
},
|
|
|
|
//parentSpanId: span.parentSpanId,
|
|
|
|
startTimeInMs,
|
|
|
|
endTimeInMs,
|
|
|
|
durationInMs,
|
|
|
|
attributes: safeAttributes,
|
|
|
|
//span.links
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = serializeSpan;
|