Skip to content

Add fix for hyphens in enum values #4202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.util.Locale;
import javax.annotation.Nullable;

public class AlertingConfig {

Expand Down Expand Up @@ -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 {
Expand All @@ -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<RequestAggregationConfig> {

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@

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 java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -49,4 +55,22 @@ 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);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Loading