当前位置: 首页 > 软件库 > 云计算 > Serverless 系统 >

dazn-lambda-powertools

授权协议 MIT License
开发语言 JavaScript
所属分类 云计算、 Serverless 系统
软件类型 开源软件
地区 不详
投 递 者 微生昌勋
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

DAZN Lambda Powertools

dazn-lambda-powertools is a collection of middlewares, AWS clients and helper libraries that make working with lambda easier.

Motivation

Writing Lambdas often involves the bootstrapping of specific tooling, like reading and forwarding on correlation-id's, emitting logs on a lambda timeout, and more.

Re-writing and maintaining this bootstrapping logic into every individual lambda can be a pain, so to prevent this re-work we created dazn-lambda-powertools.

Usage

The quickest way to get setup is to use the opinionated pattern basic package.

npm install @dazn/lambda-powertools-pattern-basic

const wrap = require('@dazn/lambda-powertools-pattern-basic')

module.exports.handler = wrap(async (event, context) => {
  return 42
})

For more control, you can pick and choose from the individual packages.

Powertools and Middy

All of the powertool middlewares use the middy library (v2.x), and therefore adhere to the middy API.

However, the other tools such as the clients are generic.

What's in Powertools

An integrated suite of powertools for Lambda functions that reduces the effort to implement common lamdba tasks, such as dealing with correlation-ids.

  • support correlation IDs

  • debug logs are turned off in production, and are instead sampled for 1% of invocations

  • debug logging decisions are respected by all the functions on a call chain

  • HTTP requests always report both latency as well as response count metrics

Overview of available tools

  • logger: structured logging with JSON, configurable log levels, and integrates with other tools to support correlation IDs and sampling (only enable debug logs on 1% of invocations)

  • correlation IDs: create and store correlation IDs that follow the DAZN naming convention

  • correlation IDs middleware: automatically extract correlation IDs from the invocation event

  • sample logging middleware: enable debug logging for 1% of invocations, or when upstream caller has made the decision to enable debug logging

  • obfuscater middleware: allows you to obfuscate the invocation event so that sensitive data (e.g. PII) is not logged accidentally

  • log timeout middleware: logs an error message when a function invocation times out

  • stop infinite loop middleware: stops infinite loops

Client libraries

  • http client: HTTP client that automatically forwards any correlation IDs you have captured or created, and records both latency as well as response count metrics

  • CloudWatchEvents client: CloudWatchEvents client that automatically forwards any correlation IDs you have captured or created when you put events to an event bus

  • EventBridge client: EventBridge client that automatically forwards any correlation IDs you have captured or created when you put events to an event bus

  • SNS client: SNS client that automatically forwards any correlation IDs you have captured or created when you publish a message to SNS

  • SQS client: SQS client that automatically forwards any correlation IDs you have captured or created when you publish a message to SQS

  • Kinesis client: Kinesis client that automatically forwards any correlation IDs you have captured or created when you publish record(s) to a Kinesis stream

  • Firehose client: Firehose client that automatically forwards any correlation IDs you have captured or created when you publish record(s) to a Firehose delivery stream

  • Step Functions client: Step Functions client that automatically forwards any correlation IDs you have captured or created when you start an execution

  • Lambda client: Lambda client that automatically forwards any correlation IDs you have captured or created when you invokes a Lambda function directly

  • DynamoDB client: DynamoDB client that automatically forwards any correlation IDs you have captured or created when you perform put or update operations against DynamoDB. These correlation IDs are then available to functions processing these events from the table's DynamoDB Stream.

Patterns

  • basic template for a function: wrapper for your function that applies and configures the function to work well with datadog metrics and sample logging

  • obfuscate template: basic template (above) + obfuscate the invocation event so sensitive data is obfuscated in the after and onError handlers.

Installing the powertools

via NPM

