Skip to content

Commit 53ddbc3

Browse files
authored
Merge pull request #202300 from jeanbisutti/java_runtime_attach
Add Java runtime attachment feature for Spring Boot
2 parents a9b7026 + 759563e commit 53ddbc3

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

articles/azure-monitor/app/java-in-process-agent.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Add `-javaagent:path/to/applicationinsights-agent-3.3.0.jar` to your application
6060
> [!TIP]
6161
> For help with configuring your application's JVM args, see [Tips for updating your JVM args](./java-standalone-arguments.md).
6262
63+
> [!TIP]
64+
> If you develop a Spring Boot application, you can replace the JVM argument by a programmatic configuration. More [here](./java-spring-boot.md).
65+
6366
#### Set the Application Insights connection string
6467

6568
1. There are two ways you can point the jar file to your Application Insights resource:
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Configure Azure Monitor Application Insights for Spring Boot
3+
description: How to configure Azure Monitor Application Insights for Spring Boot applications
4+
ms.topic: conceptual
5+
ms.date: 06/22/2022
6+
ms.devlang: java
7+
ms.custom: devx-track-java
8+
---
9+
10+
# Configure Azure Monitor Application Insights for Spring Boot
11+
12+
You can enable the Azure Monitor Application Insights agent for Java by adding an argument to the JVM. When you can't do this, you can use a programmatic configuration. We detail these two configurations below.
13+
14+
## Addition of a JVM argument
15+
16+
### Usual case
17+
18+
Add the JVM arg `-javaagent:path/to/applicationinsights-agent-3.3.0.jar` somewhere before `-jar`, for example:
19+
20+
```
21+
java -javaagent:path/to/applicationinsights-agent-3.3.0.jar -jar <myapp.jar>
22+
```
23+
24+
### Spring Boot via Docker entry point
25+
26+
If you're using the *exec* form, add the parameter `"-javaagent:path/to/applicationinsights-agent-3.3.0.jar"` to the parameter list somewhere before the `"-jar"` parameter, for example:
27+
28+
```
29+
ENTRYPOINT ["java", "-javaagent:path/to/applicationinsights-agent-3.3.0.jar", "-jar", "<myapp.jar>"]
30+
```
31+
32+
If you're using the *shell* form, add the JVM arg `-javaagent:path/to/applicationinsights-agent-3.3.0.jar` somewhere before `-jar`, for example:
33+
34+
```
35+
ENTRYPOINT java -javaagent:path/to/applicationinsights-agent-3.3.0.jar -jar <myapp.jar>
36+
```
37+
38+
## Programmatic configuration
39+
40+
To use the programmatic configuration and attach the Application Insights agent for Java during the application startup, you must add the following dependency.
41+
```xml
42+
<dependency>
43+
<groupId>com.microsoft.azure</groupId>
44+
<artifactId>applicationinsights-runtime-attach</artifactId>
45+
<version>3.3.0</version>
46+
</dependency>
47+
```
48+
49+
And invoke the `attach()` method of the `com.microsoft.applicationinsights.attach.ApplicationInsights` class.
50+
51+
> [!TIP]
52+
> ⚠ The invocation must be requested at the beginning of the `main` method.
53+
54+
Example:
55+
56+
```java
57+
@SpringBootApplication
58+
public class SpringBootApp {
59+
60+
public static void main(String[] args) {
61+
ApplicationInsights.attach();
62+
SpringApplication.run(SpringBootApp.class, args);
63+
}
64+
}
65+
```
66+
67+
If you want to use a JSON configuration:
68+
* The `applicationinsights.json` file has to be in the classpath
69+
* Or you can use an environmental variable or a system property, more in the _Configuration file path_ part on [this page](../app/java-standalone-config.md).
70+
71+
72+
> [!TIP]
73+
> With a programmatic configuration, the `applicationinsights.log` file containing the agent logs is located in the directory from where the JVM is launched (user directory). This default behavior can be changed (see the _Self-diagnostics_ part of [this page](../app/java-standalone-config.md)).

