Skip to content

Commit 3da21c9

Browse files
Merge pull request #263656 from jeanbisutti/main
Programmatic examples with AI Java runtime attachment
2 parents 52067de + a361a8c commit 3da21c9

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

articles/azure-monitor/app/java-spring-boot.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,15 @@ By default, when enabling Application Insights Java programmatically, the config
9191
will be read from the classpath (`src/main/resources`, `src/test/resources`).
9292

9393
From 3.4.3, you can configure the name of a JSON file in the classpath with the `applicationinsights.runtime-attach.configuration.classpath.file` system property.
94-
For example, with `-Dapplicationinsights.runtime-attach.configuration.classpath.file=applicationinsights-dev.json`, Application Insights will use `applicationinsights-dev.json` file for configuration.
94+
For example, with `-Dapplicationinsights.runtime-attach.configuration.classpath.file=applicationinsights-dev.json`, Application Insights will use `applicationinsights-dev.json` file for configuration. To programmatically configure another file in the classpath:
95+
96+
```java
97+
public static void main(String[] args) {
98+
System.setProperty("applicationinsights.runtime-attach.configuration.classpath.file", "applicationinsights-dev.json");
99+
ApplicationInsights.attach();
100+
SpringApplication.run(PetClinicApplication.class, args);
101+
}
102+
```
95103

96104
> [!NOTE]
97105
> Spring's `application.properties` or `application.yaml` files are not supported as
@@ -100,11 +108,43 @@ For example, with `-Dapplicationinsights.runtime-attach.configuration.classpath.
100108
See [configuration file path configuration options](./java-standalone-config.md#configuration-file-path)
101109
to change the location for a file outside the classpath.
102110

103-
#### Structure of applicationinsights-dev.json
111+
To programmatically configure a file outside the classpath:
112+
```java
113+
public static void main(String[] args) {
114+
System.setProperty("applicationinsights.configuration.file", "{path}/applicationinsights-dev.json");
115+
ApplicationInsights.attach();
116+
SpringApplication.run(PetClinicApplication.class, args);
117+
}
118+
```
119+
120+
#### Programmatically configure the connection string
121+
122+
First, add the `applicationinsights-core` dependency:
123+
124+
```xml
125+
<dependency>
126+
<groupId>com.microsoft.azure</groupId>
127+
<artifactId>applicationinsights-core</artifactId>
128+
<version>3.4.19</version>
129+
</dependency>
130+
```
131+
132+
Then, call the `ConnectionString.configure` method after `ApplicationInsights.attach()`:
133+
134+
```java
135+
public static void main(String[] args) {
136+
System.setProperty("applicationinsights.configuration.file", "{path}/applicationinsights-dev.json");
137+
ApplicationInsights.attach();
138+
SpringApplication.run(PetClinicApplication.class, args);
139+
}
140+
```
141+
Alternatively, call the `ConnectionString.configure` method from a Spring component.
142+
143+
Enable connection string configured at runtime:
104144

105145
```json
106146
{
107-
"connectionString":"Your-Intrumentation-Key"
147+
"connectionStringConfiguredAtRuntime": true
108148
}
109149
```
110150

0 commit comments

Comments
 (0)