lambda函数,函数符

两个Lambda用户的故事 (A Tale of Two Lambda Users)

故事1:业余 (Tale #1: The Amateur)

One moment everything is fine, then … Bam! Your Lambda function raises an exception, you get alerted and everything changes instantly.

一会儿一切都好,然后……Bam! 您的Lambda函数引发异常,您将收到警报,并且所有内容都会立即更改。

Critical systems could be impacted, so it’s important that you understand the root cause quickly.

关键系统可能会受到影响,因此快速了解根本原因非常重要。

Now, this isn’t some low-volume Lambda where you can scroll through CloudWatch Logs and find the error in purely manual fashion. So instead you head to CloudWatch Insights to run a query on the log group, filtering for the error:

现在,这不是小批量的Lambda,您可以在其中滚动浏览CloudWatch Logs并以纯手工方式查找错误。 因此,您转而使用CloudWatch Insights在日志组上运行查询,以过滤错误:

CloudWatch Insights query filtering on errors
CloudWatch Insights查询错误过滤

Looks like we found our error! While helpful, unfortunately it omits any other log messages associated with the failed invocation.

看起来我们发现了错误! 虽然有用,但不幸的是,它忽略了与失败的调用相关的任何其他日志消息。

With just the information shown above maybe — just maybe — you can figure out what the root cause is. But more likely than not, you won’t be confident.

仅通过上面显示的信息,也许-也许-您就可以找出根本原因了。 但是您很有可能不会自信。

Do you tell people you aren’t sure what happened and that you’ll spend more time investigating if the issue happens again? As if!

您是否告诉人们您不确定发生了什么,并且您将花费更多的时间调查问题是否再次发生? 仿佛!

So instead you head to the CloudWatch Logs Log Stream, filter records down to the relevant timestamp, and begin manually scrolling through log messages to find the full details on the specific errored invocation.

因此,您可以转至CloudWatch Logs日志流,过滤记录到相关时间戳,然后开始手动滚动日志消息以查找有关特定错误调用的完整详细信息。

Resolution Time: 1–2 hoursLambda Enjoyment Usage Index: Low

解决时间: 1-2小时Lambda享受使用率指数:

故事2:专业人士 (Tale #2: The Professional)

Same Lambda function, same error. But this time the logging and error handling are improved. As the title implies, this involves replacing print() statements with something a ‘lil better.

相同的Lambda函数,相同的错误。 但是这次日志和错误处理得到了改善。 顾名思义,这涉及用更好的东西代替print()语句。

What is that something and what does this Lambda function look like anyway? Let’s first go through what error debugging looks like for the professional, then take a look at code. Fair?

那是什么东西,这个Lambda函数到底是什么样的? 让我们首先了解一下专业人员的错误调试,然后看一下代码。 公平?

Again, we start with an Insights query:

同样,我们从一个Insights查询开始:

CloudWatch Insights query, again, filtering on errors
CloudWatch Insights再次查询以过滤错误

And again we find the error in the logs, but unlike last time, the log event now includes the @requestId from the Lambda invocation. What this allows us to do is run a second Insights query, filtered on that requestId to see the full set of logs for the exact invocation we are interested in!

我们再次在日志中找到错误,但是与上次不同,该日志事件现在包括来自Lambda调用的@requestId 。 这使我们能够执行的是运行第二个Insights查询,该查询根据该requestId进行过滤,以查看我们感兴趣的确切调用的完整日志集!

CloudWatch Insights query filtering on @requestId
CloudWatch Insights对@requestId进行查询过滤

Now we get 5 results, which together paint the full crime scene picture of what happened for this request. Most helpfully, we immediately see the exact input passed to trigger the Lambda. From this we can either deduce what happened mentally, or run the Lambda code locally with the exact same input event to debug.

现在我们得到5个结果,这些结果共同描绘了此请求发生的全部犯罪现场图片。 最有用的是,我们立即看到传递来触发Lambda的确切输入。 由此,我们可以推断出发生的事情,或者使用完全相同的输入事件在本地运行Lambda代码进行调试。

Resolution Time: 10–20 minutesLambda Enjoyment Usage Index: High!

解决时间: 10–20分钟Lambda享受使用率指数:高!

守则揭示 (The Code Reveal)

I’d like to imagine my readers are on the edge of their seats, begging to know the difference between the Amateur and the Pro’s code from the tale above.

我想想象我的读者坐在座位上,乞求从上面的故事中了解Amateur和Pro的代码之间的区别。

Whether that’s true or not, here is the Amateur Lambda:

不管是不是真的,这是Amateur Lambda:

It is, of course, intentionally simple for illustrative purposes. Errors were generated by simply passing an event dictionary without artist as a key, for example: event = {'artisans': 'Leonardo Da Vinci'}.

当然,出于说明目的,它是故意简单的。 只需通过不带artist作为关键字的事件字典即可生成错误,例如: event = {'artisans': 'Leonardo Da Vinci'}

Now for the Professional Lambda, which performs the same basic function but improves upon the print() statements and error handling.

现在是Professional Lambda,它执行相同的基本功能,但改进了print()语句和错误处理。

Interesting! So why are we using the logging module and formatting exception tracebacks?

有趣! 那么,为什么我们要使用日志记录模块并对异常回溯进行格式化?

可爱的Lambda记录 (Lovely Lambda Logging)

First, the Lambda runtime environment for python includes a customized logger that is smart to take advantage of.

首先,适用于python的Lambda运行时环境包括一个自定义的记录器 ,可以很好地利用它。

It features a formatter that, by default, includes the aws_request_id in every log message. This is the critical feature that allows for an Insights query, like the one shown above that filters on an individual @requestId, to show the full details of one Lambda invocation.

它具有一个格式化程序 ,默认情况下,每个日志消息中都包含aws_request_id 。 这是允许Insights查询的关键功能,就像上面显示的对单个@requestId过滤的显示@requestId ,以显示一个Lambda调用的完整详细信息。

异常处理 (Exceptional Exception Handling)

Next, you are probably noticing the fancy error handling. Although intimidating looking, using sys.exec_info is the standard way to retrieve information about an exception in python.

接下来,您可能会注意到奇特的错误处理。 尽管令人生畏,但使用sys.exec_info是检索有关python中异常的信息的标准方法 。

After retrieving the exception name, value, and stacktrace , we format it into a json-dumped string so all three appear in one log message, with the keys automatically parsed into fields. This also makes it easy to create custom metrics based off specific errors without requiring complex string parsing.

检索到异常的名称,值和stacktrace之后,我们将其格式化为json转储的字符串,以便所有三个都出现在一条日志消息中,并且键自动解析为字段。 这也使基于特定错误创建自定义指标变得容易,而无需复杂的字符串解析。

Lastly, it is worth noting that in contrast, logging an exception with the default Lambda logger without any formatting results in an unfortunate multi-line traceback looks something like this:

最后,值得注意的是,相比之下,使用默认的Lambda记录器记录异常而未进行任何格式化会导致不幸的多行回溯,如下所示:

Multi-line exception traceback example
多行异常回溯示例

结语 (Wrapping Up)

I hope if your Lambda functions look more like the Amateur Lambda at the moment, this article inspires you to upgrade your dance and go Pro!

我希望目前您的Lambda函数看起来更像是业余Lambda,这篇文章可以激发您升级舞蹈并升级为Pro!

Before I let you go, I should warn that the downside to replacing print statements with proper logging is that you lose any terminal output generated from local executions of your Lambda.

在我放手之前,我应该警告说,用正确的日志记录替换打印语句的弊端是,您会丢失Lambda的本地执行所生成的任何终端输出。

There are clever ways around this involving either environment variables or some setup code in a lambda_invoke_local.py type of file. If interested, let me know and I’ll be happy to go over the details in a future article.

有许多解决方法,其中包括环境变量或lambda_invoke_local.py类型的文件中的某些设置代码。 如果有兴趣,请告诉我,我将很乐意在以后的文章中详细介绍。

Lastly, as a final bit of inspiration, instead of needing to run Cloudwatch Insights queries to debug yourself, it should be possible to set up an Alarm against the Lambda Errors metric that notifies an SNS topic when in state “In Alarm”. Another Lambda could then trigger off that SNS topic to run the same debugging Insights queries as the Pro automatically, and return the relevant logs in Slack or something.

最后,作为最后的启发,无需运行Cloudwatch Insights查询来调试自己,应该可以针对Lambda错误度量标准设置警报,该警报在处于“ In Alarm”状态时通知SNS主题。 然后,另一个Lambda可以触发该SNS主题,以自动运行与Pro相同的调试Insights查询,并以Slack或其他形式返回相关日志。

Would be cool, no?

会很酷,不是吗?

翻译自: https://towardsdatascience.com/why-you-should-never-ever-print-in-a-lambda-function-f997d684a705

lambda函数,函数符


http://www.taodudu.cc/news/show-994886.html

相关文章:

  • ai 中 统计_AI统计(第2部分)
  • twitter数据分析_Twitter上最受欢迎的数据科学文章主题
  • 是什么使波西米亚狂想曲成为杰作-数据科学视角
  • 流行编程语言_编程语言的流行度排名
  • corba的兴衰_数据科学薪酬的兴衰
  • 通才与专家_那么您准备聘请数据科学家了吗? 通才还是专家?
  • 数据科学家 数据工程师_数据科学家实际上赚了多少钱?
  • spotify歌曲下载_使用Spotify数据预测哪些“ Novidades da semana”歌曲会成为热门歌曲
  • 功能测试代码python_如何使您的Python代码更具功能性
  • 普通话测试系统_普通话
  • 动漫数据推荐系统
  • Alex Hanna博士:Google道德AI小组研究员
  • python度量学习_Python的差异度量
  • 网页视频15分钟自动暂停_在15分钟内学习网页爬取
  • django 性能优化_优化Django管理员
  • ai驱动数据安全治理_JupyterLab中的AI驱动的代码完成
  • python中定义数据结构_Python中的数据结构—简介
  • 数据质量提升_合作提高数据质量
  • 删除wallet里面登机牌_登机牌丢失问题
  • 字符串操作截取后面的字符串_对字符串的5个必知的熊猫操作
  • 数据科学家访谈录 百度网盘_您应该在数据科学访谈中向THEM提问。
  • power bi函数_在Power BI中的行上使用聚合函数
  • 大数定理 中心极限定理_中心极限定理:直观的遍历
  • 探索性数据分析(EDA)-不要问如何,不要问什么
  • 安卓代码还是xml绘制页面_我们应该绘制实际还是预测,预测还是实际还是无关紧要?
  • 云尚制片管理系统_电影制片厂的未来
  • t-sne原理解释_T-SNE解释-数学与直觉
  • js合并同类数组里面的对象_通过同类群组保留估算客户生命周期价值
  • com编程创建快捷方式中文_如何以编程方式为博客创建wordcloud?
  • 基于plotly数据可视化_如何使用Plotly进行数据可视化

lambda函数,函数符_为什么您永远不应该在Lambda函数中使用print()相关推荐

  1. c语言 lookup函数怎么用_菜鸟记502用lookup和vlookup函数核对数据的一个小栗子

    欢迎转发扩散点在看 万一您身边的朋友用得着呢? 各位朋友早上好,小菜继续和您分享经验之谈,截止今日小菜已分享400+篇经验之谈,可以文章编号或关键词进行搜索. 微信推送规则发生改变,如果您想看到小菜每 ...

  2. matlab中contourf函数怎么用_有人可以解释如何使用contourf在MATLAB中绘制这个总和吗?...

    我将首先说明,是的,这是家庭作业(我在stackoverflow上的第一个作业问题!).但是我不希望你为我解决它,我只想要一些指导! 有问题的等式是这样的: 我被告知取N = 50,phi1 = 30 ...

  3. python中forward函数的引用_调用没有.forward()的forward函数

    在看一些关于姿势估计的pytorch代码时,AlphaPose我注意到一些不熟悉的语法: 基本上,我们定义了一个Darknet类,它继承了nn.Module属性,比如:class Darknet(nn ...

  4. 计算机不会输入函数怎么办,函数不正确_电脑上文件打不开,显示函数不正确怎么解决?...

    文件是谁给的,再问问去,自己编辑的话那看有备份不.电子表的函数错误,也不会打不开,就是会提示,不会计算出来结果 电脑显示函数不正确是怎么回事 运行中输入cmd,在命令提示符下输入: for%1in(% ...

  5. 1071svm函数 r语言_如何利用R语言中的rpart函数建立决策树模型

    决策树是根据若干输入变量的值构造出一个适合的模型,以此来预测输出变量的值,并用树形结构展示出来.决策树主要有两个类别:分类树和回归树.分类树主要针对离散的目标变量,回归树则针对连续的目标变量.R语言中 ...

  6. python二元函数如何编写_利用python实现PSO算法优化二元函数

    python实现PSO算法优化二元函数,具体代码如下所示: import numpy as np import random import matplotlib.pyplot as plt from ...

  7. java泛型函数类型推断_为什么javac可以推断用作参数的函数的泛型类型参数?

    在下面的示例中,为什么编译器能够推断出在Foo.test()中第一次调用Foo.create()的通用参数,但是在第二个样例中却不能这么做?我正在使用 Java 6. public class Non ...

  8. 用python画数学函数图像教程_你知道哪些用计算机画数学函数图象的方法?

    最近正好在找画图方法,我知道的就是下面这些: 用matlab.octave.scilab(就是那几个plot命令) 这类软件 用opengl(有点杀鸡用牛刀的味道) 用gnuplot(用起来很简单.可 ...

  9. java string 占位符_驳《阿里「Java开发手册」中的1个bug》?

    前两天写了一篇关于<阿里Java开发手册中的 1 个bug>的文章,评论区有点炸锅了,基本分为两派,支持老王的和质疑老王的. 首先来说,无论是那一方,我都真诚的感谢你们.特别是「二师兄」, ...

最新文章

  1. .NET Core 6.0之读取配置文件
  2. qwe qwe qw
  3. JavaScript算法(实例十)回文数 / 立方根 / 进制转化
  4. 【华为云技术分享】【论文阅读】增量式物体检测
  5. STL之adjacent_difference
  6. 大数据之-Hadoop伪分布式_配置历史服务器---大数据之hadoop工作笔记0027
  7. Mongo分片+副本集集群搭建
  8. python模块大全doc_Python doc8包_程序模块 - PyPI - Python中文网
  9. 让textbox紧贴IME
  10. SqlDataReader.GetFloat出错,类型无法转换
  11. pic12f1823单片机入门_PIC单片机入门教程V1.1
  12. 一对一、一对多、多对多(实操)
  13. 匿名上位机v2.6和V7自定义帧代码和飞控姿态代码
  14. 华为HCIP RS题库221 451-460题
  15. ASP.NET MVC 最好的视图引擎是什么?
  16. 【跨区助手】Apex入库
  17. 正点原子提供免费开源的的连接机智云平台开发教程和实例源码
  18. 8051单片机Proteus仿真与开发实例-LM35温度传感器+ADC0804仿真
  19. rtl8811au黑苹果10.15_thinkpad E480基本完美安装黑苹果10.15.3
  20. 【码蹄集】四柱河内塔

热门文章

  1. HTML如何做个播放器图表,Web绘图神器之ECharts-ts文件播放器
  2. 用模板写单链表 尹成
  3. 关于源文件用不同的编码方式编写,会导致执行结果不一样的现象及解决方法
  4. 网易云的朋友给我这份339页的Android面经,成功入职阿里
  5. 计算机网络与网站设计知识点,计算机网络技术知识点总结-20210525075410.docx-原创力文档...
  6. 思维构造——cf1090D
  7. 数据结构学习笔记(一)——《大话数据结构》
  8. MFC学习中遇到的小问题和解决方案
  9. Spark Windows
  10. ionic最新安装教程