Trace the SQL executed with the application Insights java agent

I just did what the documentation says.

https://docs.microsoft.com/ja-jp/azure/azure-monitor/app/java-agent

I feel like this.

image.png

This black-painted image is from a mysterious request that took 5 minutes to complete. You can see that the SQL query is executed 4 times and there are about 2 fucking queries. Clicking on any of the queries will bring up the SQL in the command on the right.

The content of SQL is Prepared SQL probably because it uses PreparedStatement, and it seems that you can not see it until the parameter is "?". (On the contrary, even if it is buried, confidential information may be visible, so I think this is fine) I don't even know the location of the called code. But if you are an implementer, it will be useful enough.

Of course, since the DB query information is sent to Application Insights, it takes a lot of transfer, so please be careful about that point.

Premise

--Java app --Application Insights has been installed

How it works

It seems that JavaAgent is used to modify the class file when reading the class with the class loader and write the telemetry.

All you have to do is embed the JavaAgent, so you can use it without modifying the running code.

Introduction

--Download javaagent file --Create AI-Agent.xml in the same location --Add -javaagent: ... to the JVM argument --Application restart

The installation procedure is assumed to be on Azure Web App, but Java Web applications are not limited to Azure Web App and can be installed by just reading a little.

Introduce application Insights java agent

Since it is published at https://github.com/microsoft/ApplicationInsights-Java/releases/, insert the agent ** that matches the installed ** Application Insights version.

With Azure WebApp, operate like this on kudu. I think that it will not get in the way around D: \ home \ data, but please change it as appropriate.


cd D:\home\data
curl -LO https://github.com/microsoft/ApplicationInsights-Java/releases/download/2.1.1/applicationinsights-agent-2.1.1.jar

ʻCreate AI-Agent.xml` file

ʻCreate a file called AI-Agent.xml` in the same location as the ** jar file you downloaded earlier **.

You can copy and paste the following contents.

AI-Agent.xml


<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsightsAgent>
    <Instrumentation>
        <BuiltIn enabled="true"> <!--Don't forget to look at the enabled attribute here as well.-->
            <JDBC enabled="true" /> <!--Send query execution time and prepared SQL telemetry when querying with JDBC-->
        </BuiltIn>
    </Instrumentation>
</ApplicationInsightsAgent>

This time it is only JDBC, but it seems that you can trace some HTTP calls etc. in the settings.

Add -javaagent: ... to the JVM argument

Add the following to the JVM arguments: Replace the file path as appropriate.

-javaagent:D:/home/data/applicationinsights-agent-2.1.1.jar

For apps that run on Tomcat, the environment variables JAVA_OPTS and CATALINA_OPTS are fine.

For Azure Web App, use application settings or web.config from kudu. However, ** application settings take precedence **, so please take that into consideration.

Application restart

A reboot is required for the javaagent to load.

that's all.

Other

I want to suppress redundant telemetry

At present, it seems that it is not possible to send only when it takes time.

For example, a query that is known to be fast enough, or if you are using connection pooling, telemetry will be sent even with SELECT 1 when checking for rotting when taken out of the pool. If you want to prevent it, you need to write a filter. The following issue has an implementation example of Telemetry Processor.

Reference: https://github.com/microsoft/ApplicationInsights-Java/issues/837#issuecomment-471610584

Recommended Posts

Trace the SQL executed with the application Insights java agent
[Tutorial] Download Eclipse → Run the application with Java (Pleiades)
[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
Follow the link with Selenium (Java)
Run an application made with Java8 with Java6
One-JAR Java EE application with WebSphere Liberty
Collect JVM performance counters with Application Insights
Try using the Wii remote with Java
[Java] Get the date with the LocalDateTime class
Let's make a calculator application with Java ~ Create a display area in the window
Understand Java Serializable while actually running the application
[LeJOS] Let's control the EV3 motor with Java
[Java] Set the time from the browser with jsoup
Understanding the MVC framework with server-side Java 1/4 View
Understanding the MVC framework with server-side Java 3/4 Controller
Calculate the similarity score of strings with JAVA
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
[Java / PostgreSQL] Connect the WEB application to the database
Java starting with JShell-A peek into the Java world
Understanding the MVC framework with server-side Java 2/4 Model