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

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在日志组上运行查询,以过滤错误:

Image for post
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查询开始:

Image for post
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进行过滤,以查看我们感兴趣的确切调用的完整日志集!

Image for post
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记录器记录异常而未进行任何格式化会导致不幸的多行回溯,如下所示:

Image for post
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.mzph.cn/news/388304.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ai 中 统计_AI统计(第2部分)

ai 中 统计Today I plan to cover the following topics: Linear independence, special matrices, and matrix decomposition.今天,我计划涵盖以下主题:线性独立性,特殊矩阵和矩阵分解。 线性独立 (Linear independence) A set of vectors …

twitter数据分析_Twitter上最受欢迎的数据科学文章主题

twitter数据分析If you’ve written data science articles or are trying to get started, finding the most popular topics is a big help in getting your articles read. Below are the steps to easily determine what these topics are using R and the results of the …

JAVA遇见HTML——JSP篇(JSP状态管理)

案例:Cookie在登录中的应用 URL编码与解码的工具类解决中文乱码的问题,这个工具类在java.net.*包里 编码:URLEncoder.encode(String s,String enc)//s:对哪个字符串进行编码,enc:用的字符集(例&…

PE文件讲解

我们大家都知道,在Windows 9x、NT、2000下,所有的可执行文件都是基于Microsoft设计的一种新的文件格式Portable Executable File Format(可移植的执行体),即PE格式。有一些时候,我们需要对这些可执行文件进…

是什么使波西米亚狂想曲成为杰作-数据科学视角

平均“命中率”是什么样的 (What an Average ‘Hit’ looks like) Before we break the song down, let us have a brief analysis of what the greatest hits of all time had in common. I have picked 1500 songs ( charting hits ) right from the ’50s to the’10s, spre…

流行编程语言_编程语言的流行度排名

流行编程语言There has never been a unanimous agreement on what the most popular programming languages are, and probably never will be. Yet we believe that there is merit in trying to come up with ways to rank the popularity of programming languages. It hel…

使用UIWebView加载网页

1、使用UIWebView加载网页 运行XCode 4.3&#xff0c;新建一个Single View Application&#xff0c;命名为WebViewDemo。 2、加载WebView 在ViewController.h添加WebView成员变量和在ViewController.m添加实现 [cpp] view plaincopyprint?#import <UIKit/UIKit.h> …

corba的兴衰_数据科学薪酬的兴衰

corba的兴衰意见 (Opinion) 目录 (Table of Contents) Introduction 介绍 Salary and Growth 薪资与增长 Summary 摘要 介绍 (Introduction) In the past five years, data science salary cumulative growth has varied between 12% in the United States, according to Glass…

10 个深恶痛绝的 Java 异常。。

异常是 Java 程序中经常遇到的问题&#xff0c;我想每一个 Java 程序员都讨厌异常&#xff0c;一 个异常就是一个 BUG&#xff0c;就要花很多时间来定位异常问题。 什么是异常及异常的分类请看这篇文章&#xff1a;一张图搞清楚 Java 异常机制。今天&#xff0c;栈长来列一下 J…

如何实施成功的数据清理流程

干净的数据是发现和洞察力的基础。 如果数据很脏&#xff0c;您的团队为分析&#xff0c;培养和可视化数据而付出的巨大努力完全是在浪费时间。 当然&#xff0c;肮脏的数据并不是新的。 它早在计算机变得普及之前就困扰着决策。 现在&#xff0c;计算机技术已普及到日常生活中…

通才与专家_那么您准备聘请数据科学家了吗? 通才还是专家?

通才与专家Throughout my 10-year career, I have seen people often spend their time and energy in passionate debates about what data science can deliver, and what data scientists do or do not do. I submit that these are the wrong questions to focus on when y…

ubuntu opengl 安装

安装相应的库&#xff1a; sudo apt-get install build-essential libgl1-mesa-dev sudo apt-get install freeglut3-dev sudo apt-get install libglew-dev libsdl2-dev libsdl2-image-dev libglm-dev libfreetype6-dev 实例&#xff1a; #include "GL/glut.h" void…

分享一病毒源代码,破坏MBR,危险!!仅供学习参考,勿运行(vc++2010已编译通过)

我在编译的时候&#xff0c;杀毒软件提示病毒并将其拦截&#xff0c;所以会导致编译不成功。 1>D:\c工程\windows\windows\MBR病毒.cpp : fatal error C1083: 无法打开编译器中间文件:“C:\Users\lenovo\AppData\Local\Temp\_CL_953b34fein”: Permission denied 1> 1>…

数据科学家 数据工程师_数据科学家实际上赚了多少钱?

数据科学家 数据工程师目录 (Table of Contents) Introduction 介绍 Junior Data Scientist 初级数据科学家 Mid-Level Data Scientist 中级数据科学家 Senior Data Scientist 资深数据科学家 Additional Compensation 额外补偿 Summary 摘要 介绍 (Introduction) The lucrativ…

spotify歌曲下载_使用Spotify数据预测哪些“ Novidades da semana”歌曲会成为热门歌曲

spotify歌曲下载TL; DR (TL;DR) Spotify is my favorite digital music service and I’m very passionate about the potential to extract meaningful insights from data. Therefore, I decided to do this article to consolidate my knowledge of some classification mod…

(第三周)周报

此作业要求https://edu.cnblogs.com/campus/nenu/2018fall/homework/2143 1.本周PSP 总计&#xff1a;1422 min 2.本周进度条 (1)代码累积折线图 (2)博文字数累积折线图 4.PSP饼状图 转载于:https://www.cnblogs.com/gongylx/p/9761852.html

功能测试代码python_如何使您的Python代码更具功能性

功能测试代码pythonFunctional programming has been getting more and more popular in recent years. Not only is it perfectly suited for tasks like data analysis and machine learning. It’s also a powerful way to make code easier to test and maintain.近年来&am…

layou split 属性

layou split&#xff1a;true - 显示侧分栏 转载于:https://www.cnblogs.com/jasonlai2016/p/9764450.html

C#Word转Html的类

C#Word转Html的类/**//******************************************************************** created: 2007/11/02 created: 2:11:2007 23:13 filename: D:C#程序练习WordToChmWordToHtml.cs file path: D:C#程序练习WordToChm file bas…

分库分表的几种常见形式以及可能遇到的难题

前言 在谈论数据库架构和数据库优化的时候&#xff0c;我们经常会听到“分库分表”、“分片”、“Sharding”…这样的关键词。让人感到高兴的是&#xff0c;这些朋友所服务的公司业务量正在&#xff08;或者即将面临&#xff09;高速增长&#xff0c;技术方面也面临着一些挑战。…