Azure Spring Apps で Application Insights Java インプロセス エージェントを使用する

Note

Azure Spring Apps は、Azure Spring Cloud サービスの新しい名前です。 サービスの名前は新しくなりましたが、スクリーンショット、ビデオ、図などの資産の更新に取り組んでいる間、場所によってはしばらく古い名前が表示されます。

Spring Boot ネイティブ イメージ アプリケーションでは、Application Insights Java エージェントではなく、Spring Boot ネイティブ イメージ Java アプリケーションの Azure Monitor OpenTelemetry Distro/Application Insights プロジェクトを使用します。

この記事の適用対象: ✔️ Standard 従量課金および専用 (プレビュー) ✔️ Basic/Standard ✔️ Enterprise

この記事では、Azure Spring Apps で Application Insights Java エージェントを使用してアプリケーションを監視する方法について説明します。

この機能を使用すると、次のことができます。

  • さまざまなフィルターを使用してトレース データを検索する。
  • Spring アプリケーションの依存関係のマップを表示します。
  • 要求のパフォーマンスを確認する。
  • リアルタイムのライブ メトリックを監視する。
  • 要求の失敗を確認する。
  • アプリケーション メトリックを確認する。
  • アプリケーション ログID を確認する。

Application Insights には、次のように多くの監視可能なパースペクティブが用意されています。

  • アプリケーション マップ
  • パフォーマンス
  • 障害
  • メトリック
  • ライブ メトリック
  • 可用性
  • ログ

Application Insights 機能の使用

Application Insights 機能を有効にすると、次のことができます。

  • ナビゲーション ウィンドウで [Application Insights] を選び、Application Insights の [概要] ページを表示します。 [概要] ページには、実行中のすべてのアプリケーションの概要が表示されます。

  • [アプリケーション マップ] を選択して、アプリケーション間の呼び出しの状態を確認します。

    Application Insights の [アプリケーション マップ] ページを示す Azure portal のスクリーンショット。

  • SQL からのクエリなどの詳細を表示するには、customers-service と petclinic の間のリンクを選択します。

  • エンドポイントを選択すると、エンドポイントへの要求を行っているすべてのアプリケーションが表示されます。

  • ナビゲーション ウィンドウで [パフォーマンス] を選び、すべてのアプリケーションの操作のパフォーマンス データ、依存関係およびロールを表示します。

    Application Insights の [パフォーマンス] ページを示す Azure portal のスクリーンショット。

  • ナビゲーション ウィンドウで [エラー] を選び、アプリケーションの予期しないエラーまたは例外を確認します。

    Application Insights の [失敗] ページを示す Azure portal のスクリーンショット。

  • ナビゲーション ウィンドウで [メトリック] を選び、名前空間を選ぶと、Spring Boot メトリックとカスタム メトリックの両方が表示されます (存在する場合)。

    Application Insights の [メトリック] ページを示す Azure portal のスクリーンショット。

  • ナビゲーション ウィンドウで [ライブ メトリック] を選び、さまざまなディメンションのリアルタイム メトリックを表示します。

    Application Insights の [ライブ メトリック] ページを示す Azure portal のスクリーンショット。

  • ナビゲーション ウィンドウで [可用性] を選び、[Application Insights での可用性テスト] を作成して Web アプリの可用性と応答性を監視します。

    Application Insights の [可用性] ページを示す Azure portal のスクリーンショット。

  • ナビゲーション ウィンドウで [ログ] を選ぶと、すべてのアプリケーションのログが表示されます。また、cloud_RoleName でフィルター処理すると、1 つのアプリケーションのログが表示されます。

    Application Insights の [ログ] ページを示す Azure portal のスクリーンショット。

Azure portal を使用した Application Insights の管理

次の手順を使用して、Java In-Process Agent を有効にします。

  1. サービス インスタンスの [サービス | 概要] ページに移動し、[監視] セクションで [Application Insights] を選びます。

  2. [Application Insights を有効にする] を選択して、Azure Spring Apps で Application Insights を有効にします。

  3. Application Insights の既存のインスタンスを選択するか、新規に作成します。

  4. Application Insights を有効にすると、1 つのオプションのサンプリング レート (既定値は 10.0%) を構成できます。

    Azure Spring Apps の Application Insights ページを示す Azure portal のスクリーンショット。[Application Insights を有効にする] チェックボックスが強調表示されています。

  5. [保存] を選択して変更を保存します。

Note

異なる Azure Spring Apps インスタンスで同じ Application Insights インスタンスを使わないでください。そのようなことをすると、混合データが表示されます。

ポータルを使用して、Application Insights の現在の設定を確認または更新できます。