articles/azure-monitor/app/java-standalone-arguments.md

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,7 @@ Configure [App Services](../../app-service/configure-language-java.md#set-java-r
1616

1717
## Spring Boot
1818

19-
Add the JVM arg `-javaagent:path/to/applicationinsights-agent-3.3.0.jar` somewhere before `-jar`, for example:
20-
21-
```
22-
java -javaagent:path/to/applicationinsights-agent-3.3.0.jar -jar <myapp.jar>
23-
```
24-
25-
## Spring Boot via Docker entry point
26-
27-
If you're using the *exec* form, add the parameter `"-javaagent:path/to/applicationinsights-agent-3.3.0.jar"` to the parameter list somewhere before the `"-jar"` parameter, for example:
28-
29-
```
30-
ENTRYPOINT ["java", "-javaagent:path/to/applicationinsights-agent-3.3.0.jar", "-jar", "<myapp.jar>"]
31-
```
32-
33-
If you're using the *shell* form, add the JVM arg `-javaagent:path/to/applicationinsights-agent-3.3.0.jar` somewhere before `-jar`, for example:
34-
35-
```
36-
ENTRYPOINT java -javaagent:path/to/applicationinsights-agent-3.3.0.jar -jar <myapp.jar>
37-
```
19+
Read the Spring Boot documentation [here](../app/java-in-process-agent.md).
3820

3921
## Tomcat 8 (Linux)
4022

@@ -112,7 +94,7 @@ Add `-javaagent:path/to/applicationinsights-agent-3.3.0.jar` to the existing `jv
11294
...
11395
```
11496

115-
If you are running multiple managed servers on a single host, you will need to add `applicationinsights.agent.id` to the `system-properties` for each `server`:
97+
If you're running multiple managed servers on a single host, you'll need to add `applicationinsights.agent.id` to the `system-properties` for each `server`:
11698

11799
```xml
118100
...
@@ -134,7 +116,7 @@ If you are running multiple managed servers on a single host, you will need to a
134116
...
135117
```
136118

137-
The specified `applicationinsights.agent.id` value must be unique. It is used to create a subdirectory under the applicationinsights directory, as each JVM process needs its own local applicationinsights config and local applicationinsights log file. Also, if reporting to the central collector, the `applicationinsights.properties` file is shared by the multiple managed servers, and so the specified `applicationinsights.agent.id` is needed to override the `agent.id` setting in that shared file. `applicationinsights.agent.rollup.id` can be similarly specified in the server's `system-properties` if you need to override the `agent.rollup.id` setting per managed server.
119+
The specified `applicationinsights.agent.id` value must be unique. It's used to create a subdirectory under the application insights directory, as each JVM process needs its own local application insights config and local application insights log file. Also, if reporting to the central collector, the `applicationinsights.properties` file is shared by the multiple managed servers, and so the specified `applicationinsights.agent.id` is needed to override the `agent.id` setting in that shared file. `applicationinsights.agent.rollup.id` can be similarly specified in the server's `system-properties` if you need to override the `agent.rollup.id` setting per managed server.
138120

139121

140122
## Jetty 9
@@ -171,7 +153,7 @@ Go to **servers > WebSphere application servers > Application servers**, choose
171153
```
172154
Java and Process Management > Process definition > Java Virtual Machine
173155
```
174-
In "Generic JVM arguments" add the following:
156+
In "Generic JVM arguments" add the following JVM argument:
175157
```
176158
-javaagent:path/to/applicationinsights-agent-3.3.0.jar
177159
```
@@ -187,4 +169,4 @@ Create a new file `jvm.options` in the server directory (for example `<openliber
187169

188170
## Others
189171

190-
Please see your application server documentation on how to add JVM args.
172+
See your application server documentation on how to add JVM args.

articles/azure-monitor/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,8 @@ items:
908908
items:
909909
- name: Overview
910910
href: app/java-in-process-agent.md
911+
- name: Spring Boot
912+
href: app/java-spring-boot.md
911913
- name: Configuration options
912914
href: app/java-standalone-config.md
913915
- name: Advanced configurations

0 commit comments

Comments
 (0)