Skip to content

Commit fbeeeaa

Browse files
authored
Add test to confirm request trigger parsing matches documentation (#3001)
1 parent 78dbf06 commit fbeeeaa

File tree

8 files changed

+193
-9
lines changed

8 files changed

+193
-9
lines changed

agent/agent-profiler/agent-alerting-api/src/main/java/com/microsoft/applicationinsights/alerting/aiconfig/AlertingConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class AlertingConfig {
1010

1111
public enum RequestFilterType {
12+
@JsonProperty("name-regex")
1213
NAME_REGEX
1314
}
1415

@@ -44,6 +45,7 @@ public RequestAggregationConfig(
4445
}
4546

4647
public enum RequestAggregationType {
48+
@JsonProperty("breach-ratio")
4749
BREACH_RATIO
4850
}
4951

@@ -64,6 +66,7 @@ public RequestAggregation(
6466
}
6567

6668
public enum RequestTriggerThresholdType {
69+
@JsonProperty("greater-than")
6770
GREATER_THAN
6871
}
6972

@@ -89,6 +92,7 @@ public RequestTriggerThreshold(
8992
}
9093

9194
public enum RequestTriggerThrottlingType {
95+
@JsonProperty("fixed-duration-cooldown")
9296
FIXED_DURATION_COOLDOWN
9397
}
9498

@@ -106,6 +110,7 @@ public RequestTriggerThrottling(
106110
}
107111

108112
public enum RequestTriggerType {
113+
@JsonProperty("latency")
109114
LATENCY
110115
}
111116

agent/agent-profiler/request-triggers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ An example of a complete configuration is as follows:
2020
"requestTriggerEndpoints": [
2121
{
2222
"name": "Users endpoint is responsive",
23-
"type": "LATENCY",
23+
"type": "latency",
2424
"filter": {
2525
"type": "name-regex",
2626
"value": "/users/get/.*"

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ public static class RequestTriggerThrottling {
13811381
}
13821382

13831383
public enum RequestTriggerType {
1384+
@JsonProperty("latency")
13841385
LATENCY
13851386
}
13861387

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/configuration/ConfigurationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
import io.opentelemetry.api.common.AttributeKey;
2020
import java.io.IOException;
2121
import java.nio.file.Paths;
22+
import java.util.Collection;
2223
import java.util.List;
24+
import java.util.stream.Collectors;
25+
import java.util.stream.Stream;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.DynamicTest;
2328
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.TestFactory;
2430
import org.junit.jupiter.api.extension.ExtendWith;
2531
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
2632
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
@@ -682,6 +688,26 @@ void shouldNotParseFaultyJson() {
682688
.isInstanceOf(UnrecognizedPropertyException.class);
683689
}
684690

691+
@TestFactory
692+
public Collection<DynamicTest> canParseExampleProfilerConfigurations() {
693+
return Stream.of(
694+
"profile-configs/applicationinsights-trigger-example-1.json",
695+
"profile-configs/applicationinsights-trigger-example-2.json",
696+
"profile-configs/applicationinsights-trigger-example-3.json")
697+
.map(
698+
file ->
699+
DynamicTest.dynamicTest(
700+
file,
701+
() -> {
702+
Configuration config = loadConfiguration(file, true);
703+
Assertions.assertNotNull(config);
704+
Assertions.assertNotNull(config.preview.profiler.requestTriggerEndpoints);
705+
Assertions.assertTrue(
706+
config.preview.profiler.requestTriggerEndpoints.length > 0);
707+
}))
708+
.collect(Collectors.toList());
709+
}
710+
685711
private static Configuration loadConfiguration() throws IOException {
686712
return loadConfiguration("applicationinsights.json");
687713
}

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/triggers/RequestAlertPipelineBuilderTest.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
package com.microsoft.applicationinsights.agent.internal.profiler.triggers;
55

66
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.JsonNode;
78
import com.fasterxml.jackson.databind.ObjectMapper;
89
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
910
import com.microsoft.applicationinsights.agent.internal.profiler.testutil.TestTimeSource;
1011
import com.microsoft.applicationinsights.alerting.aiconfig.AlertingConfig;
1112
import java.time.Instant;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
1216
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.DynamicTest;
1318
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.TestFactory;
1420

1521
public class RequestAlertPipelineBuilderTest {
1622

@@ -30,15 +36,45 @@ public void configurationIsCorrectlyDuplicated() throws JsonProcessingException
3036
ObjectMapper mapper = new ObjectMapper();
3137
String configurationStr = mapper.writeValueAsString(triggerConfig);
3238
String alertingConfigStr = mapper.writeValueAsString(config);
33-
34-
// Account for serialization differences
35-
alertingConfigStr =
36-
alertingConfigStr
37-
.replaceAll("NAME_REGEX", "name-regex")
38-
.replaceAll("BREACH_RATIO", "breach-ratio")
39-
.replaceAll("GREATER_THAN", "greater-than")
40-
.replaceAll("FIXED_DURATION_COOLDOWN", "fixed-duration-cooldown");
39+
;
4140

4241
Assertions.assertEquals(configurationStr, alertingConfigStr);
4342
}
43+
44+
@TestFactory
45+
public List<DynamicTest> configExamplesCanBeParsedToAlertApiConfig() {
46+
return Stream.of(
47+
"profile-configs/applicationinsights-trigger-example-1.json",
48+
"profile-configs/applicationinsights-trigger-example-2.json",
49+
"profile-configs/applicationinsights-trigger-example-3.json")
50+
.map(
51+
file ->
52+
DynamicTest.dynamicTest(
53+
file,
54+
() -> {
55+
ObjectMapper mapper = new ObjectMapper();
56+
JsonNode array =
57+
mapper
58+
.readTree(
59+
RequestAlertPipelineBuilderTest.class
60+
.getClassLoader()
61+
.getResourceAsStream(file))
62+
.get("preview")
63+
.get("profiler")
64+
.withArray("requestTriggerEndpoints");
65+
66+
array.forEach(
67+
config -> {
68+
try {
69+
AlertingConfig.RequestTrigger alertingConfig =
70+
mapper.readValue(
71+
config.toPrettyString(), AlertingConfig.RequestTrigger.class);
72+
Assertions.assertNotNull(alertingConfig);
73+
} catch (JsonProcessingException e) {
74+
Assertions.fail(e);
75+
}
76+
});
77+
}))
78+
.collect(Collectors.toList());
79+
}
4480
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-000000000000",
3+
"preview": {
4+
"profiler": {
5+
"enableRequestTriggering": true,
6+
"requestTriggerEndpoints": [
7+
{
8+
"name": "Users endpoint is responsive",
9+
"type": "latency",
10+
"filter": {
11+
"type": "name-regex",
12+
"value": "/users/get/.*"
13+
},
14+
"aggregation": {
15+
"configuration": {
16+
"thresholdMillis": 7000
17+
},
18+
"type": "breach-ratio",
19+
"windowSizeMillis": 60000
20+
},
21+
"threshold": {
22+
"value": 0.75
23+
},
24+
"profileDuration": 30,
25+
"throttling": {
26+
"value": 60
27+
}
28+
}
29+
]
30+
}
31+
}
32+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-000000000000",
3+
"preview": {
4+
"profiler": {
5+
"enabled": true,
6+
"enableRequestTriggering": true,
7+
"requestTriggerEndpoints": [
8+
{
9+
"name": "Users",
10+
"type": "latency",
11+
"profileDuration": 30,
12+
"filter": {
13+
"type": "name-regex",
14+
"value": "/users/.*"
15+
},
16+
"aggregation": {
17+
"configuration": {
18+
"thresholdMillis": 7000
19+
},
20+
"type": "breach-ratio"
21+
},
22+
"threshold": {
23+
"value": 0.75
24+
},
25+
"throttling": {
26+
"value": 60
27+
}
28+
},
29+
{
30+
"name": "Index.html",
31+
"type": "latency",
32+
"profileDuration": 60,
33+
"filter": {
34+
"type": "name-regex",
35+
"value": "/index\\.html"
36+
},
37+
"aggregation": {
38+
"configuration": {
39+
"thresholdMillis": 100
40+
},
41+
"type": "breach-ratio"
42+
},
43+
"threshold": {
44+
"value": 0.5
45+
},
46+
"throttling": {
47+
"value": 60
48+
}
49+
}
50+
]
51+
}
52+
}
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-000000000000",
3+
"preview": {
4+
"profiler": {
5+
"enableRequestTriggering": true,
6+
"requestTriggerEndpoints": [
7+
{
8+
"name": "All",
9+
"type": "latency",
10+
"profileDuration": 30,
11+
"filter": {
12+
"type": "name-regex",
13+
"value": "/.*"
14+
},
15+
"aggregation": {
16+
"configuration": {
17+
"thresholdMillis": 7000
18+
},
19+
"type": "breach-ratio"
20+
},
21+
"threshold": {
22+
"value": 0.75
23+
},
24+
"throttling": {
25+
"value": 60
26+
}
27+
}
28+
]
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)