-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcds.js
More file actions
131 lines (121 loc) · 4.51 KB
/
Copy pathcds.js
File metadata and controls
131 lines (121 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const cds = require('@sap/cds')
const { SpanKind } = require('@opentelemetry/api')
const trace = require('./trace')
const wrap = require('./wrap')
function _wrapHandler(handler) {
const phase = handler.on ? 'on' : handler.before ? 'before' : 'after'
handler.handler = wrap(handler.handler, {
phase: phase,
event: handler[phase]
})
}
const _wrapStmt = (stmt, impl, sql) => {
for (const fn of ['run', 'get', 'all', 'stream', 'runBatch']) {
if (!stmt[fn]) continue
const it = stmt[fn]
stmt[fn] = wrap(it, {
no_locate: true,
wrapper: function () {
return trace(`${impl} - stmt.${fn} ${sql}`, it, this, arguments, { sql, fn, kind: SpanKind.CLIENT })
}
})
}
return stmt
}
module.exports = () => {
const { emit: _emit, handle: _handle } = cds.Service.prototype
cds.Service.prototype.emit = wrap(_emit, {
wrapper: function emit() {
const event = arguments[0]?.event || arguments[0]
return trace({ phase: 'emit', event }, _emit, this, arguments, {})
}
})
cds.Service.prototype.handle = wrap(_handle, {
wrapper: function handle(req) {
if (!cds.env.requires.telemetry.tracing._tx && req.event in { BEGIN: 1, COMMIT: 1, ROLLBACK: 1 })
return _handle.apply(this, arguments)
return trace(req, _handle, this, arguments, {})
}
})
const { spawn: _spawn } = cds
cds.spawn = wrap(_spawn, {
wrapper: function spawn() {
const handlerFn = typeof arguments[0] === 'function' ? arguments[0] : arguments[1]
const wrappedFn = wrap(handlerFn, { event: 'cds.spawn', kind: SpanKind.CONSUMER })
if (typeof arguments[0] === 'function') arguments[0] = wrappedFn
else arguments[1] = wrappedFn
return trace('cds.spawn', _spawn, this, arguments, { kind: SpanKind.PRODUCER })
}
})
cds.on('serving', service => {
// trace individual event handlers -> INOFFICIAL!
if (cds.env.requires.telemetry.tracing?.level === 'debug') {
for (const each of ['_error', '_initial', 'on', 'before', 'after']) {
service._handlers[each].forEach(_wrapHandler)
}
}
})
cds.on('connect', service => {
if (service instanceof cds.MessagingService) {
const unboxed = cds.unboxed(service)
const { handle: _unboxed_handle } = unboxed
unboxed.handle = wrap(_unboxed_handle, {
wrapper: function handle(msg) {
if (msg.inbound) {
// TODO: w3c trace context
return _unboxed_handle.apply(this, arguments)
}
const kind = service.kind !== 'local-messaging' ? SpanKind.PRODUCER : SpanKind.INTERNAL
return trace(msg, _unboxed_handle, this, arguments, { kind })
}
})
// // TODO: w3c trace context
// if (unboxed !== service) {
// const { handle: _outboxed_handle } = service
// service.handle = wrap(_outboxed_handle, {
// wrapper: function handle(msg) {
// if (!msg.inbound) {
// const spanContext = otel_trace.getActiveSpan().spanContext()
// // taken from W3CTraceContextPropagator
// const traceParent =
// VERSION +
// '-' +
// spanContext.traceId +
// '-' +
// spanContext.spanId +
// '-0' +
// Number(spanContext.traceFlags || TraceFlags.NONE).toString(16)
// msg.headers.traceparent = traceParent
// }
// return _outboxed_handle.apply(this, arguments)
// }
// })
// }
}
})
const impl = cds.env.requires.db?.impl
if (impl?.match(/^@cap-js\//)) {
const dbService = require(impl)
cds.once('served', () => {
const { prepare: _prepare, exec: _exec } = dbService.prototype
dbService.prototype.prepare = wrap(_prepare, {
wrapper: function prepare(sql) {
const stmt = trace(`${impl} - prepare ${sql}`, _prepare, this, arguments, {
sql,
fn: 'prepare',
kind: SpanKind.CLIENT
})
if (stmt instanceof Promise) return stmt.then(stmt => _wrapStmt(stmt, impl, sql))
return _wrapStmt(stmt, impl, sql)
}
})
dbService.prototype.exec = wrap(_exec, {
wrapper: function exec(sql) {
if (!cds.env.requires.telemetry.tracing._tx && sql in { BEGIN: 1, COMMIT: 1, ROLLBACK: 1 })
return _exec.apply(this, arguments)
return trace(`${impl} - exec ${sql}`, _exec, this, arguments, { sql, fn: 'exec', kind: SpanKind.CLIENT })
}
})
})
}
}