-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathazure-durable-functions.js
More file actions
75 lines (62 loc) · 2.42 KB
/
Copy pathazure-durable-functions.js
File metadata and controls
75 lines (62 loc) · 2.42 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
'use strict'
const dc = require('dc-polyfill')
const shimmer = require('../../datadog-shimmer')
const {
addHook,
} = require('./helpers/instrument')
/**
* @type {import('diagnostics_channel').TracingChannel}
*/
const azureDurableFunctionsChannel = dc.tracingChannel('datadog:azure:durable-functions:invoke')
addHook({ name: 'durable-functions', versions: ['>=3'], patchDefault: false }, (df) => {
const { app } = df
shimmer.wrap(app, 'entity', entityWrapper)
shimmer.wrap(app, 'activity', activityHandler)
return df
})
function entityWrapper (method) {
return function (entityName, arg) {
// because this method is overloaded, the second argument can either be an object
// with the handler or the handler itself, so first we figure which type it is
if (typeof arg === 'function') {
// if a function, this is the handler we want to wrap and trace
arguments[1] = shimmer.wrapFunction(arg, handler => entityHandler(handler, entityName))
} else {
// if an object, access the handler then trace it
shimmer.wrap(arg, 'handler', handler => entityHandler(handler, entityName))
}
return method.apply(this, arguments)
}
}
function entityHandler (handler, entityName) {
return function () {
if (!azureDurableFunctionsChannel.hasSubscribers) return handler.apply(this, arguments)
const entityContext = arguments[0]
return azureDurableFunctionsChannel.traceSync(
handler,
{ trigger: 'Entity', functionName: entityName, operationName: entityContext?.df?.operationName },
this, ...arguments)
}
}
function activityHandler (method) {
return function (activityName, activityOptions) {
shimmer.wrap(activityOptions, 'handler', handler => {
const isAsync =
handler && handler.constructor && handler.constructor.name === 'AsyncFunction'
return function () {
if (!azureDurableFunctionsChannel.hasSubscribers) return handler.apply(this, arguments)
// use tracePromise if this is an async handler. otherwise, use traceSync
return isAsync
? azureDurableFunctionsChannel.tracePromise(
handler,
{ trigger: 'Activity', functionName: activityName },
this, ...arguments)
: azureDurableFunctionsChannel.traceSync(
handler,
{ trigger: 'Activity', functionName: activityName },
this, ...arguments)
}
})
return method.apply(this, arguments)
}
}