2.1.2 • Published 1 month ago

@uoa/lambda-tracing v2.1.2

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
1 month ago

npm (scoped)

UOA Lambda Tracing Library

This library contains functions to enable distributed tracing & logging in University of Auckland AWS Lambda projects.

Usage

Prerequisites

If deploying to Kong gateway, your lambda plugin should have the awsgateway_compatible flag set to true.

This ensures the trace headers will be passed through to your lambda project correctly, but you will now have to base64 decode the request body before using it.

Installation

Install the library using the command

npm install @uoa/lambda-tracing

Setup

In your src folder, create a new file tracing-wrapper.ts\ In this, add the following code

import {initializeTracing} from "@uoa/lambda-tracing";

initializeTracing();

In your lambda environment.yml file, add the following NODE_OPTIONS environment variable in the properties map

lambda:
  properties:
    NODE_OPTIONS: --require src/tracing-wrapper

The above ensures that the tracing-wrapper.ts code will be loaded before your lambda app starts, which will perform the required setup for logging and distributed tracing.

Logging

Whenever you want to use the logging provided in this library, simply import the logger using

const logger = require('@uoa/lambda-tracing/logging')(module);

Note: (module) must be passed as a parameter to this import so that the logger knows where it has been called from.

Once the logger has been imported, you can log any info using

logger.info('Hello Logger!');

Logging Levels

At UoA, we have four logging levels available to use. Ordered by decreasing priority, these are:

LevelLogger usage
ERRORlogger.error()
WARNlogger.warn()
INFOlogger.info()
DEBUGlogger.debug()

By default, any log at the INFO level or higher will be logged. However, this can be changed by adding another environment variable loggingLevel in the lambda properties map of the environment.yml file. The value of this specifies the lowest level that will be logged.

lambda:
  properties:
    NODE_OPTIONS: --require src/tracing-wrapper
    loggingLevel: WARN
logger.debug('Will not be logged');
logger.info('Will not be logged');
logger.warn('Will be logged');
logger.error('Will be logged');

Logging format

By default, logs will be produced in the following format:

date [thread] level class - [[[traceId,spanId,info]]] message

This can also be changed by adding another environment variable loggingPattern in the lambda properties map of the environment.yml file.

lambda:
  properties:
    NODE_OPTIONS: --require src/tracing-wrapper
    loggingPattern: "%date - %message | %level | %class"

Valid logging pattern placeholders are as follows:

PlaceholderReplacement valueExample value
%dateDate time when the message is logged in ISO8601 format2022-05-29T21:31:11.250Z
%threadUnused, this is only present in the default pattern to match with UoA logging standards. Value will always be --
%levelLog levelINFO
%classThe module which logged the messagesrc.handle-service
%traceIdUnique Id for the request5c0c783c965a684842608f10422fdf2c
%spanIdUnique Id for the current lambda invocation6a8b025511ac1191
%infoExtra information to help with filtering the logsusefulCode=12345
%messageThe logged messageHello Logger!

Distributed Tracing

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. 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.

X-B3-TraceId should be either a 16 or 32 character lowercase hex encoded string.\ If the passed traceId is less than 16 characters, it will be left padded with 0's to get to a length of 16.\ Likewise, if it is longer than 16 but less than 32 characters, it will be left padded with 0's to get to a length of 32.\ If it is longer than 32 characters, a new traceId will be generated.\ If the X-B3-TraceId and X-B3-SpanId headers are not passed, they will be randomly generated.

X-B3-Info can be any string value to provide extra information in your logs.\ The value of this header can be accessed by using the getTraceInfoHeader(): string function. Similarly, it can be set using the setTraceInfoHeader(string) function.\ These two functions can be imported using:

const {setTraceInfoHeader, getTraceInfoHeader} = require('./logging/tracing');

Header Propagation

To propagate the X-B3-TraceId and X-B3-Info headers to other APIs, you can import and use the uoaHttps module:

const uoaHttps = require('@uoa/lambda-tracing/uoaHttps');

The uoaHttps module exposes some functions to perform the primary HTTP operations. These will inject the tracing headers into requests before they are made:

doGetRequest(hostname, path, headers): Promise
doPostRequest(hostname, path, headers, data): Promise
doPutRequest(hostname, path, headers, data): Promise
doDeleteRequest(hostname, path, headers): Promise

There is also another function request() exposed in this module in case header propagation with operations other than the basic GET, POST, PUT, and DELETE are required.\ The usage of this is the same as the one provided by the Node https library (see specs here).

2.1.2

1 month ago

2.1.1

2 months ago

2.1.0

2 months ago

2.1.0-beta.2

2 months ago

2.0.2

1 year ago

2.0.0-beta.2

1 year ago

2.0.0-beta.1

1 year ago

2.0.0-beta.0

1 year ago

2.0.0-beta.6

1 year ago

2.0.0-beta.5

1 year ago

2.0.1

1 year ago

2.0.0-beta.4

1 year ago

2.0.0

1 year ago

2.0.0-beta.3

1 year ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.0-beta.3

2 years ago

1.0.0-beta.2

2 years ago

1.0.0-beta.1

2 years ago

1.0.0-beta.0

2 years ago