Microsoft Azure 上でアプリに対して運用監視を行うサービスとして定評のある Application Insights が Java SDK に対応 したことはご存じの方も多いと思う。今回は新たに JavaEE 向けに追加機能が提供されたので紹介する。
また、今回利用したサンプルは https://github.com/normalian/JavaEEAppInsigtsApp に配置しているので参照してほしい。
利用方法
インターセプタとして提供されるため 0.9.6 以上のバージョンの applicationinsights-web.jar を参照する(以下は pom.xml の記載例)。
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>applicationinsights-web</artifactId> <version>0.9.6</version> </dependency>
上記の jar に含まれるクラスのうち、以下を利用する。
- com.microsoft.applicationinsights.web.javaee.RequestName
- com.microsoft.applicationinsights.web.javaee.RequestNameInterceptor
まずは beans.xml に以下の様な記載をし、インターセプタを有効化する。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> <interceptors> <class>com.microsoft.applicationinsights.web.javaee.RequestNameInterceptor</class> </interceptors> </beans>
次に RequestName アノテーションを以下の様に監視対象のクラスに付与する。
package com.domain.mavenjavaeeapp1.action; import com.domain.mavenjavaeeapp1.dto.IndexViewDto; import com.microsoft.applicationinsights.web.javaee.RequestName; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; @RequestScoped @Named public class IndexAction { @Inject IndexViewDto indexViewDto; @RequestName public String do1(String arg) { System.out.println("IndexAction#do1(" + arg + ") = " + indexViewDto.getName()); return "/index.xhtml"; } public String do2() { return "0"; } }
参考となるが、利用する画面は以下になる。
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <script type="text/javascript"> //<![CDATA[ var appInsights=window.appInsights||function(config){ function s(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},r=document,f=window,e="script",o=r.createElement(e),i,u;for(o.src=config.url||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",r.getElementsByTagName(e)[0].parentNode.appendChild(o),t.cookie=r.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t }({ instrumentationKey:"your key" }); window.appInsights=appInsights; appInsights.trackPageView(); //]]> </script> <title>Facelet Title</title> </h:head> <h:body> <span>hello Facelets</span> <h:form> <div> 名前を入力して下さい <h:inputText value="#{indexViewDto.name}" /> </div> <h:commandButton value="button" action="#{indexAction.do1('some argument')}" /> </h:form> </h:body> </html>
結果
上記のアプリケーションに対してブラウザアクセスを行い、"button" のボタンを押下することで以下の様にアクセスされたアクション/HTTPメソッドが確認できる。