@teamkeel/functions-runtime 0.314.0 → 0.315.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/handleRequest.js +53 -17
- package/src/tracing.js +1 -0
package/package.json
CHANGED
package/src/handleRequest.js
CHANGED
|
@@ -13,28 +13,51 @@ const {
|
|
|
13
13
|
const { PROTO_ACTION_TYPES } = require("./consts");
|
|
14
14
|
|
|
15
15
|
const { errorToJSONRPCResponse, RuntimeErrors } = require("./errors");
|
|
16
|
+
const opentelemetry = require("@opentelemetry/api");
|
|
17
|
+
const { serviceName } = require("./tracing");
|
|
16
18
|
|
|
17
19
|
// Generic handler function that is agnostic to runtime environment (local or lambda)
|
|
18
20
|
// to execute a custom function based on the contents of a jsonrpc-2.0 payload object.
|
|
19
21
|
// To read more about jsonrpc request and response shapes, please read https://www.jsonrpc.org/specification
|
|
20
22
|
async function handleRequest(request, config) {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
23
|
+
const activeContext = opentelemetry.propagation.extract(
|
|
24
|
+
opentelemetry.context.active(),
|
|
25
|
+
// "?." is so we don't have to provide this field on tests
|
|
26
|
+
request.meta?.tracing
|
|
27
|
+
);
|
|
28
|
+
const tracer = opentelemetry.trace.getTracer(serviceName);
|
|
29
|
+
let span = tracer.startSpan(
|
|
30
|
+
`Function/${request.method}`,
|
|
31
|
+
{ attributes: {} },
|
|
32
|
+
activeContext
|
|
33
|
+
);
|
|
34
|
+
opentelemetry.trace.setSpan(activeContext, span);
|
|
35
|
+
|
|
36
|
+
//
|
|
37
|
+
// WARNING: Nothing should be done before this try block, as the finally closes the span
|
|
38
|
+
//
|
|
37
39
|
try {
|
|
40
|
+
const {
|
|
41
|
+
createFunctionAPI,
|
|
42
|
+
createContextAPI,
|
|
43
|
+
functions,
|
|
44
|
+
permissions,
|
|
45
|
+
actionTypes,
|
|
46
|
+
} = config;
|
|
47
|
+
|
|
48
|
+
if (!(request.method in functions)) {
|
|
49
|
+
const message = `no corresponding function found for '${request.method}'`;
|
|
50
|
+
span.setStatus({
|
|
51
|
+
code: opentelemetry.SpanStatusCode.ERROR,
|
|
52
|
+
message: message,
|
|
53
|
+
});
|
|
54
|
+
return createJSONRPCErrorResponse(
|
|
55
|
+
request.id,
|
|
56
|
+
JSONRPCErrorCode.MethodNotFound,
|
|
57
|
+
message
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
38
61
|
// headers reference passed to custom function where object data can be modified
|
|
39
62
|
const headers = new Headers();
|
|
40
63
|
|
|
@@ -128,14 +151,27 @@ async function handleRequest(request, config) {
|
|
|
128
151
|
return response;
|
|
129
152
|
} catch (e) {
|
|
130
153
|
if (e instanceof Error) {
|
|
154
|
+
span.recordException(e);
|
|
155
|
+
span.setStatus({
|
|
156
|
+
code: opentelemetry.SpanStatusCode.ERROR,
|
|
157
|
+
message: e.message,
|
|
158
|
+
});
|
|
131
159
|
return errorToJSONRPCResponse(request, e);
|
|
132
160
|
}
|
|
133
161
|
|
|
162
|
+
const message = JSON.stringify(e);
|
|
163
|
+
|
|
164
|
+
span.setStatus({
|
|
165
|
+
code: opentelemetry.SpanStatusCode.ERROR,
|
|
166
|
+
message: message,
|
|
167
|
+
});
|
|
134
168
|
return createJSONRPCErrorResponse(
|
|
135
169
|
request.id,
|
|
136
170
|
RuntimeErrors.UnknownError,
|
|
137
|
-
|
|
171
|
+
message
|
|
138
172
|
);
|
|
173
|
+
} finally {
|
|
174
|
+
span.end();
|
|
139
175
|
}
|
|
140
176
|
}
|
|
141
177
|
|