-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (153 loc) · 5.15 KB
/
Copy pathindex.js
File metadata and controls
175 lines (153 loc) · 5.15 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict'
const TracingPlugin = require('../../dd-trace/src/plugins/tracing')
const web = require('../../dd-trace/src/plugins/util/web')
const triggerMap = {
deleteRequest: 'Http',
http: 'Http',
get: 'Http',
patch: 'Http',
post: 'Http',
put: 'Http',
serviceBusQueue: 'ServiceBus',
serviceBusTopic: 'ServiceBus',
eventHub: 'EventHubs',
cosmosDB: 'CosmosDB',
}
class AzureFunctionsPlugin extends TracingPlugin {
static id = 'azure-functions'
static operation = 'invoke'
static kind = 'server'
static type = 'serverless'
static prefix = 'tracing:datadog:azure:functions:invoke'
bindStart (ctx) {
const meta = getMetaForTrigger(ctx)
const triggerType = triggerMap[ctx.methodName]
const isHttpTrigger = triggerType === 'Http'
const isMessagingService = (triggerType === 'ServiceBus' || triggerType === 'EventHubs')
let span
if (isHttpTrigger) {
const { httpRequest } = ctx
const path = (new URL(httpRequest.url)).pathname
const req = {
method: httpRequest.method,
headers: Object.fromEntries(httpRequest.headers),
url: path,
}
// Patch the request to create web context
const webContext = web.patch(req)
webContext.config = this.config
webContext.tracer = this.tracer
webContext.paths = [path]
// Creates a standard span and an inferred proxy span if headers are present
span = web.startServerlessSpanWithInferredProxy(
this.tracer,
this.config,
this.operationName(),
req,
ctx
)
span._integrationName = 'azure-functions'
span.context().setTag('component', 'azure-functions')
span.addTags(meta)
webContext.span = span
webContext.azureFunctionCtx = ctx
ctx.webContext = webContext
} else {
// For non-HTTP triggers, use standard flow
span = this.startSpan(this.operationName(), {
service: this.serviceName(),
type: 'serverless',
meta,
childOf: null,
}, ctx)
if (isMessagingService) {
setSpanLinks(triggerType, this.tracer, span, ctx)
}
}
ctx.span = span
return ctx.currentStore
}
error (ctx) {
this.addError(ctx.error)
ctx.currentStore.span.setTag('error.message', ctx.error)
}
asyncStart (ctx) {
const { methodName, result = {}, webContext } = ctx
const triggerType = triggerMap[methodName]
// For HTTP triggers, use web utilities to finish all spans (including inferred proxy)
if (triggerType === 'Http') {
if (webContext) {
webContext.res = { statusCode: result.status }
web.finishAll(webContext, 'serverless')
}
} else {
super.finish()
}
}
configure (config) {
return super.configure(web.normalizeConfig(config))
}
}
function getMetaForTrigger ({ functionName, methodName, invocationContext }) {
let meta = {
'aas.function.name': functionName,
'aas.function.trigger': mapTriggerTag(methodName),
'span.type': 'serverless',
}
if (triggerMap[methodName] === 'ServiceBus') {
const triggerEntity = invocationContext.options.trigger.queueName || invocationContext.options.trigger.topicName
meta = {
...meta,
'messaging.message_id': invocationContext.triggerMetadata.messageId,
'messaging.operation': 'receive',
'messaging.system': 'servicebus',
'messaging.destination.name': triggerEntity,
'resource.name': `ServiceBus ${functionName}`,
'span.kind': 'consumer',
}
} else if (triggerMap[methodName] === 'EventHubs') {
const partitionContext = invocationContext.triggerMetadata.triggerPartitionContext
meta = {
...meta,
'messaging.destination.name': partitionContext.eventHubName,
'messaging.operation': 'receive',
'messaging.system': 'eventhubs',
'resource.name': `EventHubs ${functionName}`,
'span.kind': 'consumer',
}
} else if (triggerMap[methodName] === 'CosmosDB') {
meta['resource.name'] = `CosmosDB ${functionName}`
}
return meta
}
function mapTriggerTag (methodName) {
return triggerMap[methodName] || 'Unknown'
}
// message & messages & batch with cardinality of 1 == applicationProperties
// messages with cardinality of many == applicationPropertiesArray
function setSpanLinks (triggerType, tracer, span, ctx) {
const cardinality = ctx.invocationContext.options.trigger.cardinality ?? 'one'
const triggerMetadata = ctx.invocationContext.triggerMetadata
const isServiceBus = triggerType === 'ServiceBus'
const properties = isServiceBus
? triggerMetadata.applicationProperties
: triggerMetadata.properties
const propertiesArray = isServiceBus
? triggerMetadata.applicationPropertiesArray
: triggerMetadata.propertiesArray
const addLinkFromProperties = (props) => {
if (!props) return
const spanContext = tracer.extract('text_map', props)
if (spanContext) {
span.addLink({ context: spanContext })
}
}
if (cardinality === 'many' && propertiesArray?.length > 0) {
for (const prop of propertiesArray) {
addLinkFromProperties(prop)
}
} else if (cardinality === 'one') {
addLinkFromProperties(properties)
}
}
module.exports = AzureFunctionsPlugin