diff --git a/agent/agent-profiler/agent-alerting-api/src/main/java/com/microsoft/applicationinsights/alerting/aiconfig/AlertingConfig.java b/agent/agent-profiler/agent-alerting-api/src/main/java/com/microsoft/applicationinsights/alerting/aiconfig/AlertingConfig.java index abeafcca51d..0cae0b7c77f 100644 --- a/agent/agent-profiler/agent-alerting-api/src/main/java/com/microsoft/applicationinsights/alerting/aiconfig/AlertingConfig.java +++ b/agent/agent-profiler/agent-alerting-api/src/main/java/com/microsoft/applicationinsights/alerting/aiconfig/AlertingConfig.java @@ -9,6 +9,7 @@ import com.azure.json.JsonWriter; import java.io.IOException; import java.util.Locale; +import javax.annotation.Nullable; public class AlertingConfig { @@ -58,7 +59,7 @@ public static RequestFilter fromJson(JsonReader jsonReader) throws IOException { String fieldName = reader.getFieldName(); if ("type".equals(fieldName)) { deserializedRequestFilter.setType( - RequestFilterType.valueOf(reader.getString().toUpperCase(Locale.ROOT))); + RequestFilterType.valueOf(toEnumFormat(reader.getString()))); } else if ("value".equals(fieldName)) { deserializedRequestFilter.setValue(reader.getString()); } else { @@ -70,6 +71,14 @@ public static RequestFilter fromJson(JsonReader jsonReader) throws IOException { } } + @Nullable + private static String toEnumFormat(String enumStr) { + if (enumStr == null) { + return null; + } + return enumStr.toUpperCase(Locale.ROOT).replaceAll("-", "_"); + } + public static class RequestAggregationConfig implements JsonSerializable { @@ -186,7 +195,7 @@ public static RequestAggregation fromJson(JsonReader jsonReader) throws IOExcept String fieldName = reader.getFieldName(); if ("type".equals(fieldName)) { deserializedRequestAggregation.setType( - RequestAggregationType.valueOf(reader.getString().toUpperCase(Locale.ROOT))); + RequestAggregationType.valueOf(toEnumFormat(reader.getString()))); } else if ("windowSizeMillis".equals(fieldName)) { deserializedRequestAggregation.setWindowSizeMillis(jsonReader.getLong()); } else if ("configuration".equals(fieldName)) { @@ -256,8 +265,7 @@ public static RequestTriggerThreshold fromJson(JsonReader jsonReader) throws IOE String fieldName = reader.getFieldName(); if ("type".equals(fieldName)) { deserializedRequestTriggerThreshold.setType( - RequestTriggerThresholdType.valueOf( - reader.getString().toUpperCase(Locale.ROOT))); + RequestTriggerThresholdType.valueOf(toEnumFormat(reader.getString()))); } else if ("value".equals(fieldName)) { deserializedRequestTriggerThreshold.setValue(reader.getFloat()); } else { @@ -317,8 +325,7 @@ public static RequestTriggerThrottling fromJson(JsonReader jsonReader) throws IO String fieldName = reader.getFieldName(); if ("type".equals(fieldName)) { deserializedRequestTriggerThrottling.setType( - RequestTriggerThrottlingType.valueOf( - reader.getString().toUpperCase(Locale.ROOT))); + RequestTriggerThrottlingType.valueOf(toEnumFormat(reader.getString()))); } else if ("value".equals(fieldName)) { deserializedRequestTriggerThrottling.setValue(reader.getLong()); } else { @@ -431,7 +438,7 @@ public static RequestTrigger fromJson(JsonReader jsonReader) throws IOException deserializedRequestTrigger.setName(reader.getString()); } else if ("type".equals(fieldName)) { deserializedRequestTrigger.setType( - RequestTriggerType.valueOf(reader.getString().toUpperCase(Locale.ROOT))); + RequestTriggerType.valueOf(toEnumFormat(reader.getString()))); } else if ("filter".equals(fieldName)) { deserializedRequestTrigger.setFilter(RequestFilter.fromJson(reader)); } else if ("aggregation".equals(fieldName)) { diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/config/ProfilerConfigurationTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/config/ProfilerConfigurationTest.java index 0e55c14ca3d..eb889fa33d0 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/config/ProfilerConfigurationTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/config/ProfilerConfigurationTest.java @@ -3,9 +3,16 @@ package com.microsoft.applicationinsights.agent.internal.profiler.config; +import com.azure.json.JsonOptions; +import com.azure.json.JsonReader; +import com.azure.json.implementation.DefaultJsonReader; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.microsoft.applicationinsights.alerting.aiconfig.AlertingConfig; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.Charset; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -49,4 +56,27 @@ private static ProfilerConfiguration parseConfig(String configStr) ProfilerConfiguration config = mapper.readValue(configStr, ProfilerConfiguration.class); return config; } + + @Test + public void testAlertDeserialization() { + try (InputStreamReader json = + new InputStreamReader( + ProfilerConfigurationTest.class + .getClassLoader() + .getResourceAsStream("profile-configs/sample-alert-config.json"), + Charset.forName("UTF-8"))) { + + JsonReader reader = DefaultJsonReader.fromReader(json, new JsonOptions()); + ProfilerConfiguration profilerConfiguration = ProfilerConfiguration.fromJson(reader); + Assertions.assertTrue(profilerConfiguration != null); + AlertingConfig.RequestTrigger requestTrigger = + profilerConfiguration.getRequestTriggerConfiguration().get(0); + Assertions.assertEquals(requestTrigger.getType(), AlertingConfig.RequestTriggerType.LATENCY); + Assertions.assertEquals( + requestTrigger.getFilter().getType(), AlertingConfig.RequestFilterType.NAME_REGEX); + Assertions.assertEquals(requestTrigger.getFilter().getValue(), ".*"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/agent/agent-tooling/src/test/resources/profile-configs/sample-alert-config.json b/agent/agent-tooling/src/test/resources/profile-configs/sample-alert-config.json new file mode 100644 index 00000000000..eacdd468dcd --- /dev/null +++ b/agent/agent-tooling/src/test/resources/profile-configs/sample-alert-config.json @@ -0,0 +1,38 @@ +{ + "agentConcurrency": 0, + "collectionPlan": "--single --mode immediate --immediate-profiling-duration 120 --expiration 5249691022697135638 --settings-moniker Portal_REDACTED", + "cpuTriggerConfiguration": "--cpu-threshold 80 --cpu-trigger-profilingDuration 120 --cpu-trigger-cooldown 14400 --cpu-trigger-enabled false", + "defaultConfiguration": null, + "enabled": true, + "enabledLastModified": "0001-01-01T00:00:00+00:00", + "id": "an-id", + "lastModified": "0001-01-01T00:00:00+00:00", + "memoryTriggerConfiguration": "--memory-threshold 80 --memory-trigger-profilingDuration 120 --memory-trigger-cooldown 14400 --memory-trigger-enabled false", + "requestTriggerConfiguration": [ + { + "aggregation": { + "configuration": { + "minimumSamples": 30, + "thresholdMillis": 500 + }, + "type": "breach-ratio", + "windowSizeMillis": 10000 + }, + "filter": { + "type": "name-regex", + "value": ".*" + }, + "name": "trigger", + "profileDuration": 30, + "threshold": { + "type": "greater-than", + "value": 0.9 + }, + "throttling": { + "type": "fixed-duration-cooldown", + "value": 120000 + }, + "type": "latency" + } + ] +}