🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More
Socket
Sign inDemoInstall
Socket

fastify-aws-powertools

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-aws-powertools

[![NPM Version](https://img.shields.io/npm/v/fastify-aws-powertools.svg)](https://npmjs.org/package/fastify-aws-powertools) [![CI](https://github.com/jorgevrgs/fastify-aws-powertools/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/j

0.0.16
latest
Source
npm
Version published
Weekly downloads
62
-38.61%
Maintainers
0
Weekly downloads
 
Created
Source

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 });

FAQs

Package last updated on 24 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts