-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
167 lines (141 loc) · 4.4 KB
/
index.js
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
const fp = require('fastify-plugin')
const {
context,
defaultTextMapGetter,
defaultTextMapSetter,
propagation,
SpanStatusCode,
trace,
SpanKind
} = require('@opentelemetry/api')
const { name: moduleName, version: moduleVersion } = require('./package.json')
function defaultFormatSpanName (request) {
return `${request.method} ${request.routeOptions.url || ''}`.trim()
}
const defaultFormatSpanAttributes = {
request (request) {
return {
'req.method': request.raw.method,
'req.url': request.raw.url
}
},
reply (reply) {
return {
'reply.statusCode': reply.statusCode
}
},
error (error) {
return {
'error.name': error.name,
'error.message': error.message,
'error.stack': error.stack
}
}
}
const defaultSpanOptions = { kind: SpanKind.SERVER }
async function openTelemetryPlugin (fastify, opts = {}) {
const {
wrapRoutes,
exposeApi = true,
formatSpanName = defaultFormatSpanName,
ignoreRoutes = [],
propagateToReply = false,
spanOptions = defaultSpanOptions
} = opts
const shouldIgnoreRoute = typeof ignoreRoutes === 'function'
? ignoreRoutes
: path => ignoreRoutes.includes(path)
const formatSpanAttributes = {
...defaultFormatSpanAttributes,
...(opts.formatSpanAttributes || {})
}
function getContext (request) {
return contextMap.get(request) || context.active()
}
function openTelemetry () {
const request = this
return {
get activeSpan () {
return trace.getSpan(getContext(request))
},
get context () {
return getContext(request)
},
get tracer () {
return tracer
},
inject (carrier, setter = defaultTextMapSetter) {
return propagation.inject(getContext(request), carrier, setter)
},
extract (carrier, getter = defaultTextMapGetter) {
return propagation.extract(getContext(request), carrier, getter)
}
}
}
if (exposeApi) {
fastify.decorateRequest('openTelemetry', openTelemetry)
}
const contextMap = new WeakMap()
const tracer = trace.getTracer(moduleName, moduleVersion)
async function onRequest (request, reply) {
if (shouldIgnoreRoute(request.url, request.method)) return
let activeContext = context.active()
// if not running within a local span then extract the context from the headers carrier
if (!trace.getSpan(activeContext)) {
activeContext = propagation.extract(activeContext, request.headers)
}
const reqSpanOpts = typeof spanOptions === 'function' ? spanOptions(request) : spanOptions
const span = tracer.startSpan(
formatSpanName(request),
reqSpanOpts,
activeContext
)
span.setAttributes(formatSpanAttributes.request(request))
contextMap.set(request, trace.setSpan(activeContext, span))
}
function onRequestWrapRoutes (request, reply, done) {
if (
!shouldIgnoreRoute(request.url, request.method) &&
(wrapRoutes === true || (Array.isArray(wrapRoutes) && wrapRoutes.includes(request.url)))
) {
context.with(getContext(request), done)
} else {
done()
}
}
async function onResponse (request, reply) {
if (shouldIgnoreRoute(request.url, request.method)) return
const activeContext = getContext(request)
const span = trace.getSpan(activeContext)
const spanStatus = { code: SpanStatusCode.OK }
if (reply.statusCode >= 400) {
spanStatus.code = SpanStatusCode.ERROR
}
span.setAttributes(formatSpanAttributes.reply(reply))
span.setStatus(spanStatus)
span.end()
contextMap.delete(request)
}
async function onError (request, reply, error) {
if (shouldIgnoreRoute(request.url, request.method)) return
const activeContext = getContext(request)
const span = trace.getSpan(activeContext)
span.setAttributes(formatSpanAttributes.error(error))
}
async function onSend (request, reply, payload) {
const { inject } = request.openTelemetry()
const propagationHeaders = {}
inject(propagationHeaders)
reply.headers(propagationHeaders)
return payload
};
fastify.addHook('onRequest', onRequest)
if (wrapRoutes) fastify.addHook('onRequest', onRequestWrapRoutes)
fastify.addHook('onResponse', onResponse)
fastify.addHook('onError', onError)
if (propagateToReply) fastify.addHook('onSend', onSend)
}
module.exports = fp(openTelemetryPlugin, {
fastify: '4.10.x - 5.x',
name: 'fastify-opentelemetry'
})