PHPでも簡単 Azure Application Insights

先月末に開催された、de:code 2019でMVP パーソナル スポンサーとしてPHP用のライブラリを公開しました。

Azure Application Insights

docs.microsoft.com

Application Insights は、上記のページにも書かれている通り、複数のプラットフォームで使用できる Web 開発者向けの拡張可能なアプリケーション パフォーマンス管理 (APM) サービスです。

.NETやNode.jsなどで利用する場合は、とても簡単に様々な機能を利用できるのですが、PHP用のSDKはシンプルなライブラリなので、アプリケーションに組み込むにはある程度自前での実装が必要になります。

そこで、現在、製作中の案件などで利用するために実装を進めていたものをde:code 2019での公開に向けて、公開可能な形にまとめ直したのが、Phaiです。

Phai - Application Insights PHP middleware

github.com

Phaiは、PSR-15準拠のミドルウェアです。現在、PSR-15を標準でサポートしているフレームワークZend Expressive位であまり多くはありませんが、今後サポートする方向に進むのではないかと思います。また、各フレームワークでPSR-15をサポートするライブラリが公開されているため、問題なく利用することができます。

下記で紹介している、PSR-15 Middleware support for CakePHP は今回Phaiを開発するにあたって、PSR-15 Middleware support for Slim Framework v3を参考に私が開発したものです。

PSR-15対応ライブラリ

他にもありそうですが普段使っていないフレームワークに関しては探しきれていません...。他に何かあれば教えて下さいm(__)m

使用方法

Phaiには、Slim3とCakePHP3用のサンプルが同梱されていますが、以下に簡単な使い方を解説します。

Application Insightsの作成

Azure PortalからApplication Insightsの作成を選択し、利用するサブスクリプションやリソースグループ、リソース名、配置するリージョンなどを選択し Next を押して次に進みます。

f:id:kaz_29:20190616164220j:plain

次の画面ではタグを指定できますが、今回は特になにも設定せずに Next を押して次に進みます。

f:id:kaz_29:20190616172324j:plain

問題がなければ、 Createを押してApplication Insightsを作成します。

f:id:kaz_29:20190616172342j:plain

しばらく待つとApplication Insightsの作成が完了し、以下のような画面を見ることができます。 概要(Overview)画面に表示されている、 Instrumentation Key は後ほど使用しますのでメモしておいてください。

f:id:kaz_29:20190616164302j:plain

Phaiのサンプルを実行

以下の手順でPhaiをcloneして、CakePHPのサンプルの設定をします。

$ git clone git@github.com:kaz29/phai.git
$ cd phai/example/cakephp
$ composer install 
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 86 installs, 0 updates, 0 removals
  - Installing cakephp/plugin-installer (1.1.0): Loading from cache
  - Installing aura/intl (3.0.0): Loading from cache

....

Generating autoload files
> Cake\Composer\Installer\PluginInstaller::postAutoloadDump
> App\Console\Installer::postInstall
Created `config/app.php` file
Created `/Users/kaz/dev/labo/phai/examples/cakephp/logs` directory
Created `/Users/kaz/dev/labo/phai/examples/cakephp/tmp` directory
Created `/Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache` directory
Created `/Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache/models` directory
Created `/Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache/persistent` directory
Created `/Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache/views` directory
Created `/Users/kaz/dev/labo/phai/examples/cakephp/tmp/sessions` directory
Created `/Users/kaz/dev/labo/phai/examples/cakephp/tmp/tests` directory
Set Folder Permissions ? (Default to Y) [Y,n]? y
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache/models
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache/persistent
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/tmp/cache/views
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/tmp/sessions
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/tmp/tests
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/tmp
Permissions set on /Users/kaz/dev/labo/phai/examples/cakephp/logs
Updated Security.salt value in config/app.php

以下のように、先程作成したApplication InsightsのInstrumentation Key を指定してサンプルアプリを実行します。 Your application insights "Instrumentation Key"の部分にご自身の環境のInstrumentation Keyを指定してください。

$ env APPLICATION_INSIGHTS_KEY='Your application insights "Instrumentation Key"' ./bin/cake server 

Welcome to CakePHP v3.7.7 Console
---------------------------------------------------------------
App : src
Path: /Users/kaz/dev/labo/phai/examples/cakephp/src/
DocumentRoot: /Users/kaz/dev/labo/phai/examples/cakephp/webroot
Ini Path:
---------------------------------------------------------------
built-in server is running in http://localhost:8765/
You can exit with `CTRL-C`

正常に実行できたら、表示されているURL(http://localhost:8765/)にアクセスすると、テレメトリ情報がApplication Insightsに送信されます。

Application Insightsでのログの確認

Application Insightsの Search メニューを選択し、Click hereをクリックします。

f:id:kaz_29:20190616170214j:plain

以下のように、アクセスログが、レスポンスタイム付きで表示されます。 ポータル上に反映されるのに数分かかりますので、少しお時間をおいてから確認してください。

f:id:kaz_29:20190616172453j:plain

ここで、http://localhost:8765/pages/hoge のような存在しないページにアクセスしてみます。 すると下記のような例外のログがApplication Insightsに送信されてエラーの内容を確認することができます。

f:id:kaz_29:20190616172512j:plain

ログレベルの設定

Phaiに含まれる、標準のTelemetry_Clientをラップした、\kaz29\Phai\ApplicationInsights\Telemetry_Clientを使用し、以下のように指定することで、指定したログレベル以下のものはApplication Insightsへの転送は行われません。

環境変数などを利用して、実行環境ごとに、ログレベルを設定したり一時的にデバッグログを有効にするなどの切り替えが簡単にできるようになっています。

$client = new \kaz29\Phai\ApplicationInsights\Telemetry_Client(
    null,
    null,
    \ApplicationInsights\Channel\Contracts\Severity_Level::Warning
);

サンプルコードの簡単な解説

config/bootstrap.phpの末尾で、環境変数をもとに設定情報を保存しています。

/**
 * Initialize Phai
 */
$client = new \ApplicationInsights\Telemetry_Client();
\kaz29\Phai\Phai::initialize($client, env('APPLICATION_INSIGHTS_KEY'));
Configure::write('Phai.client', $client);

src/Application.php内で以下のように、Phaiを登録しています。

    public function middleware($middlewareQueue)
    {
        $middlewareQueue
            // Catch any exceptions in the lower layers,
            // and make an error page/response
            ->add(new ErrorHandlerMiddleware(null, Configure::read('Error')))
            // ** ここで Phaiを指定している
            ->add(new PsrMiddleware(new ApplicationInsightsMiddleware(Configure::read('Phai.client'))))
            // Handle plugin/theme assets like CakePHP normally does.
            ->add(new AssetMiddleware([
                'cacheTime' => Configure::read('Asset.cacheTime')
            ]))
            // Add routing middleware.
            // Routes collection cache enabled by default, to disable route caching
            // pass null as cacheConfig, example: `new RoutingMiddleware($this)`
            // you might want to disable this cache in case your routing is extremely simple
            ->add(new RoutingMiddleware($this, '_cake_routes_'));
        return $middlewareQueue;
    }

まとめ

いかがでしたか?とても簡単にApplication Insightsを導入できることがご理解いただけたのではないかと思います。 現状は、まだ以下のような基本的な機能のみしか利用できませんが、今後も開発を続けてもっと便利にApplication Insightsを利用できるようにしていく予定です。

  • リクエストログ
  • 例外ログ
  • PSR-3 Logger Interfaceに準拠したロガー