0.0.8 • Published 2 months ago

fastify-aws-powertools v0.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

fastify-aws-powertools

NPM Version CI

Introduction

Implementation of a Fastify plugin of AWS Lambda Powertools for Typescript to take advantage of Logger, Metrics, and Tracer services for AWS Lambda. Inspired by Middy middleware created by AWS Lambda Powertools team.

Getting Started

Install the dependency:

npm i fastify-aws-powertools

Add peer dependencies (if there are not installed yet):

npm i fastify @fastify/aws-lambda

Configure the plugin:

import fastifyAwsPowertool, {
  FastifyAwsPowertoolsOptions,
} from 'fastify-aws-powertools';
import { FastifyASyncPlugin } from 'fastify';

export const plugin: FastifyASyncPlugin = (fastify) => {
  const options: FastifyAwsPowertoolsOptions = {
    loggerServiceOptions: {
      logEvent: true,
      clearState: true,
    },
    metricsOptions: {
      captureColdStartMetric: true,
    },
    tracerOptions: {
      captureResponse: true,
    },
  };
  fastify.register(fastifyAwsPowertool, options);
};

Options

PropertyDescriptionDefault
loggerLogger instanceundefined
metricsMetrics instanceundefined
tracerTracer instanceundefined
loggerServiceOptions{logEvent?: boolean; clearState?: boolean;}undefined
metricsServiceOptions{throwOnEmptyMetrics?: boolean; defaultDimensions?: Record<[key: string]: string>; captureColdStartMetric?: boolean;}undefined
tracerServiceOptions{captureResponse?: boolean;}undefined

Logger, Metrics, and Tracer instances will be provided automatically if no options are provided.

Dependencies

Install and configure @fastify/aws-lambda to use this plugin. See @fastify/aws-lambda.

Example

// index.ts
import fastify from 'fastify';
import awsLambdaFastify from '@fastify/aws-lambda';
import { MetricUnits } from '@aws-lambda-powertools/metrics';
import fastifyAwsPowertool, {
  FastifyAwsPowertoolsOptions,
} from 'fastify-aws-powertools';

const server = fastify({
  logger: false,
});

server.register(fastifyAwsPowertool);

server.get('/', async (request, reply) => {
  request.tracer.putAnnotation('successfulBooking', true);
  request.logger.info('This is a log with an extra variable', {
    foo: 'bar',
  });
  request.metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
  request.metrics.addMetadata(
    'bookingId',
    '7051cd10-6283-11ec-90d6-0242ac120003',
  );
  request.metrics.publishStoredMetrics();

  return 'Hello world!';
});

const proxy = awsLambdaFastify(server);

export const handler = async (event: any, context: any) =>
  proxy(event, context);