LoginSignup
6
3

More than 3 years have passed since last update.

New Relic for Lambdaを試してみる

Posted at

こんにちは。NewRelicアドベントカレンダー17日目のエントリになります。

NewRelic for Lambdaは今年リリースされた機能の一つです。Lambdaの実行時間、外部サービスとの通信や、コールドスタートなどなど見えるようです。さっそく試していきましょう!

one.newrelic.com

まずはNewRelic One(NewRelic ONEとは)のトップページにあるAWS Lambda setupから設定に進みます。

image.png

image.png

NewReicのアカウントとAWSアカウントを設定してモニタリングのためのリソースをいくつか作っていきます。
監視したいLambdaのラッパーとしてNR Wrapper、そのlambdaのログをNewRelicのシステムに効率的に流すLog ingestion Lambdaを用意していきます。
new-relic-lambda-monitoring-architecture.png
https://docs.newrelic.co.jp/docs/serverless-function-monitoring/aws-lambda-monitoring/get-started/enable-new-relic-monitoring-aws-lambda

Step 1. Configure AWS to communicate with New Relic

image.png
https://one.newrelic.com/launcher/lambda-nerdlets.launcher-lambda-onboarding

newrelic-lambda-cliを使って設定するようです。
newrelic-lambda integrations install を実行していきます。

$ newrelic-lambda integrations install --nr-account-id [NR_ACCOUNT_ID] \
                                                      --linked-account-name [LINKED_ACCOUNT_NAME_IN_NR] \
                                                      --nr-api-key <NR_API_KEY> \
                                                      --aws-region <AWS_REGION> \
                                                      --aws-profile <AWS_PROFILE>

Step 2. Install instrumentation

image.png

Serverless Frameworkプラグインも提供されているようですが、今回はLambdaレイヤーを足す方法でやってみます。

  1. newrelic-lambda-cliをインストールして
pip3 install newrelic-lambda-cli
  1. Lambdaの一覧を出力。NewRelic wrapperの導入状況を確認します
$ newrelic-lambda functions list
Function Name                                                     Runtime    Installed
----------------------------------------------------------------  ---------  -----------
...
EcsErrorNotify                                                    python3.6  No
...
  1. NewRelic wrapperをLambda関数に追加します
$ newrelic-lambda layers install \
  --function EcsErrorNotify \
  --nr-account-id ... \
  --aws-profile ... \
  --aws-region ...
✨ Install Complete ✨

Installed=Yesとなっていますね。よさそうです

$ newrelic-lambda functions list
Function Name                                                     Runtime    Installed
----------------------------------------------------------------  ---------  -----------
...
EcsErrorNotify                                                    python3.6  Yes
...

Step 3. Configure CloudWatch logs to stream to New Relic Lambda

$ newrelic-lambda subscriptions install --function EcsErrorNotify
Adding New Relic log subscription to 'EcsErrorNotify'
✨ Install Complete ✨

ここまでの設定でNewRelic for Lambdaのモニタリングができるようになります!

image.png

Lambda関数の実行時間と内部で実行している外部サービスとの通信ごとの時間が見えます。boto3でecsサービスのPOSTリクエストと、Slackへのapi通信の様子が見えますね。
image.png

ということで、

さくっとNewRelic for Lambdaの導入をしてみました。これから実際にいろいろと使ってきたいと思います!
NewRelicアドベントカレンダー17日目のエントリでした。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3