Package Install command
cloudwatchevents-client npm install @dazn/lambda-powertools-cloudwatchevents-client
correlation-ids npm install @dazn/lambda-powertools-correlation-ids
dynamodb-client npm install @dazn/lambda-powertools-dynamodb-client
eventbridge-client npm install @dazn/lambda-powertools-eventbridge-client
firehose-client npm install @dazn/lambda-powertools-firehose-client
http-client npm install @dazn/lambda-powertools-http-client
kinesis-client npm install @dazn/lambda-powertools-kinesis-client
lambda-client npm install @dazn/lambda-powertools-lambda-client
logger npm install @dazn/lambda-powertools-logger
middleware-correlation-ids npm install @dazn/lambda-powertools-middleware-correlation-ids
middleware-log-timeout npm install @dazn/lambda-powertools-middleware-log-timeout
middleware-obfuscater npm install @dazn/lambda-powertools-middleware-obfuscater
middleware-sample-logging npm install @dazn/lambda-powertools-middleware-sample-logging
middleware-stop-infinite-loop npm install @dazn/lambda-powertools-middleware-stop-infinite-loop
pattern-basic npm install @dazn/lambda-powertools-pattern-basic
pattern-obfuscate npm install @dazn/lambda-powertools-pattern-obfuscate
sns-client npm install @dazn/lambda-powertools-sns-client
sqs-client npm install @dazn/lambda-powertools-sqs-client
step-functions-client npm install @dazn/lambda-powertools-step-functions-client

via Lambda layer

You can also deploy the layer via our SAR app, which you can deploy either via this page (click Deploy and follow the instructions) or using CloudFormation/Serverless framework/AWS SAM:

DaznLambdaPowertoolsLayer:
  Type: AWS::Serverless::Application
  Properties:
    Location:
      ApplicationId: arn:aws:serverlessrepo:us-east-1:570995107280:applications/dazn-lambda-powertools
      SemanticVersion: <enter latest version>

and reference the output Outputs.LayerVersion to get the ARN of the layer to reference in your function. e.g. Fn::GetAtt: [DaznLambdaPowertoolsLayer, Outputs.LayerVersion].

You can find the latest version of the SAR app in the lerna.json file here, in the version property.

Design goal

Compliance with best practices around logging and monitoring should be the default behaviour. These tools make it simple for you to do the right thing and gets out of your way as much as possible.

Individually they are useful in their own right, but together they're so much more useful!

The middlewares capture incoming correlation IDs, and the logger automatically includes them in every log message, and the other clients (HTTP, Kinesis, SNS, etc.) would also automatically forward them on to external systems.

Even if your function doesn't do anything with correlation IDs, the tools make sure that it behaves correctly as these correlation IDs flow through it.

Did you consider monkey-patching the clients instead?

Instead of forcing you to use dazn-powertools AWS clients, we could have monkey patched the AWS SDK clients (which we already do in the tests). We could also monkey patch Node's http module (like what Nock does) to intercept HTTP requests and inject correlation IDs as HTTP headers.

We could apply the monkey patching when you apply the correlation IDs middleware, and your function would "automagically" forward correlation IDs without having to use our own client libraries. That way, as a user of the tools, you could use whatever HTTP client you wish, and can use the standard SDK clients as well.

We did entertain this idea, but I wanted to leave at least one decision for you to make. The rationale is that when things go wrong (e.g. unhandled error, or bug in our wrapper code) or when they don't work as expected (e.g. you're using an AWS SDK client that we don't support yet), at least you have that one decision to start debugging (change the require statement to use the official library instead of our own to see if things things still work).

Useful commands

bootstrapping locally

Because of the inter-dependencies between packages, it can be tricky to test your changes haven't broken another package.

You can use Lerna CLI to bootstrap all the dependencies with the current local version:

lerna bootstrap

run all tests

npm test

run tests for a specific package

PKG=correlation-ids npm run test-package

create a new package

lerna create <name of package>

and follow the instruction to bootstrap the new project.

Contributing

