Skip to content

Commit 5814fca

Browse files
authored
Merge pull request #1018 from microsoft/app-services-sample
Add App Services sample
2 parents 694aaec + efea886 commit 5814fca

File tree

20 files changed

+215
-3
lines changed

20 files changed

+215
-3
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,3 @@ Backend/OrderService/OrderService.iml
9898

9999
# VS Code Workspace settings
100100
.vscode/settings.json
101-
102-
#Generated POMs
103-
pom.xml

samples/app-services/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

samples/app-services/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
App Services Example with Application Insights
2+
3+
* Update `resourceGroup`, `appName` and `region` in [pom.xml](pom.xml)
4+
5+
* Optionally update `os`, `javaVersion` and `webContainer` in [pom.xml](pom.xml)
6+
7+
* Update instrumentation key in [ApplicationInsights.xml](src/main/resources/ApplicationInsights.xml)
8+
9+
* az login
10+
11+
* az account set --subscription <subscription Id>
12+
13+
* Deploy using `mvn clean package azure-webapp:deploy`

samples/app-services/pom.xml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.microsoft.azure</groupId>
7+
<artifactId>applicationinsights-app-services-sample</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<properties>
12+
<java.version>1.8</java.version>
13+
<appinsights.version>2.5.0-BETA.2</appinsights.version>
14+
</properties>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.jboss.spec</groupId>
20+
<artifactId>jboss-javaee-8.0</artifactId>
21+
<version>1.0.0.Final</version>
22+
<type>pom</type>
23+
<scope>import</scope>
24+
</dependency>
25+
</dependencies>
26+
</dependencyManagement>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.jboss.spec.javax.servlet</groupId>
31+
<artifactId>jboss-servlet-api_4.0_spec</artifactId>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.apache.httpcomponents</groupId>
36+
<artifactId>httpclient</artifactId>
37+
<version>4.5.9</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.microsoft.azure</groupId>
41+
<artifactId>applicationinsights-web-auto</artifactId>
42+
<version>${appinsights.version}</version>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<finalName>helloworld</finalName>
48+
<plugins>
49+
<plugin>
50+
<!-- download the applicationinsights-agent jar file -->
51+
<artifactId>maven-dependency-plugin</artifactId>
52+
<version>3.1.1</version>
53+
<executions>
54+
<execution>
55+
<id>copy</id>
56+
<phase>generate-resources</phase>
57+
<goals>
58+
<goal>copy</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
<configuration>
63+
<artifactItems>
64+
<artifactItem>
65+
<groupId>com.microsoft.azure</groupId>
66+
<artifactId>applicationinsights-agent</artifactId>
67+
<version>${appinsights.version}</version>
68+
</artifactItem>
69+
</artifactItems>
70+
</configuration>
71+
</plugin>
72+
<plugin>
73+
<!-- place the applicationinsights-agent jar file in the war file under META-INF -->
74+
<artifactId>maven-war-plugin</artifactId>
75+
<version>3.2.3</version>
76+
<configuration>
77+
<webResources>
78+
<resource>
79+
<directory>${project.build.directory}/dependency</directory>
80+
<targetPath>META-INF</targetPath>
81+
</resource>
82+
</webResources>
83+
</configuration>
84+
</plugin>
85+
<plugin>
86+
<groupId>com.microsoft.azure</groupId>
87+
<artifactId>azure-webapp-maven-plugin</artifactId>
88+
<version>1.7.0</version>
89+
<configuration>
90+
<schemaVersion>V2</schemaVersion>
91+
<!-- Note: you cannot have both windows and linux app services in the same resource group -->
92+
<resourceGroup>** Your resource group **</resourceGroup>
93+
<appName>** Your app name **</appName>
94+
<region>** Your region **</region>
95+
<runtime>
96+
<!-- options for os: linux, windows and docker -->
97+
<os>linux</os>
98+
<!--
99+
options for javaVersion and webContainer:
100+
see https://docs.microsoft.com/en-us/java/api/overview/azure/maven/azure-webapp-maven-plugin/readme?view=azure-java-stable#configuration-details
101+
-->
102+
<javaVersion>jre8</javaVersion>
103+
<webContainer>tomcat 8.5</webContainer>
104+
</runtime>
105+
<appSettings>
106+
<property>
107+
<!-- point to the applicationinsights-agent jar inside of the exploded war file -->
108+
<name>JAVA_OPTS</name>
109+
<value>-javaagent:/home/site/wwwroot/webapps/ROOT/META-INF/applicationinsights-agent-${appinsights.version}.jar</value>
110+
</property>
111+
</appSettings>
112+
<deployment>
113+
<resources>
114+
<resource>
115+
<directory>${project.basedir}/target</directory>
116+
<includes>
117+
<include>*.war</include>
118+
</includes>
119+
</resource>
120+
</resources>
121+
</deployment>
122+
</configuration>
123+
</plugin>
124+
</plugins>
125+
</build>
126+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package helloworld;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
import java.io.PrintWriter;
7+
8+
import javax.servlet.annotation.WebServlet;
9+
import javax.servlet.http.HttpServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
import org.apache.http.HttpResponse;
14+
import org.apache.http.client.methods.HttpGet;
15+
import org.apache.http.impl.client.CloseableHttpClient;
16+
import org.apache.http.impl.client.HttpClients;
17+
18+
import com.microsoft.applicationinsights.core.dependencies.google.common.io.CharStreams;
19+
20+
@WebServlet("")
21+
@SuppressWarnings("serial")
22+
public class HelloWorldServlet extends HttpServlet {
23+
24+
@Override
25+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
26+
get("https://bing.com");
27+
resp.setContentType("text/html");
28+
PrintWriter writer = resp.getWriter();
29+
writer.println("Hello!");
30+
writer.close();
31+
}
32+
33+
private static String get(String url) throws IOException {
34+
CloseableHttpClient httpClient = HttpClients.createDefault();
35+
HttpGet httpGet = new HttpGet(url);
36+
httpGet.setHeader("Accept", "application/json");
37+
HttpResponse response = httpClient.execute(httpGet);
38+
InputStream content = response.getEntity().getContent();
39+
String body = CharStreams.toString(new InputStreamReader(content));
40+
content.close();
41+
httpClient.close();
42+
return "Response from " + url + " was: " + body;
43+
}
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
3+
4+
<InstrumentationKey>** Your instrumentation key **</InstrumentationKey>
5+
6+
<RoleName>App Services Example</RoleName>
7+
8+
<!-- HTTP request component (not required for bare API) -->
9+
<TelemetryModules>
10+
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebRequestTrackingTelemetryModule" />
11+
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebSessionTrackingTelemetryModule" />
12+
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebUserTrackingTelemetryModule" />
13+
</TelemetryModules>
14+
15+
<!-- Events correlation (not required for bare API) -->
16+
<!-- These initializers add context data to each event -->
17+
<TelemetryInitializers>
18+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationIdTelemetryInitializer" />
19+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationNameTelemetryInitializer" />
20+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebSessionTelemetryInitializer" />
21+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserTelemetryInitializer" />
22+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserAgentTelemetryInitializer" />
23+
</TelemetryInitializers>
24+
25+
</ApplicationInsights>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ApplicationInsightsAgent>
3+
<Instrumentation>
4+
<BuiltIn enabled="true" />
5+
</Instrumentation>
6+
</ApplicationInsightsAgent>
File renamed without changes.

0 commit comments

Comments
 (0)