Azure portal を使用した Application Insights を有効にする

  1. [Application Insights] を選択します。

  2. [バインドの編集] または [バインドされていないハイパーリンク] を選択して Application Insights を有効にします。

    Azure Spring Apps インスタンスを示す Azure portal のスクリーンショット。[Application Insights] ページと [バインドの編集] オプションが強調表示されています。

  3. [Application Insights] または [サンプリング レート]を編集し、[保存] を選択します。

Application Insights を無効にする

  1. [Application Insights] を選択します。

  2. [バインドのバインド解除] を選択して Application Insights を無効にします。

    Azure Spring Apps インスタンスを示す Azure portal のスクリーンショット。[Application Insights] ページと [バインドの解除] オプションが強調表示されています。

Application Insights の設定を変更する

[Application Insights] 列の下にある名前を選択して Application Insights セクションを開きます。

Azure Spring Apps インスタンスと [Application Insights] ページを示す Azure portal のスクリーンショット。

ビルド サービスで Application Insights buildpack バインドを編集する

ビルド サービスで Application Insights buildpack バインドの現在の設定を確認して更新するには、次の手順を実行します。

  1. [ビルド サービス] を選択します。
  2. ビルダーを選択します。
  3. [バインド] 列で [編集] を選択します。

Application Insights 設定は、[バインドの種類] 列の下に一覧表示されている [applicationinsights] 項目にあります。

  1. バインドされたハイパーリンクを選択するか、楕円の [バインドの編集] を選択して、Application Insights buildpack バインドを開いて編集します。

    [既定ビルダーのバインドの編集] ペインを示す Azure portal のスクリーンショット。

  2. バインド設定を編集し、[保存] を選択します。

    [バインドの編集] ペインを示す Azure portal のスクリーンショット。

Azure CLI を使用して Application Insights を管理する

Azure CLI コマンドを使用して Application Insights を管理できます。 次のコマンドでは、<placeholder> テキストを説明されている値に置き換える必要があります。 <service-instance-name> プレースホルダーは、Azure Spring Apps インスタンスの名前を表します。

Application Insights を有効にする

Azure Spring Apps インスタンスを作成するときに Application Insights を構成するには、次のコマンドを使用します。 app-insights 引数には、Application Insights の名前またはリソース ID を指定できます。

az spring create \
    --resource-group <resource-group-name> \
    --name "service-instance-name" \
    --app-insights <name-or-resource-ID> \
    --sampling-rate <sampling-rate>
az spring create \
    --resource-group <resource-group-name> \
    --name "service-instance-name" \
    --app-insights <name-or-resource-ID> \
    --sampling-rate <sampling-rate> \
    --sku Enterprise

次の例に示すように Application Insights 接続文字列 (推奨) またはインストルメンテーション キーを使用することもできます。

az spring create \
    --resource-group <resource-group-name> \
    --name <service-instance-name> \
    --app-insights-key <connection-string-or-instrumentation-key> \
    --sampling-rate <sampling-rate>
az spring create \
    --resource-group <resource-group-name> \
    --name <service-instance-name> \
    --app-insights-key <connection-string-or-instrumentation-key> \
    --sampling-rate <sampling-rate> \
    --sku Enterprise

Application Insights を無効にする

Azure Spring Apps インスタンスを作成するときに Application Insights を無効にするには、次のコマンドを使用します。

az spring create \
    --resource-group <resource-group-name> \
    --name <service-instance-name> \
    --disable-app-insights
az spring create \
    --resource-group <resource-group-name> \
    --name <service-instance-name> \
    --disable-app-insights \
    --sku Enterprise

Application Insights の設定を確認する

既存の Azure Spring Apps インスタンスの Application Insights 設定を確認するには、次のコマンドを使用します。

az spring app-insights show \
    --resource-group <resource-group-name> \
    --name <service-instance-name>

Application Insights を更新する

接続文字列 (推奨) またはインストルメンテーション キーを使用して Application Insights を更新するには、次のコマンドを使用します。

az spring app-insights update \
    --resource-group <resource-group-name> \
    --name <service-instance-name> \
    --app-insights-key <connection-string-or-instrumentation-key> \
    --sampling-rate <sampling-rate>

リソース名または ID を使用して Application Insights を更新するには、次のコマンドを使用します。

az spring app-insights update \
    --resource-group <resource-group-name> \
    --name <service-instance-name> \
    --app-insights <name-or-resource-ID> \
    --sampling-rate <sampling-rate>

update コマンドを使用して Application Insights を無効にする

既存の Azure Spring Apps インスタンスの Application Insights を無効にするには、次のコマンドを使用します。

