0.0.16 • Published 9 months ago

fastify-aws-powertools v0.0.16

Weekly downloads
-
License
MIT
Repository
github
Last release
9 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 from 'fastify-aws-powertools';
import type { FastifyAwsPowertoolsOptions } from 'fastify-aws-powertools';
import type { FastifyASyncPlugin } from 'fastify';

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

Also, available each plugin separately:

import { fastifyAwsPowertoolsLogger } from 'fastify-aws-powertools';
import type { FastifyAwsPowertoolsOptions } from 'fastify-aws-powertools';
import type { FastifyASyncPlugin } from 'fastify';

export const plugin: FastifyASyncPlugin = async (fastify) => {
  const options: FastifyAwsPowertoolsOptions = {
    loggerOptions: {
      logEvent: true,
      clearState: true,
    }
  };
  fastify.register(fastifyAwsPowertoolsLogger, options);
};

Dependencies

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

Options

Logger, Metrics, and Tracer instances will be automatically provided if either (logger|metrics|tracer) or (logger|metrics|tracer)InstanceOptions are provided.

Logger Plugin

PropertyDescriptionDefault
loggerLogger instance, otherwise provide loggerInstanceOptionsundefined
loggerOptions{logEvent?: boolean; resetKeys?: boolean;}undefined
loggerInstanceOptionsSee ConstructorOptionsundefined

Metrics Plugin

PropertyDescriptionDefault
metricsMetrics instance, otherwise provide metricsInstanceOptionsundefined
metricsOptions{throwOnEmptyMetrics?: boolean; defaultDimensions?: Record<[key: string]: string>; captureColdStartMetric?: boolean;}undefined
metricsInstanceOptionsSee MetricsOptionsundefined

Tracer Plugin

PropertyDescriptionDefault
tracerTracer instance, otherwise provide tracerInstanceOptionsundefined
tracerOptions{captureResponse?: boolean;}undefined
tracerInstanceOptionsSee TracerOptionsundefined

PowerTools Plugin

Use fastifyAwsPowertoolsPlugin if you want to use all the plugins.

Example

Simple example using all the plugins:

// index.ts
import fastify from 'fastify';
import awsLambdaFastify from '@fastify/aws-lambda';
import { MetricUnits } from '@aws-lambda-powertools/metrics';
import fastifyAwsPowertool, {
  type 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);

Additional examples can be found in the examples folder.

import fastifyAwsPowertoolsPlugin, { type FastifyAwsPowertoolsOptions, Logger as FastifyLogger } from 'fastify-aws-powertools';
import { Logger } from '@aws-lambda-powertools/logger';
import { Metrics } from '@aws-lambda-powertools/metrics';
import { Tracer } from '@aws-lambda-powertools/tracer';
import { Fastify, type FastifyASyncPlugin } from 'fastify';


const serviceName = 'my-service';

const logger = new Logger({
    serviceName,
  });

const metrics = new Metrics({
  serviceName,
  namespace: serviceName,
});

const tracer = new Tracer({
  serviceName,
  captureHTTPsRequests: true,
});

tracer.provider.setLogger(logger);

export const myPlugin: FastifyASyncPlugin = async (fastify) => {

  const options: FastifyAwsPowertoolsOptions = {
    logger,
    metrics,
    tracer,
    loggerOptions: {
      logEvent: true,
      clearState: true,
    },
    metricsOptions: {
      captureColdStartMetric: true,
    },
    tracerOptions: {
      captureResponse: true,
    },
  };
  fastify.register(fastifyAwsPowertoolsPlugin, options);
};

const logger = new FastifyLogger({
  serviceName
});

const app = Fastify({ logger });
0.0.10

10 months ago

0.0.12

9 months ago

0.0.13

9 months ago

0.0.14

9 months ago

0.0.15

9 months ago

0.0.9

10 months ago

0.0.16

9 months ago

0.0.8

1 year ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.2

3 years ago

0.0.1

3 years ago