@uoa/lambda-tracing 1.0.0-beta.2 → 1.0.1
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/dist/tracing.d.ts +1 -0
- package/dist/tracing.js +24 -21
- package/package.json +1 -1
- package/readme.md +116 -1
- package/tracing.ts +22 -18
package/dist/tracing.d.ts
CHANGED
package/dist/tracing.js
CHANGED
|
@@ -1,32 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getRequestId = void 0;
|
|
3
|
+
exports.getRequestId = exports.initializeTracing = void 0;
|
|
4
4
|
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
5
5
|
const instrumentation_aws_lambda_1 = require("@opentelemetry/instrumentation-aws-lambda");
|
|
6
6
|
const instrumentation_1 = require("@opentelemetry/instrumentation");
|
|
7
7
|
const UoaB3Propagator_1 = require("./UoaB3Propagator");
|
|
8
8
|
const provider = new sdk_trace_node_1.NodeTracerProvider();
|
|
9
|
-
provider.register({
|
|
10
|
-
propagator: new UoaB3Propagator_1.UoaB3Propagator()
|
|
11
|
-
});
|
|
12
9
|
let requestId;
|
|
13
|
-
(
|
|
14
|
-
|
|
15
|
-
new
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
10
|
+
function initializeTracing() {
|
|
11
|
+
provider.register({
|
|
12
|
+
propagator: new UoaB3Propagator_1.UoaB3Propagator()
|
|
13
|
+
});
|
|
14
|
+
(0, instrumentation_1.registerInstrumentations)({
|
|
15
|
+
instrumentations: [
|
|
16
|
+
new instrumentation_aws_lambda_1.AwsLambdaInstrumentation({
|
|
17
|
+
requestHook: (span, { event, context }) => {
|
|
18
|
+
span.setAttribute('faas.name', context.functionName);
|
|
19
|
+
requestId = context.awsRequestId;
|
|
20
|
+
},
|
|
21
|
+
responseHook: (span, { err, res }) => {
|
|
22
|
+
if (err instanceof Error)
|
|
23
|
+
span.setAttribute('faas.error', err.message);
|
|
24
|
+
if (res)
|
|
25
|
+
span.setAttribute('faas.res', res);
|
|
26
|
+
},
|
|
27
|
+
disableAwsContextPropagation: true
|
|
28
|
+
})
|
|
29
|
+
]
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.initializeTracing = initializeTracing;
|
|
30
33
|
function getRequestId() {
|
|
31
34
|
return requestId;
|
|
32
35
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,4 +1,119 @@
|
|
|
1
1
|

|
|
2
2
|
# UOA Lambda Tracing Library
|
|
3
|
+
This library contains functions to enable distributed tracing & logging in University of Auckland AWS Lambda projects.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
### Installation
|
|
7
|
+
Install the library using the command
|
|
8
|
+
```
|
|
9
|
+
npm install @uoa/lambda-tracing
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
### Setup
|
|
13
|
+
In your src folder, create a new file `tracing-wrapper.ts`\
|
|
14
|
+
In this, add the following code
|
|
15
|
+
```
|
|
16
|
+
import {initializeTracing} from "@uoa/lambda-tracing";
|
|
17
|
+
|
|
18
|
+
initializeTracing();
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
In your lambda environment.yml file, add the following `NODE_OPTIONS` environment variable in the properties map
|
|
22
|
+
```
|
|
23
|
+
lambda:
|
|
24
|
+
properties:
|
|
25
|
+
NODE_OPTIONS: --require src/tracing-wrapper
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The above ensures that the `tracing-wrapper.ts` code will be loaded before your lambda app starts, which will
|
|
29
|
+
perform the required setup for logging and distributed tracing.
|
|
30
|
+
|
|
31
|
+
### Logging
|
|
32
|
+
Whenever you want to use the logging provided in this library, simply import the logger using
|
|
33
|
+
```
|
|
34
|
+
const logger = require('@uoa/lambda-tracing/logging')(module);
|
|
35
|
+
```
|
|
36
|
+
Note: `(module)` must be passed as a parameter to this import so that the logger knows where it has been called from.
|
|
37
|
+
|
|
38
|
+
Once the logger has been imported, you can log any info using
|
|
39
|
+
```
|
|
40
|
+
logger.info('Hello Logger!');
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Logging Levels**
|
|
44
|
+
|
|
45
|
+
At UoA, we have four logging levels available to use. Ordered by decreasing priority, these are:
|
|
46
|
+
|
|
47
|
+
|Level| Logger usage |
|
|
48
|
+
|-----|--------------------|
|
|
49
|
+
|ERROR|```logger.error()```|
|
|
50
|
+
|WARN |```logger.warn()``` |
|
|
51
|
+
|INFO |```logger.info()``` |
|
|
52
|
+
|DEBUG|```logger.debug()```|
|
|
53
|
+
|
|
54
|
+
By default, any log at the `INFO` level or higher will be logged. However, this can be changed by adding another
|
|
55
|
+
environment variable `loggingLevel` in the lambda properties map of the environment.yml file. The value of this
|
|
56
|
+
specifies the lowest level that will be logged.
|
|
57
|
+
```
|
|
58
|
+
lambda:
|
|
59
|
+
properties:
|
|
60
|
+
NODE_OPTIONS: --require src/tracing-wrapper
|
|
61
|
+
loggingLevel: WARN
|
|
62
|
+
```
|
|
63
|
+
```
|
|
64
|
+
logger.debug('Will not be logged');
|
|
65
|
+
logger.info('Will not be logged');
|
|
66
|
+
logger.warn('Will be logged');
|
|
67
|
+
logger.error('Will be logged');
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Logging format**
|
|
71
|
+
|
|
72
|
+
By default, logs will be produced in the following format:
|
|
73
|
+
```
|
|
74
|
+
date [thread] level class - [[[traceId,spanId,info]]] message
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
This can also be changed by adding another environment variable `loggingPattern` in the lambda properties map
|
|
78
|
+
of the environment.yml file.
|
|
79
|
+
```
|
|
80
|
+
lambda:
|
|
81
|
+
properties:
|
|
82
|
+
NODE_OPTIONS: --require src/tracing-wrapper
|
|
83
|
+
loggingPattern: "%date - %message | %level | %class"
|
|
84
|
+
```
|
|
85
|
+
Valid logging pattern placeholders are as follows:
|
|
86
|
+
|
|
87
|
+
|Placeholder|Replacement value|Example value|
|
|
88
|
+
|-----------|-----------------|-----|
|
|
89
|
+
|%date|Date time when the message is logged in ISO8601 format|2022-05-29T21:31:11.250Z|
|
|
90
|
+
|%thread|Unused, this is only present in the default pattern to match with UoA logging standards. Value will always be ```-```|-|
|
|
91
|
+
|%level|Log level|INFO|
|
|
92
|
+
|%class|The module which logged the message|src.handle-service|
|
|
93
|
+
|%traceId|Unique Id for the request|5c0c783c965a684842608f10422fdf2c|
|
|
94
|
+
|%spanId|Unique Id for the current lambda invocation|6a8b025511ac1191|
|
|
95
|
+
|%info|Extra information to help with filtering the logs|lambdaRequestId:dc2f1b60-96af-4869-9b75-036f9ddcdb33|
|
|
96
|
+
|%message|The logged message|Hello Logger!|
|
|
97
|
+
|
|
98
|
+
### Distributed Tracing
|
|
99
|
+
There are two headers that can be passed in API calls to your lambda project using this library: ```X-B3-TraceId``` and ```X-B3-Info```.
|
|
100
|
+
If passed to a lambda project using this library, the values of %traceId and %info in the logging pattern will be the corresponding header values.
|
|
101
|
+
|
|
102
|
+
`X-B3-TraceId` should be a 32 character lowercase hex encoded string.\
|
|
103
|
+
If the passed traceId is less than 32
|
|
104
|
+
characters, it will be left padded with 0's to get to a length of 32.\
|
|
105
|
+
If this header is not passed, it will be randomly generated.
|
|
106
|
+
|
|
107
|
+
`X-B3-Info` can be any string value to provide extra information in your logs.\
|
|
108
|
+
If this header is not passed, it will be set as `lambdaRequestId:requestId` where `requestId` is the id of the current lambda invocation.
|
|
109
|
+
|
|
110
|
+
**Header Propagation**
|
|
111
|
+
|
|
112
|
+
To propagate the ```X-B3-TraceId``` and ```X-B3-Info``` headers to other APIs, you can import and use the uoaHttps module:
|
|
113
|
+
```
|
|
114
|
+
const uoaHttps = require('@uoa/lambda-tracing/uoaHttps');
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
There are two functions exposed in this module: `get()` and `request()` which will inject the additional tracing headers into the request before it is made.\
|
|
118
|
+
The usage of these is the same as the functions provided by the Node https library (see `get()` specs [here](https://nodejs.org/api/https.html#httpsgetoptions-callback) and `request()` specs [here](https://nodejs.org/api/https.html#httpsrequestoptions-callback)).
|
|
3
119
|
|
|
4
|
-
This library contains functions to enable distributed tracing & logging in Auckland University AWS Lambda projects.
|
package/tracing.ts
CHANGED
|
@@ -4,26 +4,30 @@ import {registerInstrumentations} from '@opentelemetry/instrumentation';
|
|
|
4
4
|
import {UoaB3Propagator} from "./UoaB3Propagator";
|
|
5
5
|
|
|
6
6
|
const provider = new NodeTracerProvider();
|
|
7
|
-
provider.register({
|
|
8
|
-
propagator: new UoaB3Propagator()
|
|
9
|
-
});
|
|
10
7
|
let requestId: string;
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
new
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
9
|
+
export function initializeTracing() {
|
|
10
|
+
provider.register({
|
|
11
|
+
propagator: new UoaB3Propagator()
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
registerInstrumentations({
|
|
15
|
+
instrumentations: [
|
|
16
|
+
new AwsLambdaInstrumentation({
|
|
17
|
+
requestHook: (span, { event, context }) => {
|
|
18
|
+
span.setAttribute('faas.name', context.functionName);
|
|
19
|
+
requestId = context.awsRequestId;
|
|
20
|
+
},
|
|
21
|
+
responseHook: (span, { err, res }) => {
|
|
22
|
+
if (err instanceof Error) span.setAttribute('faas.error', err.message);
|
|
23
|
+
if (res) span.setAttribute('faas.res', res);
|
|
24
|
+
},
|
|
25
|
+
disableAwsContextPropagation: true
|
|
26
|
+
})
|
|
27
|
+
]
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
}
|
|
27
31
|
|
|
28
32
|
export function getRequestId() {
|
|
29
33
|
return requestId;
|