az spring app-insights update \
    --resource-group <resource-group-name> \
    --name <service-instance-name> \
    --disable

Application Insights buildpack バインドを管理する

このセクションは Enterprise プランのみに適用されます。また、前のセクションを補足する手順について説明します。

Azure Spring Apps Enterprise プランでは、ビルドパックのバインドを使って、Azure Application InsightsApplicationInsights 型に統合します。 詳細については、「APM 統合と CA 証明書を構成する方法」を参照してください。

Application Insights buildpack バインドを作成するには、次のコマンドを使用します。

az spring build-service builder buildpack-binding create \
    --resource-group <your-resource-group-name> \
    --service <your-service-instance-name> \
    --name <your-binding-name> \
    --builder-name <your-builder-name> \
    --type ApplicationInsights \
    --properties sampling-percentage=<your-sampling-percentage> \
                 connection-string=<your-connection-string>

すべての buildpack バインドを一覧表示し、型 ApplicationInsights の Application Insights バインドを検索するには、次のコマンドを使用します。

az spring build-service builder buildpack-binding list \
    --resource-group <your-resource-group-name> \
    --service <your-service-resource-name> \
    --builder-name <your-builder-name>

Application Insights buildpack バインドを置き換えるには、次のコマンドを使用します。

az spring build-service builder buildpack-binding set \
    --resource-group <your-resource-group-name> \
    --service <your-service-instance-name> \
    --name <your-binding-name> \
    --builder-name <your-builder-name> \
    --type ApplicationInsights \
    --properties sampling-percentage=<your-sampling-percentage> \
                 connection-string=<your-connection-string>

Application Insights buildpack バインドを取得するには、次のコマンドを使用します。

az spring build-service builder buildpack-binding show \
    --resource-group <your-resource-group-name> \
    --service <your-service-instance-name> \
    --name <your-binding-name> \
    --builder-name <your-builder-name>

Application Insights buildpack バインドを削除するには、次のコマンドを使用します。

az spring build-service builder buildpack-binding delete \
    --resource-group <your-resource-group-name> \
    --service <your-service-instance-name> \
    --name <your-binding-name> \
    --builder-name <your-builder-name>

自動化

以下のセクションでは、Bicep、Azure Resource Manager テンプレート (ARM テンプレート) または Terraform を使用して、デプロイを自動化する方法について説明します。

Bicep

Bicep ファイルを使用してデプロイするには、次の内容を main.bicep ファイルにコピーします。 詳細については、Microsoft.AppPlatform Spring/monitoringSettings を参照してください。

param springName string
param location string = resourceGroup().location

resource spring 'Microsoft.AppPlatform/Spring@2020-07-01' = {
  name: springName
  location: location
  properties: {}
}

resource monitorSetting 'Microsoft.AppPlatform/Spring/monitoringSettings@2020-11-01-preview' = {
  parent: spring
  name: 'default'
  properties: {
    appInsightsInstrumentationKey: '00000000-0000-0000-0000-000000000000'
    appInsightsSamplingRate: 88
  }
}

ARM テンプレート

ARM テンプレートを使用してデプロイするには、次の内容を azuredeploy.json ファイルにコピーします。 詳細については、Microsoft.AppPlatform Spring/monitoringSettings を参照してください。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "springName": {
      "type": "string"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.AppPlatform/Spring",
      "apiVersion": "2020-07-01",
      "name": "[parameters('springName')]",
      "location": "[parameters('location')]",
      "properties": {}
    },
    {
      "type": "Microsoft.AppPlatform/Spring/monitoringSettings",
      "apiVersion": "2020-11-01-preview",
      "name": "[format('{0}/{1}', parameters('springName'), 'default')]",
      "properties": {
        "appInsightsInstrumentationKey": "00000000-0000-0000-0000-000000000000",
        "appInsightsSamplingRate": 88
      },
      "dependsOn": [
        "[resourceId('Microsoft.AppPlatform/Spring', parameters('springName'))]"
      ]
    }
  ]
}

Terraform

Terraform デプロイの場合は、次のテンプレートを使用します。 詳細については、「azurerm_spring_cloud_service」を参照してください。

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_application_insights" "example" {
  name                = "tf-test-appinsights"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  application_type    = "web"
}

resource "azurerm_spring_cloud_service" "example" {
  name                = "example-springcloud"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  sku_name            = "S0"

  config_server_git_setting {
    uri          = "https://github.com/Azure-Samples/piggymetrics"
    label        = "config"
    search_paths = ["dir1", "dir2"]
  }

  trace {
    connection_string = azurerm_application_insights.example.connection_string
    sample_rate       = 10.0
  }

  tags = {
    Env = "staging"
  }
}

