Download PDF
Download page PythonエージェントAPIガイド.
PythonエージェントAPIガイド
PythonエージェントAPIにより、以下が可能になります。
- エージェントが自動検出しないカスタムビジネストランザクションをプログラムで定義。
- バックエンドを検出および監視するためのカスタム終了コールを作成し、エージェントにより自動検出しないようにする。
インストゥルメント化されたアプリケーションが起動し、表示されるはずのビジネストランザクションまたはバックエンドの一部が表示されない場合は、まず Python対応環境で、ご使用のフレームワークまたはイグジットポイントがサポートされていることを確認します。サポートされていない場合は、APIを使用してコードを手動でインストゥルメント化します。
カスタムビジネストランザクション
start_bt()
および end_bt()
メソッドを使用して、カスタム ビジネス トランザクションとしてモニターするコードを囲むことができます。
または、「bt」コンテキストマネージャを使用することもできます。bt
コンテキストマネージャの使用を検討します。このコンテキストマネージャで、同じコード内のビジネスアプリケーションの開始と終了を行います。たとえば、with
ステートメントでビジネスアプリケーション全体をまとめることができます。
例えば、以下のコードの場合
setup()
do_work()
teardown()
do_work()
をビジネストランザクションとして報告します。
start_bt() および end_bt()の使用
この例では、start_bt()
と end_bt()
を使用して、do_work
という名前のビジネストランザクションを作成します。
from appdynamics.agent import api as appd
setup()
bt_handle = appd.start_bt('do work')
try:
do_work()
except Exception as exc:
raise
finally:
appd.end_bt(bt_handle, exc)
teardown()
bt コンテキストマネージャの使用
ビジネストランザクションが同じコンテキストで開始および終了する場合は、bt
コンテキストマネージャを使用することもできます。以下のように、とてもシンプルです。
setup()
with bt('do work'):
do_work()
teardown()
カスタム終了コール
start_exit_call()
および end_exit_call()
メソッドを使用して、Python エージェントが自動検出しない特定のビジネストランザクションからバックエンドへのカスタム終了コールを作成できます。
ビジネストランザクションは、カスタムビジネストランザクションである必要があります。
通常自動検出されるビジネストランザクションからカスタム終了コールを作成する場合、そのビジネストランザクションを除外して自動検出されないようにし、カスタムビジネストランザクションとして作成することができます。これにより、カスタム終了コールを作成するために必要なBtHandleを取得できるようになります。ビジネストランザクションの除外については、「Python Web のカスタムマッチと除外ルールの構成」を参照してください。
以下のコードの場合
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)
独自のデータベースへの終了コールを介してクエリを送信します。
コントローラ UI で、データベースに「Financials Database
」というラベルを付ける必要があります。
バックエンドダッシュボードに表示されるバックエンドプロパティを次のように表示します。
Host:
financials-lb
Port:
3456
Vendor:
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 バックエンドへのカスタム終了コールを開始します。Flask のインポート機能を使用して、要求オブジェクトを取得し appd_get_active_bt_handle()
に渡します。
Get bt handle using the flask request context
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()
他の対応フレームワークは、要求オブジェクトを取得するためのメカニズムが異なります。