Skip to content

Commit 7c234b7

Browse files
johnoliverharsimar
andauthored
Add fix for hyphens in enum values (#4202)
Co-authored-by: Harsimar Kaur <harskaur@microsoft.com>
1 parent 7d2c088 commit 7c234b7

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.azure.json.JsonWriter;
1010
import java.io.IOException;
1111
import java.util.Locale;
12+
import javax.annotation.Nullable;
1213

1314
public class AlertingConfig {
1415

@@ -58,7 +59,7 @@ public static RequestFilter fromJson(JsonReader jsonReader) throws IOException {
5859
String fieldName = reader.getFieldName();
5960
if ("type".equals(fieldName)) {
6061
deserializedRequestFilter.setType(
61-
RequestFilterType.valueOf(reader.getString().toUpperCase(Locale.ROOT)));
62+
RequestFilterType.valueOf(toEnumFormat(reader.getString())));
6263
} else if ("value".equals(fieldName)) {
6364
deserializedRequestFilter.setValue(reader.getString());
6465
} else {
@@ -70,6 +71,14 @@ public static RequestFilter fromJson(JsonReader jsonReader) throws IOException {
7071
}
7172
}
7273

74+
@Nullable
75+
private static String toEnumFormat(String enumStr) {
76+
if (enumStr == null) {
77+
return null;
78+
}
79+
return enumStr.toUpperCase(Locale.ROOT).replaceAll("-", "_");
80+
}
81+
7382
public static class RequestAggregationConfig
7483
implements JsonSerializable<RequestAggregationConfig> {
7584

@@ -186,7 +195,7 @@ public static RequestAggregation fromJson(JsonReader jsonReader) throws IOExcept
186195
String fieldName = reader.getFieldName();
187196
if ("type".equals(fieldName)) {
188197
deserializedRequestAggregation.setType(
189-
RequestAggregationType.valueOf(reader.getString().toUpperCase(Locale.ROOT)));
198+
RequestAggregationType.valueOf(toEnumFormat(reader.getString())));
190199
} else if ("windowSizeMillis".equals(fieldName)) {
191200
deserializedRequestAggregation.setWindowSizeMillis(jsonReader.getLong());
192201
} else if ("configuration".equals(fieldName)) {
@@ -256,8 +265,7 @@ public static RequestTriggerThreshold fromJson(JsonReader jsonReader) throws IOE
256265
String fieldName = reader.getFieldName();
257266
if ("type".equals(fieldName)) {
258267
deserializedRequestTriggerThreshold.setType(
259-
RequestTriggerThresholdType.valueOf(
260-
reader.getString().toUpperCase(Locale.ROOT)));
268+
RequestTriggerThresholdType.valueOf(toEnumFormat(reader.getString())));
261269
} else if ("value".equals(fieldName)) {
262270
deserializedRequestTriggerThreshold.setValue(reader.getFloat());
263271
} else {
@@ -317,8 +325,7 @@ public static RequestTriggerThrottling fromJson(JsonReader jsonReader) throws IO
317325
String fieldName = reader.getFieldName();
318326
if ("type".equals(fieldName)) {
319327
deserializedRequestTriggerThrottling.setType(
320-
RequestTriggerThrottlingType.valueOf(
321-
reader.getString().toUpperCase(Locale.ROOT)));
328+
RequestTriggerThrottlingType.valueOf(toEnumFormat(reader.getString())));
322329
} else if ("value".equals(fieldName)) {
323330
deserializedRequestTriggerThrottling.setValue(reader.getLong());
324331
} else {
@@ -431,7 +438,7 @@ public static RequestTrigger fromJson(JsonReader jsonReader) throws IOException
431438
deserializedRequestTrigger.setName(reader.getString());
432439
} else if ("type".equals(fieldName)) {
433440
deserializedRequestTrigger.setType(
434-
RequestTriggerType.valueOf(reader.getString().toUpperCase(Locale.ROOT)));
441+
RequestTriggerType.valueOf(toEnumFormat(reader.getString())));
435442
} else if ("filter".equals(fieldName)) {
436443
deserializedRequestTrigger.setFilter(RequestFilter.fromJson(reader));
437444
} else if ("aggregation".equals(fieldName)) {

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/config/ProfilerConfigurationTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33

44
package com.microsoft.applicationinsights.agent.internal.profiler.config;
55

6+
import com.azure.json.JsonOptions;
7+
import com.azure.json.JsonReader;
8+
import com.azure.json.implementation.DefaultJsonReader;
69
import com.fasterxml.jackson.core.JsonProcessingException;
710
import com.fasterxml.jackson.databind.DeserializationFeature;
811
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.microsoft.applicationinsights.alerting.aiconfig.AlertingConfig;
13+
import java.io.IOException;
14+
import java.io.InputStreamReader;
15+
import java.nio.charset.Charset;
916
import org.junit.jupiter.api.Assertions;
1017
import org.junit.jupiter.api.Test;
1118

@@ -49,4 +56,27 @@ private static ProfilerConfiguration parseConfig(String configStr)
4956
ProfilerConfiguration config = mapper.readValue(configStr, ProfilerConfiguration.class);
5057
return config;
5158
}
59+
60+
@Test
61+
public void testAlertDeserialization() {
62+
try (InputStreamReader json =
63+
new InputStreamReader(
64+
ProfilerConfigurationTest.class
65+
.getClassLoader()
66+
.getResourceAsStream("profile-configs/sample-alert-config.json"),
67+
Charset.forName("UTF-8"))) {
68+
69+
JsonReader reader = DefaultJsonReader.fromReader(json, new JsonOptions());
70+
ProfilerConfiguration profilerConfiguration = ProfilerConfiguration.fromJson(reader);
71+
Assertions.assertTrue(profilerConfiguration != null);
72+
AlertingConfig.RequestTrigger requestTrigger =
73+
profilerConfiguration.getRequestTriggerConfiguration().get(0);
74+
Assertions.assertEquals(requestTrigger.getType(), AlertingConfig.RequestTriggerType.LATENCY);
75+
Assertions.assertEquals(
76+
requestTrigger.getFilter().getType(), AlertingConfig.RequestFilterType.NAME_REGEX);
77+
Assertions.assertEquals(requestTrigger.getFilter().getValue(), ".*");
78+
} catch (IOException e) {
79+
throw new RuntimeException(e);
80+
}
81+
}
5282
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"agentConcurrency": 0,
3+
"collectionPlan": "--single --mode immediate --immediate-profiling-duration 120 --expiration 5249691022697135638 --settings-moniker Portal_REDACTED",
4+
"cpuTriggerConfiguration": "--cpu-threshold 80 --cpu-trigger-profilingDuration 120 --cpu-trigger-cooldown 14400 --cpu-trigger-enabled false",
5+
"defaultConfiguration": null,
6+
"enabled": true,
7+
"enabledLastModified": "0001-01-01T00:00:00+00:00",
8+
"id": "an-id",
9+
"lastModified": "0001-01-01T00:00:00+00:00",
10+
"memoryTriggerConfiguration": "--memory-threshold 80 --memory-trigger-profilingDuration 120 --memory-trigger-cooldown 14400 --memory-trigger-enabled false",
11+
"requestTriggerConfiguration": [
12+
{
13+
"aggregation": {
14+
"configuration": {
15+
"minimumSamples": 30,
16+
"thresholdMillis": 500
17+
},
18+
"type": "breach-ratio",
19+
"windowSizeMillis": 10000
20+
},
21+
"filter": {
22+
"type": "name-regex",
23+
"value": ".*"
24+
},
25+
"name": "trigger",
26+
"profileDuration": 30,
27+
"threshold": {
28+
"type": "greater-than",
29+
"value": 0.9
30+
},
31+
"throttling": {
32+
"type": "fixed-duration-cooldown",
33+
"value": 120000
34+
},
35+
"type": "latency"
36+
}
37+
]
38+
}

0 commit comments

Comments
 (0)