Enterprise プランの自動化はサポートが保留されています。 使用可能になり次第、ドキュメントが追加されます。

Java エージェントの更新またはアップグレード

Java エージェントは、JDK を使って定期的に更新またはアップグレードされます。そのため、次のシナリオが影響を受ける可能性があります。

Note

JDK バージョンは、四半期ごとに 1 回更新またはアップグレードされます。

  • 更新またはアップグレードの前に Java エージェントを使用していた既存のアプリケーションは影響を受けません。
  • 更新またはアップグレードの後に作成されたアプリケーションは、新しいバージョンの Java エージェントを使用します。
  • 以前に Java エージェントを使用していなかった既存のアプリケーションでは、新しいバージョンの Java エージェントを使用するには再起動または再展開が必要になります。

buildpack が更新された場合、Java エージェントは更新またはアップグレードされます。

Java エージェント構成のホット ローディング

Azure Spring Apps には、アプリケーションを再起動することなくエージェント構成の設定を調整できるホットローディング メカニズムがあります。

Note

ホット ローディングのメカニズムには、分単位の遅延があります。

  • Java エージェントが既に有効な場合、Application Insights インスタンスまたは SamplingRate 値を変更しても、アプリケーションを再起動する必要はありません。

  • Java エージェントを有効にする場合は、アプリケーションを再起動する必要があります。

  • Java エージェントを無効にすると、数分が経過した後に、アプリケーションはすべての監視データの送信を停止します。 アプリケーションを再起動すると、このエージェントを Java ランタイム環境から削除できます。

Azure Spring Apps と Application Insights 間の概念の照合

Azure Spring Apps Application Insights
App * アプリケーション マップ/ロール
* ライブ メトリック/ロール
* 失敗/ロール/クラウド ロール
* パフォーマンス/ロール/クラウド ロール
App Instance * アプリケーション マップ/ロール インスタンス
* ライブ メトリック/サーバー名
* 失敗/ロール/クラウド インスタンス
* パフォーマンス/ロール/クラウド インスタンス

Azure Spring Apps の名前 App Instance は、次のシナリオで変更または生成されます。

  • 新しいアプリケーションを作成した。
  • JAR ファイルまたはソース コードを既存のアプリケーションにデプロイした。
  • ブルー/グリーン デプロイを開始した。
  • アプリケーションを再起動した。
  • アプリケーションのデプロイを停止し、再起動した。

データが Application Insights に保存される場合、Application Insights には、Java エージェントが有効にされた後に作成またはデプロイされた Azure Spring Apps アプリ インスタンスの履歴が含まれます。 たとえば、Application Insights ポータルでは、前日に作成され、その後、特定の時間範囲内 (たとえば、過去 24 時間) に削除されたアプリケーション データを確認できます。 次のシナリオに、この仕組みを示します。

  • Java エージェントが有効になっている Azure Spring Apps から今日の午前 8 時頃にアプリケーションを作成し、その後、今日の午前 8 時 10 分頃にそのアプリケーションに JAR ファイルをデプロイしました。 テストの後、コードを変更し、今日の午前 8 時 30 分にこのアプリケーションに新しい JAR ファイルをデプロイします。 その後、休憩を取り、午前 11 時頃に戻って、Application Insights のデータを確認します。 次のものがあります。
    • 時間範囲が過去 24 時間以内の [アプリケーション マップ] の 3 つのインスタンスと、[失敗]、[パフォーマンス]、[メトリック]。
    • 時間範囲が過去 24 時間以内の [アプリケーション マップ] の 1 つのインスタンスと、[失敗]、[パフォーマンス]、[メトリック]。
    • [ライブ メトリック] の 1 つのインスタンス。
  • Java エージェントが有効になっている Azure Spring Apps から今日の午前 8 時頃にアプリケーションを作成し、その後、今日の午前 8 時 10 分頃にそのアプリケーションに JAR ファイルをデプロイしました。 今日の午前 8 時 30 分頃、別の JAR ファイルでブルー/グリーン デプロイを試みます。 現時点で、このアプリケーションに対して 2 つのデプロイがあります。 今日の午前 11:00 頃に休憩した後に、Application Insights のデータを確認します。 次のものがあります。
    • 時間範囲が過去 24 時間以内の [アプリケーション マップ] の 3 つのインスタンスと、[失敗]、[パフォーマンス]、[メトリック]。
    • 時間の範囲が過去 24 時間以内の [アプリケーション マップ] の 2 つのインスタンスと、[失敗]、[パフォーマンス]、[メトリック]。
    • [ライブ メトリック] の 2 つのインスタンス。

次のステップ