diff --git a/lib/tracing/trace.js b/lib/tracing/trace.js index f9296c2..8960f53 100644 --- a/lib/tracing/trace.js +++ b/lib/tracing/trace.js @@ -292,6 +292,7 @@ function trace(name, fn, targetObj, args, options = {}) { */ return otel.context.with(cds.context?._otelctx.setValue(cds.context._otelKey, span), () => { const onSuccess = res => { + addDbRowCount(span, res) span.setStatus({ code: SpanStatusCode.OK }) return res } @@ -317,3 +318,20 @@ function trace(name, fn, targetObj, args, options = {}) { } module.exports = trace + +const addDbRowCount = (span, res) => { + if(!span.attributes["db.statement"] || !["all", "run"].includes(span.attributes["code.function"])) { + return + } + let rowCount; + switch(span.attributes["db.operation"]) { + case "DELETE": + case "UPDATE": + case "CREATE": + rowCount = res.changes; + break; + case "READ": + rowCount = res.length ?? 1 + } + span.setAttribute("db.rowCount", rowCount) +} \ No newline at end of file