Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lenisha/spring-demo-monitor
SpringBoot - App Insights integration for Azure Web App
https://github.com/lenisha/spring-demo-monitor
appinsights azure-app-service azure-application-insights spring-boot
Last synced: about 1 month ago
JSON representation
SpringBoot - App Insights integration for Azure Web App
- Host: GitHub
- URL: https://github.com/lenisha/spring-demo-monitor
- Owner: lenisha
- Created: 2019-05-29T18:47:53.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-08T20:00:05.000Z (over 3 years ago)
- Last Synced: 2024-10-06T18:44:58.561Z (about 1 month ago)
- Topics: appinsights, azure-app-service, azure-application-insights, spring-boot
- Language: Java
- Homepage:
- Size: 13.5 MB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Getting Started with SpringBoot Logging and App Insights on Azure AppService (Windows)
This repo has demonstrates using App Insights Logging Appenders for both `log4j` and `logback` logging frameworks.
### LogBack
- Add required libraries to enable integratiob with App Insights```xml
com.microsoft.azure
applicationinsights-spring-boot-starter
1.1.2com.microsoft.azure
applicationinsights-logging-logback
2.3.1```
where `applicationinsights-spring-boot-starter` is enabling telemetry data to flow to AppInsights,
and `applicationinsights-logging-logback` is providing LogBack AppInsights appender.
Logback is default Logging framework for SpringBoot and is part of spring web starter.- Add AI instrumentation keys to `application.properties` pointing to environment variable (it is available in Application Settings if AI was enabled for Web App)
```
azure.application-insights.instrumentation-key=${APPINSIGHTS_INSTRUMENTATIONKEY}
```- Add AppInsights Appender to logback config , typically `logback-spring.xml` file under `main\resources`
instrumentation key would be sourced from spring boot properties```xml
${INSTRUMENTATION_KEY}
```- Add `web.config` that is used to start SpringBoot JAR in Azure App Service
Environment variable `logging.file` is used by default by `FILE` appender using by springboot default config```xml
```
### Log4J
Refer to `log4j` branch to see the config, it requires a bit more config comparing to logback
- add required libraries to `pom.xml`, exclude default Logback injected in spring starter and include log4j specific starter.
```xml
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
com.microsoft.azure
applicationinsights-spring-boot-starter
1.1.2
com.microsoft.azure
applicationinsights-logging-log4j2
2.3.1
```- where `applicationinsights-spring-boot-starter` is enabling telemetry data to flow to AppInsights,
and `applicationinsights-logging-log4j2` is providing Log4j AppInsights appender.- Add AI instrumentation keys to `application.properties` pointing to environment variable (it is available in Application Settings if AI was enabled for Web App)
```
azure.application-insights.instrumentation-key=${APPINSIGHTS_INSTRUMENTATIONKEY}
```
- Add AppInsights Appender to Log4j2 config , typically `log4j2-spring.xml` file under `main\resources````xml
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
%d %p %c{1.} [%t] %m%n
```
- Use same `web.config` as described above, to make sure File output makes use of environment variable `logging.file` set `fileName="${sys:logging.file:-boot.log}` for the appender as shown above.
## Deploy using Maven plugin
See `pom.xml` for setting up the latest plugin and run
`mvn clean package azure-webapp:deploy`## App Insights integration
Spring starter App Insights integration allows to see basic performance metric for web controllers and send custom metrics and traces.To use multiple dimensions with the metric pass `HashMap` of properties along with request to send Telemetry or metric
```java
int rand = new Random().nextInt(10);
// track custom dimension
RequestTelemetry telemetry = new RequestTelemetry();
telemetry.getProperties().put("team", "team" + rand);
telemetry.getProperties().put("client", "client" + rand);telemetryClient.track(telemetry);
```It will show up in metrics and logs
![metrics](img/custom.png)- To query custom metrics use Kusto language to define aggregation by client dimension:
```
requests | where customDimensions["client"] != "" |
summarize Requests = count(id) by bin(timestamp, 30m), tostring(customDimensions["client"]) |
summarize maxReqs = max(Requests) by customDimensions_client |
order by maxReqs desc |
render barchart
```
![metrics](img/query.png)## Custom Metrics Dimension
As of today Dimension collection needs to be explicitly turned on for the aggregated custom metrics. You can find the setting to enable this under the “Usage and estimated costs” blade:
![metrics](img/customdimensions.png)Heere is example for our custom metrics in HelloController with "Apply Splitting" by client:
![metrics](img/applysplit.png)## App Insights Java agent integration
Azure App Insights agent allows you to capture automatically dependencies such as JDBC, HTTP calls and profile specific methods and exceptions. For the list of capabilities and configuration walkthrough to refer to [Java App Insights Agent Docs](https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-agent)It requires Java agent Jar file to be passed to application startup with `-javaagent=path to jar` options.
Agent also expects configuration file `AI-Agent.xml` to be in the same directory as the jar file.
For the format of config file refer to [AppInsights-Java Wiki](https://github.com/microsoft/ApplicationInsights-Java/wiki/AI-Agent.xml), which explains all the xml options and same github repo contains releases of the agent.- To include agent modify `web.config` to pass `JAVA_OPTS` that will pass java agent to the java process
```xml
```
- Set Application Settings `JAVA_OPTS` to the full path of the agent (where it will be uploaded), in this example we set it in maven plugin in `pom.xml`
```xml
JAVA_OPTS
-javaagent:D:\home\site\wwwroot\agent\applicationinsights-agent-2.4.0-BETA.jar
```
- Set maven plugin in `pom.xml` to upload the agent files to App Service
```xml
${project.basedir}/agent
agent
*
```
The resulting view in the file system:
![metrics](img/agent.png)### Profiling specific classes
To profile time execution of specific classes you have to list them in `AI-Agent.xml`, either on class level or down to method level
```xml
```As a result we will see the full Application Map with Dependencies and will be able to track exceptions and time executions:
![metrics](img/appmap.png)![metrics](img/endtoend.png)
Explicitly configured class will also have caught exptions tracking enabled and see in End to End view:
![metrics](img/exception.png)## Add Springboot Micrometer Metrics
Micrometer application monitoring measures metrics for JVM-based application code and lets you export the data to Azure Monitor (and other monitoring systems).
- add metrics starter jar in pom.xml
```xml
com.microsoft.azure
azure-spring-boot-metrics-starter
2.1.6
```There are lots of default metrics exposed by various Spring components , see list here [Azure Monitor Micrometer](https://docs.microsoft.com/en-us/azure/azure-monitor/app/micrometer-java)
- Navigate to App Insights metrics and Add Metric from `azure.applicationinsights` namespace
See below Custom metric explicitly from HelloController using `telemetryClient.trackMetric()` call![metrics](img/metrics.png)
See below standard Spring Metrics
![metrics](img/springmetrics.png)### @Timed annotation to send metrics to Azure Monitor
- You can use Spring `@Timed` annotation to explicitly
- add dependency to pom.xml
```xml
com.microsoft.azure
azure-spring-boot-metrics-starter
2.1.6
org.springframework.boot
spring-boot-starter-actuatororg.springframework.boot
spring-boot-starter-aop```
- Add Configuration to register aspectSee below standard Spring Metrics
![metrics](img/timedmetric.png)### Guides
The following guides illustrate how to use some features concretely:
* [Configure a Spring Boot Initializer app to use Application Insights](https://docs.microsoft.com/en-us/java/azure/spring-framework/configure-spring-boot-java-applicationinsights?view=azure-java-stable)* [How to use Micrometer with Azure Monitor](https://docs.microsoft.com/en-us/azure/azure-monitor/app/micrometer-java)
* [Java App Insights Agent Docs](https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-agent)
* [AppInsights-Java Wiki](https://github.com/microsoft/ApplicationInsights-Java/wiki/AI-Agent.xml)