Skip to content

Commit b2b9dbe

Browse files
authored
Distributed tracing (#17)
* Application changes - Add app insight agent * Dockerfile changes for integrating App Insights for ACA email processor job * Terraform - app insights
1 parent c05e96e commit b2b9dbe

File tree

10 files changed

+57
-9
lines changed

10 files changed

+57
-9
lines changed

apps/contoso-fiber/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@
142142
<artifactId>spring-cloud-azure-starter-storage-blob</artifactId>
143143
</dependency>
144144

145+
<dependency>
146+
<groupId>com.microsoft.azure</groupId>
147+
<artifactId>applicationinsights-runtime-attach</artifactId>
148+
</dependency>
149+
145150
</dependencies>
146151

147152
<build>

apps/contoso-fiber/src/main/java/com/contoso/cams/CamsApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
import org.springframework.web.bind.annotation.ExceptionHandler;
1414
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
1515

16+
import com.microsoft.applicationinsights.attach.ApplicationInsights;
17+
1618
import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;
1719

1820
@SpringBootApplication
1921
@EnableCaching
2022
public class CamsApplication {
2123

2224
public static void main(String[] args) {
25+
ApplicationInsights.attach();
2326
SpringApplication.run(CamsApplication.class, args);
2427
}
2528

apps/contoso-fiber/src/main/java/com/contoso/cams/support/SupportGuideEmailResponseProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class SupportGuideEmailResponseProcessor {
2929
Consumer<Message<byte[]>> consumeemailresponse() {
3030
return message -> {
3131
Checkpointer checkpointer = (Checkpointer) message.getHeaders().get(CHECKPOINTER);
32-
log.info("Received message: {}", message.getPayload());
32+
log.info("Received message: {}", new String(message.getPayload()));
3333

3434
EmailResponse emailResponse = parseMessage(message);
3535

@@ -43,7 +43,7 @@ Consumer<Message<byte[]>> consumeemailresponse() {
4343

4444
// Checkpoint after processing the message
4545
checkpointer.success()
46-
.doOnSuccess(s -> log.info("Message '{}' successfully checkpointed", message.getPayload()))
46+
.doOnSuccess(s -> log.info("Message '{}' successfully checkpointed", new String(message.getPayload())))
4747
.doOnError(e -> log.error("Exception found", e))
4848
.block();
4949
};

apps/email-processor/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM mcr.microsoft.com/openjdk/jdk:17-ubuntu
22
VOLUME /tmp
3-
ARG JAR_FILE
4-
COPY ${JAR_FILE} app.jar
5-
ENTRYPOINT ["java","-jar","/app.jar"]
3+
COPY target/email-processor.jar app.jar
4+
COPY target/agent/applicationinsights-agent.jar applicationinsights-agent.jar
5+
ENTRYPOINT ["java", "-javaagent:applicationinsights-agent.jar", "-jar", "/app.jar"]

apps/email-processor/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@
7070
<groupId>org.springframework.boot</groupId>
7171
<artifactId>spring-boot-maven-plugin</artifactId>
7272
</plugin>
73+
<plugin>
74+
<artifactId>maven-dependency-plugin</artifactId>
75+
<version>3.8.0</version>
76+
<executions>
77+
<execution>
78+
<id>copy-agent</id>
79+
<phase>package</phase>
80+
<goals>
81+
<goal>copy</goal>
82+
</goals>
83+
</execution>
84+
</executions>
85+
<configuration>
86+
<artifactItems>
87+
<artifactItem>
88+
<groupId>com.microsoft.azure</groupId>
89+
<artifactId>applicationinsights-agent</artifactId>
90+
<version>${applicationinsights.version}</version>
91+
<outputDirectory>${project.build.directory}/agent</outputDirectory>
92+
<destFileName>applicationinsights-agent.jar</destFileName>
93+
</artifactItem>
94+
</artifactItems>
95+
</configuration>
96+
</plugin>
7397
</plugins>
7498
</build>
7599

azure.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,3 @@ services:
2424
host: containerapp
2525
docker:
2626
path: ./Dockerfile
27-
buildArgs:
28-
- "JAR_FILE=target/email-processor.jar"

infra/shared/terraform/modules/aca/main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ resource "azurerm_container_app" "container_app" {
116116
name = "AZURE_SERVICEBUS_EMAIL_RESPONSE_QUEUE_NAME"
117117
value = var.email_response_queue_name
118118
}
119+
env {
120+
name = "APPLICATIONINSIGHTS_CONNECTION_STRING"
121+
value = var.app_insights_connection_string
122+
}
119123
}
120124
max_replicas = 10
121125
min_replicas = 1
@@ -150,4 +154,4 @@ resource "azurerm_private_dns_zone_virtual_network_link" "virtual_network_link_a
150154
private_dns_zone_name = azurerm_private_dns_zone.dns_for_aca[0].name
151155
virtual_network_id = var.spoke_vnet_id
152156
resource_group_name = var.resource_group
153-
}
157+
}

infra/shared/terraform/modules/aca/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ variable "application_name" {
1919
description = "The name of your application"
2020
}
2121

22+
variable "app_insights_connection_string" {
23+
type = string
24+
description = "The app insights connection string"
25+
}
26+
2227
variable "log_analytics_workspace_id" {
2328
type = string
2429
description = "The ID of the Log Analytics workspace"
@@ -70,4 +75,4 @@ variable "spoke_vnet_id" {
7075
type = string
7176
description = "The ID of the spoke VNET"
7277
default = null
73-
}
78+
}

infra/terraform/aca.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module "aca" {
1616
servicebus_namespace = module.servicebus[0].namespace_name
1717
container_registry_user_assigned_identity_id = module.acr[0].container_registry_user_assigned_identity_id
1818
acr_login_server = module.acr[0].acr_login_server
19+
app_insights_connection_string = module.hub_app_insights[0].connection_string
1920
log_analytics_workspace_id = var.environment == "prod" ? module.hub_app_insights[0].log_analytics_workspace_id: module.dev_app_insights[0].log_analytics_workspace_id
2021
servicebus_namespace_primary_connection_string = module.servicebus[0].servicebus_namespace_primary_connection_string
2122
}
@@ -37,6 +38,7 @@ module "secondary_aca" {
3738
container_registry_user_assigned_identity_id = module.acr[0].container_registry_user_assigned_identity_id
3839
acr_login_server = module.acr[0].acr_login_server
3940
log_analytics_workspace_id = module.hub_app_insights[0].log_analytics_workspace_id
41+
app_insights_connection_string = module.hub_app_insights[0].connection_string
4042
servicebus_namespace_primary_connection_string = module.secondary_servicebus[0].servicebus_namespace_primary_connection_string
4143
}
4244

@@ -60,5 +62,6 @@ module "dev_aca" {
6062
container_registry_user_assigned_identity_id = module.dev_acr[0].container_registry_user_assigned_identity_id
6163
acr_login_server = module.dev_acr[0].acr_login_server
6264
log_analytics_workspace_id = module.dev_app_insights[0].log_analytics_workspace_id
65+
app_insights_connection_string = module.dev_app_insights[0].connection_string
6366
servicebus_namespace_primary_connection_string = module.dev_servicebus[0].servicebus_namespace_primary_connection_string
6467
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<commons-lang3.version>3.14.0</commons-lang3.version>
3434
<spring-cloud-azure.version>5.12.0</spring-cloud-azure.version>
3535
<spring-cloud.version>2023.0.1</spring-cloud.version>
36+
<applicationinsights.version>3.5.4</applicationinsights.version>
3637
</properties>
3738

3839
<dependencyManagement>
@@ -66,6 +67,11 @@
6667
<artifactId>protobuf-java-util</artifactId>
6768
<version>${protobuf.java.util.version}</version>
6869
</dependency>
70+
<dependency>
71+
<groupId>com.microsoft.azure</groupId>
72+
<artifactId>applicationinsights-runtime-attach</artifactId>
73+
<version>${applicationinsights.version}</version>
74+
</dependency>
6975
</dependencies>
7076
</dependencyManagement>
7177

0 commit comments

Comments
 (0)