@uoa/lambda-tracing 1.0.0-beta.3 → 1.0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/readme.md +113 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uoa/lambda-tracing",
3
- "version": "1.0.0-beta.3",
3
+ "version": "1.0.0",
4
4
  "description": "Library for logging & distributed tracing in UoA Lambda projects",
5
5
  "repository": {
6
6
  "type": "git",
package/readme.md CHANGED
@@ -1,4 +1,116 @@
1
1
  ![npm (scoped)](https://img.shields.io/npm/v/@uoa/lambda-tracing)
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
+ At UoA, we have four logging levels available to use. Ordered by decreasing priority, these are:
45
+
46
+ |Level| Logger usage |
47
+ |-----|--------------------|
48
+ |ERROR|```logger.error()```|
49
+ |WARN |```logger.warn()``` |
50
+ |INFO |```logger.info()``` |
51
+ |DEBUG|```logger.debug()```|
52
+
53
+ By default, any log at the `INFO` level or higher will be logged. However, this can be changed by adding another
54
+ environment variable `loggingLevel` in the lambda properties map of the environment.yml file. The value of this
55
+ specifies the lowest level that will be logged.
56
+ ```
57
+ lambda:
58
+ properties:
59
+ NODE_OPTIONS: --require src/tracing-wrapper
60
+ loggingLevel: WARN
61
+ ```
62
+ ```
63
+ logger.debug('Will not be logged');
64
+ logger.info('Will not be logged');
65
+ logger.warn('Will be logged');
66
+ logger.error('Will be logged');
67
+ ```
68
+
69
+ ####Logging format
70
+ By default, logs will be produced in the following format:
71
+ ```
72
+ date [thread] level class - [[[traceId,spanId,info]]] message
73
+ ```
74
+
75
+ This can also be changed by adding another environment variable `loggingPattern` in the lambda properties map
76
+ of the environment.yml file.
77
+ ```
78
+ lambda:
79
+ properties:
80
+ NODE_OPTIONS: --require src/tracing-wrapper
81
+ loggingPattern: "%date - %message | %level | %class"
82
+ ```
83
+ Valid logging pattern placeholders are as follows:
84
+
85
+ |Placeholder|Replacement value|Example value|
86
+ |-----------|-----------------|-----|
87
+ |%date|Date time when the message is logged in ISO8601 format|2022-05-29T21:31:11.250Z|
88
+ |%thread|Unused, this is only present in the default pattern to match with UoA logging standards. Value will always be ```-```|-|
89
+ |%level|Log level|INFO|
90
+ |%class|The module which logged the message|src.handle-service|
91
+ |%traceId|Unique Id for the request|5c0c783c965a684842608f10422fdf2c|
92
+ |%spanId|Unique Id for the current lambda invocation|6a8b025511ac1191|
93
+ |%info|Extra information to help with filtering the logs|lambdaRequestId:dc2f1b60-96af-4869-9b75-036f9ddcdb33|
94
+ |%message|The logged message|Hello Logger!|
95
+
96
+ ### Distributed Tracing
97
+ 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```.
98
+ 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.
99
+
100
+ `X-B3-TraceId` should be a 32 character lowercase hex encoded string.\
101
+ If the passed traceId is less than 32
102
+ characters, it will be left padded with 0's to get to a length of 32.\
103
+ If this header is not passed, it will be randomly generated.
104
+
105
+ `X-B3-Info` can be any string value to provide extra information in your logs.\
106
+ If this header is not passed, it will be set as `lambdaRequestId:requestId` where `requestId` is the id of the current lambda invocation.
107
+
108
+ ####Header Propagation
109
+ To propagate the ```X-B3-TraceId``` and ```X-B3-Info``` headers to other APIs, you can import and use the uoaHttps module:
110
+ ```
111
+ const uoaHttps = require('@uoa/lambda-tracing/uoaHttps');
112
+ ```
113
+
114
+ 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.\
115
+ 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
116
 
4
- This library contains functions to enable distributed tracing & logging in Auckland University AWS Lambda projects.