Python サーバレストレーサ
このページでは、開発時における Python 関数のインストルメンテーション プロセスの要件と概要について説明します。これらのマニュアルで使用されている Amazon Web サービス、AWS ロゴ、AWS、およびその他の AWS マークは、米国およびその他の国における 1, Inc. またはその関連会社の商標です。Splunk AppDynamicsLambda Extension。「Splunk AppDynamicsAppDynamics AWS Lambda Extension を使用して、実行時にサーバーレス APM をインストゥルメントする 」を参照してください。
Before You Begin
Ensure that your setup meets the following requirements:
- Existing AWS Lambda functions implemented in .Python 3.9 to 3.13
- Python Package Installer (the
pipversion must match the Python version of your AWS Lambda function) - Splunk AppDynamics SaaS Controller >= 4.5.16
Install the Python Serverless Tracer
The Python Serverless Tracer is available for download in PyPi repository. You can install the Python Serverless Tracer locally or package it with your AWS Lambda function code.
Install Locally
Run a pip install command to install the tracer in your local environment:
pip install appdynamics-lambda-tracer
If you install the tracer locally, you need to package the tracer with your AWS Lambda function at runtime.
Package with AWS Lambda Functions
Run the following command to include the tracer in your function's package:
pip install --target ./package/ appdynamics-lambda-tracer
See AWS documentation to learn more about packaging dependencies in your functions.
関数コードのインストゥルメント化
AWS Lambda 関数をインストゥルメント化するには、次のコード行を追加します。
import appdynamics # Add Splunk AppDynamics libraries. Must be the first line of code
@appdynamics.tracer # Must come before the handler function
def my_handler(event, context):
print("Hello world!")
exit コールのインストルメンテーション
デフォルトでは、Python サーーバレストレーサは、HTTP、Amazon DynamoDB、および AWS Lambda 間の exit コールを自動的に検出します。関数によって行われた他のタイプの外部コールを可視化する場合は、Python Tracer API を使用して exit コールを作成します。「Python サーバーレストレーサ API」を参照してください。
Python サーバレストレーサ API
このページでは、Python サーバレストレーサ API を使用して、AWS Lambda 関数内のトレーサの動作を変更する方法について説明します。
カスタム exit コール
start_exit_call() および end_exit_call() メソッドを使用して、特定のビジネストランザクションから Python エージェントが自動的に検出しないバックエンドへのカスタム exit コールを作成できます。
try:
db = custom_db.connect(host='financials-lb', port=3456)
all_employees = db.query_path('/financials/employees')
individual_contributors = all_employees.filter(lambda r: r.level < 3)
salaries_by_dept = individual_contributors.sum(value='salary', group='dept', as='total')
for dept, total in salaries_by_dept.extract('dept', 'total'):
report_salary_data(dept, total)
独自のデータベースへの exit コールを介してクエリを送信します。
コントローラ UI で、データベースに「Financials Database」というラベルを付ける必要があります。
バックエンドダッシュボードに表示されるバックエンドプロパティを次のように表示します。
- ホスト
-
financials-lb - ポート
-
3456 - ベンダー
-
custom db
以下の例では、コードの別の部分に作成された「department rollup」という名前のカスタム ビジネス トランザクションで終了コールをまとめることを仮定しています。
start_exit_call() および end_exit_call()の使用
start_exit_call() と end_exit_call() を使用します。
from appdynamics.agent import api as appd
appd.init()
# Set the identifying properties
FINANCIALS_ID_PROPS = {'Host': 'financials-lb', 'Port': 3456, 'Vendor': 'custom db'}
with appd.bt('department rollup') as bt_handle:
# Start the exit call
exit_call = appd.start_exit_call(bt_handle, appd.EXIT_DB, 'Financials Database', FINANCIALS_ID_PROPS)
exc = None
try:
db = custom_db.connect(host='financials-lb', port=3456)
all_employees = db.query_path('/financials/employees')
individual_contributors = all_employees.filter(lambda r: r.level < 3)
salaries_by_dept = individual_contributors.sum(value='salary', group='dept', as='total')
for dept, total in salaries_by_dept.extract('dept', 'total'):
report_salary_data(dept, total)
except Exception as exc:
raise # Assuming something above handles exceptions for you
finally:
#End the exit call
end_exit_call(exit_call, exc)
exit_call コンテキストマネージャの使用
exit_call コンテキストマネージャを使用するほうが簡単です。
from appdynamics.agent import api as appd
appd.init()
with appd.bt('department rollup') as bt_handle:
with appd.exit_call(bt_handle, appd.EXIT_DB, 'Financials Database', FINANCIALS_ID_PROPS):
db = custom_db.connect(host='financials-lb', port=3456)
all_employees = db.query_path('/financials/employees')
individual_contributors = all_employees.filter(lambda r: r.level < 3)
salaries_by_dept = individual_contributors.sum(value='salary', group='dept', as='total')
for dept, total in salaries_by_dept.extract('dept', 'total'):
report_salary_data(dept, total)
次の例では、Python エージェントのデフォルト Flask インストルメンテーションにより自動検出されたビジネストランザクションから、Cassandra バックエンドへのカスタム exit コールを開始します。Flask のインポート機能を使用して、要求オブジェクトを取得し appd_get_active_bt_handle() に渡します。
from flask import request
from appdynamics.agent import api as appd
@app.route('/metrics/recent')
def metrics_recent():
bt = appd.get_active_bt_handle(request) # Get the active BT from the Flask request object
with appd.exit_call(bt, appd.EXIT_DB, 'cassandra time-series', {'VENDOR': 'Cassandra', 'SERVER POOL': '10.0.0.1'}):
load_recent_data_from_cassandra()
他の対応フレームワークは、要求オブジェクトを取得するためのメカニズムが異なります。
Create Exit Call Error Reports
The ExitCallContextManager method automatically reports errors. If you want to add custom error reports, use the report_exit_call_error method. The method takes the following parameters:
error_name: Name of the errorerror_message(Optional): Descriptive error messagehttp_status_code(Optional): The HTTP status code if available.
The code sample below demonstrates how you can use the exit call error report method:
import appdynamics
@appdynamics.tracer
def handler(event, context):
#Create an exit call
with appdynamics.ExitCallContextManager("DB", "DB", {"HOST": "ec2-12-123-123-12.us-west-2.compute.amazonaws.com", "PORT": "3306", "DATABASE": "movies", "VENDOR": "MYSQL"}) as ec:
movies = fetch_movies_from_mysql_db()
if movies == None:
#Create an exit call error report
ec.report_exit_call_error(error_name="DBError", error_message="Item not found") #ec is the object returned by ExitCallContextManager above
Create Transaction Error Reports
By default, the Python Tracer automatically detects transaction error reports. You can customize these error reports using the appdynamics.report_error(error_name: str, error_message: str) method to report transaction errors. The method takes the following parameters:
-
error_name: Name of the error -
error_message(Optional): Descriptive error message
The code sample below demonstrates how you can use the transaction error report method:
import appdynamics # Add AppDynamics libraries. Must be the first line of code
import requests #Add request library
@appdynamics.tracer # Must come before the handler function
def my_handler(event, context):
try:
r = requests.get('https://api.github.com/events')
except Exception as e:
appdynamics.report_error(error_name=type(e).__name__, error_message=str(e)) # Reports a transaction error
エンドユーザモニタリングと Python トレーサの統合
AWS Lambda 向けサーバレス APM は、既存のエンドユーザーモニタリング(EUM)設定と統合できるように設計されています。EUM 統合は、web およびモバイルアプリケーションのパフォーマンスに対する完全なエンドツーエンドの可視性を確保し、エンドユーザデバイスからサーバレス機能を介してエンドユーザデバイスのコールを関連付け、ビジネストランザクションを続行します。
AWS Lambda 関数は、EUM と AWS Lambda によって発信されたビジネストランザクションを、次の EUM エージェントとともに関連付けることができます。
- ブラウザ リアル ユーザー モニタリング(RUM)
- モバイル リアル ユーザー モニタリング(モバイル RUM)の利点モバイルリアルユーザーモニタリング
- IoT モニタリング