Please read our contribution guide to see how you can contribute towards this project.

  • lambda定义 lambda是一个匿名函数(没有函数名的函数)。 在其他OOF语言中,lambda是一段匿名代码片段,在Java中结合函数式接口的含义,可以理解lambda是一类(对象,OOP思想)可以传递的匿名对象(接口实例),一段可以传递的功能代码。 lambda语法结构 语法结构 形参列表 -> lambda方法体 举例:(a,b) -> Integer.compare(a,b) 形参列表

  • 提示:自己学习帮助记忆,有问题指出,不喜勿喷! 前言 lambda: lambda表达式时jdk8推出的重要新特性之一,使用它设计的代码会更加简洁,当开发者在编写lambda表达式时,也会随之被编译成一个函数式接口。 提示:素材来源于网络整合 一、lambda是什么? 可以将lambda表达式理解为匿名函数,允许将一个函数作为另一个函数的参数,我们可以把lambda表达式理解为一段可以传递的代码(

  • In python, lambda is actually a high-order function. for example,  f = lambda x, y : x if x > y else y equals to: def f(x, y): if x > y: return x else: return y furtur more, consider we alreay

  • lambda用法 1、函数式接口 函数式接口用@FunctionalInterface注解标注的接口类,lambda必须和函数式接口的抽象函数描述一样的参数类型。 一个接口类只包含一个抽象方法,这个接口为函数式接口。(jdk8之后可以再接口类中声明其他方法,需要在方法前面添加default,即默认方法。) 关于函数式接口第一个条件: 如果一个接口只有一个抽象方法,那么这个接口是一个函数式接口; 如

  • Java 8 于 2014 年 3 月 18 日发布以来,Lambdas 现在已经成为 Java 环境中熟悉的一部分。带来了期待已久的 Lambda 表达式(又名闭包)特性。它们对我们用 Java 编程的影响比平台历史上的任何其他变化都要大。 什么是 Lambda 表达式? 在数学和计算中,Lambda 表达式通常是一个函数:对于某些或所有输入值的组合,它指定一个输出值。Java 中的 Lambd

  • 代码参考: 200行JS代码实现lambda解释器 PP大佬代码 解释器构造 一个lambda interpreter主要由一下几个方面构成: 词法分析器(Lexer):将字符流分解为符号流(token流) 语法分析器(Parser):根据语法,利用符号流构建抽象语法树AST 语法解释器/语法制导的翻译(Interpreter):遍历处理AST,进行对语法树进行求值 其他工具类: TokenTyp

  • 1、概述 Lambda表示数学符号"λ",计算机领域中的λ代表"λ演算",表达了计算机中最基本的概念:"调用"和"置换" 是一个匿名函数,可以理解为一段可以传递的代码 是JDK1.8的一个新特性 2、使用原因 Java语言是面向对象的语言,不能像函数式语言那样嵌套定义方法 Java的匿名内部类只能存在于创建它的线程中,不能运行在多线程中,无法充分利用多核的硬件优势 匿名内部类的缺点还有: 语法相对

  • 首先: yum install -y dnf-plugins-core 然后: yum config-manager --set-enabled powertools或PowerTools

 相关资料
  • 问题内容: 我正在尝试执行新的JDK 8函数式编程领域中似乎是相对基本的事情,但是我无法使其工作。我有这个工作代码: 它接受一个数字列表,并产生可以打印出来的功能列表。但是,显式强制转换为Callable似乎是多余的。在我和IntelliJ中看来。我们都同意这也应该起作用: 但是我得到一个错误: 问题答案: 您遇到了Java 8目标类型的限制,该限制适用于方法调用的 接收者 。尽管目标类型在大多数

  • 我正试图做一件在新JDK似乎是相对基本的事情 它获取一个数字列表并生成一个可以将它们打印出来的函数列表。然而,Callable的显式强制执行似乎是多余的。对我和IntelliJ来说似乎是这样。我们都同意这也应该有效: 但是,我收到一个错误:

  • 有时您可能只需要在程序中的一个位置使用一个函数,并且该函数非常简单,您可能不会给它命名,或者可能不想将它存储在符号表中,而宁愿编写一个未命名或匿名的函数。 LISP允许您编写仅在程序中遇到它们时才计算的匿名函数。 这些函数称为Lambda functions. 您可以使用lambda表达式创建此类函数。 lambda表达式的语法如下 - (lambda (parameters) body) 无法

  • 头文件: "boost/lambda/lambda.hpp" 它包括了本库的核心部分。 "boost/lambda/bind.hpp" 它定义了 bind 函数。 "boost/lambda/if.hpp" 它定义了相当于 if 的 lambda ,以及条件操作符。 "boost/lambda/loops.hpp" 它定义了循环结构(例如,while_loop 和 for_loop)。 "b

  • AWS Lambda是一种响应式云服务,可检查应用程序中的操作,并通过部署用户定义的代码(称为functions响应。 它可以自动管理多个可用区域中的计算资源,并在触发新操作时对其进行扩展。 AWS Lambda支持使用Java,Python和Node.js编写的代码,该服务可以使用Amazon Linux支持的语言(包括Bash,Go和Ruby)启动流程。 以下是使用AWS Lambda时的一些

  • Lambda表达式在Java 8中引入,并被吹捧为Java 8的最大特性.Lambda表达式有助于函数式编程,并简化了很多开发。 语法 (Syntax) lambda表达式的特征在于以下语法。 parameter -> expression body 以下是lambda表达式的重要特征。 Optional type declaration - 无需声明参数类型。 编译器可以从参数的值推断出相同的