Python サーバレストレーサ
このページでは、開発時における Python 関数のインストルメンテーション プロセスの要件と概要について説明します。これらのマニュアルで使用されている Amazon Web サービス、AWS ロゴ、AWS、およびその他の AWS マークは、米国およびその他の国における 1, Inc. またはその関連会社の商標です。Splunk AppDynamicsLambda Extension。「Splunk AppDynamicsAppDynamics AWS Lambda Extension を使用して、実行時にサーバーレス APM をインストゥルメントする 」を参照してください。
はじめる前に
セットアップが次の要件を満たしていることを確認します。
- Python 3.9 ~ 3.13 で導入されている既存の AWS Lambda 関数
- Python パッケージインストーラ(
pipバージョンは、AWS Lambda 関数の Python バージョンと一致する必要があります) - Splunk AppDynamics SaaS コントローラ 4.5.16 以降
Python サーバレストレーサのインストール
Python サーバレストレーサは、PyPi リポジトリからダウンロードできます。Python サーバレストレーサは、ローカルにインストールすることも、AWS Lambda 関数コードにパッケージ化することもできます。
ローカルへのインストール
pip install コマンドを実行して、トレーサをローカル環境にインストールします。
pip install appdynamics-lambda-tracer
トレーサをローカルにインストールする場合は、実行時にトレーサを AWS Lambda 関数にパッケージ化する必要があります。
AWS Lambda 関数にパッケージ化
次のコマンドを実行して、関数のパッケージにトレーサを含めます。
pip install --target ./package/ appdynamics-lambda-tracer
関数のパッケージ化の依存関係の詳細については、AWS のドキュメントを参照してください。
関数コードのインストゥルメント化
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()
他の対応フレームワークは、要求オブジェクトを取得するためのメカニズムが異なります。
exit コールエラーレポートの作成
ExitCallContextManager メソッドは自動的にエラーを報告します。カスタムエラーレポートを追加する場合は、report_exit_call_error メソッドを使用します。このメソッドは、次のパラメータを使用します。
error_name:エラーの名前error_message(オプション):説明的なエラーメッセージhttp_status_code(オプション):使用可能な場合は HTTP ステータスコード
次のコードサンプルは、exit コールエラーレポート メソッドを使用する方法を示しています。
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
トランザクション エラー レポートの作成
デフォルトでは、Python トレーサはトランザクション エラー レポートを自動的に検出します。トランザクションエラーを報告するために appdynamics.report_error(error_name: str, error_message: str) メソッドを使用して、これらのエラーレポートをカスタマイズできます。このメソッドは、次のパラメータを使用します。
-
error_name:エラーの名前 -
error_message(オプション):説明的なエラーメッセージ
次のコードサンプルは、トランザクション エラーレポート メソッドを使用する方法を示しています。
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
Integrate the Python Tracer with End User Monitoring
Serverless APM for AWS Lambda is designed to integrate with your existing End User Monitoring (EUM) configurations. EUM integration provides complete end-to-end visibility on the performance of your web and mobile applications, linking calls from an end-user device through your serverless functions to continue your business transactions
AWS Lambda functions can correlate EUM and AWS Lambda-originated business transactions, in conjunction with the following EUM agents:
- Browser Real User Monitoring (Browser RUM)
- Mobile Real User Monitoring (Mobile RUM)
- IoT Monitoring