1
1

More than 3 years have passed since last update.

Application Insights2.xでSpring Bootを可視化する

Posted at

Application Insights2.xとSpring Boot

APMとしてApplication Insights2.xを使い、Spring Bootの稼働状態を可視化する方法について解説します。
Application Insightは3.xがすでに出ていますが、正直2.xの方が使い勝手がいいです…

エージェントと設定ファイルの用意

以下の2つのファイルを準備します。

  • applicationinsights-agent-2.6.3.jar
  • AI-Agent.xml

applicationinsights-agent-2.6.3.jarは以下からダウンロードします。

AI-Agent.xml

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsightsAgent>
   <Instrumentation>
      <BuiltIn enabled="true"> 
         <!-- capture logging via Log4j 1.2, Log4j2, and Logback, default is true -->
         <Logging enabled="true" />
         <!-- capture outgoing HTTP calls performed through Apache HttpClient, OkHttp,
              and java.net.HttpURLConnection, default is true -->
         <HTTP enabled="true" /> 
         <!-- capture JDBC queries, default is true -->
         <JDBC enabled="true" />
         <!-- capture Redis calls, default is true -->
         <Jedis enabled="true" /> 
         <!-- capture query plans for JDBC queries that exceed this value (MySQL, PostgreSQL),
              default is 10000 milliseconds -->
         <MaxStatementQueryLimitInMS>1000</MaxStatementQueryLimitInMS> 
      </BuiltIn>
   </Instrumentation>
</ApplicationInsightsAgent>

コンテナで動かす場合はDockerfileに以下のように記述し、エージェントと設定ファイルをJVMに認識させます。AI-Agent.xmlはapplicationinsights-agent-2.6.3.jarと同じところに配置し、かつその場所がWORKDIRである必要があります。

Dockerfileの抜粋

COPY ./build/resources/main/applicationinsights-agent-2.6.3.jar /usr/local/applicationinsights-agent-2.6.3.jar
COPY ./build/resources/main/AI-Agent.xml /usr/local/AI-Agent.xml
WORKDIR /usr/local
CMD java -Xmx2g -javaagent:/usr/local/applicationinsights-agent-2.6.3.jar -jar /usr/local/api-SNAPSHOT.jar

依存関係の設定

依存する以下のJARをbuild.gradleで認識させる必要があります。

バージョン/2021-09-07 時点 用途 必要性
1 com.microsoft.azure:applicationinsights-spring-boot-starter 2.6.3 Application Insightsについての設定をapplication.propertiesに集約することができる 必須
2 com.microsoft.azure:azure-spring-boot-metrics-starter 2.3.5 MicrometerのラッパーでJVM内部の主要なメトリクスを自動で吸い上げてくれる 必須
3 com.microsoft.azure:applicationinsights-logging-log4j2 2.6.3 ログをテレメトリーとして送信する。一連の流れとして可視化するという意味では良いがログの量が多いと負荷がかかる ログ量次第

build.gradle

dependencies {
        implementation 'com.microsoft.azure:applicationinsights-spring-boot-starter:2.6.3'
        implementation 'com.microsoft.azure:azure-spring-boot-metrics-starter:2.3.5'
        implementation 'com.microsoft.azure:applicationinsights-logging-log4j2:2.6.3'

com.microsoft.azure:azure-spring-boot-metrics-starterの導入でNoClassDefFoundError: javax/xml/bind/JAXBExceptionが発生した場合は依存関係に以下を追加します。

implementation 'javax.xml.bind:jaxb-api:2.3.0'

送信先のApplication Insightsの指定

送信先のApplication Insightsを指定する方法は複数ありますが、環境変数を指定する方法が一番可搬性があります。

APPINSIGHTS_INSTRUMENTATIONKEY=$instrumentationKey

APPINSIGHTS_INSTRUMENTATIONKEY=e286f9f0-bbf5-430b-9f2c-xxxxxxxxxxx

データソース別にメトリクスを取得する

あとで書きます…

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1