Windows Server 2019 で Datadog APM 機能をテストするためのシンプルな Python Flask アプリケーションです。
- レイテンシテスト: 設定可能な応答遅延エンドポイント(100ms〜5秒以上)
- エラーテスト: 各種HTTPエラー(400、404、500)と例外の生成
- ネスト操作: シンプルおよび複雑なネスト操作のテスト
- データベースシミュレーション: データベース操作のシミュレーション
- Webインターフェース: 全テストエンドポイントを実行できるユーザーフレンドリーなHTML画面
- Windows Server 2019
- 管理者権限
- インターネット接続(インストール用)
- Datadog Agent(APM有効化済み)
PowerShell(推奨):
powershell -ExecutionPolicy Bypass -File .\install_python.ps1またはバッチファイル:
install_python.batPowerShell:
powershell -ExecutionPolicy Bypass -File .\run_app.ps1またはバッチファイル:
run_app.bat| ファイル | 説明 |
|---|---|
install_python.ps1 |
Python 3.11 インストール用 PowerShell スクリプト |
install_python.bat |
Python 3.11 インストール用バッチスクリプト |
run_app.ps1 |
アプリケーション実行用 PowerShell スクリプト |
run_app.bat |
アプリケーション実行用バッチスクリプト |
cleanup.ps1 |
クリーンアップ用 PowerShell スクリプト |
cleanup.bat |
クリーンアップ用バッチスクリプト |
app.py |
Flask アプリケーション |
requirements.txt |
Python 依存パッケージ |
install_python.ps1:
.\install_python.ps1 [-PythonVersion "3.11.9"]run_app.ps1:
.\run_app.ps1 [-Port 5000] [-ServiceName "apm-test-python"]
[-Environment "windows-test"] [-AppVersion "1.0.0"]
[-RunInBackground]| エンドポイント | 説明 |
|---|---|
GET / |
Web インターフェース |
GET /health |
ヘルスチェック |
GET /latency/fast |
100ms 遅延 |
GET /latency/medium |
500ms 遅延 |
GET /latency/slow |
2秒 遅延 |
GET /latency/very-slow |
5秒 遅延 |
GET /latency/custom?delay=<ms> |
カスタム遅延 |
GET /error/500 |
Internal Server Error |
GET /error/404 |
Not Found Error |
GET /error/400 |
Bad Request Error |
GET /error/exception |
未処理例外 |
GET /error/random |
50%の確率でエラー |
GET /nested/simple |
シンプルなネスト操作 |
GET /nested/complex |
複雑なネスト操作 |
GET /nested/database |
DB操作シミュレーション |
http://localhost:5000 にアクセスすると、以下のテスト機能を持つ Web UI が表示されます:
アプリケーションの稼働状態を確認します。
- Fast (100ms) - 高速レスポンス
- Medium (500ms) - 中程度の遅延
- Slow (2s) - 遅いレスポンス
- Very Slow (5s) - 非常に遅いレスポンス
- カスタム遅延 - 任意のミリ秒を指定可能
- 500 Internal Error - サーバー内部エラー
- 404 Not Found - リソース未検出エラー
- 400 Bad Request - 不正リクエストエラー
- Unhandled Exception - 未処理例外
- Random Error (50%) - 50%の確率でエラー発生
- Simple Nested - シンプルなネスト操作
- Complex Nested - 複雑な多層ネスト操作
- Simulated DB Calls - データベース操作シミュレーション
複数のリクエストを同時に送信して APM の負荷時の挙動をテストします。
| オプション | 説明 |
|---|---|
| Fast (100ms) | 高速エンドポイントへの負荷テスト |
| Medium (500ms) | 中程度の遅延エンドポイントへの負荷テスト |
| Slow (2s) | 遅延エンドポイントへの負荷テスト |
| Error 500 | エラーレスポンスの負荷テスト |
| Random Error (50%) | ランダムエラーの負荷テスト |
| Health Check | ヘルスチェックの負荷テスト |
リクエスト数は 1〜100 の範囲で指定できます。
このアプリケーションは ddtrace ライブラリを使用して Datadog APM と連携します。 Windows 環境では Single Step Instrumentation (SSI) がサポートされていないため、コード内で直接 ddtrace を初期化します。
ddtraceパッケージがrequirements.txtに含まれています- アプリケーションコード内で
patch_all()を呼び出して計装を有効化 patch_all()が自動的に Flask アプリケーションを計装します- トレースデータは Datadog Agent に送信されます
注意: Windows では ddtrace-run コマンドは「OSError: Exec format error」エラーが発生するため、コード内での初期化方式を採用しています。
-
Windows に Datadog Agent をインストール
-
datadog.yamlで APM を有効化:apm_config: enabled: true
-
Datadog Agent を再起動:
Restart-Service DatadogAgent
アプリケーションは以下の環境変数を設定します:
| 変数 | デフォルト値 | 説明 |
|---|---|---|
DD_SERVICE |
apm-test-python | Datadog でのサービス名 |
DD_ENV |
windows-test | 環境名 |
DD_VERSION |
1.0.0 | アプリケーションバージョン |
DD_LOGS_INJECTION |
true | ログにトレースIDを注入 |
DD_TRACE_SAMPLE_RATE |
1 | サンプリングレート(1=100%) |
スクリプトを使用せずに手動で実行する場合:
set DD_SERVICE=apm-test-python
set DD_ENV=windows-test
set DD_VERSION=1.0.0
python app.py注意: Windows では ddtrace-run コマンドは「OSError: Exec format error」エラーが発生するため使用できません。代わりに、コード内で patch_all() を呼び出すことで APM 計装を行います。
- Datadog にログイン
- APM > Traces に移動
- サービスでフィルター:
apm-test-python - 環境でフィルター:
windows-test
新しいコマンドプロンプト/PowerShell ウィンドウを開いて環境変数を更新してください。
ポートを変更してください:
.\run_app.ps1 -Port 8080管理者として実行するか、手動でルールを作成してください:
New-NetFirewallRule -DisplayName "APM Test App" -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow-
Datadog Agent が実行中か確認:
Get-Service DatadogAgent -
Agent のステータスを確認:
"C:\Program Files\Datadog\Datadog Agent\bin\agent.exe" status -
APM セクションを確認して、トレースが受信されているか確認
-
Agent 設定で APM が有効になっているか確認:
apm_config: enabled: true
Visual C++ Build Tools が必要な場合があります:
- https://visualstudio.microsoft.com/visual-cpp-build-tools/ からダウンロード
- "Desktop development with C++" ワークロードを選択してインストール
pip install ddtraceを再実行
PowerShell(推奨):
# 基本クリーンアップ(アプリ停止 + ファイアウォールルール削除)
powershell -ExecutionPolicy Bypass -File .\cleanup.ps1
# Python パッケージも削除
powershell -ExecutionPolicy Bypass -File .\cleanup.ps1 -RemovePackages
# Python 自体もアンインストール
powershell -ExecutionPolicy Bypass -File .\cleanup.ps1 -UninstallPython
# 確認なしで実行
powershell -ExecutionPolicy Bypass -File .\cleanup.ps1 -Forceバッチファイル:
cleanup.bat| パラメータ | 説明 |
|---|---|
-RemovePackages |
Flask, ddtrace パッケージをアンインストール |
-UninstallPython |
Python をアンインストール |
-Force |
確認プロンプトをスキップ |
方法A: タスクマネージャーから
- タスクマネージャーを開く(Ctrl + Shift + Esc)
- 「詳細」タブを選択
python.exeを探して選択- 「タスクの終了」をクリック
方法B: コマンドラインから
taskkill /F /IM python.exe方法C: 特定のポートのプロセスを停止
REM ポート5000を使用しているプロセスを確認
netstat -ano | findstr :5000
REM 表示されたPIDを使って停止(例:PID 1234)
taskkill /F /PID 1234方法A: コマンドラインから
netsh advfirewall firewall delete rule name="Datadog APM Test App"方法B: GUI から
- Windows Defender ファイアウォール を開く
- 「詳細設定」をクリック
- 「受信の規則」を選択
- 「Datadog APM Test App」を探して右クリック
- 「削除」を選択
pip uninstall Flask ddtrace -y- 設定 > アプリ > アプリと機能 を開く
- 「Python 3.x」を検索
- 選択して「アンインストール」をクリック
または PowerShell から:
# Python インストールディレクトリ内の uninstall.exe を実行
C:\Python311\uninstall.exe /quietREM Python インストールディレクトリの削除
rmdir /s /q C:\Python311
REM pip キャッシュの削除
rmdir /s /q %LOCALAPPDATA%\pip\cache