Skip to content

Commit

Permalink
Merge branch 'main' into cleanup-tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjvans committed Jan 14, 2025
2 parents f3419a6 + 67933be commit 9b8decd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 1.2.0 - tbd

### Added

- Trace attribute `db.client.response.returned_rows` for queries via `cds.ql`

### Changed

### Fixed

### Removed

## Version 1.1.2 - 2024-12-10

### Fixed
Expand Down
18 changes: 18 additions & 0 deletions lib/tracing/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ function _addAttributes(span, fn, that, options, args, parent) {
_setAttributes(span, otherAttributes)
}

const _addDbRowCount = (span, res) => {
if (!span.attributes['db.statement']) return
if (!['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
}
if (rowCount != null) span.setAttribute('db.client.response.returned_rows', rowCount)
}

function trace(req, fn, that, args, opts = {}) {
// only trace once served and there is a cds.context
if (!tracer || !cds.context) return fn.apply(that, args)
Expand Down Expand Up @@ -333,6 +350,7 @@ function trace(req, fn, that, args, opts = {}) {
try {
let res = fn.apply(that, args)
if (res instanceof Promise) res = await res
_addDbRowCount(span, res)
span.setStatus({ code: SpanStatusCode.OK })
return res
} catch (err) {
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cap-js/telemetry",
"version": "1.1.2",
"version": "1.2.0",
"description": "CDS plugin providing observability features, incl. automatic OpenTelemetry instrumentation.",
"repository": {
"type": "git",
Expand All @@ -21,8 +21,8 @@
"dependencies": {
"@opentelemetry/api": "^1.9",
"@opentelemetry/core": "^1.27",
"@opentelemetry/instrumentation": "^0.55",
"@opentelemetry/instrumentation-http": "^0.55",
"@opentelemetry/instrumentation": "^0.57",
"@opentelemetry/instrumentation-http": "^0.57",
"@opentelemetry/resources": "^1.27",
"@opentelemetry/sdk-metrics": "^1.27",
"@opentelemetry/sdk-trace-base": "^1.27",
Expand All @@ -37,12 +37,12 @@
"@cap-js/telemetry": "file:.",
"@dynatrace/oneagent-sdk": "^1.5.0",
"@grpc/grpc-js": "^1.9.14",
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.56.0",
"@opentelemetry/exporter-metrics-otlp-proto": "^0.56.0",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.56.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.56.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.57.0",
"@opentelemetry/exporter-metrics-otlp-proto": "^0.57.0",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.57.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.57.0",
"@opentelemetry/host-metrics": "^0.35.0",
"@opentelemetry/instrumentation-runtime-node": "^0.11.0",
"@opentelemetry/instrumentation-runtime-node": "^0.12.0",
"@sap/cds-mtxs": "^2.0.5",
"axios": "^1.6.7",
"chai": "^4.4.1",
Expand Down
38 changes: 38 additions & 0 deletions test/tracing-attributes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
process.env.cds_requires_telemetry_tracing_exporter_module = '@opentelemetry/sdk-trace-node'

const cds = require('@sap/cds')
const { expect, data } = cds.test().in(__dirname + '/bookshop')

describe('tracing attributes', () => {
beforeEach(data.reset)

const log = jest.spyOn(console, 'dir')
beforeEach(log.mockClear)

describe('db.client.response.returned_rows', () => {
test('SELECT', async () => {
await SELECT.from('sap.capire.bookshop.Books')
const output = JSON.stringify(log.mock.calls)
expect(output).to.match(/db\.client\.response.returned_rows":5/)
})

test('INSERT', async () => {
await INSERT.into('sap.capire.bookshop.Books').entries([{ ID: 1 }, { ID: 2 }])
const output = JSON.stringify(log.mock.calls)
expect(output).to.match(/db\.client\.response.returned_rows":2/)
})

test('UPDATE', async () => {
await UPDATE('sap.capire.bookshop.Books').set({ stock: 42 }).where('ID > 250')
const output = JSON.stringify(log.mock.calls)
expect(output).to.match(/db\.client\.response.returned_rows":3/)
})

test('DELETE', async () => {
await DELETE.from('sap.capire.bookshop.Books')
const output = JSON.stringify(log.mock.calls)
expect(output).to.match(/db\.client\.response.returned_rows":0/) //> texts
expect(output).to.match(/db\.client\.response.returned_rows":5/)
})
})
})

0 comments on commit 9b8decd

Please sign in to comment.