diff --git a/compatibility-tests/sdk-test-java/README.md b/compatibility-tests/sdk-test-java/README.md index 7a43de1c90..f8d3da318f 100644 --- a/compatibility-tests/sdk-test-java/README.md +++ b/compatibility-tests/sdk-test-java/README.md @@ -57,6 +57,31 @@ just test-java AWS credentials are always `test` / `test` / `us-east-1`. +## Metric filter publishing checks + +`CloudWatchLogsMetricFilterTest` and `CloudFormationLogsMetricFilterTest` run +with the rest of the suite. Against a server on another port: + +```bash +# From compatibility-tests/sdk-test-java +FLOCI_ENDPOINT=http://127.0.0.1:18084 ../../mvnw test \ + -Dtest=CloudWatchLogsMetricFilterTest,CloudFormationLogsMetricFilterTest +``` + +The publishing tests read their expected values from +`src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json`, a +byte-identical copy of the root fixture recorded from AWS. `MetricFilterFixturePackagingTest` +checks the copy loads from the classpath, and the root +`CloudWatchLogsMetricFilterFixturePackagingTest` fails when either SDK module +copy drifts from the root file. The SDK selects the CloudWatch JSON protocol; +`MetricFilterQueryAssertions` signs the same reads as legacy form-encoded Query +requests, so `GetMetricStatistics` and `GetMetricData` on both wire formats are +asserted against every statistic the fixture records (Sum, SampleCount, Minimum, +Maximum). Reads poll for the sample, since the server publishes it shortly after +`PutLogEvents` returns. Passing +these tests shows Floci matches the recorded values; it is not live AWS +verification. For the opt-in replay against AWS see `../sdk-test-python/README.md`. + ## Docker ```bash diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLogsMetricFilterTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLogsMetricFilterTest.java new file mode 100644 index 0000000000..9b35d742e2 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLogsMetricFilterTest.java @@ -0,0 +1,328 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.services.cloudformation.CloudFormationClient; +import software.amazon.awssdk.services.cloudformation.model.CloudFormationException; +import software.amazon.awssdk.services.cloudformation.model.Output; +import software.amazon.awssdk.services.cloudformation.model.Parameter; +import software.amazon.awssdk.services.cloudformation.model.StackEvent; +import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; +import software.amazon.awssdk.services.cloudwatch.model.Datapoint; +import software.amazon.awssdk.services.cloudwatch.model.Dimension; +import software.amazon.awssdk.services.cloudwatch.model.Statistic; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent; +import software.amazon.awssdk.services.cloudwatchlogs.model.MetricFilter; +import software.amazon.awssdk.services.cloudwatchlogs.model.MetricTransformation; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.BooleanSupplier; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** SDK checks of backing resources and emitted metrics, not just CFN success-shaped status. */ +@DisplayName("CloudFormation AWS::Logs::MetricFilter") +class CloudFormationLogsMetricFilterTest { + private static CloudFormationClient cfn; + private static CloudWatchLogsClient logs; + private static CloudWatchClient metrics; + private String stack; + private String group; + private Set priorEvents = Set.of(); + + @BeforeAll + static void clients() { + cfn = TestFixtures.cloudFormationClient(); + logs = TestFixtures.cloudWatchLogsClient(); + metrics = TestFixtures.cloudWatchClient(); + } + + @BeforeEach + void setup() { + stack = TestFixtures.uniqueName("compat-cfn-metric-filter"); + group = "/test/" + stack; + logs.createLogGroup(r -> r.logGroupName(group)); + logs.createLogStream(r -> r.logGroupName(group).logStreamName("events")); + } + + @AfterEach + void cleanup() throws InterruptedException { + deleteStack(); + logs.deleteLogGroup(r -> r.logGroupName(group)); + } + + @AfterAll + static void closeClients() { + cfn.close(); + logs.close(); + metrics.close(); + } + + @Test + void observedRetainReplacementDeletesFirstAtQuotaAndKeepsUnrelatedFilters() throws InterruptedException { + String template = template("", ""); + create(template, "errors", "ERROR", "1"); + assertIdentity("errors"); + update(template("Retain", ""), "errors", "WARN", "2", "UPDATE_COMPLETE"); + assertIdentity("errors"); + assertThat(filter("errors").filterPattern()).isEqualTo("WARN"); + assertSample("WARN", 2, null); + assertThat(cfn.getTemplate(r -> r.stackName(stack)).templateBody()).contains("\"UpdateReplacePolicy\": \"Retain\""); + + for (int i = 0; i < 99; i++) { + String name = "unrelated-" + i; + logs.putMetricFilter(r -> r.logGroupName(group).filterName(name).filterPattern("UNRELATED") + .metricTransformations(MetricTransformation.builder().metricName("Untouched") + .metricNamespace(stack + "/unrelated").metricValue("7").build())); + } + Map unrelated = filters().stream().filter(f -> f.filterName().startsWith("unrelated-")) + .collect(Collectors.toMap(MetricFilter::filterName, f -> f)); + update(template("Retain", ""), "renamed", "WARN", "2", "UPDATE_COMPLETE"); + assertIdentity("renamed"); + assertThat(filters()).hasSize(100).noneMatch(f -> "errors".equals(f.filterName())); + assertUnrelated(unrelated); + assertSequence(List.of("errors:UPDATE_IN_PROGRESS", "errors:DELETE_IN_PROGRESS", "errors:DELETE_COMPLETE", + "renamed:CREATE_IN_PROGRESS", "renamed:CREATE_COMPLETE", "renamed:UPDATE_COMPLETE")); + assertThat(newEvents()).noneMatch(e -> "DELETE_SKIPPED".equals(e.resourceStatusAsString())); + assertThat(newEvents()).anyMatch(e -> "DELETE_IN_PROGRESS".equals(e.resourceStatusAsString()) + && ("Requested update requires the replacement of the existing resource; " + + "deleting existing resource, then creating a new one.").equals(e.resourceStatusReason())); + deleteStack(); + assertThat(filters()).hasSize(99); + assertUnrelated(unrelated); + } + + @Test + void failedReplacementRestoresActualDefinitionTemplateAndNameOnlyIdentity() throws InterruptedException { + String template = template("", ""); + create(template, "old", "WARN", "2"); + MetricFilter before = filter("old"); + update(template, "invalid", "{", "9", "UPDATE_ROLLBACK_COMPLETE"); + assertIdentity("old"); + assertThat(filters()).hasSize(1); + MetricFilter after = filter("old"); + assertThat(after.toBuilder().creationTime(before.creationTime()).build()).isEqualTo(before); + assertThat(after.creationTime()).isGreaterThan(before.creationTime()); + assertThat(cfn.getTemplate(r -> r.stackName(stack)).templateBody()).isEqualTo(template); + assertSequence(List.of("old:UPDATE_IN_PROGRESS", "old:DELETE_IN_PROGRESS", "old:DELETE_COMPLETE", + "invalid:CREATE_IN_PROGRESS", "invalid:CREATE_FAILED", "invalid:UPDATE_FAILED", + "invalid:UPDATE_IN_PROGRESS", "invalid:DELETE_IN_PROGRESS", "invalid:DELETE_COMPLETE", + "invalid:CREATE_IN_PROGRESS", "old:CREATE_COMPLETE", "old:UPDATE_COMPLETE")); + assertSample("WARN", 2, null); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void laterDependentFailureRestoresTheFilterThatActuallyMutated(boolean replacement) throws InterruptedException { + String template = template("", ""); + create(template, "old", "WARN", "2"); + MetricFilter before = filter("old"); + String target = replacement ? "new" : "old"; + update(template("", """ + ,"BadSecret":{"Type":"AWS::SecretsManager::Secret","DependsOn":"Filter","Properties":{ + "Name":"%s-bad","SecretString":"explicit","GenerateSecretString":{"PasswordLength":32}}} + """.formatted(stack)), target, "FATAL", "9", "UPDATE_ROLLBACK_COMPLETE"); + assertIdentity("old"); + MetricFilter after = filter("old"); + assertThat(after.toBuilder().creationTime(before.creationTime()).build()).isEqualTo(before); + assertThat(filters()).hasSize(1); + List events = newEvents(); + int changed = indexOf(events, "Filter", "UPDATE_COMPLETE", target); + int failed = indexOf(events, "BadSecret", "UPDATE_FAILED", null); + assertThat(changed).as("filter update completed before dependent failure").isGreaterThanOrEqualTo(0); + assertThat(failed).isGreaterThan(changed); + assertSample("WARN", 2, null); + } + + @Test + void generatedNameStaysStableWhenPatternAndValueReallyChange() throws InterruptedException { + String template = """ + {"Resources":{"Filter":{"Type":"AWS::Logs::MetricFilter","Properties":{ + "LogGroupName":"%s","FilterPattern":"{ $.latency = * }", + "MetricTransformations":[{"MetricName":"Count","MetricNamespace":"%s","MetricValue":"$.latency", + "Dimensions":[{"Key":"Route","Value":"$.route"}],"Unit":"Count"}]}}}, + "Outputs":{"FilterRef":{"Value":{"Ref":"Filter"}}}} + """.formatted(group, stack); + cfn.createStack(r -> r.stackName(stack).templateBody(template)); + awaitStatus("CREATE_COMPLETE"); + String name = output(); + assertThat(name).startsWith(stack + "-Filter-"); + captureEvents(); + cfn.updateStack(r -> r.stackName(stack).templateBody(template.replace("$.latency = *", "$.latency > 10") + .replace("\"MetricValue\":\"$.latency\"", "\"MetricValue\":\"3\""))); + awaitStatus("UPDATE_COMPLETE"); + assertIdentity(name); + assertThat(filters()).hasSize(1); + assertThat(filter(name).filterPattern()).isEqualTo("{ $.latency > 10 }"); + assertThat(filter(name).metricTransformations().get(0).metricValue()).isEqualTo("3"); + assertSample("{\"latency\":20,\"route\":\"/orders\"}", 3, "/orders"); + } + + @Test + void collidingReplacementNeverAdoptsOrRollbackDeletesUnrelatedFilter() throws InterruptedException { + create(template("", ""), "old", "WARN", "2"); + logs.putMetricFilter(r -> r.logGroupName(group).filterName("taken").filterPattern("UNRELATED") + .metricTransformations(MetricTransformation.builder().metricName("Other") + .metricNamespace(stack + "/unrelated").metricValue("7").build())); + MetricFilter unrelated = filter("taken"); + update(template("", ""), "taken", "FATAL", "9", "UPDATE_ROLLBACK_COMPLETE"); + assertIdentity("old"); + assertThat(filter("taken")).isEqualTo(unrelated); + assertThat(filters()).hasSize(2); + assertSample("WARN", 2, null); + deleteStack(); + assertThat(filters()).containsExactly(unrelated); + } + + private void create(String template, String name, String pattern, String value) throws InterruptedException { + cfn.createStack(r -> r.stackName(stack).templateBody(template).parameters(parameters(name, pattern, value))); + awaitStatus("CREATE_COMPLETE"); + } + + private void update(String template, String name, String pattern, String value, String status) + throws InterruptedException { + captureEvents(); + cfn.updateStack(r -> r.stackName(stack).templateBody(template).parameters(parameters(name, pattern, value))); + awaitStatus(status); + } + + private String template(String retain, String extra) { + return """ + {"Parameters":{"FilterName":{"Type":"String"},"Pattern":{"Type":"String"},"Value":{"Type":"String"}}, + "Resources":{"Filter":{"Type":"AWS::Logs::MetricFilter",%s"Properties":{ + "LogGroupName":"%s","FilterName":{"Ref":"FilterName"},"FilterPattern":{"Ref":"Pattern"}, + "MetricTransformations":[{"MetricName":"Count","MetricNamespace":"%s","MetricValue":{"Ref":"Value"}, + "DefaultValue":7,"Unit":"Count"}],"ApplyOnTransformedLogs":false}}%s}, + "Outputs":{"FilterRef":{"Value":{"Ref":"Filter"}}}} + """.formatted(retain.isEmpty() ? "" : "\"UpdateReplacePolicy\": \"" + retain + "\",", group, stack, extra); + } + + private static List parameters(String name, String pattern, String value) { + return List.of(Parameter.builder().parameterKey("FilterName").parameterValue(name).build(), + Parameter.builder().parameterKey("Pattern").parameterValue(pattern).build(), + Parameter.builder().parameterKey("Value").parameterValue(value).build()); + } + + private List filters() { + return logs.describeMetricFiltersPaginator(r -> r.logGroupName(group)).metricFilters().stream().toList(); + } + + private MetricFilter filter(String name) { + return filters().stream().filter(f -> name.equals(f.filterName())).findFirst() + .orElseThrow(() -> new AssertionError("Missing backing filter " + name + ": " + filters())); + } + + private void assertUnrelated(Map expected) { + assertThat(filters().stream().filter(f -> f.filterName().startsWith("unrelated-")) + .collect(Collectors.toMap(MetricFilter::filterName, f -> f))).isEqualTo(expected); + } + + private String output() { + return cfn.describeStacks(r -> r.stackName(stack)).stacks().get(0).outputs().stream() + .filter(o -> "FilterRef".equals(o.outputKey())).map(Output::outputValue).findFirst().orElseThrow(); + } + + private void assertIdentity(String name) { + assertThat(output()).isEqualTo(name); + assertThat(cfn.describeStackResource(r -> r.stackName(stack).logicalResourceId("Filter")) + .stackResourceDetail().physicalResourceId()).isEqualTo(name); + } + + private void captureEvents() { + priorEvents = cfn.describeStackEvents(r -> r.stackName(stack)).stackEvents().stream() + .map(StackEvent::eventId).collect(Collectors.toSet()); + } + + private List newEvents() { + List events = new ArrayList<>(cfn.describeStackEvents(r -> r.stackName(stack)).stackEvents()); + Collections.reverse(events); + return events.stream().filter(e -> !priorEvents.contains(e.eventId())).toList(); + } + + private void assertSequence(List expected) { + List events = newEvents(); + assertThat(events).extracting(StackEvent::eventId).doesNotHaveDuplicates().doesNotContainNull(); + assertThat(events.stream().filter(e -> "Filter".equals(e.logicalResourceId())) + .map(e -> e.physicalResourceId() + ":" + e.resourceStatusAsString()).toList()).isEqualTo(expected); + } + + private static int indexOf(List events, String logical, String status, String physical) { + for (int i = 0; i < events.size(); i++) { + StackEvent e = events.get(i); + if (logical.equals(e.logicalResourceId()) && status.equals(e.resourceStatusAsString()) + && (physical == null || physical.equals(e.physicalResourceId()))) { + return i; + } + } + return -1; + } + + private void awaitStatus(String status) throws InterruptedException { + await(() -> status.equals(cfn.describeStacks(r -> r.stackName(stack)).stacks().get(0).stackStatusAsString()) + && newEvents().stream().anyMatch(e -> stack.equals(e.logicalResourceId()) + && status.equals(e.resourceStatusAsString())), "new operation " + status); + } + + private void await(BooleanSupplier condition, String expected) throws InterruptedException { + long deadline = System.nanoTime() + 60_000_000_000L; + while (System.nanoTime() < deadline) { + if (condition.getAsBoolean()) { + return; + } + Thread.sleep(100); + } + throw new AssertionError(stack + " timed out waiting for " + expected + "; events: " + newEvents()); + } + + private void assertSample(String message, double expected, String route) throws InterruptedException { + Instant timestamp = Instant.now(); + logs.putLogEvents(r -> r.logGroupName(group).logStreamName("events") + .logEvents(InputLogEvent.builder().timestamp(timestamp.toEpochMilli()).message(message).build())); + await(() -> { + List data = metrics.getMetricStatistics(r -> { + r.namespace(stack).metricName("Count").period(60).startTime(timestamp.minusSeconds(60)) + .endTime(timestamp.plusSeconds(60)).statistics(Statistic.SUM, Statistic.SAMPLE_COUNT); + if (route != null) { + r.dimensions(Dimension.builder() + .name("Route").value(route).build()); + } + }).datapoints(); + return data.stream().mapToDouble(Datapoint::sum).sum() == expected + && data.stream().mapToDouble(Datapoint::sampleCount).sum() == 1; + }, "one published sample of " + expected); + } + + private void deleteStack() throws InterruptedException { + try { + cfn.deleteStack(r -> r.stackName(stack)); + await(() -> { + try { + return "DELETE_COMPLETE".equals(cfn.describeStacks(r -> r.stackName(stack)).stacks().get(0) + .stackStatusAsString()); + } catch (CloudFormationException e) { + if ("ValidationError".equals(e.awsErrorDetails().errorCode()) && e.getMessage().contains("does not exist")) { + return true; + } + throw e; + } + }, "stack deletion"); + } catch (CloudFormationException e) { + if (!"ValidationError".equals(e.awsErrorDetails().errorCode()) || !e.getMessage().contains("does not exist")) { + throw e; + } + } + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsMetricFilterTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsMetricFilterTest.java new file mode 100644 index 0000000000..2d5e688f91 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsMetricFilterTest.java @@ -0,0 +1,424 @@ +package com.floci.test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; +import software.amazon.awssdk.services.cloudwatch.model.Datapoint; +import software.amazon.awssdk.services.cloudwatch.model.Dimension; +import software.amazon.awssdk.services.cloudwatch.model.Metric; +import software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery; +import software.amazon.awssdk.services.cloudwatch.model.MetricDataResult; +import software.amazon.awssdk.services.cloudwatch.model.MetricStat; +import software.amazon.awssdk.services.cloudwatch.model.Statistic; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent; +import software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException; +import software.amazon.awssdk.services.cloudwatchlogs.model.MetricFilter; +import software.amazon.awssdk.services.cloudwatchlogs.model.MetricFilterMatchRecord; +import software.amazon.awssdk.services.cloudwatchlogs.model.MetricTransformation; +import software.amazon.awssdk.services.cloudwatchlogs.model.ResourceNotFoundException; +import software.amazon.awssdk.services.cloudwatchlogs.model.StandardUnit; +import software.amazon.awssdk.services.sts.StsClient; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Metric filters through the CloudWatch Logs SDK: put, describe, test and delete, and the metric a + * filter publishes for matching log events, read back through the CloudWatch SDK. + */ +@DisplayName("CloudWatch Logs metric filters") +class CloudWatchLogsMetricFilterTest { + + private static CloudWatchLogsClient logs; + private static CloudWatchClient cloudWatch; + private static String group; + private static String namespace; + + @BeforeAll + static void setup() { + logs = TestFixtures.cloudWatchLogsClient(); + cloudWatch = TestFixtures.cloudWatchClient(); + group = "/test/" + TestFixtures.uniqueName("metric-filter"); + namespace = TestFixtures.uniqueName("MetricFilterTest"); + logs.createLogGroup(r -> r.logGroupName(group)); + logs.createLogStream(r -> r.logGroupName(group).logStreamName("web")); + } + + @AfterAll + static void cleanup() { + if (logs != null) { + try { + logs.deleteLogGroup(r -> r.logGroupName(group)); + } catch (Exception e) { + System.err.println("metric filter cleanup skipped: " + e.getMessage()); + } + logs.close(); + } + if (cloudWatch != null) { + cloudWatch.close(); + } + } + + @Test + void aFilterIsStoredDescribedTestedAndPublishesItsMetric() throws InterruptedException { + logs.putMetricFilter(r -> r + .logGroupName(group) + .filterName("volume") + .filterPattern("[..., status_code, size]") + .metricTransformations(MetricTransformation.builder() + .metricName("Volume") + .metricNamespace(namespace) + .metricValue("$size") + .dimensions(Map.of("Status", "$status_code")) + .unit(StandardUnit.BYTES) + .build())); + + List described = logs.describeMetricFilters(r -> r.logGroupName(group)).metricFilters(); + assertThat(described).hasSize(1); + MetricFilter filter = described.get(0); + assertThat(filter.filterName()).isEqualTo("volume"); + assertThat(filter.logGroupName()).isEqualTo(group); + assertThat(filter.filterPattern()).isEqualTo("[..., status_code, size]"); + assertThat(filter.creationTime()).isPositive(); + MetricTransformation t = filter.metricTransformations().get(0); + assertThat(t.metricName()).isEqualTo("Volume"); + assertThat(t.metricNamespace()).isEqualTo(namespace); + assertThat(t.metricValue()).isEqualTo("$size"); + assertThat(t.dimensions()).containsExactlyEntriesOf(Map.of("Status", "$status_code")); + assertThat(t.unit()).isEqualTo(StandardUnit.BYTES); + assertThat(logs.describeMetricFilters(r -> r.metricName("Volume").metricNamespace(namespace)).metricFilters()) + .extracting(MetricFilter::logGroupName) + .contains(group); + assertThat(logs.describeLogGroups(r -> r.logGroupNamePrefix(group)).logGroups().get(0).metricFilterCount()) + .isEqualTo(1); + + List matches = logs.testMetricFilter(r -> r + .filterPattern("[..., status_code=200, size]") + .logEventMessages( + "127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 1534", + "127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] \"GET /apache_pb.gif HTTP/1.0\" 500 5324")) + .matches(); + assertThat(matches).hasSize(1); + assertThat(matches.get(0).eventNumber()).isEqualTo(1); + assertThat(matches.get(0).extractedValues()).containsEntry("$size", "1534").containsEntry("$status_code", "200") + .containsEntry("$1", "127.0.0.1"); + + List jsonMatches = logs.testMetricFilter(r -> r + .filterPattern("{ $.latency = * }") + .logEventMessages("plain text", "{\"latency\":50}")).matches(); + assertThat(jsonMatches).hasSize(1); + assertThat(jsonMatches.get(0).eventNumber()).isEqualTo(2); + assertThat(jsonMatches.get(0).extractedValues()).isEmpty(); + + long now = System.currentTimeMillis(); + logs.putLogEvents(r -> r.logGroupName(group).logStreamName("web").logEvents( + InputLogEvent.builder().timestamp(now).message( + "127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] \"GET /index.html HTTP/1.0\" 200 1534").build(), + InputLogEvent.builder().timestamp(now + 1).message( + "127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] \"GET /index.html HTTP/1.0\" 500 5324").build(), + InputLogEvent.builder().timestamp(now + 2).message( + "127.0.0.1 - frank [10/Oct/2000:13:50:35 -0700] \"GET /index.html HTTP/1.0\" 200 4355").build())); + + long deadline = System.nanoTime() + 10_000_000_000L; + List datapoints; + do { + datapoints = cloudWatch.getMetricStatistics(r -> r + .namespace(namespace) + .metricName("Volume") + .dimensions(Dimension.builder().name("Status").value("200").build()) + .startTime(Instant.ofEpochMilli(now).minusSeconds(120)) + .endTime(Instant.ofEpochMilli(now).plusSeconds(120)) + .period(300) + .statistics(Statistic.SUM, Statistic.SAMPLE_COUNT)).datapoints(); + if (datapoints.stream().mapToDouble(Datapoint::sampleCount).sum() >= 2) { + break; + } + Thread.sleep(50); + } while (System.nanoTime() < deadline); + assertThat(datapoints.stream().mapToDouble(Datapoint::sum).sum()).isEqualTo(1534 + 4355); + assertThat(datapoints.stream().mapToDouble(Datapoint::sampleCount).sum()).isEqualTo(2); + + logs.deleteMetricFilter(r -> r.logGroupName(group).filterName("volume")); + assertThat(logs.describeMetricFilters(r -> r.logGroupName(group)).metricFilters()).isEmpty(); + assertThatThrownBy(() -> logs.deleteMetricFilter(r -> r.logGroupName(group).filterName("volume"))) + .isInstanceOf(ResourceNotFoundException.class); + } + + @Test + void theApiRejectsWhatAwsRejects() { + assertThatThrownBy(() -> logs.putMetricFilter(r -> r + .logGroupName(group) + .filterName("broken") + .filterPattern("{ $.a = }") + .metricTransformations(MetricTransformation.builder() + .metricName("A").metricNamespace(namespace).metricValue("1").build()))) + .isInstanceOf(InvalidParameterException.class) + .hasMessageContaining("Invalid filter pattern"); + assertThatThrownBy(() -> logs.putMetricFilter(r -> r + .logGroupName("/test/no-such-group") + .filterName("x") + .filterPattern("ERROR") + .metricTransformations(MetricTransformation.builder() + .metricName("A").metricNamespace(namespace).metricValue("1").build()))) + .isInstanceOf(ResourceNotFoundException.class); + assertThatThrownBy(() -> logs.putMetricFilter(r -> r + .logGroupName(group) + .filterName("dims") + .filterPattern("ERROR") + .metricTransformations(MetricTransformation.builder() + .metricName("A").metricNamespace(namespace).metricValue("1") + .dimensions(Map.of("a", "$.a")).build()))) + .as("dimensions need a JSON or space-delimited pattern") + .isInstanceOf(InvalidParameterException.class); + assertThatThrownBy(() -> logs.describeMetricFilters(r -> r.metricNamespace(namespace))) + .isInstanceOf(InvalidParameterException.class) + .hasMessageContaining("must contain both MetricName and MetricNamespace"); + assertThatThrownBy(() -> logs.describeMetricFilters(r -> r.metricName("A"))) + .isInstanceOf(InvalidParameterException.class) + .hasMessageContaining("must contain both MetricName and MetricNamespace"); + } + + @Test + void recordedDefaultsAndQuietTrafficReadIdenticallyOverJsonAndQuery() throws Exception { + JsonNode fixture = publishingFixture().get("metricDefaults"); + String isolated = createIsolatedGroup(); + Instant minute = Instant.ofEpochSecond((Instant.now().getEpochSecond() / 60 - 30) * 60); + try { + int index = 0; + for (JsonNode scenario : fixture.get("cases")) { + String metric = "defaults" + index++; + put(isolated, metric, fixture, null, List.of()); + for (JsonNode batch : scenario.get("batches")) { + List messages = new ArrayList<>(); + batch.forEach(m -> messages.add(m.asText())); + ingest(isolated, minute, messages); + } + assertSeries(metric, minute, List.of(), scenario.get("expected")); + minute = minute.plusSeconds(60); + } + put(isolated, "quiet", fixture, null, List.of()); + ingest(isolated, minute, List.of("INFO", "INFO", "INFO")); + JsonNode quiet = new ObjectMapper().readTree("{\"Sum\":21,\"SampleCount\":3}"); + assertSeries("quiet", minute, List.of(), quiet); + assertSeries("quiet", minute, List.of(), quiet); + assertSeries("quiet", minute.plusSeconds(60), List.of(), null); + } finally { + logs.deleteLogGroup(r -> r.logGroupName(isolated)); + } + } + + @Test + void recordedExtractionAndOrdinaryDimensionSeriesReadIdenticallyOverJsonAndQuery() throws Exception { + JsonNode fixture = publishingFixture(); + String isolated = createIsolatedGroup(); + Instant minute = Instant.ofEpochSecond((Instant.now().getEpochSecond() / 60 - 20) * 60); + try { + JsonNode extraction = fixture.get("extraction"); + put(isolated, "extraction", extraction, null, List.of()); + List minutes = new ArrayList<>(); + for (JsonNode scenario : extraction.get("cases")) { + minutes.add(minute); + ingest(isolated, minute, List.of(scenario.get("message").toString())); + minute = minute.plusSeconds(60); + } + // The publisher writes batches in order: once the last scenario's control is visible, + // the earlier scenarios, including the one that must publish nothing, are settled. + List cases = new ArrayList<>(); + extraction.get("cases").forEach(cases::add); + int last = cases.size() - 1; + assertSeries("extraction", minutes.get(last), List.of(), cases.get(last).get("expected")); + for (int i = 0; i < cases.size(); i++) { + assertSeries("extraction", minutes.get(i), List.of(), cases.get(i).get("expected")); + } + JsonNode dimensions = fixture.get("ordinaryDimensions"); + put(isolated, "dimensions", dimensions, Map.of("A", "$.a", "B", "$.b"), List.of()); + List messages = new ArrayList<>(); + dimensions.get("messages").forEach(m -> messages.add(m.toString())); + ingest(isolated, minute, messages); + for (JsonNode series : dimensions.get("expectedSeries")) { + assertSeries("dimensions", minute, dimensions(series.get("dimensions"), ""), series); + } + for (JsonNode series : dimensions.get("absentSeries")) { + assertSeries("dimensions", minute, dimensions(series.get("dimensions"), ""), null); + } + assertThat(cloudWatch.listMetrics(r -> r.namespace(namespace).metricName("dimensions")).metrics()) + .extracting(m -> m.dimensions().stream().map(d -> d.name() + "=" + d.value()).sorted().toList()) + .containsExactlyInAnyOrder(List.of(), List.of("A=alpha", "B=beta")); + } finally { + logs.deleteLogGroup(r -> r.logGroupName(isolated)); + } + } + + @Test + void recordedSystemDimensionsDoNotAttachToPatternNonmatchDefaults() throws Exception { + JsonNode fixture = publishingFixture().get("systemDimensions"); + String isolated = createIsolatedGroup(); + String account; + try (StsClient sts = TestFixtures.stsClient()) { + account = sts.getCallerIdentity().account(); + } + Instant minute = Instant.ofEpochSecond((Instant.now().getEpochSecond() / 60 - 10) * 60); + try { + int index = 0; + for (JsonNode scenario : fixture.get("cases")) { + List fields = new ArrayList<>(); + scenario.get("emitSystemFieldDimensions").forEach(f -> fields.add(f.asText())); + String metric = "system" + index++; + put(isolated, metric, fixture, null, fields); + ingest(isolated, minute, List.of("ERROR", "INFO")); + for (JsonNode series : scenario.get("expectedSeries")) { + assertSeries(metric, minute, dimensions(series.get("dimensions"), account), series); + } + assertThat(cloudWatch.listMetrics(r -> r.namespace(namespace).metricName(metric)).metrics()).hasSize(2); + minute = minute.plusSeconds(60); + } + } finally { + logs.deleteLogGroup(r -> r.logGroupName(isolated)); + } + } + + @Test + void quietDefaultsDriveASumAlarmWithoutLaterIngestion() throws Exception { + String isolated = createIsolatedGroup(); + String alarm = TestFixtures.uniqueName("metric-filter-alarm"); + try { + put(isolated, "alarm", publishingFixture().get("metricDefaults"), null, List.of()); + cloudWatch.putMetricAlarm(r -> r.alarmName(alarm).namespace(namespace).metricName("alarm") + .statistic(Statistic.SUM).period(60).evaluationPeriods(1).threshold(20.0) + .comparisonOperator("GreaterThanThreshold").treatMissingData("notBreaching").actionsEnabled(false)); + Instant minute = Instant.ofEpochSecond((Instant.now().getEpochSecond() / 60 - 1) * 60); + ingest(isolated, minute, List.of("INFO", "INFO", "INFO")); + assertSeries("alarm", minute, List.of(), new ObjectMapper().readTree("{\"Sum\":21,\"SampleCount\":3}")); + long deadline = System.nanoTime() + 25_000_000_000L; + String state; + do { + state = cloudWatch.describeAlarms(r -> r.alarmNames(alarm)).metricAlarms().get(0).stateValueAsString(); + if ("ALARM".equals(state)) { + break; + } + Thread.sleep(100); + } while (System.nanoTime() < deadline); + assertThat(state).isEqualTo("ALARM"); + } finally { + cloudWatch.deleteAlarms(r -> r.alarmNames(alarm)); + logs.deleteLogGroup(r -> r.logGroupName(isolated)); + } + } + + private static JsonNode publishingFixture() throws Exception { + return MetricFilterFixture.load(); + } + + private static String createIsolatedGroup() { + String isolated = group + "-" + TestFixtures.uniqueName("publishing"); + logs.createLogGroup(r -> r.logGroupName(isolated)); + try { + logs.createLogStream(r -> r.logGroupName(isolated).logStreamName("s")); + } catch (RuntimeException failure) { + logs.deleteLogGroup(r -> r.logGroupName(isolated)); + throw failure; + } + return isolated; + } + + private static void put(String isolated, String metric, JsonNode definition, + Map dimensions, List systemFields) { + logs.putMetricFilter(r -> r.logGroupName(isolated).filterName("probe") + .filterPattern(definition.get("filterPattern").asText()).emitSystemFieldDimensions(systemFields) + .metricTransformations(MetricTransformation.builder().metricNamespace(namespace).metricName(metric) + .metricValue(definition.get("metricValue").asText()) + .defaultValue(definition.has("defaultValue") ? definition.get("defaultValue").asDouble() : null) + .dimensions(dimensions).unit(StandardUnit.COUNT).build())); + } + + private static void ingest(String isolated, Instant minute, List messages) { + List events = new ArrayList<>(); + for (int i = 0; i < messages.size(); i++) { + events.add(InputLogEvent.builder().timestamp(minute.toEpochMilli() + i).message(messages.get(i)).build()); + } + assertThat(logs.putLogEvents(r -> r.logGroupName(isolated).logStreamName("s").logEvents(events))) + .satisfies(response -> assertThat(response.rejectedLogEventsInfo()).isNull()); + } + + private static List dimensions(JsonNode values, String account) { + List dimensions = new ArrayList<>(); + values.fields().forEachRemaining(e -> dimensions.add(Dimension.builder().name(e.getKey()).value( + e.getKey().equals("@aws.region") ? "us-east-1" + : e.getValue().asText().replace("", account)).build())); + return dimensions; + } + + /** + * A present series is awaited: the publisher writes it shortly after PutLogEvents returns, as + * AWS does. An absent series is asserted once; callers await a later positive control first. + * The window ends at second 59 so a sample stamped on the next minute is never in scope. + */ + private static void assertSeries(String metric, Instant minute, List dimensions, JsonNode expected) + throws Exception { + boolean present = expected != null && expected.has("Sum"); + List statistics = MetricFilterQueryAssertions.STATISTICS.stream().map(Statistic::fromValue).toList(); + List points = statistics(metric, minute, dimensions, statistics); + long deadline = System.nanoTime() + 10_000_000_000L; + while (present && points.isEmpty() && System.nanoTime() < deadline) { + Thread.sleep(50); + points = statistics(metric, minute, dimensions, statistics); + } + assertThat(points).as(metric + " at " + minute + " " + dimensions).hasSize(present ? 1 : 0); + List recorded = present ? MetricFilterQueryAssertions.recorded(expected) : List.of(); + if (present) { + Datapoint point = points.get(0); + assertThat(point.timestamp()).isEqualTo(minute); + for (String stat : recorded) { + assertThat(value(point, stat)).as(stat).isEqualTo(expected.get(stat).asDouble()); + } + assertThat(point.unitAsString()).isEqualTo("Count"); + } + List queries = new ArrayList<>(); + for (String stat : present ? recorded : MetricFilterQueryAssertions.STATISTICS) { + queries.add(MetricDataQuery.builder().id(stat.toLowerCase()).returnData(true).metricStat( + MetricStat.builder().period(60).stat(stat).metric(Metric.builder().namespace(namespace) + .metricName(metric).dimensions(dimensions).build()).build()).build()); + } + List results = cloudWatch.getMetricData(r -> r.startTime(minute) + .endTime(minute.plusSeconds(59)).metricDataQueries(queries)).metricDataResults(); + assertThat(results).extracting(MetricDataResult::id) + .containsExactlyInAnyOrderElementsOf(queries.stream().map(MetricDataQuery::id).toList()); + for (MetricDataResult result : results) { + assertThat(result.statusCodeAsString()).isEqualTo("Complete"); + assertThat(result.timestamps()).containsExactlyElementsOf(present ? List.of(minute) : List.of()); + if (present) { + String stat = recorded.stream().filter(name -> name.toLowerCase().equals(result.id())).findFirst().orElseThrow(); + assertThat(result.values()).containsExactly(expected.get(stat).asDouble()); + } else { + assertThat(result.values()).isEmpty(); + } + } + MetricFilterQueryAssertions.assertSeries(namespace, metric, minute, dimensions, expected); + } + + private static List statistics(String metric, Instant minute, List dimensions, + List statistics) { + return cloudWatch.getMetricStatistics(r -> r.namespace(namespace).metricName(metric).dimensions(dimensions) + .startTime(minute).endTime(minute.plusSeconds(59)).period(60).statistics(statistics)).datapoints(); + } + + private static double value(Datapoint point, String stat) { + return switch (stat) { + case "Sum" -> point.sum(); + case "SampleCount" -> point.sampleCount(); + case "Minimum" -> point.minimum(); + case "Maximum" -> point.maximum(); + default -> throw new IllegalArgumentException(stat); + }; + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterFixture.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterFixture.java new file mode 100644 index 0000000000..11ac7b319c --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterFixture.java @@ -0,0 +1,21 @@ +package com.floci.test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.io.InputStream; + +final class MetricFilterFixture { + private MetricFilterFixture() {} + + static JsonNode load() throws IOException { + try (InputStream fixture = MetricFilterFixture.class.getResourceAsStream( + "/cloudwatchlogs/metric-filter-publishing-aws.json")) { + if (fixture == null) { + throw new IOException("Missing packaged metric-filter publishing oracle"); + } + return new ObjectMapper().readTree(fixture); + } + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterFixturePackagingTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterFixturePackagingTest.java new file mode 100644 index 0000000000..4623e915df --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterFixturePackagingTest.java @@ -0,0 +1,20 @@ +package com.floci.test; + +import com.fasterxml.jackson.databind.JsonNode; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Offline: deliberately has no SDK clients or SDK lifecycle hooks. */ +class MetricFilterFixturePackagingTest { + @Test + void canonicalPublishingFixtureLoadsFromTheModuleClasspath() throws Exception { + JsonNode fixture = MetricFilterFixture.load(); + assertThat(fixture).as("The module must load its packaged publishing oracle").isNotNull(); + assertThat(fixture.get("verification").asText()).isEqualTo("Verified on Live AWS"); + assertThat(fixture.get("metricDefaults").get("cases").get(0).get("expected").get("Sum").asDouble()) + .isEqualTo(13); + assertThat(fixture.get("extraction").get("cases").size()).isEqualTo(4); + assertThat(fixture.get("systemDimensions").get("cases").size()).isEqualTo(3); + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterQueryAssertions.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterQueryAssertions.java new file mode 100644 index 0000000000..c22d04272a --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/MetricFilterQueryAssertions.java @@ -0,0 +1,144 @@ +package com.floci.test; + +import com.fasterxml.jackson.databind.JsonNode; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.signer.Aws4Signer; +import software.amazon.awssdk.auth.signer.params.Aws4SignerParams; +import software.amazon.awssdk.http.SdkHttpFullRequest; +import software.amazon.awssdk.http.SdkHttpMethod; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudwatch.model.Dimension; + +import java.io.ByteArrayInputStream; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilderFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** The current SDK selects JSON. Exercise the legacy AWS Query contract without an older SDK dependency. */ +final class MetricFilterQueryAssertions { + private static final HttpClient HTTP = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); + + /** Every statistic the AWS oracle can record for a series; a fixture asserts the ones it has. */ + static final List STATISTICS = List.of("Sum", "SampleCount", "Minimum", "Maximum"); + + private MetricFilterQueryAssertions() {} + + static List recorded(JsonNode expected) { + return STATISTICS.stream().filter(expected::has).toList(); + } + + static void assertSeries(String namespace, String metric, Instant minute, + List dimensions, JsonNode expected) throws Exception { + boolean present = expected != null && expected.has("Sum"); + Map params = window(minute); + params.put("Namespace", namespace); + params.put("MetricName", metric); + params.put("Period", "60"); + for (int i = 0; i < STATISTICS.size(); i++) { + params.put("Statistics.member." + (i + 1), STATISTICS.get(i)); + } + dimensions(params, "Dimensions", dimensions); + Document stats = query("GetMetricStatistics", params); + NodeList points = stats.getElementsByTagNameNS("*", "Timestamp"); + assertThat(points.getLength()).isEqualTo(present ? 1 : 0); + if (present) { + assertThat(Instant.parse(points.item(0).getTextContent())).isEqualTo(minute); + for (String stat : recorded(expected)) { + assertThat(Double.parseDouble(text(stats.getDocumentElement(), stat))).as(stat) + .isEqualTo(expected.get(stat).asDouble()); + } + assertThat(text(stats.getDocumentElement(), "Unit")).isEqualTo("Count"); + } + for (String stat : present ? recorded(expected) : STATISTICS) { + params = window(minute); + String prefix = "MetricDataQueries.member.1."; + params.put(prefix + "Id", "probe"); + params.put(prefix + "ReturnData", "true"); + params.put(prefix + "MetricStat.Stat", stat); + params.put(prefix + "MetricStat.Period", "60"); + params.put(prefix + "MetricStat.Metric.Namespace", namespace); + params.put(prefix + "MetricStat.Metric.MetricName", metric); + dimensions(params, prefix + "MetricStat.Metric.Dimensions", dimensions); + Document data = query("GetMetricData", params); + assertThat(text(data.getDocumentElement(), "Id")).isEqualTo("probe"); + assertThat(text(data.getDocumentElement(), "StatusCode")).isEqualTo("Complete"); + Element values = (Element) data.getElementsByTagNameNS("*", "Values").item(0); + Element times = (Element) data.getElementsByTagNameNS("*", "Timestamps").item(0); + assertThat(values.getElementsByTagNameNS("*", "member").getLength()).isEqualTo(present ? 1 : 0); + assertThat(times.getElementsByTagNameNS("*", "member").getLength()).isEqualTo(present ? 1 : 0); + if (present) { + assertThat(Double.parseDouble(text(values, "member"))).isEqualTo(expected.get(stat).asDouble()); + assertThat(Instant.parse(text(times, "member"))).isEqualTo(minute); + } + } + } + + private static Map window(Instant minute) { + Map params = new LinkedHashMap<>(); + params.put("StartTime", minute.toString()); + params.put("EndTime", minute.plusSeconds(59).toString()); + return params; + } + + private static void dimensions(Map params, String prefix, List dimensions) { + for (int i = 0; i < dimensions.size(); i++) { + params.put(prefix + ".member." + (i + 1) + ".Name", dimensions.get(i).name()); + params.put(prefix + ".member." + (i + 1) + ".Value", dimensions.get(i).value()); + } + } + + private static String text(Element element, String name) { + return element.getElementsByTagNameNS("*", name).item(0).getTextContent(); + } + + private static Document query(String action, Map params) throws Exception { + params.put("Action", action); + params.put("Version", "2010-08-01"); + String body = params.entrySet().stream().map(e -> URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8) + + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8)).collect(Collectors.joining("&")); + byte[] bytes = body.getBytes(StandardCharsets.UTF_8); + SdkHttpFullRequest signed = Aws4Signer.create().sign(SdkHttpFullRequest.builder().uri(TestFixtures.endpoint()) + .method(SdkHttpMethod.POST).putHeader("Content-Type", "application/x-www-form-urlencoded") + .contentStreamProvider(() -> new ByteArrayInputStream(bytes)).build(), + Aws4SignerParams.builder().awsCredentials(AwsBasicCredentials.create("test", "test")) + .signingRegion(Region.US_EAST_1).signingName("monitoring").build()); + HttpRequest.Builder request = HttpRequest.newBuilder(TestFixtures.endpoint()).timeout(Duration.ofSeconds(10)) + .POST(HttpRequest.BodyPublishers.ofByteArray(bytes)); + signed.headers().forEach((name, values) -> { + if (!name.equalsIgnoreCase("Host") && !name.equalsIgnoreCase("Content-Length")) { + values.forEach(value -> request.header(name, value)); + } + }); + HttpResponse response = HTTP.send(request.build(), HttpResponse.BodyHandlers.ofString()); + assertThat(response.statusCode()).as(response.body()).isEqualTo(200); + // This independent SDK module cannot import root XmlParser. Mirror its hardened JDK DOM configuration. + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + factory.setXIncludeAware(false); + factory.setExpandEntityReferences(false); + Document document = factory.newDocumentBuilder().parse(new ByteArrayInputStream( + response.body().getBytes(StandardCharsets.UTF_8))); + assertThat(document.getDocumentElement().getLocalName()).isEqualTo(action + "Response"); + assertThat(document.getDocumentElement().getNamespaceURI()).isEqualTo("http://monitoring.amazonaws.com/doc/2010-08-01/"); + return document; + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json b/compatibility-tests/sdk-test-java/src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json new file mode 100644 index 0000000000..bf828c42b6 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json @@ -0,0 +1,112 @@ +{ + "verification": "Verified on Live AWS", + "observedDate": "2026-09-16", + "region": "eu-west-1", + "awsCliVersion": "2.36.45", + "description": "Normalized observations from isolated synthetic PutMetricFilter/PutLogEvents/GetMetricStatistics probes. Values are independent AWS results, not emulator-generated expectations.", + "period": 60, + "observedThrough": "2026-09-16T09:06:51Z", + "metricDefaults": { + "filterPattern": "ERROR", + "metricValue": "3", + "defaultValue": 7, + "cases": [ + { + "name": "mixed events in one batch", + "batches": [["ERROR", "INFO", "ERROR"]], + "expected": {"Sum": 13, "SampleCount": 3, "Minimum": 3, "Maximum": 7} + }, + { + "name": "hit then miss in separate batches", + "batches": [["ERROR"], ["INFO"]], + "expected": {"Sum": 10, "SampleCount": 2, "Minimum": 3, "Maximum": 7} + }, + { + "name": "miss then hit in separate batches", + "batches": [["INFO"], ["ERROR"]], + "expected": {"Sum": 10, "SampleCount": 2, "Minimum": 3, "Maximum": 7} + } + ], + "qualification": "Each case used one isolated event-time minute. Results converged through repeated reads over approximately 107 seconds. No minute-wide suppression or default deduplication was observed." + }, + "extraction": { + "filterPattern": "{ $.probe = \"value\" || $.value = * }", + "metricValue": "$.value", + "defaultValue": 7, + "cases": [ + { + "name": "matching event with missing value", + "message": {"probe": "value", "case": "missing"}, + "expected": {"Sum": 7, "SampleCount": 1} + }, + { + "name": "matching event with null value", + "message": {"probe": "value", "value": null, "case": "null"}, + "expected": {"Sum": 7, "SampleCount": 1} + }, + { + "name": "matching event with nonnumeric string", + "message": {"probe": "value", "value": "not-a-number", "case": "nonnumeric"}, + "expected": {"datapoints": []} + }, + { + "name": "matching numeric control", + "message": {"probe": "value", "value": 3, "case": "numeric-control"}, + "expected": {"Sum": 3, "SampleCount": 1} + } + ], + "qualification": "Cases occupied distinct event-time minutes. Matching did not depend on the value field being present. The numeric control succeeded; negative results remained absent through the completed observation window." + }, + "ordinaryDimensions": { + "filterPattern": "{ $.probe = \"dimension\" || $.a = * || $.b = * }", + "metricValue": "1", + "dimensions": {"A": "$.a", "B": "$.b"}, + "messages": [ + {"probe": "dimension", "a": "alpha", "b": "beta"}, + {"probe": "dimension", "a": "alpha"}, + {"probe": "dimension", "b": "beta"}, + {"probe": "dimension"} + ], + "expectedSeries": [ + {"dimensions": {"A": "alpha", "B": "beta"}, "Sum": 1, "SampleCount": 1}, + {"dimensions": {}, "Sum": 3, "SampleCount": 3} + ], + "absentSeries": [ + {"dimensions": {"A": "alpha"}}, + {"dimensions": {"B": "beta"}} + ], + "qualification": "No default configured. All events occupied one minute. ListMetrics confirmed complete and dimensionless series, not partial sets. Null or empty dimension values were not measured." + }, + "systemDimensions": { + "filterPattern": "ERROR", + "metricValue": "3", + "defaultValue": 7, + "messages": ["ERROR", "INFO"], + "accountTransformerPolicyCount": 0, + "groupTransformerConfig": [], + "cases": [ + { + "emitSystemFieldDimensions": ["@aws.account"], + "expectedSeries": [ + {"dimensions": {"@aws.account": ""}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + }, + { + "emitSystemFieldDimensions": ["@aws.region"], + "expectedSeries": [ + {"dimensions": {"@aws.region": "eu-west-1"}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + }, + { + "emitSystemFieldDimensions": ["@aws.account", "@aws.region"], + "expectedSeries": [ + {"dimensions": {"@aws.account": "", "@aws.region": "eu-west-1"}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + } + ], + "qualification": "Regular noncentralized group. Matching events receive requested system dimensions; pattern-nonmatch defaults are dimensionless. This does not measure extraction-fallback defaults or centralized source provenance." + } +} diff --git a/compatibility-tests/sdk-test-python/README.md b/compatibility-tests/sdk-test-python/README.md index 3b99f4fcfc..ce26482b1c 100644 --- a/compatibility-tests/sdk-test-python/README.md +++ b/compatibility-tests/sdk-test-python/README.md @@ -51,6 +51,49 @@ just test-python AWS credentials are always `test` / `test` / `us-east-1`. +## Opt-in metric filter replay against AWS + +`tests/metric_filter_replay.py` is a standalone script, not a test: pytest does +not collect it, and `tests/test_metric_filter_replay.py` checks its logic offline +with the network blocked. It does not change `conftest.py`, and `FLOCI_TARGET=aws` +does not select real AWS. + +From the repository root, with the declared requirements installed: + +```bash +python -m pytest compatibility-tests/sdk-test-python/tests/test_metric_filter_replay.py + +# Against a local Floci +python compatibility-tests/sdk-test-python/tests/metric_filter_replay.py \ + --endpoint http://127.0.0.1:4566 --region eu-west-1 \ + --timeout 30 --stable-seconds 2 --poll-interval 1 +``` + +Real AWS mode requires all three safety arguments. **It creates billable +CloudWatch metrics and a tagged log group, and metric series cannot be deleted**; +they remain until AWS ages them out. + +```bash +python compatibility-tests/sdk-test-python/tests/metric_filter_replay.py \ + --aws --profile YOUR_DISPOSABLE_TEST_PROFILE --region eu-west-1 \ + --ack-live-writes I_ACCEPT_AWS_WRITES +``` + +Only commercial AWS regions are accepted, the clients pin the official regional +endpoints, and the group is deleted in `finally` only after its ownership tag is +verified. A cleanup failure exits nonzero and prints the group name. Credentials, +account ids, profile names and raw service errors are never printed. + +The expected values come from `tests/fixtures/metric-filter-publishing-aws.json`, +a byte-identical copy of the root fixture +`src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json`; the root +`CloudWatchLogsMetricFilterFixturePackagingTest` fails when either SDK module copy +drifts. The replay ingests each scenario into its own backdated minute, runs the +quiet-default scenario last with no later ingestion, and accepts a result only +after every expected series, absent series and the idle minute after the quiet one +have stayed stable for a full window (AWS defaults: 240 second deadline, 30 second +window). Output is JSON lines of observations, not a regenerated fixture. + ## Docker ```bash diff --git a/compatibility-tests/sdk-test-python/tests/fixtures/metric-filter-publishing-aws.json b/compatibility-tests/sdk-test-python/tests/fixtures/metric-filter-publishing-aws.json new file mode 100644 index 0000000000..bf828c42b6 --- /dev/null +++ b/compatibility-tests/sdk-test-python/tests/fixtures/metric-filter-publishing-aws.json @@ -0,0 +1,112 @@ +{ + "verification": "Verified on Live AWS", + "observedDate": "2026-09-16", + "region": "eu-west-1", + "awsCliVersion": "2.36.45", + "description": "Normalized observations from isolated synthetic PutMetricFilter/PutLogEvents/GetMetricStatistics probes. Values are independent AWS results, not emulator-generated expectations.", + "period": 60, + "observedThrough": "2026-09-16T09:06:51Z", + "metricDefaults": { + "filterPattern": "ERROR", + "metricValue": "3", + "defaultValue": 7, + "cases": [ + { + "name": "mixed events in one batch", + "batches": [["ERROR", "INFO", "ERROR"]], + "expected": {"Sum": 13, "SampleCount": 3, "Minimum": 3, "Maximum": 7} + }, + { + "name": "hit then miss in separate batches", + "batches": [["ERROR"], ["INFO"]], + "expected": {"Sum": 10, "SampleCount": 2, "Minimum": 3, "Maximum": 7} + }, + { + "name": "miss then hit in separate batches", + "batches": [["INFO"], ["ERROR"]], + "expected": {"Sum": 10, "SampleCount": 2, "Minimum": 3, "Maximum": 7} + } + ], + "qualification": "Each case used one isolated event-time minute. Results converged through repeated reads over approximately 107 seconds. No minute-wide suppression or default deduplication was observed." + }, + "extraction": { + "filterPattern": "{ $.probe = \"value\" || $.value = * }", + "metricValue": "$.value", + "defaultValue": 7, + "cases": [ + { + "name": "matching event with missing value", + "message": {"probe": "value", "case": "missing"}, + "expected": {"Sum": 7, "SampleCount": 1} + }, + { + "name": "matching event with null value", + "message": {"probe": "value", "value": null, "case": "null"}, + "expected": {"Sum": 7, "SampleCount": 1} + }, + { + "name": "matching event with nonnumeric string", + "message": {"probe": "value", "value": "not-a-number", "case": "nonnumeric"}, + "expected": {"datapoints": []} + }, + { + "name": "matching numeric control", + "message": {"probe": "value", "value": 3, "case": "numeric-control"}, + "expected": {"Sum": 3, "SampleCount": 1} + } + ], + "qualification": "Cases occupied distinct event-time minutes. Matching did not depend on the value field being present. The numeric control succeeded; negative results remained absent through the completed observation window." + }, + "ordinaryDimensions": { + "filterPattern": "{ $.probe = \"dimension\" || $.a = * || $.b = * }", + "metricValue": "1", + "dimensions": {"A": "$.a", "B": "$.b"}, + "messages": [ + {"probe": "dimension", "a": "alpha", "b": "beta"}, + {"probe": "dimension", "a": "alpha"}, + {"probe": "dimension", "b": "beta"}, + {"probe": "dimension"} + ], + "expectedSeries": [ + {"dimensions": {"A": "alpha", "B": "beta"}, "Sum": 1, "SampleCount": 1}, + {"dimensions": {}, "Sum": 3, "SampleCount": 3} + ], + "absentSeries": [ + {"dimensions": {"A": "alpha"}}, + {"dimensions": {"B": "beta"}} + ], + "qualification": "No default configured. All events occupied one minute. ListMetrics confirmed complete and dimensionless series, not partial sets. Null or empty dimension values were not measured." + }, + "systemDimensions": { + "filterPattern": "ERROR", + "metricValue": "3", + "defaultValue": 7, + "messages": ["ERROR", "INFO"], + "accountTransformerPolicyCount": 0, + "groupTransformerConfig": [], + "cases": [ + { + "emitSystemFieldDimensions": ["@aws.account"], + "expectedSeries": [ + {"dimensions": {"@aws.account": ""}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + }, + { + "emitSystemFieldDimensions": ["@aws.region"], + "expectedSeries": [ + {"dimensions": {"@aws.region": "eu-west-1"}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + }, + { + "emitSystemFieldDimensions": ["@aws.account", "@aws.region"], + "expectedSeries": [ + {"dimensions": {"@aws.account": "", "@aws.region": "eu-west-1"}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + } + ], + "qualification": "Regular noncentralized group. Matching events receive requested system dimensions; pattern-nonmatch defaults are dimensionless. This does not measure extraction-fallback defaults or centralized source provenance." + } +} diff --git a/compatibility-tests/sdk-test-python/tests/metric_filter_replay.py b/compatibility-tests/sdk-test-python/tests/metric_filter_replay.py new file mode 100644 index 0000000000..6ecc66cc96 --- /dev/null +++ b/compatibility-tests/sdk-test-python/tests/metric_filter_replay.py @@ -0,0 +1,234 @@ +"""Opt-in stateful replay. Import and normal pytest execution never contact AWS.""" + +import argparse +from contextlib import contextmanager, ExitStack +from datetime import datetime, timedelta, timezone +import json +from pathlib import Path +import sys +import time +from urllib.parse import urlsplit +import uuid + +import boto3 +import botocore.session +from botocore.config import Config + +FIXTURE = Path(__file__).resolve().parent / "fixtures/metric-filter-publishing-aws.json" +OWNER_TAG = "floci-metric-replay" + + +class ReplayError(Exception): + """Only fixed, sanitized diagnostics belong in this exception.""" + + +class SafeParser(argparse.ArgumentParser): + def error(self, message): + self.exit(2, "Invalid replay arguments; use --help. No replay was started.\n") + + +def parse_args(argv=None): + parser = SafeParser(description=__doc__) + mode = parser.add_mutually_exclusive_group(required=True) + mode.add_argument("--aws", action="store_true", help="Write to real commercial-partition AWS") + mode.add_argument("--endpoint", help="Explicit loopback Floci endpoint, with dummy credentials") + parser.add_argument("--profile") + parser.add_argument("--region") + parser.add_argument("--ack-live-writes", choices=["I_ACCEPT_AWS_WRITES"]) + parser.add_argument("--timeout", type=float, default=240, help="Maximum observation seconds") + parser.add_argument("--stable-seconds", type=float, default=30, help="Complete stable observation window") + parser.add_argument("--poll-interval", type=float, default=5) + args = parser.parse_args(argv) + if args.aws: + if not args.profile or not args.region or not args.ack_live_writes: + parser.error("AWS requires explicit profile, region and acknowledgement") + if args.region not in botocore.session.get_session().get_available_regions("logs", partition_name="aws"): + parser.error("Only known commercial AWS regions are supported") + if args.stable_seconds < 30 or args.timeout < 120: + parser.error("AWS observations require at least 120 seconds and a 30-second stable window") + else: + try: + endpoint = urlsplit(args.endpoint) + except ValueError: + parser.error("Invalid local endpoint") + if (endpoint.scheme not in ("http", "https") + or endpoint.hostname not in ("localhost", "127.0.0.1", "::1") + or endpoint.username or endpoint.password or endpoint.path not in ("", "/") + or endpoint.query or endpoint.fragment or args.profile or args.ack_live_writes): + parser.error("Local mode requires a credential-free loopback endpoint") + args.region = args.region or "eu-west-1" + if not (0 < args.poll_interval <= args.stable_seconds < args.timeout <= 900): + parser.error("Invalid bounded observation window") + return args + + +def clients(args, session=None): + session = session or (boto3.Session(profile_name=args.profile, region_name=args.region) + if args.aws else boto3.Session(region_name=args.region)) + config = Config(ignore_configured_endpoint_urls=True, connect_timeout=5, read_timeout=15, + retries={"max_attempts": 2, "mode": "standard"}) + result = [] + for service, endpoint_prefix in (("logs", "logs"), ("cloudwatch", "monitoring"), ("sts", "sts")): + endpoint = f"https://{endpoint_prefix}.{args.region}.amazonaws.com" if args.aws else args.endpoint + credentials = {} if args.aws else { + "aws_access_key_id": "test", "aws_secret_access_key": "test", "aws_session_token": ""} + result.append(session.client(service, region_name=args.region, endpoint_url=endpoint, + config=config, **credentials)) + return tuple(result) + + +@contextmanager +def owned_group(logs, group, token): + # A rejected create must never cause deletion of an existing resource. + logs.create_log_group(logGroupName=group, tags={OWNER_TAG: token}) + try: + if logs.list_tags_log_group(logGroupName=group).get("tags", {}).get(OWNER_TAG) != token: + raise ReplayError("created group ownership could not be verified") + logs.create_log_stream(logGroupName=group, logStreamName="probe") + yield + finally: + try: + if logs.list_tags_log_group(logGroupName=group).get("tags", {}).get(OWNER_TAG) != token: + raise ReplayError("cleanup refused: group ownership could not be verified") + logs.delete_log_group(logGroupName=group) + except Exception: + raise ReplayError("cleanup failed or refused; inspect the owned run's log group") from None + + +def await_stable(read, timeout, stable_seconds, interval, monotonic=None, sleep=None): + monotonic, sleep = monotonic or time.monotonic, sleep or time.sleep + deadline = monotonic() + timeout + stable_since = None + while True: + complete = read() + now = monotonic() + if now > deadline: + raise ReplayError("observations did not converge within the bounded window") + stable_since = (now if stable_since is None else stable_since) if complete else None + if stable_since is not None and now - stable_since >= stable_seconds: + return + if now >= deadline: + raise ReplayError("observations did not converge within the bounded window") + sleep(min(interval, deadline - now)) + + +def safe_error(error): + return str(error) if isinstance(error, ReplayError) else type(error).__name__ + + +def scenarios(fixture, start, account, region): + cases = [] + + def add(name, definition, batches, expected, system_fields=None): + cases.append({"name": name, "minute": start + timedelta(minutes=len(cases)), + "definition": definition, "batches": batches, + "expected": expected, "system_fields": system_fields or []}) + + defaults = fixture["metricDefaults"] + for i, scenario in enumerate(defaults["cases"]): + add(f"default-{i}", defaults, scenario["batches"], [{"dimensions": {}, **scenario["expected"]}]) + extraction = fixture["extraction"] + for i, scenario in enumerate(extraction["cases"]): + add(f"extraction-{i}", extraction, [[json.dumps(scenario["message"])]], + [{"dimensions": {}, **scenario["expected"]}]) + ordinary = fixture["ordinaryDimensions"] + add("dimensions", ordinary, [[json.dumps(m) for m in ordinary["messages"]]], + ordinary["expectedSeries"] + [{"datapoints": [], **s} for s in ordinary["absentSeries"]]) + system = fixture["systemDimensions"] + for i, scenario in enumerate(system["cases"]): + expected = [] + for series in scenario["expectedSeries"]: + dimensions = {k: account if k == "@aws.account" else region for k in series["dimensions"]} + expected.append({**series, "dimensions": dimensions}) + add(f"system-{i}", system, [system["messages"]], expected, scenario["emitSystemFieldDimensions"]) + add("quiet", defaults, [["INFO", "INFO", "INFO"]], [{"dimensions": {}, "Sum": 21, "SampleCount": 3}]) + return cases + + +def observe(cw, namespace, cases): + complete = True + observations = [] + for case in cases: + for expected in case["expected"]: + dimensions = expected["dimensions"] + stats = [name for name in ("Sum", "SampleCount", "Minimum", "Maximum") if name in expected] + points = cw.get_metric_statistics(Namespace=namespace, MetricName=case["name"], + Dimensions=[{"Name": k, "Value": v} for k, v in dimensions.items()], + StartTime=case["minute"], EndTime=case["minute"] + timedelta(minutes=1), + Period=60, Statistics=stats or ["Sum", "SampleCount"])["Datapoints"] + present = "Sum" in expected + matches = len(points) == (1 if present else 0) + if matches and present: + matches = (points[0]["Timestamp"] == case["minute"] + and all(points[0].get(stat) == expected[stat] for stat in stats)) + complete = complete and matches + observations.append({"case": case["name"], "timestamp": case["minute"].isoformat(), + "dimensions": {k: "" if k == "@aws.account" else v for k, v in dimensions.items()}, + "datapoints": [{"timestamp": p["Timestamp"].isoformat(), + **{s: p[s] for s in ("Sum", "SampleCount", "Minimum", "Maximum") if s in p}} + for p in points]}) + return complete, observations + + +def main(argv=None): + args = parse_args(argv) + token = uuid.uuid4().hex + group = "/floci-metric-replay/" + token + namespace = "FlociMetricReplay/" + token + print(json.dumps({"mode": "AWS LIVE WRITES" if args.aws else "local Floci only", + "group": group, "namespace": namespace, + "warning": "Metric series cannot be deleted. They remain until the service ages them out."})) + try: + with ExitStack() as stack: + logs, cw, sts = clients(args) + for client in (logs, cw, sts): + stack.callback(client.close) + account = sts.get_caller_identity()["Account"] + start = datetime.now(timezone.utc).replace(second=0, microsecond=0) - timedelta(minutes=40) + cases = scenarios(json.loads(FIXTURE.read_text()), start, account, args.region) + observations = [] + with owned_group(logs, group, token): + for case in cases: + definition = case["definition"] + transformation = {"metricNamespace": namespace, "metricName": case["name"], + "metricValue": definition["metricValue"], "unit": "Count"} + for field in ("defaultValue", "dimensions"): + if field in definition: + transformation[field] = definition[field] + request = {"logGroupName": group, "filterName": "probe", + "filterPattern": definition["filterPattern"], + "metricTransformations": [transformation]} + if case["system_fields"]: + request["emitSystemFieldDimensions"] = case["system_fields"] + logs.put_metric_filter(**request) + for batch in case["batches"]: + response = logs.put_log_events(logGroupName=group, logStreamName="probe", + logEvents=[{"timestamp": int(case["minute"].timestamp() * 1000) + i, "message": message} + for i, message in enumerate(batch)]) + if response.get("rejectedLogEventsInfo"): + raise ReplayError("replay log events were rejected") + + checks = cases + [{"name": "quiet", "minute": cases[-1]["minute"] + timedelta(minutes=1), + "expected": [{"dimensions": {}, "datapoints": []}]}] + + def read(): + nonlocal observations + complete, observations = observe(cw, namespace, checks) + return complete + + try: + await_stable(read, args.timeout, args.stable_seconds, args.poll_interval) + finally: + print(json.dumps({"observations": observations})) + print(json.dumps({"result": "converged", "cleanup": "owned log group deleted", + "qualification": "Local results are not live AWS verification." + if not args.aws else "Only these replayed scenarios were observed."})) + return 0 + except Exception as error: + print(json.dumps({"result": "failed", "error": safe_error(error), + "cleanup_hint": group}), file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/compatibility-tests/sdk-test-python/tests/test_metric_filter_replay.py b/compatibility-tests/sdk-test-python/tests/test_metric_filter_replay.py new file mode 100644 index 0000000000..962f15c182 --- /dev/null +++ b/compatibility-tests/sdk-test-python/tests/test_metric_filter_replay.py @@ -0,0 +1,247 @@ +"""Offline checks only: no AWS clients or sockets are opened by these tests.""" +import importlib.util +from pathlib import Path +from datetime import datetime, timedelta, timezone +import json +import socket +from types import SimpleNamespace +from unittest.mock import Mock + +import pytest + +SPEC = importlib.util.spec_from_file_location( + "metric_filter_replay", Path(__file__).resolve().parent / "metric_filter_replay.py") +replay = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(replay) + +@pytest.fixture(autouse=True) +def no_network(monkeypatch): + def reject(*args, **kwargs): + pytest.fail("offline replay checks must not open a network connection") + monkeypatch.setattr(socket.socket, "connect", reject) + + +@pytest.fixture +def orchestration(monkeypatch): + logs, cw, sts = Mock(), Mock(), Mock() + state = SimpleNamespace(metric=None, ingestions=[], observations=0, now=0) + sts.get_caller_identity.return_value = {"Account": "123456789012"} + logs.list_tags_log_group.side_effect = lambda **kwargs: {"tags": logs.create_log_group.call_args.kwargs["tags"]} + def put_filter(**request): + state.metric = request["metricTransformations"][0]["metricName"] + def put_events(**request): + state.ingestions.append((state.metric, request["logEvents"])) + return {} + def sleep(seconds): + state.now += seconds + logs.put_metric_filter.side_effect = put_filter + logs.put_log_events.side_effect = put_events + monkeypatch.setattr(replay, "clients", lambda args: (logs, cw, sts)) + monkeypatch.setattr(replay.time, "monotonic", lambda: state.now) + monkeypatch.setattr(replay.time, "sleep", sleep) + return state + + +def test_fixture_is_packaged_beside_the_module_not_in_a_repository_ancestor(): + expected = Path(__file__).resolve().parent / "fixtures/metric-filter-publishing-aws.json" + assert replay.FIXTURE == expected + assert replay.FIXTURE.is_file() + + +def test_quiet_ingestion_is_last_through_successful_stable_observation(monkeypatch, orchestration): + state = orchestration + def observe(*args): + state.observations += 1 + assert state.ingestions[-1][0] == "quiet", "later traffic could flush a broken quiet publisher" + assert [e["message"] for e in state.ingestions[-1][1]] == ["INFO", "INFO", "INFO"] + assert sum(metric == "quiet" for metric, _ in state.ingestions) == 1 + return state.observations > 1, [] + monkeypatch.setattr(replay, "observe", observe) + assert replay.main(["--endpoint", "http://127.0.0.1:4566", "--timeout", "10", + "--stable-seconds", "2", "--poll-interval", "1"]) == 0 + assert state.observations >= 4 + assert state.ingestions[-1][0] == "quiet" + + +def test_idle_window_is_distinct_unwritten_and_checked_through_convergence(monkeypatch, orchestration): + state = orchestration + def observe(cw, namespace, cases): + state.observations += 1 + quiet = [case for case in cases if case["name"] == "quiet"] + assert len(quiet) == 2, "check the positive quiet minute and a separate empty minute" + populated, idle = sorted(quiet, key=lambda case: case["minute"]) + assert idle["minute"] == populated["minute"] + timedelta(minutes=1) + assert populated["expected"][0]["Sum"] == 21 + assert idle["expected"] == [{"dimensions": {}, "datapoints": []}] + start = int(idle["minute"].timestamp() * 1000) + assert all(not start <= event["timestamp"] < start + 60_000 + for _, events in state.ingestions for event in events) + return state.observations > 1, [] + monkeypatch.setattr(replay, "observe", observe) + assert replay.main(["--endpoint", "http://127.0.0.1:4566", "--timeout", "10", + "--stable-seconds", "2", "--poll-interval", "1"]) == 0 + assert state.observations >= 4 + + +@pytest.mark.parametrize("args", [ + [], ["--aws"], ["--aws", "--profile", "synthetic", "--region", "eu-west-1"], + ["--aws", "--ack-live-writes", "I_ACCEPT_AWS_WRITES", "--region", "eu-west-1"], + ["--aws", "--ack-live-writes", "I_ACCEPT_AWS_WRITES", "--profile", "synthetic"], + ["--endpoint", "https://logs.eu-west-1.amazonaws.com"], + ["--endpoint", "http://127.0.0.1:4566@evil.example"], + ["--endpoint", "http://localhost:4566", "--profile", "synthetic"], + ["--endpoint", "http://127.0.0.1:4566/path"], + ["--endpoint", "http://[malformed-private-endpoint"], + # GovCloud and China are real botocore Regions but not the commercial partition the endpoints pin. + ["--aws", "--profile", "synthetic", "--region", "us-gov-west-1", "--ack-live-writes", "I_ACCEPT_AWS_WRITES"], + ["--aws", "--profile", "synthetic", "--region", "cn-north-1", "--ack-live-writes", "I_ACCEPT_AWS_WRITES"], +]) +def test_rejects_unguarded_or_remote_writes_before_creating_clients(args): + with pytest.raises(SystemExit): + replay.parse_args(args) + + +def test_live_clients_pin_official_endpoints_despite_environment(monkeypatch): + monkeypatch.setenv("AWS_ENDPOINT_URL", "https://untrusted.invalid") + monkeypatch.setenv("AWS_ENDPOINT_URL_CLOUDWATCH_LOGS", "https://untrusted.invalid") + args = replay.parse_args(["--aws", "--profile", "synthetic", "--region", "eu-west-1", + "--ack-live-writes", "I_ACCEPT_AWS_WRITES"]) + session = Mock() + replay.clients(args, session) + calls = {call.args[0]: call.kwargs for call in session.client.call_args_list} + assert calls["logs"]["endpoint_url"] == "https://logs.eu-west-1.amazonaws.com" + assert calls["cloudwatch"]["endpoint_url"] == "https://monitoring.eu-west-1.amazonaws.com" + assert calls["sts"]["endpoint_url"] == "https://sts.eu-west-1.amazonaws.com" + assert all(c["config"].ignore_configured_endpoint_urls for c in calls.values()) + + +def test_local_clients_use_only_explicit_loopback_and_dummy_credentials(): + args = replay.parse_args(["--endpoint", "http://127.0.0.1:18084"]) + session = Mock() + replay.clients(args, session) + for call in session.client.call_args_list: + assert call.kwargs["endpoint_url"] == "http://127.0.0.1:18084" + assert call.kwargs["aws_access_key_id"] == "test" + assert call.kwargs["aws_secret_access_key"] == "test" + assert call.kwargs["aws_session_token"] == "" + + +def test_owned_group_is_verified_again_before_cleanup_even_when_body_fails(): + logs = Mock() + logs.list_tags_log_group.return_value = {"tags": {"floci-metric-replay": "token"}} + with pytest.raises(RuntimeError, match="body"): + with replay.owned_group(logs, "/owned", "token"): + raise RuntimeError("body") + assert logs.list_tags_log_group.call_count == 2 + logs.delete_log_group.assert_called_once_with(logGroupName="/owned") + + +def test_cleanup_does_not_delete_a_group_whose_ownership_changed(): + logs = Mock() + logs.list_tags_log_group.side_effect = [ + {"tags": {"floci-metric-replay": "token"}}, {"tags": {"floci-metric-replay": "someone-else"}}] + with pytest.raises(replay.ReplayError, match="cleanup"): + with replay.owned_group(logs, "/owned", "token"): + pass + logs.delete_log_group.assert_not_called() + + +def test_failed_create_never_deletes_an_existing_group(): + logs = Mock() + logs.create_log_group.side_effect = RuntimeError("private endpoint and credentials") + with pytest.raises(RuntimeError, match="private endpoint"): + with replay.owned_group(logs, "/owned", "token"): + pytest.fail("must not start replay") + logs.delete_log_group.assert_not_called() + + +def test_cleanup_failure_is_visible_and_does_not_leak_service_exception(): + logs = Mock() + logs.list_tags_log_group.return_value = {"tags": {"floci-metric-replay": "token"}} + logs.delete_log_group.side_effect = RuntimeError("private account credentials") + with pytest.raises(replay.ReplayError, match="cleanup failed") as error: + with replay.owned_group(logs, "/owned", "token"): + raise ValueError("private profile") + assert "private" not in str(error.value) + + +def test_polling_requires_positive_controls_then_a_stable_complete_window(): + clock = SimpleNamespace(now=0) + def sleep(seconds): + clock.now += seconds + observations = iter([False, False, True, False, True, True, True]) + reads = [] + def read(): + reads.append(clock.now) + return next(observations) + replay.await_stable(read, timeout=20, stable_seconds=2, interval=1, + monotonic=lambda: clock.now, sleep=sleep) + assert reads == [0, 1, 2, 3, 4, 5, 6] + + +def test_never_converging_observations_fail_at_a_bounded_deadline(): + clock = SimpleNamespace(now=0) + def sleep(seconds): + clock.now += seconds + with pytest.raises(replay.ReplayError, match="converge"): + replay.await_stable(lambda: False, timeout=3, stable_seconds=2, interval=1, + monotonic=lambda: clock.now, sleep=sleep) + assert clock.now == 3 + + +def test_slow_final_read_cannot_succeed_after_the_deadline(): + clock = SimpleNamespace(now=0) + def sleep(seconds): + clock.now += seconds + def read(): + if clock.now: + clock.now += 3 + return True + with pytest.raises(replay.ReplayError, match="converge"): + replay.await_stable(read, timeout=3, stable_seconds=2, interval=1, + monotonic=lambda: clock.now, sleep=sleep) + + +def test_invalid_argument_output_never_echoes_private_values(capsys): + with pytest.raises(SystemExit): + replay.parse_args(["--unknown", "private-profile-name"]) + assert "private-profile-name" not in capsys.readouterr().err + + +def test_error_output_is_sanitized(): + assert replay.safe_error(RuntimeError("profile secret account 123456789012")) == "RuntimeError" + assert replay.safe_error(replay.ReplayError("cleanup failed")) == "cleanup failed" + + +def test_scenarios_use_immutable_recorded_values_and_distinct_event_minutes(): + fixture = json.loads(replay.FIXTURE.read_text()) + before = json.dumps(fixture) + cases = replay.scenarios(fixture, datetime(2026, 9, 16, tzinfo=timezone.utc), "123456789012", "eu-west-1") + by_name = {case["name"]: case for case in cases} + assert len(cases) == 12 + assert len({case["minute"] for case in cases}) == 12 + assert [by_name[name]["expected"][0]["Sum"] for name in ("default-0", "default-1", "default-2", "quiet")] == [13, 10, 10, 21] + assert by_name["extraction-0"]["expected"][0]["Sum"] == 7 + assert by_name["extraction-1"]["expected"][0]["Sum"] == 7 + assert by_name["extraction-2"]["expected"][0] == {"dimensions": {}, "datapoints": []} + assert by_name["extraction-3"]["expected"][0]["Sum"] == 3 + assert len(by_name["dimensions"]["expected"]) == 4 + assert json.dumps(fixture) == before + + +def test_observation_rejects_wrong_timestamps_partial_counts_and_sanitizes_account(): + minute = datetime(2026, 9, 16, tzinfo=timezone.utc) + case = {"name": "system", "minute": minute, "expected": [ + {"dimensions": {"@aws.account": "123456789012"}, "Sum": 3, "SampleCount": 1}]} + cw = Mock() + point = {"Timestamp": minute, "Sum": 3.0, "SampleCount": 1.0, "Unit": "Count"} + cw.get_metric_statistics.return_value = {"Datapoints": [point]} + complete, observations = replay.observe(cw, "namespace", [case]) + assert complete + assert "123456789012" not in json.dumps(observations) + assert "" in json.dumps(observations) + point["SampleCount"] = 2 + assert not replay.observe(cw, "namespace", [case])[0] + point["SampleCount"] = 1 + point["Timestamp"] = datetime(2026, 9, 16, 0, 1, tzinfo=timezone.utc) + assert not replay.observe(cw, "namespace", [case])[0] diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index 1839c60807..43836cb1f8 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -53,6 +53,52 @@ StackSets support both `SELF_MANAGED` and the Cloud Launchpad `SERVICE_MANAGED` Operation IDs are recorded and validated. Duplicate IDs return `OperationIdAlreadyExistsException`, missing stack sets return `StackSetNotFoundException`, and missing operation IDs return `OperationNotFoundException`. Invalid targets and request shapes use the CloudFormation query-protocol validation errors. Operations complete locally, so `OperationInProgressException` is only reachable when local operation state actually overlaps; Floci does not inject concurrency failures solely to exercise an error code. +## CloudWatch Logs metric filters + +`AWS::Logs::MetricFilter` returns the filter name alone for both `Ref` and +`PhysicalResourceId`. Floci stores the log group separately for cleanup, including when names +contain `|`. Mutable updates keep generated names stable. + +Changing `FilterName` or `LogGroupName` deletes the old filter before creating the replacement. +This works at the 100-filter group limit without temporarily exceeding the quota. The resource's +DELETE and CREATE events precede its final UPDATE event. The +[recorded AWS observations](cloudwatch-metric-filters-verification.md) also show this ordering +with an already-installed `UpdateReplacePolicy: Retain`. This is specific to metric-filter +replacement, not a change to ordinary stack `DeletionPolicy: Retain`. + +Rollback restores the complete prior backing definition, not just its reference. A replacement +recreates the deleted filter and receives a new creation time. Failed restoration keeps its +snapshot for retry; a filter absent before the update remains absent after rollback. +That absence is retained as private nonownership metadata: later stack operations cannot adopt +or delete another filter that reuses the same group and name. +Named creates reject an existing filter rather than adopting it through Logs' upsert API. +If storage fails both the write and its ownership inspection, cleanup reports failure rather +than guessing ownership. The snapshot remains available, but an indeterminate surviving filter +requires operator reconciliation before rollback or deletion can finish. +`DeleteStack` preserves failed-rollback metadata until resource-aware cleanup runs. It removes +confirmed owned restorations or newly created filters whose rollback deletion failed. An +indeterminate outcome keeps the stack in `DELETE_FAILED` with its metadata available for retry. + +CFN mutations record `APPLIED`, `NOT_APPLIED`, or `UNKNOWN` while holding the canonical filter +storage lock, including when storage throws after applying a write or delete. Each group/name +address tracks confirmed ownership, nonownership, or uncertainty. A confirmed delete relinquishes +ownership before another creator can enter, so neither rollback nor a later delete retry can +adopt that creator's filter. Unknown outcomes cannot authorize an upsert or deletion of a +surviving row. Confirmed absence permits safe recovery and finishes account-scoped pending +publication cleanup. Older private rollback flags that cannot establish an outcome are treated +conservatively as unknown. +Baseline snapshots containing `logGroupName` and `filterName` are retained as historical metadata, +not replayed as compensation for a new update: that older format could survive a successful +in-place update. Its explicit group/name fields preserve pipe-containing names without ambiguity. +After confirmed absence, a new update creates the requested definition rather than reviving +the stale snapshot. + +CloudFormation dimensions use a `[{Key, Value}]` array, not the Logs API's object shape. +CFN model validation precedes mutation, while provider validation (such as parsing a replacement +filter pattern) can fail after deletion and trigger restoration. The CFN schema allows a +256-character metric namespace, but Floci's Logs API validation currently accepts at most 255: +passing model validation does not imply provider acceptance. + ## Supported Resource Types Resource types provisioned during `CreateStack` / `UpdateStack` / `DeleteStack`. Each delegates to @@ -103,7 +149,7 @@ cross-resource references. | IoT Core | `DomainConfiguration` (`ServerCertificates` resolves to a JSON string), `Policy` (deleted after detaching it from its principals; on AWS the delete fails with `DeleteConflictException` while the policy is attached), `Thing`, `TopicRule` | | CloudFront | `CachePolicy`, `Distribution`, `OriginAccessControl`, `OriginRequestPolicy`, `ResponseHeadersPolicy` | | CloudWatch | `Alarm`, `Dashboard` | -| CloudWatch Logs | `LogGroup` | +| CloudWatch Logs | `LogGroup`, `MetricFilter` | | WAFv2 | `WebACL` | | Config | `ConfigRule` | | CloudFormation | `CustomResource`, `Custom::DynamoDBReplica` (applied natively against DynamoDB, not via a provider Lambda), `Stack` (nested stacks), `Custom::*` (Lambda-backed) | diff --git a/docs/services/cloudwatch-metric-filters-verification.md b/docs/services/cloudwatch-metric-filters-verification.md new file mode 100644 index 0000000000..55b23e048d --- /dev/null +++ b/docs/services/cloudwatch-metric-filters-verification.md @@ -0,0 +1,96 @@ +# Metric-filter contract verification + +This record separates AWS observations from emulator tests and documentation-derived requirements. +It is not a claim that every CloudWatch Logs behavior has been tested. + +| Label | Meaning | +|---|---| +| **Verified on Live AWS** | The stated scenario was executed against official AWS endpoints. Variations outside that scenario are not implied. | +| **SDK-tested against Floci** | An AWS SDK client exercised Floci. This establishes client interoperability, not an independent AWS behavior oracle. | +| **Documentation-backed** | An AWS API reference, guide or public schema supports the requirement; this label does not claim a live execution. | + +## Live observation context + +The stateful probes ran on 2026-09-16 in `eu-west-1`, using isolated synthetic log groups, +metric filters and one CloudFormation stack. AWS CLI version: `2.36.45`. +STS identity was checked privately and service calls used explicit official endpoints. +No credentials, account IDs, real log messages or temporary resource identifiers belong in fixtures. + +Metric queries used `GetMetricStatistics`, period 60, and Sum, SampleCount, Minimum and Maximum. +The main observation ended at 08:40:45 UTC. The exact mixed-event follow-up converged through +08:56:48 UTC, including repeated reads after an initially incomplete result. The final +system-dimension probe and cleanup completed at 09:06:51 UTC. +An early empty or partial result was not treated as the final contract. + +The pattern corpus is separately captured by +`tools/aws-capture/cloudwatchlogs/capture_filter_patterns.py`. Its 148 synthetic cases record +their request, AWS response or error, endpoint, timestamp and documentation reference in +`filter-pattern-aws*.json`. Replaying that opt-in script calls real AWS; normal tests consume +the recorded files without credentials or network access. +Normalized stateful observations are recorded in `metric-filter-publishing-aws.json` in the +same resource directory. The scalar-wire comparison has its own +`metric-filter-default-value-wire-aws.json` provenance record. + +## Observed behavior + +Every row in this table is **Verified on Live AWS** for its stated scope. + +| Area | Scenario | Observation | +|---|---|---| +| Group regex quota | Five metric filters, each containing two regex expressions | Accepted. A sixth regex-bearing filter was rejected with `LimitExceededException`. The group quota counts regex-containing filters. | +| Regex replacement | Replace a regex-bearing pattern with plain text; later attempt plain-to-regex at capacity | The first change released its slot. The rejected update preserved the plain definition; deleting another regex-bearing filter allowed the update. | +| Pattern regex quota | Three expressions in one pattern | Rejected with `InvalidParameterException`; two expressions were accepted. | +| Default contribution | Three nonmatches in one request, default 7, then no further ingestion | Sum 21, SampleCount 3, Minimum 7, Maximum 7. No extra points appeared in later idle minutes. | +| Mixed batch | Pattern `ERROR`, metric value 3, default 7; `ERROR`, `INFO`, `ERROR` in one batch | Sum 13, SampleCount 3, Minimum 3, Maximum 7. Matching events did not suppress the nonmatch's default. | +| Separate batches | The same filter; hit then miss, or miss then hit, in the same event-time minute | Both orders converged to Sum 10 and SampleCount 2. | +| Late append | Default 11 observed first; later matching value 3 timestamped in the same historical minute | Sum 14 and SampleCount 2. The earlier default was not retracted. | +| Timestamp placement | Newly ingested backdated, contemporary and future-timestamped events | Metrics appeared in the event-timestamp minutes. An event timestamp before filter creation also worked when ingestion occurred after creation. | +| Extracted value | Match on a separate discriminator; metric value references `$.value`; default 7 | Missing or explicit-null value produced 7. Numeric 3 produced 3. A nonnumeric string produced no point within the completed observation window. | +| Transformation reference validation | Scalar metric-value and dimension selectors not mentioned in a JSON filter pattern; then wildcard metric value `$.values[*]` | Unmentioned scalar selectors were accepted. The wildcard metric value was rejected with `InvalidParameterException`. These four normalized validation observations are in `metric-filter-reference-validation-aws.json`; no publication or raw HTTP response was measured in this probe. | +| No default | The same extraction cases, without a default | Only valid numeric values produced points. | +| Ordinary dimensions | Configure A from `$.a` and B from `$.b`; send complete, missing-A, missing-B and missing-both events | The complete set produced its dimensioned series. The three incomplete sets produced dimensionless samples, not partial-dimension series or dropped events. | +| Default JSON type | Numeric `defaultValue: 7`, then string `defaultValue: "7"` in raw signed requests | Number: HTTP 200, empty body. String: HTTP 400 `SerializationException`, `STRING_VALUE cannot be converted to Double`. | +| Default and dimensions | Default with system-only dimensions, then with ordinary transformation dimensions | System-only dimensions were accepted. Ordinary dimensions plus a default were rejected. Acceptance is not a claim about every emitted dimension value. | +| System-dimension publication | Regular noncentralized group; pattern `ERROR`, value 3, default 7; one `ERROR` and one `INFO`; request account, region, then both system dimensions | The matching value received the requested dimensions. The pattern-nonmatch default was dimensionless. Each case accounted for exactly two samples totaling 10. Extraction-fallback defaults were not measured in this probe. | +| Dimension count | Two ordinary dimensions plus one system dimension, then plus two system dimensions | Total three accepted; total four rejected with `InvalidParameterException`. | +| Selection operators | Word-form `AND` and `OR` in `fieldSelectionCriteria` | Accepted. | +| Transformed-log flag | `true` and `false` on a newly created group whose `GetTransformer` returned an empty configuration | Both accepted and round-tripped. A subsequent account-policy read returned zero transformer policies; it was not simultaneous with the initial flag probe. This does not test transformer execution. | +| CloudFormation identity | Explicitly named metric filter; stack output `Ref` and `DescribeStackResource` | Both values were the filter name alone, not `LogGroupName|FilterName`. Generated-name variations were not independently observed here. | +| CloudFormation replacement | Rename an explicitly named filter | DELETE_IN_PROGRESS and DELETE_COMPLETE for the old filter preceded CREATE_IN_PROGRESS and CREATE_COMPLETE for the replacement. The stack completed its update. | +| CloudFormation rollback | Rename with invalid filter pattern `{` after a valid `WARN`/value `2` definition | The old filter was deleted, replacement creation failed, and rollback recreated the previous name and definition. Logs reads, changed creationTime, Ref and the restored template confirmed it; the attempted filter did not survive. | +| CloudFormation replacement policy | Install `UpdateReplacePolicy: Retain`, then rename the filter | This type still deleted the old filter before creating its replacement. No DELETE_SKIPPED event appeared, and only the replacement survived. This does not describe other resource types or stack DeletionPolicy. | +| TestMetricFilter numbering | Recorded pattern-corpus requests with matching and nonmatching messages | Matched event numbers were one-based. | +| TestMetricFilter JSON extraction | Recorded JSON-pattern matches | Public `extractedValues` was an empty object. Internal field extraction is still needed to publish metric values. | + +The default-value observations contradict a once-per-minute default cap. CloudWatch aggregates +the event contributions by minute; a minute accumulator that suppresses distinct nonmatching +events is not an equivalent implementation. + +The timestamp observation does not imply retroactive processing of logs that were already +ingested before a filter existed. + +## Documentation-backed and unmeasured boundaries + +The shared metric/subscription regex quota is **Documentation-backed** by both Put operation +references and the regex syntax guide. The stateful probe did not create a subscription destination, +so it does not establish live mixed-writer quota behavior. + +The current `AWS::Logs::MetricFilter` registry schema specifies `delete_then_create`, and a +subsequent live CFN update probe confirmed that order and rollback recreation. Its normalized +events and observations are in `metric-filter-cfn-aws.json`. The probe did not fill the group +to 100 filters or test collisions with another stack's resource. + +Null or duplicate system-dimension entries, every invalid dimension-value variant, full transformer +execution, centralized source metadata, crash recovery and publication-failure injection are not +covered by the live observations above. + +## Sources + +- [PutMetricFilter API](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutMetricFilter.html) +- [PutSubscriptionFilter API](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutSubscriptionFilter.html) +- [TestMetricFilter API](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_TestMetricFilter.html) +- [Filter and pattern syntax](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html) +- [Metric-filter values and dimensions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntaxForMetricFilters.html) +- [Metric-filter concepts](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/MonitoringLogData.html) +- [CloudFormation MetricFilter reference](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-logs-metricfilter.html) +- [CloudFormation registry schemas](https://schema.cloudformation.us-east-1.amazonaws.com/CloudformationSchema.zip) diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.md index b47ca469b7..e9e1b61e56 100644 --- a/docs/services/cloudwatch.md +++ b/docs/services/cloudwatch.md @@ -36,6 +36,10 @@ Floci supports both CloudWatch Logs and CloudWatch Metrics. | `PutSubscriptionFilter` | Create or update a subscription filter (stored only, see note below) | | `DescribeSubscriptionFilters` | List subscription filters on a log group | | `DeleteSubscriptionFilter` | Delete a subscription filter | +| `PutMetricFilter` | Create or replace a metric filter on a log group (see [Metric filters](#metric-filters)) | +| `DescribeMetricFilters` | List metric filters by log group and name prefix, or by metric name and namespace | +| `DeleteMetricFilter` | Delete a metric filter | +| `TestMetricFilter` | Run a filter pattern over sample messages and return the matches with their extracted values | | `PutResourcePolicy` | Create or update an account-level resource policy | | `DescribeResourcePolicies` | List account-level resource policies | | `PutDestination` | Create or update a cross-account subscription destination | @@ -51,7 +55,7 @@ Log group deletion protection defaults to disabled and is persisted with the log enabled, `DeleteLogGroup` returns `ValidationException` until protection is explicitly disabled with `PutLogGroupDeletionProtection`. -Two actions are currently simplified: +Three actions are currently simplified: - **`PutSubscriptionFilter`** stores the filter so that `DescribeSubscriptionFilters` returns it, but log events are **not** forwarded to the destination ARN. Lambda, @@ -59,6 +63,125 @@ Two actions are currently simplified: - **`GetDataProtectionPolicy`** does not model data-protection policies. It returns HTTP 200 with the resolved `logGroupIdentifier` and no `policyDocument` — including for a log group that does not exist, where real AWS returns `ResourceNotFoundException`. +- **`FilterLogEvents`** matches `filterPattern` as a plain substring of the message. The full + filter pattern syntax described below is applied by metric filters. + +### Metric filters {#metric-filters} + +A metric filter turns newly ingested log events into CloudWatch metric samples. Each matching +event contributes the literal `metricValue`, or its referenced field (`$.latency` in JSON, +`$size` in space-delimited logs), at the **event timestamp**, not ingestion or filter-creation time. +Events ingested before the filter existed are not replayed. Backdated and future event timestamps +remain backdated and future metric timestamps. + +A configured `defaultValue` contributes **once per nonmatching event**, including alongside matches +in the same batch or minute. A matching event with a missing or JSON-null metric-value field also +uses the default; a present nonnumeric value is skipped, not replaced by the default. Without a +default, those nonmatches and missing/null values produce no sample. No events means no samples. +Healthy publication is immediate and needs no subsequent ingestion, reads, or timer. For example, +pattern `ERROR`, value 3 and default 7 produce Sum 13 / SampleCount 3 for `ERROR, INFO, ERROR`, +and three quiet nonmatches produce Sum 21 / SampleCount 3. A late match appends its value without +retracting earlier defaults. CloudWatch aggregates these contributions by period; Floci has no +minute-default accumulator or deduplication history. + +Ordinary transformation dimensions are all-or-none: a complete configured set is emitted, while +a missing member produces a dimensionless sample, not a partial set. Requested system dimensions +(`@aws.account`, `@aws.region`) are populated on matches using the resolved receiving account and +region, including regular noncentralized Logs ingestion. **Pattern-nonmatch defaults are +dimensionless**, even when system dimensions are requested. These scenarios are supported by the +recorded [live AWS observations](cloudwatch-metric-filters-verification.md). + +**Inferred emulator policy, not a live AWS claim:** matching missing/null metric-value fallbacks +retain requested system dimensions. Likewise, an incomplete ordinary dimension set is discarded +as a unit while requested system dimensions are retained. Null ordinary dimension values are +treated as missing; empty strings retain the existing scalar-extraction behavior. These combinations +were not measured by the recorded probes. Floci does not infer centralized source provenance. + +**Publication runs off the request thread:** `PutLogEvents` evaluates the group's filters as they +are at that moment, builds one immutable sample per contributing event (canonical account, region, +group, filter, transformation snapshot, event timestamp and a stable internal publication ID) and +hands them to a process-wide queue. One managed worker writes them to the Metrics store, so +ingestion latency does not depend on the Metrics sink and a `PutMetricFilter` or a reset waits for +at most one sample write. The queue holds **100,000 samples**; a batch that does not fit is written +on the request thread under the filter-store monitor instead of being dropped, so accepted samples +are never lost to the bound and only an overfull queue makes the request wait on the sink. +A read issued immediately after `PutLogEvents` may run before the worker has written the sample, +as on AWS; SDK tests should poll. Shutdown drains the queue before storage shuts down. + +**Runtime publication failures:** accepted Logs ingestion remains successful if the Metrics sink +reports a failure. Each failed sample keeps its snapshot and publication ID, and retries replace +that same metric-store key, making partial or ambiguous writes safe without deduplicating distinct +identical log events. The existing AWS `PutMetricData` APIs continue appending samples as before. + +The same worker retries failed publications at one-second intervals while both Logs and Metrics +services are enabled; idle ticks never create defaults. The retry map holds at most **10,000 +samples**, not 10,000 batches. This fixed conservative limit bounds outage memory without +introducing a configuration surface. When full, new failed samples are dropped with an **ERROR** +containing account, region, group, filter and dropped count; already queued samples and their IDs are +preserved. Successful retries and cancellations release capacity. Each filter-owned outage logs +one detailed stack, followed by a stack-free aggregate warning every 60 failed retry ticks. +Recovery, cancellation and reset release that suppression. This is bounded best effort, not +lossless delivery through an indefinite outage. Pending work is in memory only; crash-durable +pending publication is not implemented. Storage failures that a backend only logs rather than +throws are not observable to this retry path; configured storage durability semantics are unchanged. + +A filter update or deletion affects new ingestion only: samples already accepted for a batch still +publish under the definition that was current at ingestion, as on AWS, and deleting a filter or +group cancels only its own account/region-scoped failed retries without retracting stored metrics. +Reset pauses publication, waits for the active sample write, discards queued and failed samples, +wipes storage and resumes the same live service. The canonical filter-store monitor serializes +each sample write with update/delete; reset hooks release it before the storage-factory wipe to +avoid lock inversion. Shutdown writes the queued samples, abandons failed retries and stops the +worker (up to five seconds to await executor termination). Storage backends expose synchronous +operations without a cancellation deadline; a stuck backend can delay that drain. Runtime +cancellation is not a crash-recovery guarantee. + +Samples are readable with `GetMetricStatistics` and `GetMetricData`, and alarms evaluate normally. + +The filter pattern syntax follows the AWS reference: + +- Terms: `ERROR ARGUMENTS` (all present), `?ERROR ?ARGUMENTS` (any present), `ERROR -ARGUMENTS` + (exclusion), `"exact phrase"`. Terms are case-sensitive substrings of the message. +- Regular expressions between percent signs, `%^[hc]at%`, with the operators and escapes AWS + allows. Parentheses and characters outside ASCII are rejected, as on AWS. +- JSON patterns: `{ $.eventType = "UpdateTrail" && $.code >= 400 }`, with `=`, `!=`, `<`, `<=`, + `>`, `>=`, `IS NULL`, `IS TRUE`, `IS FALSE`, `NOT EXISTS`, `&&`, `||`, parentheses, array indexes, + `[*]` and `.*` wildcards, and `$.['a.b']` for a property with a dot in its name. AWS's wildcard + quotas apply: one per property selector and three per pattern. +- Space-delimited patterns: `[ip, ..., status_code = 4*, bytes]`, with named fields, `...` for any + number of fields, conditions on any field, `w1`/`w2` indicators, and `%regex%` values. Text + between double quotes or square brackets is one field. + +`PutMetricFilter` applies the rules AWS applies: the log group must exist, the pattern must parse, +at most two regular expressions per pattern, exactly one transformation whose `metricValue` is a +number or a single-valued field reference, at most three dimensions in total, and 100 metric +filters per log group. Scalar JSON metric-value and dimension references need not be mentioned +in the filter pattern. Wildcard metric-value references such as `$.values[*]` are rejected; +wildcards remain available in the filter pattern itself. Ordinary dimensions are +available only for JSON or space-delimited patterns and cannot be combined with a `defaultValue`; +system-only dimensions can. The shared group regex quota counts up to five **regex-bearing filters** +across metric and subscription filters, independently of the two expressions allowed per pattern. +This shared pool is documentation-backed, not a new live mixed-writer claim. `TestMetricFilter` +reports one-based event numbers. JSON matches have empty public `extractedValues`; space-delimited +matches expose named fields as `$name` and positional fields as `$1`, `$2` and so on. + +`fieldSelectionCriteria` selects which batches a filter processes from the system fields +`@aws.account` and `@aws.region`, with `=`, `!=`, `IN`, `NOT IN` and the `AND` and `OR` the API +documents, as in `@aws.account IN ["111111111111"]`. Both fields describe the ingested batch rather +than the individual event, so one evaluation decides the batch. A criterion that does not parse is +rejected when the filter is stored. + +`emitSystemFieldDimensions` adds `@aws.account` and `@aws.region` on matching contributions as +described above. They count toward the same combined limit of three dimensions. + +`applyOnTransformedLogs` is stored and returned unchanged. Request acceptance was verified on AWS, +including `true` on the tested group without a group-level transformer. Floci does not execute +group-level or account-level log transformations, including stored `TRANSFORMER_POLICY` +configuration. Accepting the flag therefore does not establish transformed-log processing or +full compatibility with an AWS group that has an active transformer. + +See [Metric-filter contract verification](cloudwatch-metric-filters-verification.md) for the +scenarios tagged **Verified on Live AWS**, the recorded fixtures and their evidence boundaries. ### Logs Insights {#logs-insights} diff --git a/mkdocs.yml b/mkdocs.yml index d1683979a0..20c9d7f9ef 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -154,6 +154,7 @@ nav: - EventBridge Pipes: services/pipes.md - EventBridge Scheduler: services/scheduler.md - CloudWatch: services/cloudwatch.md + - CloudWatch Metric Filter Verification: services/cloudwatch-metric-filters-verification.md - CloudWatch OAM: services/oam.md - CloudWatch RUM: services/rum.md - Managed Prometheus (AMP): services/managed-prometheus.md diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java b/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java index 4f12b6da82..1aead9edfa 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java @@ -1,9 +1,17 @@ package io.github.hectorvent.floci.core.common; /** - * Interface for services or components that hold in-memory state - * and need to be cleared when the emulator state is reset or nuked. + * Services with runtime state cleared by emulator reset/nuke. Storage is wiped before clear(), + * allowing clear() to recreate bootstrap data. Background publishers may additionally quiesce + * before the wipe and resume afterwards. Hooks must release their locks before returning: the + * controller never holds StorageFactory's monitor while invoking them. */ public interface Resettable { + /** Stop accepting work and drain active writes before any storage is cleared. */ + default void beforeReset() {} + void clear(); + + /** Resume after reset, including when a storage/clear operation failed. */ + default void afterReset() {} } diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java index d43d85637e..898e144f8d 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java @@ -23,7 +23,9 @@ import jakarta.ws.rs.POST; import java.io.IOException; import java.nio.file.Files; import java.security.GeneralSecurityException; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Optional; @@ -153,17 +155,47 @@ public class EmulatorInfoController { return reset(); } - private void performReset() { + private synchronized void performReset() { // Containers first: they are tracked independently of StorageBackend, so this can run // in any order relative to the storage wipe below, but stopping them here means a // client's reset actually reflects a clean slate instead of leaving Batch, CodeBuild, // or SageMaker containers running with no record of them left in the store. ContainerTeardowns.stopAll(containerTeardowns, LOG); - // Storage before resettables: services re-create their bootstrap state in clear(), and - // a wipe afterwards would remove it again until the next restart. - storageFactory.clearAll(); - for (Resettable r : resettables) { - r.clear(); + // Resolve live instances before taking any storage locks. Serialize resets so one reset + // cannot resume a publisher while another is still wiping its storage. + List services = new ArrayList<>(); + for (Resettable service : resettables) { + services.add(service); + } + List prepared = new ArrayList<>(); + RuntimeException failure = null; + try { + for (Resettable service : services) { + prepared.add(service); + service.beforeReset(); + } + // Storage still precedes clear(): services recreate their bootstrap state there. + storageFactory.clearAll(); + for (Resettable service : services) { + service.clear(); + } + } catch (RuntimeException e) { + failure = e; + } finally { + for (Resettable service : prepared.reversed()) { + try { + service.afterReset(); + } catch (RuntimeException e) { + if (failure == null) { + failure = e; + } else { + failure.addSuppressed(e); + } + } + } + } + if (failure != null) { + throw failure; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index 6d76c88eb0..a2c6a869e2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudformation; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudformation.model.StackEvent; import io.github.hectorvent.floci.services.cloudformation.provisioners.ReplacementCleanup; import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnRollback; import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry; @@ -52,6 +53,7 @@ import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.*; import java.util.concurrent.TimeoutException; +import java.util.function.Consumer; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -221,6 +223,14 @@ public class CloudFormationResourceProvisioner { CloudFormationTemplateEngine engine, String region, String accountId, String stackName, String existingPhysicalId, Map existingAttributes) { + return provision(logicalId, resourceType, properties, engine, region, accountId, stackName, + existingPhysicalId, existingAttributes, event -> {}); + } + + public StackResource provision(String logicalId, String resourceType, JsonNode properties, + CloudFormationTemplateEngine engine, String region, String accountId, + String stackName, String existingPhysicalId, + Map existingAttributes, Consumer progress) { StackResource resource = new StackResource(); resource.setLogicalId(logicalId); resource.setResourceType(resourceType); @@ -231,7 +241,7 @@ public class CloudFormationResourceProvisioner { CfnResourceProvisioner extracted = resourceRegistry.forType(resourceType).orElse(null); if (extracted != null) { extracted.provision(resource, properties, - new ProvisionContext(engine, region, accountId, stackName, existingPhysicalId)); + new ProvisionContext(engine, region, accountId, stackName, existingPhysicalId, progress)); resource.setStatus("CREATE_COMPLETE"); return resource; } @@ -1924,6 +1934,21 @@ public class CloudFormationResourceProvisioner { .map(owner -> owner.updateCleanupPhysicalId(resource)) .orElse(null); } + + /** Only an opted-in provisioner may identify cleanup owed by an UPDATE_FAILED resource. */ + boolean hasPendingRollbackCleanup(StackResource resource) { + return resourceRegistry.forType(resource.getResourceType()) + .map(owner -> owner.hasPendingRollbackCleanup(resource)) + .orElse(false); + } + + /** Only an opted-in provisioner may keep a failed update attempt in place of the previous resource. */ + boolean retainsFailedUpdateState(StackResource resource) { + return resourceRegistry.forType(resource.getResourceType()) + .map(owner -> owner.retainsFailedUpdateState(resource)) + .orElse(false); + } + /** * Whether this update replaced the resource's physical entity, so the stack has cleanup * pending. @@ -1944,8 +1969,12 @@ public class CloudFormationResourceProvisioner { * the stack update, delegated to the provisioner that owns the type. */ boolean rollbackUpdate(StackResource resource) { + return rollbackUpdate(resource, event -> {}); + } + + boolean rollbackUpdate(StackResource resource, Consumer progress) { return resourceRegistry.forType(resource.getResourceType()) - .map(owner -> owner.rollbackUpdate(resource)) + .map(owner -> owner.rollbackUpdate(resource, progress)) .orElse(false); } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java index 6ed211d358..49052d97a8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java @@ -1210,7 +1210,9 @@ public class CloudFormationService implements ResourceProvider { } else { resource = provisioner.provision(logicalId, type, props.isMissingNode() ? null : props, engine, region, accountId, stack.getStackName(), - resource.getPhysicalId(), resource.getAttributes()); + resource.getPhysicalId(), resource.getAttributes(), + event -> addEvent(stack, logicalId, event.getPhysicalResourceId(), type, + event.getResourceStatus(), event.getResourceStatusReason())); } resource.setUpdateReplacePolicy( resDef.path("UpdateReplacePolicy").asText(null)); @@ -1235,7 +1237,10 @@ public class CloudFormationService implements ResourceProvider { if ("CREATE_FAILED".equals(resource.getStatus()) || "UPDATE_FAILED".equals(resource.getStatus())) { failedResource = resource; - if (!isCreate && previousResource != null) { + // A provisioner that keeps the failed attempt's identity and tracking for + // its own rollback is not restored here; the rollback walker owns it. + if (!isCreate && previousResource != null + && !provisioner.retainsFailedUpdateState(resource)) { // Provisioners work on a copy of the stored resource metadata. Keep the // last known-good identity and status when an update attempt fails so a // later retry or stack deletion still manages the original resource. @@ -1657,7 +1662,9 @@ public class CloudFormationService implements ResourceProvider { resource.getResourceType(), "UPDATE_FAILED", reason); } else if ("true".equals(resource.getAttributes().remove( CloudFormationResourceProvisioner.UPDATE_ROLLBACK_RESTORED_ATTR)) - || provisioner.rollbackUpdate(resource)) { + || provisioner.rollbackUpdate(resource, + event -> addEvent(stack, resource.getLogicalId(), event.getPhysicalResourceId(), + resource.getResourceType(), event.getResourceStatus(), event.getResourceStatusReason()))) { resource.setStatus(previous.getStatus()); resource.setStatusReason(previous.getStatusReason()); addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(), @@ -1932,10 +1939,13 @@ public class CloudFormationService implements ResourceProvider { for (StackResource resource : resources) { // CREATE_COMPLETE/UPDATE_COMPLETE: first delete attempt. DELETE_FAILED: a previous // delete left the resource behind (e.g. the bucket was non-empty); AWS re-attempts - // it on retry. + // it on retry. Failed updates are included only when their provisioner still + // tracks an ownership-aware cleanup obligation. boolean deletable = "CREATE_COMPLETE".equals(resource.getStatus()) || "UPDATE_COMPLETE".equals(resource.getStatus()) - || "DELETE_FAILED".equals(resource.getStatus()); + || "DELETE_FAILED".equals(resource.getStatus()) + || ("UPDATE_FAILED".equals(resource.getStatus()) + && provisioner.hasPendingRollbackCleanup(resource)); if (resource.getPhysicalId() == null || !deletable) { continue; } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java index 602d19ee84..a1a739d009 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java @@ -2,8 +2,10 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners; import com.fasterxml.jackson.databind.JsonNode; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudformation.model.StackEvent; import java.util.Set; +import java.util.function.Consumer; /** * Provisions and deletes the CloudFormation resource types for a single service, replacing one @@ -45,6 +47,26 @@ public interface CfnResourceProvisioner { return false; } + default boolean rollbackUpdate(StackResource resource, Consumer progress) { + return rollbackUpdate(resource); + } + + /** + * Whether a failed update still needs this provisioner's ownership-aware delete. Opt-in: + * a failed resource is not otherwise assumed to own a backing entity. + */ + default boolean hasPendingRollbackCleanup(StackResource resource) { + return false; + } + + /** + * True when this provisioner keeps the failed update attempt's own identity and tracking for + * its {@code rollbackUpdate}, so the engine must not restore the previous resource over it. + */ + default boolean retainsFailedUpdateState(StackResource resource) { + return false; + } + /** * The physical id of the entity this update's replacement displaced, or null when this type * owes no replacement cleanup. {@code CloudFormationService} announces it as DELETE_IN_PROGRESS diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java index 76a1c23dd0..e6c9c34534 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java @@ -81,6 +81,14 @@ public final class CfnRollback { */ public static final String DASHBOARD_UPDATE_SNAPSHOT_ATTR = "__FlociDashboardUpdateSnapshot"; + /** + * Holds the complete prior metric filter, identity, name mode and per-address mutation outcomes + * and ownership states. + * Written before either an in-place put or a delete-then-create replacement; retained across + * failed restoration attempts and spent only after rollback or commit succeeds. + */ + public static final String METRIC_FILTER_UPDATE_SNAPSHOT_ATTR = "__FlociMetricFilterUpdateSnapshot"; + /** * Holds the pipe a rename displaced: the name it still lives under, the region that addresses * it, how many times deleting it has been attempted, and when the replacement was created. diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LogsMetricFilterCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LogsMetricFilterCfnProvisioner.java new file mode 100644 index 0000000000..64d1cf26b2 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LogsMetricFilterCfnProvisioner.java @@ -0,0 +1,570 @@ +package io.github.hectorvent.floci.services.cloudformation.provisioners; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.MissingNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.cloudformation.model.StackEvent; +import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService.MutationOutcome; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService.MutationResult; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; + +import static io.github.hectorvent.floci.services.cloudformation.provisioners.ProvisionContext.report; + +/** + * Metric-filter Ref and physical id are the name alone. Group identity is private metadata. + * AWS replaces this type by deleting first, including with UpdateReplacePolicy Retain. + * The complete prior definition survives until commit or successful rollback. + */ +@ApplicationScoped +public class LogsMetricFilterCfnProvisioner implements CfnResourceProvisioner { + private static final String TYPE = "AWS::Logs::MetricFilter"; + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final String GROUP_ATTR = "__FlociMetricFilterLogGroupName"; + private static final String NAME_MODE_ATTR = "__FlociMetricFilterNameMode"; + private static final String SNAPSHOT = CfnRollback.METRIC_FILTER_UPDATE_SNAPSHOT_ATTR; + private static final String UNCERTAIN_ATTR = "__FlociMetricFilterOwnershipUncertain"; + private static final String ABSENT_ATTR = "__FlociMetricFilterBackingAbsent"; + private static final String STATE_ATTR = "__FlociMetricFilterState"; + private static final String REPLACEMENT_REASON = "Requested update requires the replacement of the existing resource; " + + "deleting existing resource, then creating a new one."; + private static final Set UNITS = Set.of("Seconds", "Microseconds", "Milliseconds", "Bytes", "Kilobytes", + "Megabytes", "Gigabytes", "Terabytes", "Bits", "Kilobits", "Megabits", "Gigabits", "Terabits", "Percent", + "Count", "Bytes/Second", "Kilobytes/Second", "Megabytes/Second", "Gigabytes/Second", "Terabytes/Second", + "Bits/Second", "Kilobits/Second", "Megabits/Second", "Gigabits/Second", "Terabits/Second", "Count/Second", "None"); + private final CloudWatchLogsMetricFilterService metricFilters; + + private enum Ownership { OWNED, UNOWNED, UNKNOWN } + private enum Operation { CREATE, UPDATE, DELETE, OBSERVE } + + @Inject + public LogsMetricFilterCfnProvisioner(CloudWatchLogsMetricFilterService metricFilters) { + this.metricFilters = metricFilters; + } + + @Override + public Set resourceTypes() { + return Set.of(TYPE); + } + + @Override + public void provision(StackResource r, JsonNode props, ProvisionContext ctx) { + JsonNode resolved = pruneNoValue(ctx.engine().resolveNode(markNoValue(props))); + MetricFilter definition = definition(resolved); + String explicitName = string(resolved, "FilterName", false, 1, 512, "[^:*]+"); + Identity prior = ctx.isUpdate() ? identity(r, ctx.priorPhysicalId()) : null; + String name = explicitName != null ? explicitName + : prior != null && !"explicit".equals(r.getAttributes().get(NAME_MODE_ATTR)) + ? prior.name() : ctx.generatePhysicalName(r.getLogicalId(), 512, false); + definition.setFilterName(name); + Identity target = new Identity(definition.getLogGroupName(), name); + ObjectNode snapshot = prior == null ? null : snapshot(r, prior, target, ctx.region()); + boolean replacement = snapshot != null && snapshot.path("replacement").asBoolean(); + ObjectNode state = snapshot == null ? state(target, Ownership.UNOWNED) : targetState(snapshot); + if (replacement) { + report(ctx.progress(), prior.name(), "DELETE_IN_PROGRESS", REPLACEMENT_REASON); + try { + mutate(r, priorState(snapshot), snapshot, Operation.DELETE, null, ctx.region()); + } catch (RuntimeException e) { + report(ctx.progress(), prior.name(), "DELETE_FAILED", e.getMessage()); + throw e; + } + report(ctx.progress(), prior.name(), "DELETE_COMPLETE", null); + setIdentity(r, target, explicitName != null); + report(ctx.progress(), target.name(), "CREATE_IN_PROGRESS", null); + } + if (snapshot == null) { + r.getAttributes().put(GROUP_ATTR, target.group()); + r.getAttributes().put(NAME_MODE_ATTR, explicitName != null ? "explicit" : "generated"); + } + try { + mutate(r, state, snapshot, prior == null || replacement ? Operation.CREATE : Operation.UPDATE, + definition, ctx.region()); + } catch (RuntimeException e) { + if (replacement) { + report(ctx.progress(), target.name(), "CREATE_FAILED", e.getMessage()); + } + throw e; + } + setIdentity(r, target, explicitName != null); + if (replacement) { + report(ctx.progress(), target.name(), "CREATE_COMPLETE", null); + } + } + + private static void setIdentity(StackResource r, Identity identity, boolean explicit) { + r.setPhysicalId(identity.name()); + r.getAttributes().put(GROUP_ATTR, identity.group()); + r.getAttributes().put(NAME_MODE_ATTR, explicit ? "explicit" : "generated"); + } + + private record Identity(String group, String name) {} + + private static Identity identity(StackResource r, String physicalId) { + String group = r.getAttributes().get(GROUP_ATTR); + if (group == null) { + throw new IllegalStateException("Missing metric filter group identity for " + r.getLogicalId()); + } + return new Identity(group, physicalId); + } + + private ObjectNode snapshot(StackResource r, Identity prior, Identity target, String region) { + if (r.getAttributes().containsKey(SNAPSHOT)) { + throw new IllegalStateException("Metric filter update still needs rollback or commit: " + r.getLogicalId()); + } + ObjectNode priorState = currentState(r, prior); + resolveUnknown(r, priorState, null, region); + ObjectNode snapshot = MAPPER.createObjectNode(); + snapshot.put("region", region); + snapshot.put("replacement", !prior.equals(target)); + snapshot.set("attributes", MAPPER.valueToTree(r.getAttributes())); + if (ownership(priorState) == Ownership.UNOWNED) { + snapshot.put("absent", true); + } else { + metricFilters.findMetricFilter(prior.group(), prior.name(), region).ifPresentOrElse( + filter -> snapshot.set("definition", MAPPER.valueToTree(filter)), + () -> { + snapshot.put("absent", true); + priorState.put("ownership", Ownership.UNOWNED.name()); + }); + } + snapshot.set("prior", priorState); + if (!prior.equals(target)) { + snapshot.set("target", state(target, Ownership.UNOWNED)); + } + save(r, snapshot); + return snapshot; + } + + private static void save(StackResource r, ObjectNode snapshot) { + r.getAttributes().put(SNAPSHOT, snapshot.toString()); + } + + @Override + public void delete(StackResource resource, String region) { + String raw = resource.getAttributes().get(SNAPSHOT); + if (raw != null) { + ObjectNode snapshot = object(raw); + if (snapshot.path("replacement").asBoolean()) { + mutate(resource, targetState(snapshot), snapshot, Operation.DELETE, null, region); + } + mutate(resource, priorState(snapshot), snapshot, Operation.DELETE, null, region); + } else if (resource.getPhysicalId() != null) { + mutate(resource, currentState(resource, identity(resource, resource.getPhysicalId())), + null, Operation.DELETE, null, region); + } + } + + private static ObjectNode state(Identity identity, Ownership ownership) { + ObjectNode state = MAPPER.createObjectNode(); + state.put("group", identity.group()); + state.put("name", identity.name()); + state.put("ownership", ownership.name()); + state.put("operation", Operation.OBSERVE.name()); + state.put("outcome", ownership == Ownership.UNKNOWN + ? MutationOutcome.UNKNOWN.name() : MutationOutcome.NOT_APPLIED.name()); + return state; + } + + private ObjectNode currentState(StackResource resource, Identity identity) { + String raw = resource.getAttributes().get(STATE_ATTR); + if (raw != null) { + ObjectNode state = object(raw); + if (!identity.equals(address(state))) { + throw new IllegalStateException("Metric filter state does not match its physical identity"); + } + return state; + } + if ("true".equals(resource.getAttributes().get(UNCERTAIN_ATTR))) { + return state(identity, Ownership.UNKNOWN); + } + if ("true".equals(resource.getAttributes().get(ABSENT_ATTR))) { + return state(identity, Ownership.UNOWNED); + } + boolean completed = "CREATE_COMPLETE".equals(resource.getStatus()) || "UPDATE_COMPLETE".equals(resource.getStatus()); + return state(identity, completed ? Ownership.OWNED : Ownership.UNKNOWN); + } + + private static Ownership ownership(ObjectNode state) { + return Ownership.valueOf(state.path("ownership").asText()); + } + + private static Identity address(ObjectNode state) { + return new Identity(state.path("group").asText(), state.path("name").asText()); + } + + private static ObjectNode priorState(ObjectNode snapshot) { + return (ObjectNode) snapshot.get("prior"); + } + + private static ObjectNode targetState(ObjectNode snapshot) { + return snapshot.path("replacement").asBoolean() ? (ObjectNode) snapshot.get("target") : priorState(snapshot); + } + + private static ObjectNode object(String raw) { + try { + if (MAPPER.readTree(raw) instanceof ObjectNode object) { + return object; + } + throw new IllegalStateException("Metric filter mutation metadata must be an object"); + } catch (JsonProcessingException e) { + throw new IllegalStateException("Invalid metric filter mutation metadata", e); + } + } + + private static void saveTracking(StackResource resource, ObjectNode state, ObjectNode snapshot) { + if (snapshot == null) { + resource.getAttributes().put(STATE_ATTR, state.toString()); + } else { + save(resource, snapshot); + } + } + + private void resolveUnknown(StackResource resource, ObjectNode state, ObjectNode snapshot, String region) { + if (ownership(state) != Ownership.UNKNOWN) { + return; + } + saveTracking(resource, state, snapshot); + Identity identity = address(state); + if (!metricFilters.confirmMetricFilterAbsent(identity.group(), identity.name(), region, () -> { + state.put("ownership", Ownership.UNOWNED.name()); + state.put("operation", Operation.OBSERVE.name()); + state.put("outcome", MutationOutcome.NOT_APPLIED.name()); + saveTracking(resource, state, snapshot); + })) { + throw new IllegalStateException("Metric filter write outcome is uncertain for " + + identity.group() + "/" + identity.name() + + "; refusing to delete or adopt a filter without confirmed ownership"); + } + } + + private void mutate(StackResource resource, ObjectNode state, ObjectNode snapshot, Operation requested, + MetricFilter definition, String region) { + resolveUnknown(resource, state, snapshot, region); + Ownership before = ownership(state); + if (requested == Operation.DELETE && before == Ownership.UNOWNED) { + return; + } + Operation operation = requested == Operation.UPDATE && before == Ownership.UNOWNED ? Operation.CREATE : requested; + Identity identity = address(state); + boolean firstCreate = snapshot == null && operation == Operation.CREATE && resource.getPhysicalId() == null; + state.put("ownership", Ownership.UNKNOWN.name()); + state.put("operation", operation.name()); + state.put("outcome", MutationOutcome.UNKNOWN.name()); + saveTracking(resource, state, snapshot); + if (firstCreate) { + resource.setPhysicalId(identity.name()); + resource.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true"); + } + Consumer outcome = result -> { + Ownership after; + if (result.outcome() == MutationOutcome.UNKNOWN) { + after = Ownership.UNKNOWN; + } else if (operation == Operation.CREATE) { + after = result.outcome() == MutationOutcome.APPLIED ? Ownership.OWNED : Ownership.UNOWNED; + } else { + after = result.present() == null ? before : result.present() ? Ownership.OWNED : Ownership.UNOWNED; + } + state.put("ownership", after.name()); + state.put("outcome", result.outcome().name()); + saveTracking(resource, state, snapshot); + if (snapshot == null) { + if (firstCreate && after == Ownership.UNOWNED) { + resource.setPhysicalId(null); + resource.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR); + } + finishState(resource, state); + } + }; + switch (operation) { + case CREATE -> metricFilters.createMetricFilter(definition, region, outcome); + case UPDATE -> metricFilters.updateMetricFilter(definition, region, outcome); + case DELETE -> metricFilters.deleteMetricFilter(identity.group(), identity.name(), region, outcome); + case OBSERVE -> throw new IllegalStateException("Observation is not a mutation"); + } + } + + private static void finishState(StackResource resource, ObjectNode state) { + resource.getAttributes().put(STATE_ATTR, state.toString()); + resource.getAttributes().remove(UNCERTAIN_ATTR); + if (ownership(state) == Ownership.OWNED) { + resource.getAttributes().remove(ABSENT_ATTR); + } else if (ownership(state) == Ownership.UNOWNED) { + resource.getAttributes().put(ABSENT_ATTR, "true"); + } + } + + @Override + public void clearUpdate(StackResource resource) { + String raw = resource.getAttributes().get(SNAPSHOT); + if (raw == null) { + return; + } + ObjectNode snapshot = object(raw); + if (!canCommit(snapshot)) { + throw new IllegalStateException("Cannot commit unconfirmed metric filter mutations"); + } + finishState(resource, targetState(snapshot)); + resource.getAttributes().remove(SNAPSHOT); + } + + @Override + public UpdateCleanupResult completeUpdate(StackResource resource) { + // DeleteStack also invokes this hook, including after a failed rollback. + String raw = resource.getAttributes().get(SNAPSHOT); + if ("UPDATE_COMPLETE".equals(resource.getStatus()) && raw != null + && canCommit(object(raw))) { + clearUpdate(resource); + } + return UpdateCleanupResult.notApplicable(); + } + + private static boolean canCommit(ObjectNode snapshot) { + return ownership(targetState(snapshot)) == Ownership.OWNED + && (!snapshot.path("replacement").asBoolean() || ownership(priorState(snapshot)) == Ownership.UNOWNED); + } + + @Override + public boolean hasPendingRollbackCleanup(StackResource resource) { + return resource.getAttributes().containsKey(SNAPSHOT) + || resource.getAttributes().containsKey(STATE_ATTR) + || resource.getAttributes().containsKey(UNCERTAIN_ATTR) + || resource.getAttributes().containsKey(GROUP_ATTR) + || resource.getAttributes().containsKey(NAME_MODE_ATTR) + || "true".equals(resource.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR)); + } + + @Override + public boolean retainsFailedUpdateState(StackResource resource) { + return resource.getAttributes().containsKey(SNAPSHOT); + } + + @Override + public boolean rollbackUpdate(StackResource resource) { + return rollbackUpdate(resource, event -> {}); + } + + @Override + public boolean rollbackUpdate(StackResource resource, Consumer progress) { + String raw = resource.getAttributes().get(SNAPSHOT); + if (raw == null) { + return true; + } + try { + ObjectNode snapshot = object(raw); + String region = snapshot.path("region").asText(); + boolean replacement = snapshot.path("replacement").asBoolean(); + ObjectNode priorState = priorState(snapshot); + ObjectNode targetState = targetState(snapshot); + Identity prior = address(priorState); + Identity target = address(targetState); + if (replacement) { + report(progress, target.name(), "UPDATE_IN_PROGRESS", "Resource update rolled back"); + } + if (replacement || snapshot.path("absent").asBoolean()) { + resolveUnknown(resource, targetState, snapshot, region); + boolean announceDelete = replacement && (ownership(targetState) == Ownership.OWNED + || metricFilters.findMetricFilter(target.group(), target.name(), region).isEmpty()); + if (announceDelete) { + report(progress, target.name(), "DELETE_IN_PROGRESS", null); + } + mutate(resource, targetState, snapshot, Operation.DELETE, null, region); + if (announceDelete) { + report(progress, target.name(), "DELETE_COMPLETE", null); + } + } + if (!snapshot.path("absent").asBoolean()) { + if (replacement) { + report(progress, target.name(), "CREATE_IN_PROGRESS", null); + } + MetricFilter definition = MAPPER.treeToValue(snapshot.get("definition"), MetricFilter.class); + mutate(resource, priorState, snapshot, Operation.UPDATE, definition, region); + if (replacement) { + report(progress, prior.name(), "CREATE_COMPLETE", null); + } + } + + Map attributes = new HashMap<>(); + snapshot.path("attributes").fields().forEachRemaining(e -> attributes.put(e.getKey(), e.getValue().asText())); + resource.setAttributes(attributes); + resource.setPhysicalId(prior.name()); + resource.getAttributes().put(GROUP_ATTR, prior.group()); + finishState(resource, priorState); + return true; + } catch (JsonProcessingException e) { + throw new IllegalStateException("Invalid metric filter rollback snapshot for " + resource.getLogicalId(), e); + } + } + + // Preserve NoValue through intrinsic resolution without confusing it with a valid empty + // FilterPattern. The shared engine otherwise renders both as an empty string. + private static JsonNode markNoValue(JsonNode node) { + if (node == null) { + return null; + } + if (node.isObject()) { + if ("AWS::NoValue".equals(node.path("Ref").asText())) { + return MissingNode.getInstance(); + } + ObjectNode result = MAPPER.createObjectNode(); + node.fields().forEachRemaining(e -> result.set(e.getKey(), markNoValue(e.getValue()))); + return result; + } + if (node.isArray()) { + ArrayNode result = MAPPER.createArrayNode(); + node.forEach(item -> result.add(markNoValue(item))); + return result; + } + return node; + } + + private static JsonNode pruneNoValue(JsonNode node) { + if (node != null && node.isObject()) { + ObjectNode result = MAPPER.createObjectNode(); + node.fields().forEachRemaining(e -> { + if (!e.getValue().isMissingNode()) { + result.set(e.getKey(), pruneNoValue(e.getValue())); + } + }); + return result; + } + if (node != null && node.isArray()) { + ArrayNode result = MAPPER.createArrayNode(); + node.forEach(item -> { + if (!item.isMissingNode()) { + result.add(pruneNoValue(item)); + } + }); + return result; + } + return node; + } + + private static MetricFilter definition(JsonNode props) { + if (props == null || !props.isObject()) { + throw invalid("Properties must be an object"); + } + onlyMembers(props, Set.of("LogGroupName", "FilterName", "FilterPattern", "MetricTransformations", + "ApplyOnTransformedLogs", "FieldSelectionCriteria", "EmitSystemFieldDimensions")); + MetricFilter filter = new MetricFilter(); + filter.setLogGroupName(string(props, "LogGroupName", true, 1, 512, "[.\\-_/#A-Za-z0-9]+")); + filter.setFilterPattern(string(props, "FilterPattern", true, 0, 1024, null)); + JsonNode transformations = props.get("MetricTransformations"); + if (transformations == null || !transformations.isArray() || transformations.size() != 1 + || !transformations.get(0).isObject()) { + throw invalid("MetricTransformations must contain exactly one object"); + } + filter.setMetricTransformations(List.of(transformation(transformations.get(0)))); + JsonNode transformed = props.get("ApplyOnTransformedLogs"); + if (transformed != null) { + if (!transformed.isBoolean() && (!transformed.isTextual() + || !Set.of("true", "false").contains(transformed.asText()))) { + throw invalid("ApplyOnTransformedLogs must be a boolean"); + } + filter.setApplyOnTransformedLogs(Boolean.parseBoolean(transformed.asText())); + } + filter.setFieldSelectionCriteria(string(props, "FieldSelectionCriteria", false, 0, 2000, null)); + JsonNode fields = props.get("EmitSystemFieldDimensions"); + if (fields != null) { + if (!fields.isArray()) { + throw invalid("EmitSystemFieldDimensions must be an array"); + } + List values = new ArrayList<>(); + for (JsonNode field : fields) { + if (!field.isTextual()) { + throw invalid("EmitSystemFieldDimensions entries must be strings"); + } + values.add(field.textValue()); + } + filter.setEmitSystemFieldDimensions(values); + } + return filter; + } + + private static MetricTransformation transformation(JsonNode node) { + onlyMembers(node, Set.of("MetricName", "MetricNamespace", "MetricValue", "DefaultValue", "Dimensions", "Unit")); + MetricTransformation t = new MetricTransformation(); + t.setMetricName(string(node, "MetricName", true, 1, 255, "[\\x00-\\x7F&&[^:*$]]+")); + t.setMetricNamespace(string(node, "MetricNamespace", true, 1, 256, "[0-9a-zA-Z.\\-_/#]+")); + t.setMetricValue(string(node, "MetricValue", true, 1, 100, ".+")); + JsonNode defaultValue = node.get("DefaultValue"); + if (defaultValue != null && !defaultValue.isNull()) { + try { + if ((!defaultValue.isNumber() && !defaultValue.isTextual()) + || !defaultValue.asText().matches("[-+]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][-+]?\\d+)?")) { + throw invalid("DefaultValue must be a finite number"); + } + double value = Double.parseDouble(defaultValue.asText()); + if (!Double.isFinite(value)) { + throw invalid("DefaultValue must be a finite number"); + } + t.setDefaultValue(value); + } catch (NumberFormatException e) { + throw invalid("DefaultValue must be a finite number"); + } + } + JsonNode dimensions = node.get("Dimensions"); + if (dimensions != null) { + if (!dimensions.isArray() || dimensions.isEmpty() || dimensions.size() > 3) { + throw invalid("Dimensions must be an array of one to three Key/Value objects"); + } + Map byKey = new LinkedHashMap<>(); + for (JsonNode dimension : dimensions) { + onlyMembers(dimension, Set.of("Key", "Value")); + String key = string(dimension, "Key", true, 1, 255, "[\\x00-\\x7F]+"); + String value = string(dimension, "Value", true, 1, 255, null); + if (key.isBlank() || key.startsWith(":") || byKey.putIfAbsent(key, value) != null) { + throw invalid("Dimensions need unique ASCII keys, nonblank and not starting with ':'"); + } + } + t.setDimensions(byKey); + } + t.setUnit(string(node, "Unit", false, 1, 64, null)); + if (t.getUnit() != null && !UNITS.contains(t.getUnit())) { + throw invalid("Unit must be a CloudWatch metric unit"); + } + return t; + } + + private static void onlyMembers(JsonNode node, Set members) { + node.fieldNames().forEachRemaining(member -> { + if (!members.contains(member)) { + throw invalid("Unknown property " + member); + } + }); + } + + private static String string(JsonNode node, String member, boolean required, int min, int max, String pattern) { + JsonNode value = node == null ? null : node.get(member); + if (value == null && !required) { + return null; + } + if (value == null || !value.isTextual() || value.textValue().length() < min + || value.textValue().length() > max + || pattern != null && !value.textValue().matches(pattern)) { + throw invalid(member + " must be a string of length " + min + " to " + max + + (pattern == null ? "" : " matching " + pattern)); + } + return value.textValue(); + } + + private static AwsException invalid(String message) { + return new AwsException("ValidationError", TYPE + " " + message, 400); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java index fa617a92a5..c57d94ff6d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java @@ -2,12 +2,14 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners; import com.fasterxml.jackson.databind.JsonNode; import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; +import io.github.hectorvent.floci.services.cloudformation.model.StackEvent; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.function.Consumer; /** * The per-provision context every resource handler drew from: the template engine (for resolving @@ -16,7 +18,22 @@ import java.util.UUID; * so extracted provisioners produce byte-identical physical ids and resolved values. */ public record ProvisionContext(CloudFormationTemplateEngine engine, String region, - String accountId, String stackName, String priorPhysicalId) { + String accountId, String stackName, String priorPhysicalId, + Consumer progress) { + + public ProvisionContext(CloudFormationTemplateEngine engine, String region, + String accountId, String stackName, String priorPhysicalId) { + this(engine, region, accountId, stackName, priorPhysicalId, event -> {}); + } + + /** Intermediate resource events retain the stack pipeline's event IDs and metadata. */ + public static void report(Consumer progress, String physicalId, String status, String reason) { + StackEvent event = new StackEvent(); + event.setPhysicalResourceId(physicalId); + event.setResourceStatus(status); + event.setResourceStatusReason(reason); + progress.accept(event); + } /** A context for a first-time create, with no prior physical id. */ public ProvisionContext(CloudFormationTemplateEngine engine, String region, diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java index 86fd3e679b..43da18d4c1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java @@ -28,14 +28,17 @@ public class CloudWatchLogsHandler { private final CloudWatchLogsService logsService; private final CloudWatchLogsCrossAccountService crossAccountService; + private final CloudWatchLogsMetricFilterHandler metricFilterHandler; private final ObjectMapper objectMapper; @Inject public CloudWatchLogsHandler(CloudWatchLogsService logsService, CloudWatchLogsCrossAccountService crossAccountService, + CloudWatchLogsMetricFilterService metricFilterService, ObjectMapper objectMapper) { this.logsService = logsService; this.crossAccountService = crossAccountService; + this.metricFilterHandler = new CloudWatchLogsMetricFilterHandler(metricFilterService, objectMapper); this.objectMapper = objectMapper; } @@ -62,6 +65,8 @@ public class CloudWatchLogsHandler { case "PutSubscriptionFilter" -> handlePutSubscriptionFilter(request, region); case "DescribeSubscriptionFilters" -> handleDescribeSubscriptionFilters(request, region); case "DeleteSubscriptionFilter" -> handleDeleteSubscriptionFilter(request, region); + case "PutMetricFilter", "DescribeMetricFilters", "DeleteMetricFilter", "TestMetricFilter" -> + metricFilterHandler.handle(action, request, region); case "AssociateKmsKey" -> handleAssociateKmsKey(request, region); case "DisassociateKmsKey" -> handleDisassociateKmsKey(request, region); case "PutResourcePolicy" -> handlePutResourcePolicy(request, region); @@ -130,7 +135,7 @@ public class CloudWatchLogsHandler { node.put("kmsKeyId", g.getKmsKeyId()); } node.put("storedBytes", 0); - node.put("metricFilterCount", 0); + node.put("metricFilterCount", metricFilterHandler.metricFilterCount(g.getLogGroupName(), region)); groupsArray.add(node); } response.set("logGroups", groupsArray); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterHandler.java new file mode 100644 index 0000000000..cf1a4ac7a3 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterHandler.java @@ -0,0 +1,218 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import jakarta.ws.rs.core.Response; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * The wire shapes of the metric filter operations, reached through {@link CloudWatchLogsHandler}: + * request members in the JSON 1.1 form the Logs API takes, responses as the API reference lists + * them. + */ +class CloudWatchLogsMetricFilterHandler { + + private final CloudWatchLogsMetricFilterService metricFilters; + private final ObjectMapper objectMapper; + + CloudWatchLogsMetricFilterHandler(CloudWatchLogsMetricFilterService metricFilters, ObjectMapper objectMapper) { + this.metricFilters = metricFilters; + this.objectMapper = objectMapper; + } + + Response handle(String action, JsonNode request, String region) { + requireObject(request, "request"); + return switch (action) { + case "PutMetricFilter" -> putMetricFilter(request, region); + case "DescribeMetricFilters" -> describeMetricFilters(request, region); + case "DeleteMetricFilter" -> deleteMetricFilter(request, region); + case "TestMetricFilter" -> testMetricFilter(request); + default -> throw new IllegalStateException("not a metric filter action: " + action); + }; + } + + int metricFilterCount(String logGroupName, String region) { + return metricFilters.countMetricFilters(logGroupName, region); + } + + private Response putMetricFilter(JsonNode request, String region) { + MetricFilter definition = new MetricFilter(); + definition.setLogGroupName(text(request, "logGroupName")); + definition.setFilterName(text(request, "filterName")); + definition.setFilterPattern(text(request, "filterPattern")); + List transformations = new ArrayList<>(); + JsonNode transformationNodes = request.get("metricTransformations"); + requireArray(transformationNodes, "metricTransformations"); + transformationNodes.forEach(node -> transformations.add(transformation(node))); + definition.setMetricTransformations(transformations); + if (request.has("applyOnTransformedLogs")) { + JsonNode flag = request.get("applyOnTransformedLogs"); + if (!flag.isBoolean()) { + throw invalid("applyOnTransformedLogs must be a boolean."); + } + definition.setApplyOnTransformedLogs(flag.booleanValue()); + } + definition.setFieldSelectionCriteria(text(request, "fieldSelectionCriteria")); + if (request.has("emitSystemFieldDimensions")) { + List fields = new ArrayList<>(); + JsonNode fieldNodes = request.get("emitSystemFieldDimensions"); + requireArray(fieldNodes, "emitSystemFieldDimensions"); + fieldNodes.forEach(node -> fields.add(string(node, "emitSystemFieldDimensions member"))); + definition.setEmitSystemFieldDimensions(fields); + } + metricFilters.putMetricFilter(definition, region); + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private static MetricTransformation transformation(JsonNode node) { + requireObject(node, "metricTransformation"); + MetricTransformation t = new MetricTransformation(); + t.setMetricName(text(node, "metricName")); + t.setMetricNamespace(text(node, "metricNamespace")); + t.setMetricValue(text(node, "metricValue")); + if (node.has("defaultValue")) { + JsonNode defaultValue = node.get("defaultValue"); + if (defaultValue.isTextual()) { + throw new AwsException("SerializationException", "STRING_VALUE cannot be converted to Double", 400); + } + if (!defaultValue.isNumber()) { + throw invalid("defaultValue must be a number."); + } + double value = defaultValue.asDouble(); + if (!Double.isFinite(value)) { + throw invalid("defaultValue must be finite."); + } + t.setDefaultValue(value); + } + if (node.has("dimensions")) { + Map dimensions = new LinkedHashMap<>(); + requireObject(node.get("dimensions"), "dimensions"); + node.get("dimensions").fields().forEachRemaining(entry -> + dimensions.put(entry.getKey(), string(entry.getValue(), "dimension value"))); + t.setDimensions(dimensions); + } + t.setUnit(text(node, "unit")); + return t; + } + + private Response describeMetricFilters(JsonNode request, String region) { + CloudWatchLogsMetricFilterService.DescribeMetricFiltersResult result = metricFilters.describeMetricFilters( + text(request, "logGroupName"), text(request, "filterNamePrefix"), text(request, "metricName"), + text(request, "metricNamespace"), text(request, "nextToken"), + limit(request), region); + ObjectNode response = objectMapper.createObjectNode(); + ArrayNode filters = response.putArray("metricFilters"); + result.metricFilters().forEach(filter -> filters.add(render(filter))); + if (result.nextToken() != null) { + response.put("nextToken", result.nextToken()); + } + return Response.ok(response).build(); + } + + private ObjectNode render(MetricFilter filter) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("filterName", filter.getFilterName()); + node.put("filterPattern", filter.getFilterPattern()); + ArrayNode transformations = node.putArray("metricTransformations"); + for (MetricTransformation t : filter.getMetricTransformations()) { + ObjectNode rendered = transformations.addObject(); + rendered.put("metricName", t.getMetricName()); + rendered.put("metricNamespace", t.getMetricNamespace()); + rendered.put("metricValue", t.getMetricValue()); + if (t.getDefaultValue() != null) { + rendered.put("defaultValue", t.getDefaultValue()); + } + if (t.getDimensions() != null && !t.getDimensions().isEmpty()) { + ObjectNode dimensions = rendered.putObject("dimensions"); + t.getDimensions().forEach(dimensions::put); + } + if (t.getUnit() != null) { + rendered.put("unit", t.getUnit()); + } + } + node.put("creationTime", filter.getCreationTime()); + node.put("logGroupName", filter.getLogGroupName()); + if (filter.getApplyOnTransformedLogs() != null) { + node.put("applyOnTransformedLogs", filter.getApplyOnTransformedLogs()); + } + if (filter.getFieldSelectionCriteria() != null) { + node.put("fieldSelectionCriteria", filter.getFieldSelectionCriteria()); + } + if (filter.getEmitSystemFieldDimensions() != null) { + ArrayNode fields = node.putArray("emitSystemFieldDimensions"); + filter.getEmitSystemFieldDimensions().forEach(fields::add); + } + return node; + } + + private Response deleteMetricFilter(JsonNode request, String region) { + metricFilters.deleteMetricFilter(text(request, "logGroupName"), text(request, "filterName"), region); + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response testMetricFilter(JsonNode request) { + List messages = new ArrayList<>(); + JsonNode messageNodes = request.get("logEventMessages"); + requireArray(messageNodes, "logEventMessages"); + messageNodes.forEach(node -> messages.add(string(node, "logEventMessages member"))); + List matches = metricFilters.testMetricFilter( + text(request, "filterPattern"), messages); + ObjectNode response = objectMapper.createObjectNode(); + ArrayNode rendered = response.putArray("matches"); + for (CloudWatchLogsMetricFilterService.MetricFilterMatchRecord match : matches) { + ObjectNode node = rendered.addObject(); + node.put("eventNumber", match.eventNumber()); + node.put("eventMessage", match.eventMessage()); + ObjectNode extracted = node.putObject("extractedValues"); + match.extractedValues().forEach(extracted::put); + } + return Response.ok(response).build(); + } + + private static String text(JsonNode node, String member) { + return node.has(member) ? string(node.get(member), member) : null; + } + + private static String string(JsonNode node, String member) { + if (!node.isTextual()) { + throw invalid(member + " must be a string."); + } + return node.textValue(); + } + + private static void requireObject(JsonNode node, String member) { + if (node == null || !node.isObject()) { + throw invalid(member + " must be an object."); + } + } + + private static void requireArray(JsonNode node, String member) { + if (node == null || !node.isArray()) { + throw invalid(member + " must be an array."); + } + } + + private static Integer limit(JsonNode request) { + if (!request.has("limit")) { + return null; + } + JsonNode node = request.get("limit"); + if (!node.isIntegralNumber() || !node.canConvertToInt() || node.intValue() < 1 || node.intValue() > 50) { + throw invalid("limit must be an integer between 1 and 50."); + } + return node.intValue(); + } + + private static AwsException invalid(String message) { + return new AwsException("InvalidParameterException", message, 400); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterService.java new file mode 100644 index 0000000000..ce96c01230 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterService.java @@ -0,0 +1,883 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.Resettable; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.services.cloudwatch.logs.filter.FilterMatch; +import io.github.hectorvent.floci.services.cloudwatch.logs.filter.FilterPattern; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogEvent; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum; +import io.quarkus.runtime.ShutdownEvent; +import io.quarkus.runtime.StartupEvent; +import jakarta.annotation.PreDestroy; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; +import org.jboss.logging.Logger; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; +import java.util.regex.Pattern; + +import static io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService.requireFilterPattern; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.MAX_DIMENSIONS; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.NUMBER; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.invalid; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.number; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.selects; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.validateFieldSelectionCriteria; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.validateSystemFields; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.withSystemDimensions; + +/** + * Metric filters of CloudWatch Logs: the PutMetricFilter, DescribeMetricFilters, DeleteMetricFilter + * and TestMetricFilter operations, and the part AWS does at ingestion, publishing a metric value for + * every log event a filter matches. The filters live in their own store, keyed by Region, log group + * and name. Both filter families share storage coordination with the log service, which tells + * this one about ingestion and deleted groups through CDI events. + */ +@ApplicationScoped +public class CloudWatchLogsMetricFilterService implements Resettable { + + private static final Logger LOG = Logger.getLogger(CloudWatchLogsMetricFilterService.class); + + /** AWS's cap on metric filters per log group. */ + public static final int MAX_FILTERS_PER_LOG_GROUP = 100; + private static final int MAX_METRIC_VALUE_LENGTH = 100; + private static final int MAX_DIMENSION_LENGTH = 255; + private static final int MAX_TEST_MESSAGES = 50; + private static final int MAX_DESCRIBE_LIMIT = 50; + private static final Pattern FILTER_NAME = Pattern.compile("[^:*]{1,512}"); + private static final Pattern METRIC_NAME = Pattern.compile("[^:*$]{1,255}"); + private static final Set UNITS = Set.of("Seconds", "Microseconds", "Milliseconds", "Bytes", "Kilobytes", + "Megabytes", "Gigabytes", "Terabytes", "Bits", "Kilobits", "Megabits", "Gigabits", "Terabits", "Percent", + "Count", "Bytes/Second", "Kilobytes/Second", "Megabytes/Second", "Gigabytes/Second", "Terabytes/Second", + "Bits/Second", "Kilobits/Second", "Megabits/Second", "Gigabits/Second", "Terabits/Second", "Count/Second", + "None"); + + private final StorageBackend store; + private final CloudWatchLogsService logsService; + private final CloudWatchMetricsService metricsService; + private final RegionResolver regionResolver; + + /** + * Accepted samples wait here for the publisher thread, so PutLogEvents never writes to the Metrics + * sink itself. ponytail: one fixed ceiling and one worker; a batch that does not fit is written + * inline on the request thread instead of being dropped. Shard the queue if that shows in traces. + */ + static final int MAX_QUEUED_SAMPLES = 100_000; + private final int queueCapacity; + private final ArrayDeque queued = new ArrayDeque<>(); + private int queuedSamples; + /** Bumped by every reset or clear, so a batch taken before one is never written after it. */ + private int resetGeneration; + private final AtomicBoolean drainScheduled = new AtomicBoolean(); + /** Failed samples retried by the worker. Process-local best effort, bounded by samples. No durable outbox. */ + private static final int MAX_PENDING_SAMPLES = 10_000; + private final Map pending = new LinkedHashMap<>(); + // One detail per owning filter's pending-work outage, then one aggregate warning per 60 + // failed worker ticks (at least a minute at the production cadence). No exceptions retained. + private static final int RETRY_FAILURE_SUMMARY_TICKS = 60; + private final Set reportedOutages = new HashSet<>(); + private int failedRetryTicks; + // The canonical filter backend is the only monitor, shared with quotas and group deletion. + // Never acquire StorageFactory's monitor while holding a separate runtime-state monitor. + private ScheduledExecutorService publisher; + private boolean enabled = true; + private boolean paused; + private boolean stopped; + + private record Owner(String account, String region, String group, String filter) {} + + /** One filter's samples for one PutLogEvents request, evaluated at ingestion and immutable after. */ + private record Batch(Owner owner, List publications) { + Batch { + publications = List.copyOf(publications); + } + } + + private record Publication(String id, Owner owner, String namespace, String metricName, String unit, + List dimensions, double value, long timestamp) { + Publication { + dimensions = List.copyOf(dimensions); + } + + MetricDatum datum() { + MetricDatum datum = new MetricDatum(); + datum.setMetricName(metricName); + datum.setUnit(unit); + datum.setDimensions(dimensions); + datum.setValue(value); + datum.setTimestamp(timestamp); + return datum; + } + } + + @Inject + public CloudWatchLogsMetricFilterService(CloudWatchLogsService logsService, + CloudWatchMetricsService metricsService, RegionResolver regionResolver) { + this(logsService, metricsService, regionResolver, MAX_QUEUED_SAMPLES); + } + + CloudWatchLogsMetricFilterService(CloudWatchLogsService logsService, CloudWatchMetricsService metricsService, + RegionResolver regionResolver, int queueCapacity) { + this.store = logsService.metricFilterStore(); + this.logsService = logsService; + this.metricsService = metricsService; + this.regionResolver = regionResolver; + this.queueCapacity = queueCapacity; + } + + /** A page of DescribeMetricFilters. */ + public record DescribeMetricFiltersResult(List metricFilters, String nextToken) {} + + /** One TestMetricFilter match: the event's position in the request, its text and what the pattern extracted. */ + public record MetricFilterMatchRecord(long eventNumber, String eventMessage, Map extractedValues) {} + + /** + * Creates the filter, or replaces it when the log group already has one with that name, keeping + * the original creation time. The definition is checked the way AWS checks it: the group must + * exist, the pattern must parse, the one transformation must name a metric and a value that is + * a number or a field the pattern can supply, dimensions are limited to three field references + * of a JSON or space-delimited pattern and exclude a default value, and the group holds at + * most 100 filters. + */ + public MetricFilter putMetricFilter(MetricFilter definition, String region) { + return putMetricFilter(definition, region, false, null); + } + + public enum MutationOutcome { APPLIED, NOT_APPLIED, UNKNOWN } + + /** Internal mutation evidence; presence is null when it could not be inspected. */ + public record MutationResult(MutationOutcome outcome, Boolean present) {} + + /** Internal CFN create: never adopts an existing filter through the public API's upsert. */ + public MetricFilter createMetricFilter(MetricFilter definition, String region, + Consumer outcome) { + return putMetricFilter(definition, region, true, outcome); + } + + /** Internal CFN update of a confirmed-owned identity, including snapshot restoration. */ + public MetricFilter updateMetricFilter(MetricFilter definition, String region, + Consumer outcome) { + return putMetricFilter(definition, region, false, outcome); + } + + private MetricFilter putMetricFilter(MetricFilter definition, String region, + boolean createOnly, Consumer outcome) { + synchronized (store) { + String logGroupName; + String filterName; + String key; + MetricFilter before = null; + Boolean present = null; + MetricFilter filter; + try { + logGroupName = requireLogGroup(definition.getLogGroupName(), region); + filterName = requireFilterName(definition.getFilterName()); + key = key(region, logGroupName, filterName); + if (createOnly) { + before = store.get(key).orElse(null); + present = before != null; + if (before != null) { + throw new AwsException("AlreadyExistsException", + "Metric filter " + filterName + " already exists in " + logGroupName, 400); + } + } + FilterPattern pattern = requireFilterPattern(definition.getFilterPattern()); + MetricTransformation transformation = requireTransformation(definition.getMetricTransformations(), pattern); + validateSystemFields(definition.getEmitSystemFieldDimensions(), transformation); + validateFieldSelectionCriteria(definition.getFieldSelectionCriteria()); + if (!createOnly) { + before = store.get(key).orElse(null); + present = before != null; + } + if (before == null && countMetricFilters(logGroupName, region) >= MAX_FILTERS_PER_LOG_GROUP) { + throw new AwsException("LimitExceededException", + "The log group " + logGroupName + " already has the maximum of " + MAX_FILTERS_PER_LOG_GROUP + + " metric filters.", 400); + } + logsService.validateFilterRegexQuota(logGroupName, filterName, pattern, true, region); + + filter = new MetricFilter(); + filter.setLogGroupName(logGroupName); + filter.setFilterName(filterName); + filter.setFilterPattern(definition.getFilterPattern()); + filter.setMetricTransformations(List.of(copy(transformation))); + filter.setCreationTime(before == null ? System.currentTimeMillis() : before.getCreationTime()); + filter.setApplyOnTransformedLogs(definition.getApplyOnTransformedLogs()); + filter.setFieldSelectionCriteria(definition.getFieldSelectionCriteria()); + filter.setEmitSystemFieldDimensions(definition.getEmitSystemFieldDimensions() == null + ? null : List.copyOf(definition.getEmitSystemFieldDimensions())); + } catch (RuntimeException failure) { + completeMutation(new MutationResult(MutationOutcome.NOT_APPLIED, present), outcome, null, failure); + throw failure; + } + mutateStore(key, before, filter, outcome, null); + LOG.infov("Put metric filter {0} on log group {1}", filterName, logGroupName); + return filter; + } + } + + /** The caller holds the canonical monitor through inspection and metadata recording. */ + private void mutateStore(String key, MetricFilter before, MetricFilter after, + Consumer outcome, Runnable onAbsent) { + RuntimeException failure = null; + MutationResult result = new MutationResult(MutationOutcome.APPLIED, after != null); + try { + if (after == null) { + store.delete(key); + } else { + store.put(key, after); + } + } catch (RuntimeException storageFailure) { + failure = storageFailure; + result = new MutationResult(MutationOutcome.UNKNOWN, null); + if (outcome != null) { + try { + MetricFilter current = store.get(key).orElse(null); + if (current == after) { + result = new MutationResult(MutationOutcome.APPLIED, current != null); + } else if (current == before) { + result = new MutationResult(MutationOutcome.NOT_APPLIED, current != null); + } + } catch (RuntimeException inspectionFailure) { + if (storageFailure != inspectionFailure) { + storageFailure.addSuppressed(inspectionFailure); + } + } + } + } + completeMutation(result, outcome, onAbsent, failure); + } + + private static void completeMutation(MutationResult result, Consumer outcome, + Runnable onAbsent, RuntimeException failure) { + if (outcome != null) { + try { + outcome.accept(result); + } catch (RuntimeException recordingFailure) { + if (failure == null) { + failure = recordingFailure; + } else if (failure != recordingFailure) { + failure.addSuppressed(recordingFailure); + } + } + } + if (onAbsent != null && Boolean.FALSE.equals(result.present())) { + try { + onAbsent.run(); + } catch (RuntimeException cleanupFailure) { + if (failure == null) { + failure = cleanupFailure; + } else if (failure != cleanupFailure) { + failure.addSuppressed(cleanupFailure); + } + } + } + if (failure != null) { + throw failure; + } + } + + private String requireLogGroup(String logGroupName, String region) { + if (logGroupName == null || logGroupName.isBlank()) { + throw invalid("logGroupName is required."); + } + if (!logsService.logGroupExists(logGroupName, region)) { + throw new AwsException("ResourceNotFoundException", "The specified log group does not exist.", 400); + } + return logGroupName; + } + + private static String requireFilterName(String filterName) { + if (filterName == null || filterName.isBlank()) { + throw invalid("filterName is required."); + } + if (!FILTER_NAME.matcher(filterName).matches()) { + throw invalid("filterName must be 1 to 512 characters and must not contain ':' or '*'."); + } + return filterName; + } + + private static MetricTransformation requireTransformation(List transformations, + FilterPattern pattern) { + if (transformations == null || transformations.size() != 1 || transformations.getFirst() == null) { + throw invalid("metricTransformations must contain exactly one transformation."); + } + MetricTransformation t = transformations.getFirst(); + if (t.getMetricName() == null || !METRIC_NAME.matcher(t.getMetricName()).matches()) { + throw invalid("metricName is required, must be at most 255 characters and must not contain ':', '*' or '$'."); + } + if (t.getMetricNamespace() == null || !METRIC_NAME.matcher(t.getMetricNamespace()).matches()) { + throw invalid("metricNamespace is required, must be at most 255 characters and must not contain ':', '*' or '$'."); + } + String value = t.getMetricValue(); + if (value == null || value.isBlank() || value.length() > MAX_METRIC_VALUE_LENGTH) { + throw invalid("metricValue is required and must be at most " + MAX_METRIC_VALUE_LENGTH + " characters."); + } + if (NUMBER.matcher(value).matches() ? number(value) == null : !pattern.declaresSingleValueField(value)) { + throw invalid("Invalid metric transformation: metric value " + value + " must be a valid number"); + } + if (t.getDefaultValue() != null && !Double.isFinite(t.getDefaultValue())) { + throw invalid("defaultValue must be a finite number."); + } + Map dimensions = t.getDimensions(); + if (dimensions != null && !dimensions.isEmpty()) { + if (dimensions.size() > MAX_DIMENSIONS) { + throw invalid("A metric filter can include at most " + MAX_DIMENSIONS + " dimensions."); + } + if (pattern.kind() != FilterPattern.Kind.JSON && pattern.kind() != FilterPattern.Kind.SPACE_DELIMITED) { + throw invalid("Dimensions are only available for JSON and space-delimited filter patterns."); + } + if (t.getDefaultValue() != null) { + throw invalid("A metric filter with dimensions cannot have a default value."); + } + for (Map.Entry dimension : dimensions.entrySet()) { + String name = dimension.getKey(); + String reference = dimension.getValue(); + if (name == null || name.isBlank() || name.length() > MAX_DIMENSION_LENGTH || name.startsWith(":")) { + throw invalid("Dimension names must be 1 to 255 characters and must not start with ':'."); + } + if (reference == null || reference.length() > MAX_DIMENSION_LENGTH || !pattern.declaresField(reference)) { + throw invalid("Dimension " + name + " must refer to a field of the filter pattern such as $.field or" + + " $field, got '" + reference + "'."); + } + } + } + if (t.getUnit() != null && !UNITS.contains(t.getUnit())) { + throw invalid("unit must be one of the CloudWatch standard units, got '" + t.getUnit() + "'."); + } + return t; + } + + private static MetricTransformation copy(MetricTransformation t) { + MetricTransformation copy = new MetricTransformation(); + copy.setMetricName(t.getMetricName()); + copy.setMetricNamespace(t.getMetricNamespace()); + copy.setMetricValue(t.getMetricValue()); + copy.setDefaultValue(t.getDefaultValue()); + copy.setDimensions(t.getDimensions() == null ? new LinkedHashMap<>() : new LinkedHashMap<>(t.getDimensions())); + copy.setUnit(t.getUnit()); + return copy; + } + + /** + * Lists filters, sorted by name as AWS does, for one log group or for the whole Region. As on + * AWS, {@code filterNamePrefix} applies only together with {@code logGroupName}, and + * {@code metricName} and {@code metricNamespace} must be given together or not at all. + */ + public DescribeMetricFiltersResult describeMetricFilters(String logGroupName, String filterNamePrefix, + String metricName, String metricNamespace, + String nextToken, Integer limit, String region) { + int pageSize = limit == null ? MAX_DESCRIBE_LIMIT : limit; + if (pageSize < 1 || pageSize > MAX_DESCRIBE_LIMIT) { + throw invalid("limit must be between 1 and " + MAX_DESCRIBE_LIMIT + "."); + } + if ((metricName == null) != (metricNamespace == null)) { + throw invalid("Describe Metric Filters request must contain both MetricName and MetricNamespace"); + } + boolean byGroup = logGroupName != null && !logGroupName.isBlank(); + if (byGroup) { + requireLogGroup(logGroupName, region); + } + String prefix = byGroup ? groupPrefix(region, logGroupName) : region + "::"; + List all = store.scan(key -> key.startsWith(prefix)); + List filtered = all.stream() + .filter(f -> !byGroup || filterNamePrefix == null || filterNamePrefix.isBlank() + || f.getFilterName().startsWith(filterNamePrefix)) + .filter(f -> metricNamespace == null || transformationMatches(f, metricName, metricNamespace)) + .sorted(Comparator.comparing(MetricFilter::getFilterName).thenComparing(MetricFilter::getLogGroupName)) + .toList(); + + int offset = 0; + if (nextToken != null && !nextToken.isBlank()) { + try { + offset = Integer.parseInt(nextToken); + } catch (NumberFormatException e) { + throw invalid("The specified nextToken is invalid."); + } + if (offset < 0 || offset > filtered.size()) { + throw invalid("The specified nextToken is invalid."); + } + } + int end = Math.min(offset + pageSize, filtered.size()); + String token = end < filtered.size() ? String.valueOf(end) : null; + return new DescribeMetricFiltersResult(filtered.subList(offset, end), token); + } + + private static boolean transformationMatches(MetricFilter filter, String metricName, String metricNamespace) { + return filter.getMetricTransformations().stream().anyMatch(t -> + metricNamespace.equals(t.getMetricNamespace()) && metricName.equals(t.getMetricName())); + } + + public void deleteMetricFilter(String logGroupName, String filterName, String region) { + synchronized (store) { + requireLogGroup(logGroupName, region); + if (filterName == null || filterName.isBlank()) { + throw invalid("filterName is required."); + } + String key = key(region, logGroupName, filterName); + if (store.get(key).isEmpty()) { + throw new AwsException("ResourceNotFoundException", "The specified metric filter does not exist.", 400); + } + store.delete(key); + cancelPublications(logGroupName, filterName, region); + LOG.infov("Deleted metric filter {0} on log group {1}", filterName, logGroupName); + } + } + + /** Internal CFN delete with evidence recorded before another creator can acquire the monitor. */ + public void deleteMetricFilter(String logGroupName, String filterName, String region, + Consumer outcome) { + synchronized (store) { + String key = key(region, logGroupName, filterName); + MetricFilter before; + try { + before = store.get(key).orElse(null); + } catch (RuntimeException failure) { + completeMutation(new MutationResult(MutationOutcome.NOT_APPLIED, null), outcome, null, failure); + throw failure; + } + Runnable cancel = () -> cancelPublications(logGroupName, filterName, region); + if (before == null) { + completeMutation(new MutationResult(MutationOutcome.NOT_APPLIED, false), outcome, cancel, null); + return; + } + mutateStore(key, before, null, outcome, cancel); + LOG.infov("Deleted metric filter {0} on log group {1}", filterName, logGroupName); + } + } + + /** Confirms absence and records relinquished ownership atomically, without touching a surviving row. */ + public boolean confirmMetricFilterAbsent(String logGroupName, String filterName, String region, Runnable onAbsent) { + synchronized (store) { + if (store.get(key(region, logGroupName, filterName)).isPresent()) { + return false; + } + completeMutation(new MutationResult(MutationOutcome.NOT_APPLIED, false), ignored -> onAbsent.run(), + () -> cancelPublications(logGroupName, filterName, region), null); + return true; + } + } + + private void cancelPublications(String logGroupName, String filterName, String region) { + Owner owner = new Owner(regionResolver.getAccountId(), region, logGroupName, filterName); + pending.values().removeIf(p -> p.owner().equals(owner)); + forgetCompletedOutages(); + } + + public Optional findMetricFilter(String logGroupName, String filterName, String region) { + return store.get(key(region, logGroupName, filterName)); + } + + public int countMetricFilters(String logGroupName, String region) { + return filtersOf(logGroupName, region).size(); + } + + /** Runs a pattern over sample messages, the TestMetricFilter operation; event numbers start at one. */ + public List testMetricFilter(String filterPattern, List messages) { + FilterPattern pattern = requireFilterPattern(filterPattern); + if (messages == null || messages.isEmpty() || messages.size() > MAX_TEST_MESSAGES) { + throw invalid("logEventMessages must contain between 1 and " + MAX_TEST_MESSAGES + " messages."); + } + List matches = new ArrayList<>(); + for (int i = 0; i < messages.size(); i++) { + String message = messages.get(i); + if (message == null || message.isEmpty()) { + throw invalid("logEventMessages must not contain an empty message."); + } + FilterMatch match = pattern.match(message); + if (match.matched()) { + matches.add(new MetricFilterMatchRecord(i + 1L, message, match.extractedValues())); + } + } + return matches; + } + + /** Logs cascades first; retain idempotent cleanup for standalone observer callers as well. */ + void onLogGroupDeleted(@Observes LogGroupDeleted event) { + synchronized (store) { + String account = regionResolver.getAccountId(); + String prefix = groupPrefix(event.region(), event.logGroupName()); + store.keys().stream().filter(key -> key.startsWith(prefix)).toList().forEach(store::delete); + pending.values().removeIf(p -> p.owner().account().equals(account) + && p.owner().region().equals(event.region()) && p.owner().group().equals(event.logGroupName())); + forgetCompletedOutages(); + } + } + + /** + * Evaluates the group's filters as they are at ingestion and queues each event's contribution, + * independent of other events or later traffic. The Metrics writes happen on the publisher thread. + */ + void onLogEventsIngested(@Observes LogEventsIngested event) { + synchronized (store) { + if (!enabled || paused || stopped) { + return; + } + String account = event.accountId() == null || event.accountId().isBlank() + ? regionResolver.getAccountId() : event.accountId(); + try { + for (MetricFilter filter : filtersOf(event.logGroupName(), event.region(), account)) { + try { + if (selects(filter, account, event.region())) { + enqueue(evaluate(filter, event, account)); + } + } catch (RuntimeException e) { + LOG.errorv(e, "Cannot evaluate metric filter: account={0}, region={1}, group={2}, filter={3}", + account, event.region(), event.logGroupName(), filter.getFilterName()); + } + } + } catch (RuntimeException e) { + // Stored Logs remain successful even if the definition store cannot be read. + LOG.errorv(e, "Cannot load metric filters; publication not queued: account={0}, region={1}, group={2}", + account, event.region(), event.logGroupName()); + } + } + } + + private Batch evaluate(MetricFilter filter, LogEventsIngested event, String account) { + FilterPattern pattern = FilterPattern.parse(filter.getFilterPattern()); + MetricTransformation t = filter.getMetricTransformations().getFirst(); + Double literal = number(t.getMetricValue()); + Owner owner = new Owner(account, event.region(), event.logGroupName(), filter.getFilterName()); + List publications = new ArrayList<>(); + for (LogEvent logEvent : event.events()) { + FilterMatch match = pattern.match(logEvent.getMessage()); + Double value = t.getDefaultValue(); + List dimensions = new ArrayList<>(); + if (match.matched()) { + // Preserve raw text: only missing/null falls back. A present nonnumeric value skips. + String raw = match.value(t.getMetricValue()); + value = literal != null ? literal : raw == null ? t.getDefaultValue() : number(raw); + if (t.getDimensions() != null) { + for (Map.Entry dimension : t.getDimensions().entrySet()) { + String extracted = match.value(dimension.getValue()); + if (extracted == null) { + dimensions.clear(); + break; + } + dimensions.add(new Dimension(dimension.getKey(), extracted)); + } + } + // Policy for unmeasured combinations: matching fallback and incomplete ordinary + // dimensions retain system dimensions. Pattern-nonmatch defaults are dimensionless. + withSystemDimensions(filter, account, event.region(), dimensions); + } + if (value != null) { + publications.add(new Publication(UUID.randomUUID().toString(), owner, t.getMetricNamespace(), + t.getMetricName(), t.getUnit() == null ? "None" : t.getUnit(), dimensions, + value, logEvent.getTimestamp() / 1000)); + } + } + return new Batch(owner, publications); + } + + /** Under the monitor. A batch the queue cannot hold is written here, on the request thread. */ + private void enqueue(Batch batch) { + int size = batch.publications().size(); + if (size == 0) { + return; + } + if (queuedSamples + size > queueCapacity) { + write(batch); + return; + } + queued.addLast(batch); + queuedSamples += size; + scheduleDrain(); + } + + private void scheduleDrain() { + if (publisher == null || !drainScheduled.compareAndSet(false, true)) { + return; + } + try { + publisher.execute(() -> { + drainScheduled.set(false); + try { + publishQueued(); + } catch (RuntimeException e) { + LOG.errorv(e, "Metric publication drain failed; queued work retained"); + } + }); + } catch (RejectedExecutionException e) { + // Racing stop(), whose synchronous drain picks the batch up. + drainScheduled.set(false); + LOG.debugv("Metric publisher already stopping; {0} queued samples drain on shutdown", queuedSamples); + } + } + + /** Writes every queued batch. The worker's entry point, also used by tests and the shutdown drain. */ + void publishQueued() { + while (true) { + Batch batch; + int generation; + synchronized (store) { + if (paused) { + return; + } + batch = queued.pollFirst(); + if (batch == null) { + return; + } + queuedSamples -= batch.publications().size(); + generation = resetGeneration; + } + write(batch, generation); + } + } + + /** Under the monitor, for a batch the queue cannot hold. */ + private void write(Batch batch) { + write(batch, resetGeneration); + } + + /** + * One monitor acquisition per sample, so a PutMetricFilter or a reset waits for at most one + * write. A reset seen mid-batch, paused or already completed, discards the rest: the samples + * belong to the state that was wiped. + */ + private void write(Batch batch, int generation) { + int failed = 0; + int dropped = 0; + RuntimeException firstFailure = null; + for (Publication publication : batch.publications()) { + synchronized (store) { + if (paused || generation != resetGeneration) { + return; + } + try { + write(publication); + } catch (RuntimeException e) { + failed++; + if (firstFailure == null) { + firstFailure = e; + } + if (pending.size() < MAX_PENDING_SAMPLES) { + pending.put(publication.id(), publication); + } else { + dropped++; + } + } + } + } + Owner owner = batch.owner(); + if (failed > dropped) { + // Only retained work owns suppression state. Dropped-only work gets the ERROR below. + synchronized (store) { + logPublicationFailureOnce(owner, firstFailure); + } + } + if (dropped > 0) { + LOG.errorv("Metric publication retry queue overflow: account={0}, region={1}, group={2}, filter={3}, dropped={4}, capacity={5}", + owner.account(), owner.region(), owner.group(), owner.filter(), dropped, MAX_PENDING_SAMPLES); + } + } + + private void write(Publication publication) { + metricsService.publishMetricForAccount(publication.owner().account(), publication.namespace(), + publication.datum(), publication.owner().region(), publication.id()); + } + + /** The production worker's entry point, also used by deterministic fault/race tests. */ + void retryPending() { + try { + synchronized (store) { + if (!enabled || paused || stopped) { + return; + } + for (Publication publication : List.copyOf(pending.values())) { + try { + write(publication); + pending.remove(publication.id()); + } catch (RuntimeException e) { + // Retain this exact snapshot/ID. Other filters still get their attempt. + logPublicationFailureOnce(publication.owner(), e); + } + } + forgetCompletedOutages(); + if (!pending.isEmpty() && ++failedRetryTicks >= RETRY_FAILURE_SUMMARY_TICKS) { + LOG.warnv("Metric publication retries still failing: filters={0}, failedSamples={1}", + reportedOutages.size(), pending.size()); + failedRetryTicks = 0; + } + } + } catch (RuntimeException e) { + // Scheduled executors suppress every future tick if any invocation escapes. + LOG.errorv(e, "Metric publication retry tick failed; pending work retained"); + } + } + + private void logPublicationFailureOnce(Owner owner, RuntimeException failure) { + if (reportedOutages.add(owner)) { + LOG.warnv(failure, "Metric publication failed: account={0}, region={1}, group={2}, filter={3}, pending={4}", + owner.account(), owner.region(), owner.group(), owner.filter(), pending.size()); + } + } + + /** Called under the canonical monitor after drain/cancellation. State is bounded by pending owners. */ + private void forgetCompletedOutages() { + Set activeOwners = new HashSet<>(); + for (Publication publication : pending.values()) { + activeOwners.add(publication.owner()); + } + reportedOutages.retainAll(activeOwners); + if (pending.isEmpty()) { + failedRetryTicks = 0; + } + } + + void onStart(@Observes StartupEvent event, EmulatorConfig config) { + start(config.services().cloudwatchlogs().enabled(), config.services().cloudwatchmetrics().enabled()); + } + + void start(boolean logsEnabled, boolean metricsEnabled) { + synchronized (store) { + if (publisher != null || stopped) { + return; + } + enabled = logsEnabled && metricsEnabled; + if (!enabled) { + discardQueued(); + return; + } + publisher = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "logs-metric-publisher")); + publisher.scheduleWithFixedDelay(this::retryPending, 1, 1, TimeUnit.SECONDS); + if (!queued.isEmpty()) { + scheduleDrain(); + } + } + } + + // Stop before EmulatorLifecycle's default-priority storage flush/shutdown observer. + void onStop(@Observes @Priority(1000) ShutdownEvent event) { + stop(); + } + + /** Accepted samples are written before storage shuts down; failed retries are abandoned. */ + @PreDestroy + void stop() { + ScheduledExecutorService worker; + synchronized (store) { + stopped = true; + worker = publisher; + } + publishQueued(); + if (worker != null) { + worker.shutdownNow(); + try { + if (!worker.awaitTermination(5, TimeUnit.SECONDS)) { + LOG.errorv("Metric publisher did not terminate within 5 seconds"); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LOG.warnv(e, "Interrupted while stopping the metric publisher"); + } + } + // After the worker is gone, so a write failing during the drain cannot repopulate the map. + synchronized (store) { + pending.clear(); + forgetCompletedOutages(); + } + } + + int queuedSamples() { + synchronized (store) { + return queuedSamples; + } + } + + int pendingSamples() { + synchronized (store) { + return pending.size(); + } + } + + boolean publisherRunning() { + synchronized (store) { + return publisher != null && !publisher.isShutdown(); + } + } + + @Override + public void beforeReset() { + synchronized (store) { + // Acquiring the canonical monitor drains the active write. Release it before the + // controller enters StorageFactory.clearAll(), avoiding factory/backend inversion. + paused = true; + discardQueued(); + pending.clear(); + forgetCompletedOutages(); + } + } + + @Override + public void afterReset() { + synchronized (store) { + paused = false; + } + } + + @Override + public void clear() { + synchronized (store) { + discardQueued(); + pending.clear(); + forgetCompletedOutages(); + } + } + + private void discardQueued() { + queued.clear(); + queuedSamples = 0; + resetGeneration++; + } + + private List filtersOf(String logGroupName, String region) { + return filtersOf(logGroupName, region, null); + } + + /** The group's filters in {@code accountId}'s partition, or in the caller's own when it is null. */ + private List filtersOf(String logGroupName, String region, String accountId) { + String prefix = groupPrefix(region, logGroupName); + if (accountId != null && store instanceof AccountAwareStorageBackend rawAware) { + @SuppressWarnings("unchecked") + AccountAwareStorageBackend aware = (AccountAwareStorageBackend) rawAware; + return aware.scanForAccount(accountId, key -> key.startsWith(prefix)); + } + return store.scan(key -> key.startsWith(prefix)); + } + + private static String groupPrefix(String region, String logGroupName) { + return region + "::" + logGroupName + "::"; + } + + private static String key(String region, String logGroupName, String filterName) { + return groupPrefix(region, logGroupName) + filterName; + } + +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java index 5030f0b123..aca5ac6be9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java @@ -7,13 +7,17 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.cloudwatch.logs.filter.FilterPattern; +import io.github.hectorvent.floci.services.cloudwatch.logs.filter.FilterPatternException; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogEvent; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogGroup; import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogStream; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; import io.github.hectorvent.floci.services.cloudwatch.logs.model.ResourcePolicy; import io.github.hectorvent.floci.services.cloudwatch.logs.model.SubscriptionFilter; import com.fasterxml.jackson.core.type.TypeReference; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Event; import jakarta.inject.Inject; import org.jboss.logging.Logger; @@ -37,10 +41,14 @@ import io.github.hectorvent.floci.core.resource.SupportedResourceType; import java.time.Instant; import java.util.Set; +import static io.github.hectorvent.floci.services.cloudwatch.logs.MetricFilterRules.invalid; + @ApplicationScoped public class CloudWatchLogsService implements ResourceProvider { private static final Logger LOG = Logger.getLogger(CloudWatchLogsService.class); + private static final int MAX_REGEX_FILTERS_PER_LOG_GROUP = 5; + private static final int MAX_FILTER_PATTERN_LENGTH = 1024; /** * Orders events by timestamp, then by ingestion sequence so that events sharing the same @@ -57,8 +65,15 @@ public class CloudWatchLogsService implements ResourceProvider { private final StorageBackend streamStore; private final StorageBackend eventStore; private final StorageBackend subscriptionFilterStore; + private final StorageBackend metricFilterStore; private final StorageBackend resourcePolicyStore; private final RegionResolver regionResolver; + /** + * What ingestion and group deletion tell the rest of the emulator, so features AWS drives from + * them (metric filters) stay out of this class. Null when built by the test constructors. + */ + private final Event logEventsIngested; + private final Event logGroupDeleted; private final int maxEventsPerQuery; /** Ceiling on stored events across every group in an account; the oldest are evicted first once exceeded. */ private final int maxStoredEvents; @@ -94,7 +109,9 @@ public class CloudWatchLogsService implements ResourceProvider { @Inject public CloudWatchLogsService(StorageFactory storageFactory, EmulatorConfig config, - RegionResolver regionResolver) { + RegionResolver regionResolver, + Event logEventsIngested, + Event logGroupDeleted) { this( storageFactory.create("cloudwatchlogs", "cwlogs-groups.json", new TypeReference<>() {}), @@ -104,13 +121,17 @@ public class CloudWatchLogsService implements ResourceProvider { new TypeReference<>() {}), storageFactory.create("cloudwatchlogs", "cwlogs-subscription-filters.json", new TypeReference<>() {}), + storageFactory.create("cloudwatchlogs", "cwlogs-metric-filters.json", + new TypeReference<>() {}), storageFactory.create("cloudwatchlogs", "cwlogs-resource-policies.json", new TypeReference<>() {}), config.services().cloudwatchlogs().maxEventsPerQuery(), config.services().cloudwatchlogs().maxStoredEvents(), regionResolver, config.services().cloudwatchlogs().queryCompletionDelayMs(), - System::currentTimeMillis + System::currentTimeMillis, + logEventsIngested, + logGroupDeleted ); } @@ -118,9 +139,10 @@ public class CloudWatchLogsService implements ResourceProvider { StorageBackend streamStore, StorageBackend eventStore, StorageBackend subscriptionFilterStore, + StorageBackend metricFilterStore, int maxEventsPerQuery, RegionResolver regionResolver) { - this(groupStore, streamStore, eventStore, subscriptionFilterStore, new InMemoryStorage<>(), + this(groupStore, streamStore, eventStore, subscriptionFilterStore, metricFilterStore, new InMemoryStorage<>(), maxEventsPerQuery, Integer.MAX_VALUE, regionResolver, 0L, System::currentTimeMillis); } @@ -128,10 +150,11 @@ public class CloudWatchLogsService implements ResourceProvider { StorageBackend streamStore, StorageBackend eventStore, StorageBackend subscriptionFilterStore, + StorageBackend metricFilterStore, int maxEventsPerQuery, int maxStoredEvents, RegionResolver regionResolver) { - this(groupStore, streamStore, eventStore, subscriptionFilterStore, new InMemoryStorage<>(), + this(groupStore, streamStore, eventStore, subscriptionFilterStore, metricFilterStore, new InMemoryStorage<>(), maxEventsPerQuery, maxStoredEvents, regionResolver, 0L, System::currentTimeMillis); } @@ -139,11 +162,12 @@ public class CloudWatchLogsService implements ResourceProvider { StorageBackend streamStore, StorageBackend eventStore, StorageBackend subscriptionFilterStore, + StorageBackend metricFilterStore, int maxEventsPerQuery, RegionResolver regionResolver, long queryCompletionDelayMs, LongSupplier clock) { - this(groupStore, streamStore, eventStore, subscriptionFilterStore, new InMemoryStorage<>(), + this(groupStore, streamStore, eventStore, subscriptionFilterStore, metricFilterStore, new InMemoryStorage<>(), maxEventsPerQuery, Integer.MAX_VALUE, regionResolver, queryCompletionDelayMs, clock); } @@ -151,16 +175,35 @@ public class CloudWatchLogsService implements ResourceProvider { StorageBackend streamStore, StorageBackend eventStore, StorageBackend subscriptionFilterStore, + StorageBackend metricFilterStore, StorageBackend resourcePolicyStore, int maxEventsPerQuery, int maxStoredEvents, RegionResolver regionResolver, long queryCompletionDelayMs, LongSupplier clock) { + this(groupStore, streamStore, eventStore, subscriptionFilterStore, metricFilterStore, resourcePolicyStore, + maxEventsPerQuery, maxStoredEvents, regionResolver, queryCompletionDelayMs, clock, null, null); + } + + CloudWatchLogsService(StorageBackend groupStore, + StorageBackend streamStore, + StorageBackend eventStore, + StorageBackend subscriptionFilterStore, + StorageBackend metricFilterStore, + StorageBackend resourcePolicyStore, + int maxEventsPerQuery, + int maxStoredEvents, + RegionResolver regionResolver, + long queryCompletionDelayMs, + LongSupplier clock, + Event logEventsIngested, + Event logGroupDeleted) { this.groupStore = groupStore; this.streamStore = streamStore; this.eventStore = eventStore; this.subscriptionFilterStore = subscriptionFilterStore; + this.metricFilterStore = metricFilterStore; this.resourcePolicyStore = resourcePolicyStore; this.maxEventsPerQuery = maxEventsPerQuery; this.maxStoredEvents = maxStoredEvents; @@ -173,6 +216,8 @@ public class CloudWatchLogsService implements ResourceProvider { // A negative delay is meaningless; treat it as instant completion. this.queryCompletionDelayMs = Math.max(0, queryCompletionDelayMs); this.clock = clock; + this.logEventsIngested = logEventsIngested; + this.logGroupDeleted = logGroupDeleted; } // ──────────────────────────── Log Groups ──────────────────────────── @@ -206,55 +251,67 @@ public class CloudWatchLogsService implements ResourceProvider { public void createLogGroupForAccount( String accountId, String name, Integer retentionInDays, Map tags, boolean deletionProtectionEnabled, String kmsKeyId, String region) { - if (name == null || name.isBlank()) { - throw new AwsException("InvalidParameterException", "logGroupName is required.", 400); + synchronized (metricFilterStore) { + if (name == null || name.isBlank()) { + throw new AwsException("InvalidParameterException", "logGroupName is required.", 400); + } + String key = groupKey(region, name); + if (getForAccount(groupStore, accountId, key).isPresent()) { + throw new AwsException("ResourceAlreadyExistsException", + "The specified log group already exists: " + name, 400); + } + LogGroup group = new LogGroup(); + group.setLogGroupName(name); + group.setCreatedTime(System.currentTimeMillis()); + group.setRetentionInDays(retentionInDays); + group.setDeletionProtectionEnabled(deletionProtectionEnabled); + if (kmsKeyId != null && !kmsKeyId.isBlank()) { + group.setKmsKeyId(kmsKeyId); + } + if (tags != null) { + group.setTags(new HashMap<>(tags)); + } + putForAccount(groupStore, accountId, key, group); + LOG.infov("Created log group: {0} in region {1}", name, region); } - String key = groupKey(region, name); - if (getForAccount(groupStore, accountId, key).isPresent()) { - throw new AwsException("ResourceAlreadyExistsException", - "The specified log group already exists: " + name, 400); - } - LogGroup group = new LogGroup(); - group.setLogGroupName(name); - group.setCreatedTime(System.currentTimeMillis()); - group.setRetentionInDays(retentionInDays); - group.setDeletionProtectionEnabled(deletionProtectionEnabled); - if (kmsKeyId != null && !kmsKeyId.isBlank()) { - group.setKmsKeyId(kmsKeyId); - } - if (tags != null) { - group.setTags(new HashMap<>(tags)); - } - putForAccount(groupStore, accountId, key, group); - LOG.infov("Created log group: {0} in region {1}", name, region); } public void deleteLogGroup(String name, String region) { - String key = groupKey(region, name); - LogGroup group = groupStore.get(key) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", - "The specified log group does not exist: " + name, 400)); - if (group.isDeletionProtectionEnabled()) { - throw new AwsException("ValidationException", - "The specified log group has deletion protection enabled. " - + "Disable deletion protection before deleting the log group.", - 400); - } + synchronized (metricFilterStore) { + String key = groupKey(region, name); + LogGroup group = groupStore.get(key) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", + "The specified log group does not exist: " + name, 400)); + if (group.isDeletionProtectionEnabled()) { + throw new AwsException("ValidationException", + "The specified log group has deletion protection enabled. " + + "Disable deletion protection before deleting the log group.", + 400); + } - // Cascade: delete all streams and events for this group - String streamPrefix = streamKeyPrefix(region, name); - List streamKeys = streamStore.keys().stream() - .filter(k -> k.startsWith(streamPrefix)) - .toList(); - for (String sk : streamKeys) { - LogStream stream = streamStore.get(sk).orElse(null); - if (stream != null) { - deleteEventsForStream(region, name, stream.getLogStreamName()); - streamStore.delete(sk); + // Cascade: delete all streams, events and filters before removing the group. + String streamPrefix = streamKeyPrefix(region, name); + List streamKeys = streamStore.keys().stream() + .filter(k -> k.startsWith(streamPrefix)) + .toList(); + for (String sk : streamKeys) { + LogStream stream = streamStore.get(sk).orElse(null); + if (stream != null) { + deleteEventsForStream(region, name, stream.getLogStreamName()); + streamStore.delete(sk); + } + } + metricFilterStore.keys().stream().filter(k -> k.startsWith(streamPrefix)) + .toList().forEach(metricFilterStore::delete); + String subscriptionPrefix = subscriptionFilterKeyPrefix(region, name); + subscriptionFilterStore.keys().stream().filter(k -> k.startsWith(subscriptionPrefix)) + .toList().forEach(subscriptionFilterStore::delete); + groupStore.delete(key); + LOG.infov("Deleted log group: {0}", name); + if (logGroupDeleted != null) { + logGroupDeleted.fire(new LogGroupDeleted(region, name)); } } - groupStore.delete(key); - LOG.infov("Deleted log group: {0}", name); } public boolean logGroupExists(String name, String region) { @@ -556,6 +613,7 @@ public class CloudWatchLogsService implements ResourceProvider { long totalBytes = 0; Long minTs = null; Long maxTs = null; + List stored = new ArrayList<>(events.size()); for (Map evt : events) { long ts = toLong(evt.get("timestamp"), now); @@ -570,6 +628,7 @@ public class CloudWatchLogsService implements ResourceProvider { String eventKey = eventKey(region, groupName, streamName, ts, logEvent.getEventId()); putForAccount(eventStore, accountId, eventKey, logEvent); + stored.add(logEvent); totalBytes += msg.getBytes().length + 26; // approx overhead if (minTs == null || ts < minTs) { minTs = ts; } @@ -594,6 +653,9 @@ public class CloudWatchLogsService implements ResourceProvider { stream.setUploadSequenceToken(nextToken); putForAccount(streamStore, accountId, streamKey, stream); + if (logEventsIngested != null && !stored.isEmpty()) { + logEventsIngested.fire(new LogEventsIngested(accountId, region, groupName, streamName, List.copyOf(stored))); + } return nextToken; } @@ -1021,24 +1083,80 @@ public class CloudWatchLogsService implements ResourceProvider { // ──────────────────────────── Subscription Filters ──────────────────────────── + /** + * The canonical factory-backed metric store is also the shared monitor for both filter + * families' check-and-write operations and group creation/deletion. Direct constructors must + * supply this same store rather than silently creating a second quota pool. + */ + StorageBackend metricFilterStore() { + return metricFilterStore; + } + + static FilterPattern requireFilterPattern(String filterPattern) { + if (filterPattern == null) { + throw invalid("filterPattern is required."); + } + if (filterPattern.length() > MAX_FILTER_PATTERN_LENGTH) { + throw invalid("filterPattern must be at most " + MAX_FILTER_PATTERN_LENGTH + " characters."); + } + try { + return FilterPattern.parse(filterPattern); + } catch (FilterPatternException e) { + throw invalid(e.getMessage()); + } + } + + /** Called with the metric store monitor held through the caller's subsequent write. */ + void validateFilterRegexQuota(String logGroupName, String filterName, FilterPattern pattern, + boolean metricFilter, String region) { + if (pattern.regexCount() == 0) { + return; + } + int used = 1; + String metricPrefix = region + "::" + logGroupName + "::"; + for (MetricFilter other : metricFilterStore.scan(k -> k.startsWith(metricPrefix))) { + if ((!metricFilter || !filterName.equals(other.getFilterName())) + && requireFilterPattern(other.getFilterPattern()).regexCount() > 0) { + used++; + } + } + String subscriptionPrefix = subscriptionFilterKeyPrefix(region, logGroupName); + for (SubscriptionFilter other : subscriptionFilterStore.scan(k -> k.startsWith(subscriptionPrefix))) { + if ((metricFilter || !filterName.equals(other.getFilterName())) + && requireFilterPattern(other.getFilterPattern()).regexCount() > 0) { + used++; + } + } + if (used > MAX_REGEX_FILTERS_PER_LOG_GROUP) { + throw new AwsException("LimitExceededException", + "The log group " + logGroupName + " already has the maximum of " + + MAX_REGEX_FILTERS_PER_LOG_GROUP + " filters containing regular expressions.", 400); + } + } + public void putSubscriptionFilter(String logGroupName, String filterName, String filterPattern, String destinationArn, String distribution, String region) { - String groupKey = groupKey(region, logGroupName); - groupStore.get(groupKey) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", - "The specified log group does not exist: " + logGroupName, 400)); + synchronized (metricFilterStore) { + String groupKey = groupKey(region, logGroupName); + groupStore.get(groupKey) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", + "The specified log group does not exist: " + logGroupName, 400)); + String normalizedPattern = filterPattern != null ? filterPattern : ""; + FilterPattern pattern = requireFilterPattern(normalizedPattern); + validateFilterRegexQuota(logGroupName, filterName, pattern, false, region); - SubscriptionFilter filter = new SubscriptionFilter(); - filter.setFilterName(filterName); - filter.setLogGroupName(logGroupName); - filter.setFilterPattern(filterPattern != null ? filterPattern : ""); - filter.setDestinationArn(destinationArn); - filter.setDistribution(distribution != null ? distribution : "ByLogStream"); - filter.setCreationTime(System.currentTimeMillis()); + SubscriptionFilter filter = new SubscriptionFilter(); + filter.setFilterName(filterName); + filter.setLogGroupName(logGroupName); + filter.setFilterPattern(normalizedPattern); + filter.setDestinationArn(destinationArn); + filter.setDistribution(distribution != null ? distribution : "ByLogStream"); + filter.setCreationTime(System.currentTimeMillis()); - String filterKey = subscriptionFilterKey(region, logGroupName, filterName); - subscriptionFilterStore.put(filterKey, filter); - LOG.infov("Created subscription filter: {0} on log group: {1}", filterName, logGroupName); + String filterKey = subscriptionFilterKey(region, logGroupName, filterName); + subscriptionFilterStore.put(filterKey, filter); + LOG.infov("Created subscription filter: {0} on log group: {1}", filterName, logGroupName); + } } public record DescribeSubscriptionFiltersResult(List subscriptionFilters, String nextToken) {} @@ -1076,17 +1194,19 @@ public class CloudWatchLogsService implements ResourceProvider { } public void deleteSubscriptionFilter(String logGroupName, String filterName, String region) { - String groupKey = groupKey(region, logGroupName); - groupStore.get(groupKey) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", - "The specified log group does not exist: " + logGroupName, 400)); + synchronized (metricFilterStore) { + String groupKey = groupKey(region, logGroupName); + groupStore.get(groupKey) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", + "The specified log group does not exist: " + logGroupName, 400)); - String filterKey = subscriptionFilterKey(region, logGroupName, filterName); - subscriptionFilterStore.get(filterKey) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", - "The specified subscription filter does not exist: " + filterName, 400)); - subscriptionFilterStore.delete(filterKey); - LOG.infov("Deleted subscription filter: {0} on log group: {1}", filterName, logGroupName); + String filterKey = subscriptionFilterKey(region, logGroupName, filterName); + subscriptionFilterStore.get(filterKey) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", + "The specified subscription filter does not exist: " + filterName, 400)); + subscriptionFilterStore.delete(filterKey); + LOG.infov("Deleted subscription filter: {0} on log group: {1}", filterName, logGroupName); + } } // ──────────────────────────── Resource Policies ──────────────────────────── diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogEventsIngested.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogEventsIngested.java new file mode 100644 index 0000000000..3ba7e5557d --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogEventsIngested.java @@ -0,0 +1,15 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogEvent; + +import java.util.List; + +/** + * Fired by {@link CloudWatchLogsService} once a PutLogEvents batch is stored, with the events as + * stored, so the features AWS drives from ingestion (metric filters today) run without the log + * service knowing about them. {@code accountId} is the account the batch was written for when the + * writer named one, as a container streaming logs outside any request does, and null when the + * batch belongs to the caller's own account. + */ +public record LogEventsIngested(String accountId, String region, String logGroupName, String logStreamName, + List events) {} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogGroupDeleted.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogGroupDeleted.java new file mode 100644 index 0000000000..dc7c185f26 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogGroupDeleted.java @@ -0,0 +1,4 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +/** Fired by {@link CloudWatchLogsService} once a log group is deleted, so what hangs off the group goes with it. */ +public record LogGroupDeleted(String region, String logGroupName) {} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/MetricFilterRules.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/MetricFilterRules.java new file mode 100644 index 0000000000..6f536e4c79 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/MetricFilterRules.java @@ -0,0 +1,137 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.cloudwatch.logs.filter.FilterPatternException; +import io.github.hectorvent.floci.services.cloudwatch.logs.filter.SystemFieldSelection; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; +import org.jboss.logging.Logger; + +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; + +/** + * The rules PutMetricFilter applies to the three options that describe a filter rather than its + * pattern: {@code emitSystemFieldDimensions}, {@code fieldSelectionCriteria} and the dimensions + * they share a limit with, plus what those options do at ingestion. They live beside + * {@link CloudWatchLogsMetricFilterService} so the service keeps to storing filters and publishing + * their metrics. + * + *

{@code applyOnTransformedLogs} has no rule here on purpose. It selects the transformed view of + * a log group that has a log transformer, Floci has no transformers, and the Logs API documents no + * error for setting it without one, so it is stored and returned unchanged. + */ +final class MetricFilterRules { + + private static final Logger LOG = Logger.getLogger(MetricFilterRules.class); + + /** A metric value or default value written as a number, rather than a field of the pattern. */ + static final Pattern NUMBER = Pattern.compile("[+-]?(\\d+\\.?\\d*|\\.\\d+)([eE][+-]?\\d+)?"); + + private MetricFilterRules() { + } + + /** AWS's cap on dimensions per metric filter. */ + public static final int MAX_DIMENSIONS = 3; + + private static final int MAX_FIELD_SELECTION_CRITERIA_LENGTH = 2000; + + private static final String ACCOUNT_FIELD = "@aws.account"; + private static final String REGION_FIELD = "@aws.region"; + private static final Set SYSTEM_FIELDS = Set.of(ACCOUNT_FIELD, REGION_FIELD); + + static void validateSystemFields(List systemFields, MetricTransformation t) { + if (systemFields == null) { + return; + } + for (String field : systemFields) { + if (field == null || !SYSTEM_FIELDS.contains(field)) { + throw invalid("emitSystemFieldDimensions must contain only @aws.account and @aws.region, got '" + + field + "'."); + } + } + int dimensions = t.getDimensions() == null ? 0 : t.getDimensions().size(); + if (dimensions + systemFields.size() > MAX_DIMENSIONS) { + throw invalid("System field dimensions count toward the limit of " + MAX_DIMENSIONS + + " dimensions per metric filter."); + } + } + +/** + * A criterion selects which events the filter processes from the batch's system fields, so it + * has to parse before it is stored; AWS caps it at 2000 characters. + */ + static void validateFieldSelectionCriteria(String criteria) { + if (criteria == null || criteria.isBlank()) { + return; + } + if (criteria.length() > MAX_FIELD_SELECTION_CRITERIA_LENGTH) { + throw invalid("fieldSelectionCriteria must be at most " + MAX_FIELD_SELECTION_CRITERIA_LENGTH + + " characters."); + } + try { + SystemFieldSelection.parse(criteria); + } catch (FilterPatternException e) { + throw invalid("fieldSelectionCriteria is not valid: " + e.getMessage()); + } + } + +/** + * Whether a batch is one this filter processes. A filter with no selection criteria processes + * every batch. Corrupted stored criteria are logged and skipped, never treated as selecting + * every account and Region. + */ + static boolean selects(MetricFilter filter, String account, String region) { + String criteria = filter.getFieldSelectionCriteria(); + if (criteria == null || criteria.isBlank()) { + return true; + } + try { + return SystemFieldSelection.parse(criteria).test(account, region); + } catch (FilterPatternException unparsable) { + LOG.warnv("Skipping metric filter {0} on log group {1} in account {2}, Region {3}: " + + "fieldSelectionCriteria no longer parses: {4}", + filter.getFilterName(), filter.getLogGroupName(), account, region, unparsable.getMessage()); + return false; + } + } + +/** + * Appends the system fields the filter emits as dimensions to the ones its transformation + * names. AWS counts both against the same limit of three, which {@code validateSystemFields} + * enforces when the filter is stored. + */ + static List withSystemDimensions(MetricFilter filter, String account, String region, + List dimensions) { + List fields = filter.getEmitSystemFieldDimensions(); + if (fields == null) { + return dimensions; + } + for (String field : fields) { + if (ACCOUNT_FIELD.equals(field)) { + dimensions.add(new Dimension(field, account)); + } else if (REGION_FIELD.equals(field)) { + dimensions.add(new Dimension(field, region)); + } + } + return dimensions; + } + + static AwsException invalid(String message) { + return new AwsException("InvalidParameterException", message, 400); + } + + static Double number(String text) { + if (text == null || !NUMBER.matcher(text).matches()) { + return null; + } + try { + double value = Double.parseDouble(text); + return Double.isFinite(value) ? value : null; + } catch (NumberFormatException e) { + return null; + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/AwsRegex.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/AwsRegex.java new file mode 100644 index 0000000000..1c0c8f2118 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/AwsRegex.java @@ -0,0 +1,96 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +/** + * The regular expression subset CloudWatch Logs accepts between percent signs: letters, digits, a + * short list of symbols, the operators {@code ^ $ ? [ ] { } | \ * + .} and the escapes {@code \d \D + * \s \S \w \W \xhh}. Parentheses, other symbols and characters outside ASCII are rejected, as AWS + * rejects them. What passes is Java regex syntax with the same meaning, so it compiles as is. + */ +final class AwsRegex { + + private static final String SYMBOLS = ":_#=@/;,- "; + private static final String OPERATORS = "^$?[]{}|\\*+."; + private static final String ESCAPES = "dDsSwW"; + + private final Pattern pattern; + + private AwsRegex(Pattern pattern) { + this.pattern = pattern; + } + + static AwsRegex compile(String body) { + if (body.isEmpty()) { + throw new FilterPatternException("Invalid filter pattern: empty regular expression"); + } + boolean characterClass = false; + boolean quantifier = false; + for (int i = 0; i < body.length(); i++) { + char c = body.charAt(i); + if (c == '\\') { + i = checkEscape(body, i); + quantifier = false; + continue; + } + if (c > 127) { + throw new FilterPatternException("Invalid filter pattern: regular expressions support ASCII only, found '" + c + "'"); + } + boolean allowed = Character.isLetterOrDigit(c) || SYMBOLS.indexOf(c) >= 0 || OPERATORS.indexOf(c) >= 0; + if (!allowed) { + throw new FilterPatternException("Invalid filter pattern: '" + c + "' is not supported in a regular expression"); + } + if (c == '[') { + characterClass = true; + } else if (c == ']') { + characterClass = false; + } + if (!characterClass && c == '+' && quantifier) { + throw new FilterPatternException("Invalid filter pattern: possessive quantifiers are not supported"); + } + quantifier = !characterClass && (c == '*' || c == '+' || c == '?' || c == '}'); + } + try { + return new AwsRegex(Pattern.compile(body)); + } catch (PatternSyntaxException e) { + throw new FilterPatternException("Invalid filter pattern: bad regular expression '" + body + "': " + + e.getDescription()); + } + } + + /** Validates the escape starting at {@code index} and returns the index of its last character. */ + private static int checkEscape(String body, int index) { + if (index + 1 >= body.length()) { + throw new FilterPatternException("Invalid filter pattern: regular expression ends with a backslash"); + } + char escaped = body.charAt(index + 1); + if (escaped == 'x') { + if (index + 3 < body.length() && isHex(body.charAt(index + 2)) && isHex(body.charAt(index + 3))) { + return index + 3; + } + throw new FilterPatternException("Invalid filter pattern: \\x needs two hexadecimal digits"); + } + if (Character.isLetterOrDigit(escaped)) { + if (ESCAPES.indexOf(escaped) < 0) { + throw new FilterPatternException("Invalid filter pattern: '\\" + escaped + "' is not a supported escape"); + } + return index + 1; + } + // A backslash gives an operator its literal meaning; it does not admit characters AWS + // rejects, so an escaped parenthesis or exclamation mark is still rejected. + if (OPERATORS.indexOf(escaped) < 0 && SYMBOLS.indexOf(escaped) < 0) { + throw new FilterPatternException("Invalid filter pattern: '" + escaped + + "' is not supported in a regular expression, escaped or not"); + } + return index + 1; + } + + private static boolean isHex(char c) { + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); + } + + boolean find(String text) { + return pattern.matcher(text).find(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/Condition.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/Condition.java new file mode 100644 index 0000000000..22c5cb3113 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/Condition.java @@ -0,0 +1,64 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +/** + * A boolean expression over one log event, shared by the JSON and space-delimited syntaxes: atoms + * joined with {@code &&} and {@code ||}, grouped with parentheses, {@code &&} binding tighter. + * System-field selection opts into keyword operators without changing either log grammar. + * {@code C} is what an atom is evaluated against, a parsed JSON document or the event's fields. + */ +@FunctionalInterface +interface Condition { + + boolean test(C context); + + static Condition and(List> operands) { + return context -> operands.stream().allMatch(operand -> operand.test(context)); + } + + static Condition or(List> operands) { + return context -> operands.stream().anyMatch(operand -> operand.test(context)); + } + + /** + * Parses {@code or := and ('||' and)*; and := unary ('&&' unary)*; unary := '(' or ')' | atom} + * at the cursor, reading atoms through {@code atom}. + */ + static Condition parse(PatternCursor cursor, Function> atom) { + return parse(cursor, atom, false); + } + + static Condition parse(PatternCursor cursor, Function> atom, + boolean keywordOperators) { + List> alternatives = new ArrayList<>(); + do { + alternatives.add(parseAnd(cursor, atom, keywordOperators)); + cursor.skipWhitespace(); + } while (cursor.consume("||") || keywordOperators && cursor.consumeKeyword("OR")); + return alternatives.size() == 1 ? alternatives.getFirst() : or(alternatives); + } + + private static Condition parseAnd(PatternCursor cursor, Function> atom, + boolean keywordOperators) { + List> operands = new ArrayList<>(); + do { + operands.add(parseUnary(cursor, atom, keywordOperators)); + cursor.skipWhitespace(); + } while (cursor.consume("&&") || keywordOperators && cursor.consumeKeyword("AND")); + return operands.size() == 1 ? operands.getFirst() : and(operands); + } + + private static Condition parseUnary(PatternCursor cursor, Function> atom, + boolean keywordOperators) { + cursor.skipWhitespace(); + if (cursor.consume("(")) { + Condition inner = parse(cursor, atom, keywordOperators); + cursor.expect(')'); + return inner; + } + return atom.apply(cursor); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterMatch.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterMatch.java new file mode 100644 index 0000000000..91f4582f4c --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterMatch.java @@ -0,0 +1,47 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import java.util.Collections; +import java.util.Map; +import java.util.function.Function; + +/** + * The outcome of matching one log event message against a {@link FilterPattern}: whether it + * matched, the values the pattern extracted (what {@code TestMetricFilter} reports), and a way to + * read any field of the event by reference, which is how a metric filter finds its metric value + * ({@code $.latency}, {@code $bytes}) and its dimensions. + */ +public final class FilterMatch { + + public static final FilterMatch NONE = new FilterMatch(false, Map.of(), reference -> null); + + private final boolean matched; + private final Map extractedValues; + private final Function values; + + FilterMatch(boolean matched, Map extractedValues, Function values) { + this.matched = matched; + this.extractedValues = Collections.unmodifiableMap(extractedValues); + this.values = values; + } + + static FilterMatch of(Map extractedValues, Function values) { + return new FilterMatch(true, extractedValues, values); + } + + public boolean matched() { + return matched; + } + + /** The named values the pattern pulled out of the event, in pattern order; empty for term patterns. */ + public Map extractedValues() { + return extractedValues; + } + + /** + * The text of the event field a reference names ({@code $.path} for a JSON event, {@code $name} + * or {@code $3} for a space-delimited one), or null when the event has no such scalar field. + */ + public String value(String reference) { + return reference == null ? null : values.apply(reference); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPattern.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPattern.java new file mode 100644 index 0000000000..d721a8c08e --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPattern.java @@ -0,0 +1,78 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import java.util.Map; + +/** + * A CloudWatch Logs filter pattern, parsed once and matched against many log event messages. The + * four syntaxes AWS documents are told apart by the first character: {@code {} is a JSON pattern, + * {@code [} a space-delimited one, a blank pattern matches everything, and anything else is a list + * of terms. + * + *

Instances are immutable and safe to share between threads. + */ +public abstract class FilterPattern { + + public enum Kind { MATCH_ALL, TERMS, JSON, SPACE_DELIMITED } + + /** AWS's quota on regular expressions in one pattern. */ + static final int MAX_REGEXES = 2; + + private static final FilterPattern MATCH_ALL = new FilterPattern() { + @Override + public Kind kind() { + return Kind.MATCH_ALL; + } + + @Override + public FilterMatch match(String message) { + return FilterMatch.of(Map.of(), reference -> null); + } + + @Override + public boolean declaresField(String reference) { + return false; + } + }; + + /** Parses a pattern, throwing {@link FilterPatternException} when it does not follow the syntax. */ + public static FilterPattern parse(String pattern) { + String text = pattern == null ? "" : pattern.strip(); + FilterPattern parsed; + if (text.isEmpty()) { + return MATCH_ALL; + } else if (text.startsWith("{")) { + parsed = JsonPattern.of(text); + } else if (text.startsWith("[")) { + parsed = SpaceDelimitedPattern.of(text); + } else { + parsed = TermPattern.of(text); + } + if (parsed.regexCount() > MAX_REGEXES) { + throw new FilterPatternException("Invalid filter pattern: at most " + MAX_REGEXES + + " regular expressions are allowed in a filter pattern"); + } + return parsed; + } + + public abstract Kind kind(); + + /** Matches one message; never throws, an event the pattern cannot read simply does not match. */ + public abstract FilterMatch match(String message); + + /** + * Whether a field reference can be read off this pattern's matches: any + * {@code $.path} for a JSON pattern, a declared {@code $name} or a position {@code $3} for a + * space-delimited one, nothing for the others. + */ + public abstract boolean declaresField(String reference); + + /** Whether a reference addresses one field rather than a wildcard selection. */ + public boolean declaresSingleValueField(String reference) { + return declaresField(reference); + } + + /** How many {@code %regex%} the pattern holds, for the per-pattern and per-log-group quotas. */ + public int regexCount() { + return 0; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternException.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternException.java new file mode 100644 index 0000000000..7dcdcbf4b4 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternException.java @@ -0,0 +1,9 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +/** A filter pattern that does not follow the CloudWatch Logs syntax; the message says where. */ +public class FilterPatternException extends IllegalArgumentException { + + public FilterPatternException(String message) { + super(message); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/JsonPattern.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/JsonPattern.java new file mode 100644 index 0000000000..c360ae4a0f --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/JsonPattern.java @@ -0,0 +1,141 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; + +import java.util.Map; + +/** + * A pattern in curly braces over a JSON log event: conditions on property selectors, each a + * comparison with a literal, {@code IS NULL}, {@code IS TRUE}, {@code IS FALSE} or + * {@code NOT EXISTS}, joined with {@code &&} and {@code ||}. A comparison holds when any node the + * selector points at is a scalar that satisfies it; an object, an array or a missing property + * satisfies no scalar comparison. Wildcard inequality excludes a match if any selected value + * equals the literal. + */ +final class JsonPattern extends FilterPattern { + + private static final ObjectReader JSON = new ObjectMapper() + .enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .readerFor(JsonNode.class); + + /** AWS's quota on wildcard selectors in one filter pattern. */ + private static final int MAX_WILDCARDS = 3; + + private final Condition condition; + private final int regexes; + + private JsonPattern(Condition condition, int regexes) { + this.condition = condition; + this.regexes = regexes; + } + + static JsonPattern of(String text) { + PatternCursor cursor = new PatternCursor(text); + cursor.expect('{'); + int[] regexes = {0}; + int[] wildcards = {0}; + Condition condition = Condition.parse(cursor, c -> atom(c, regexes, wildcards)); + if (wildcards[0] > MAX_WILDCARDS) { + throw new FilterPatternException("Invalid filter pattern: at most " + MAX_WILDCARDS + + " wildcard selectors are allowed in a filter pattern"); + } + cursor.expect('}'); + cursor.expectEnd(); + return new JsonPattern(condition, regexes[0]); + } + + private static Condition atom(PatternCursor cursor, int[] regexes, int[] wildcards) { + JsonSelector selector = JsonSelector.read(cursor); + wildcards[0] += selector.wildcards(); + cursor.skipWhitespace(); + if (Character.isLetter(cursor.peek())) { + String keyword = cursor.identifier().toUpperCase(); + if (keyword.equals("IS")) { + String state = cursor.identifier().toUpperCase(); + return switch (state) { + case "NULL" -> root -> selector.resolve(root).stream() + .anyMatch(node -> isNull(node, selector.wildcards() == 0)); + case "TRUE" -> root -> selector.resolve(root).stream() + .anyMatch(node -> node.isBoolean() && node.booleanValue()); + case "FALSE" -> root -> selector.resolve(root).stream() + .anyMatch(node -> node.isBoolean() && !node.booleanValue()); + default -> throw cursor.error("IS must be followed by NULL, TRUE or FALSE"); + }; + } + if (keyword.equals("NOT")) { + if (!cursor.identifier().equalsIgnoreCase("EXISTS")) { + throw cursor.error("NOT must be followed by EXISTS"); + } + return root -> selector.resolve(root).isEmpty(); + } + throw cursor.error("expected a comparison operator, IS or NOT EXISTS"); + } + Literal.Operator op = Literal.Operator.read(cursor); + Literal literal = Literal.read(cursor, "&|)}"); + literal.validateOperator(op); + if (literal.isRegex()) { + regexes[0]++; + } + if (op == Literal.Operator.NE && selector.wildcards() > 0) { + return root -> selector.wildcardParentExists(root) && selector.resolve(root).stream() + .noneMatch(node -> literal.test(Literal.Operator.EQ, node)); + } + return root -> selector.resolve(root).stream().anyMatch(node -> literal.test(op, node)); + } + + private static boolean isNull(JsonNode node, boolean includeArrayElements) { + if (node.isNull()) { + return true; + } + if (includeArrayElements && node.isArray()) { + for (JsonNode element : node) { + if (element.isNull()) { + return true; + } + } + } + return false; + } + + @Override + public Kind kind() { + return Kind.JSON; + } + + @Override + public FilterMatch match(String message) { + JsonNode root; + try { + root = message == null ? null : JSON.readValue(message); + } catch (JsonProcessingException e) { + return FilterMatch.NONE; + } + if (root == null || !root.isObject() || !condition.test(root)) { + return FilterMatch.NONE; + } + return FilterMatch.of(Map.of(), reference -> { + JsonSelector selector = JsonSelector.parse(reference); + return selector == null ? null : selector.firstScalar(root); + }); + } + + @Override + public boolean declaresField(String reference) { + return JsonSelector.parse(reference) != null; + } + + @Override + public boolean declaresSingleValueField(String reference) { + JsonSelector selector = JsonSelector.parse(reference); + return selector != null && selector.wildcards() == 0; + } + + @Override + public int regexCount() { + return regexes; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/JsonSelector.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/JsonSelector.java new file mode 100644 index 0000000000..eef7f52b74 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/JsonSelector.java @@ -0,0 +1,215 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import com.fasterxml.jackson.databind.JsonNode; + +import java.util.ArrayList; +import java.util.List; + +/** + * A property selector of a JSON pattern: {@code $} followed by {@code .name}, {@code [index]}, + * the wildcard {@code .*} or {@code [*]} for every field or element, and {@code ['name.with.dots']} + * for a property a dot could not name. Resolving it against a document yields the nodes it points + * at, several when a wildcard is used. + */ +final class JsonSelector { + + private static final String SELECTOR_END = " \t\r\n=!<>)}&|,"; + + /** AWS's quota on wildcards in one property selector. */ + private static final int MAX_WILDCARDS = 1; + + private sealed interface Segment permits Name, Index, Wildcard {} + + private record Name(String name) implements Segment {} + + private record Index(int index) implements Segment {} + + private record Wildcard() implements Segment {} + + private final String text; + private final List segments; + private final int wildcards; + + private JsonSelector(String text, List segments) { + this.text = text; + this.segments = segments; + this.wildcards = (int) segments.stream().filter(Wildcard.class::isInstance).count(); + } + + /** Reads the selector at the cursor, which must start with {@code $}. */ + static JsonSelector read(PatternCursor cursor) { + cursor.skipWhitespace(); + if (cursor.peek() != '$') { + throw cursor.error("expected a property selector starting with '$'"); + } + StringBuilder text = new StringBuilder("$"); + cursor.next(); + if (cursor.peek() != '.') { + throw cursor.error("expected '.' after '$'"); + } + List segments = new ArrayList<>(); + while (!cursor.atEnd() && SELECTOR_END.indexOf(cursor.peek()) < 0) { + char c = cursor.next(); + text.append(c); + if (c == '.') { + if (cursor.peek() == '[') { + continue; + } + segments.add(readName(cursor, text)); + } else if (c == '[') { + segments.add(readBracket(cursor, text)); + } else { + throw cursor.error("unexpected '" + c + "' in a property selector"); + } + } + JsonSelector selector = new JsonSelector(text.toString(), List.copyOf(segments)); + if (segments.isEmpty()) { + throw cursor.error("expected a property name"); + } + if (selector.wildcards() > MAX_WILDCARDS) { + throw cursor.error("at most " + MAX_WILDCARDS + " wildcard is allowed in a property selector"); + } + return selector; + } + + /** Parses a whole reference such as {@code $.latency}, or returns null when it is not a selector. */ + static JsonSelector parse(String reference) { + if (reference == null || !reference.startsWith("$.")) { + return null; + } + try { + PatternCursor cursor = new PatternCursor(reference); + JsonSelector selector = read(cursor); + return cursor.atEnd() ? selector : null; + } catch (FilterPatternException e) { + return null; + } + } + + private static Segment readName(PatternCursor cursor, StringBuilder text) { + if (cursor.peek() == '*') { + text.append(cursor.next()); + return new Wildcard(); + } + StringBuilder name = new StringBuilder(); + while (!cursor.atEnd() && (Character.isLetterOrDigit(cursor.peek()) || cursor.peek() == '_' + || cursor.peek() == '-')) { + name.append(cursor.next()); + } + if (name.isEmpty()) { + throw cursor.error("expected a property name after '.'"); + } + text.append(name); + return new Name(name.toString()); + } + + private static Segment readBracket(PatternCursor cursor, StringBuilder text) { + Segment segment; + if (cursor.peek() == '\'') { + char quote = cursor.next(); + StringBuilder name = new StringBuilder(); + while (!cursor.atEnd() && cursor.peek() != quote) { + name.append(cursor.next()); + } + if (cursor.atEnd()) { + throw cursor.error("unterminated quoted property name"); + } + cursor.next(); + text.append(quote).append(name).append(quote); + segment = new Name(name.toString()); + } else if (cursor.peek() == '*') { + text.append(cursor.next()); + segment = new Wildcard(); + } else { + StringBuilder digits = new StringBuilder(); + while (!cursor.atEnd() && Character.isDigit(cursor.peek())) { + digits.append(cursor.next()); + } + if (digits.isEmpty()) { + throw cursor.error("expected an array index, '*' or a quoted property name inside '[ ]'"); + } + text.append(digits); + try { + segment = new Index(Integer.parseInt(digits.toString())); + } catch (NumberFormatException e) { + throw cursor.error("array index " + digits + " is too large"); + } + } + if (cursor.peek() != ']') { + throw cursor.error("expected ']'"); + } + text.append(cursor.next()); + return segment; + } + + String text() { + return text; + } + + /** How many {@code .*} or {@code [*]} the selector holds, for the quota. */ + int wildcards() { + return wildcards; + } + + /** The nodes the selector points at in {@code root}; empty when the path is absent. */ + List resolve(JsonNode root) { + List current = List.of(root); + for (Segment segment : segments) { + List next = new ArrayList<>(); + for (JsonNode node : current) { + step(node, segment, next); + } + if (next.isEmpty()) { + return next; + } + current = next; + } + return current; + } + + boolean wildcardParentExists(JsonNode root) { + List current = List.of(root); + for (Segment segment : segments) { + if (segment instanceof Wildcard) { + return !current.isEmpty(); + } + List next = new ArrayList<>(); + for (JsonNode node : current) { + step(node, segment, next); + } + current = next; + } + return false; + } + + private static void step(JsonNode node, Segment segment, List into) { + switch (segment) { + case Name name -> { + JsonNode child = node.isObject() ? node.get(name.name()) : null; + if (child != null) { + into.add(child); + } + } + case Index index -> { + if (node.isArray() && index.index() < node.size()) { + into.add(node.get(index.index())); + } + } + case Wildcard wildcard -> { + if (node.isObject() || node.isArray()) { + node.forEach(into::add); + } + } + } + } + + /** The text of the first scalar node the selector points at, or null. */ + String firstScalar(JsonNode root) { + for (JsonNode node : resolve(root)) { + if (node.isValueNode() && !node.isNull()) { + return node.asText(); + } + } + return null; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/Literal.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/Literal.java new file mode 100644 index 0000000000..595b57c3c6 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/Literal.java @@ -0,0 +1,191 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import com.fasterxml.jackson.databind.JsonNode; + +import java.math.BigDecimal; +import java.util.regex.Pattern; + +/** + * The right-hand side of a JSON or space-delimited condition, and how it compares with a value from + * a log event. A quoted or bare word compares as text, a word with an asterisk as a wildcard, a + * number by value, and a {@code %regex%} by search. JSON strings compare with the literal's text, + * never by numeric coercion. Space-delimited numeric fields compare by value. Ordering requires + * a numeric literal and, for JSON events, a numeric node. + */ +final class Literal { + + enum Operator { + EQ("="), NE("!="), LT("<"), LE("<="), GT(">"), GE(">="); + + private final String token; + + Operator(String token) { + this.token = token; + } + + /** Reads the longest operator at the cursor, or throws. */ + static Operator read(PatternCursor cursor) { + cursor.skipWhitespace(); + for (Operator op : new Operator[] {NE, LE, GE, EQ, LT, GT}) { + if (cursor.consume(op.token)) { + if (op == EQ && cursor.peek() == '=') { + throw cursor.error("'==' is not an operator, use '='"); + } + return op; + } + } + throw cursor.error("expected a comparison operator"); + } + } + + private static final Pattern NUMBER = Pattern.compile("[+-]?(\\d+\\.?\\d*|\\.\\d+)([eE][+-]?\\d+)?"); + + private final String text; + private final BigDecimal number; + private final Pattern glob; + private final AwsRegex regex; + + private Literal(String text, BigDecimal number, Pattern glob, AwsRegex regex) { + this.text = text; + this.number = number; + this.glob = glob; + this.regex = regex; + } + + /** A quoted value: text, or a wildcard when it holds an asterisk. */ + static Literal quoted(String text) { + return new Literal(text, null, globOf(text), null); + } + + /** A bare value: a number, a wildcard, or a word. */ + static Literal bare(String text) { + if (NUMBER.matcher(text).matches()) { + try { + int exponent = Math.max(text.indexOf('e'), text.indexOf('E')); + if (exponent >= 0) { + Integer.parseInt(text.substring(exponent + 1)); + } + return new Literal(text, new BigDecimal(text), null, null); + } catch (NumberFormatException e) { + throw new FilterPatternException("Invalid filter pattern: numeric literal is out of range"); + } + } + if (text.equals("null") || !text.matches("[\\p{L}\\p{N}_.*-]+")) { + throw new FilterPatternException("Invalid filter pattern: invalid unquoted value '" + text + "'"); + } + return new Literal(text, null, globOf(text), null); + } + + static Literal regex(AwsRegex regex) { + return new Literal(null, null, null, regex); + } + + /** Reads a quoted string, a {@code %regex%} or a bare value from the cursor. */ + static Literal read(PatternCursor cursor, String bareDelimiters) { + cursor.skipWhitespace(); + if (cursor.peek() == '"') { + return quoted(cursor.quoted()); + } + if (cursor.peek() == '%') { + return regex(cursor.regex()); + } + return bare(cursor.bareValue(bareDelimiters)); + } + + private static Pattern globOf(String text) { + if (text.indexOf('*') < 0) { + return null; + } + StringBuilder regex = new StringBuilder(); + for (String piece : text.split("\\*", -1)) { + if (!regex.isEmpty()) { + regex.append(".*"); + } + regex.append(Pattern.quote(piece)); + } + return Pattern.compile(regex.toString(), Pattern.DOTALL); + } + + boolean isRegex() { + return regex != null; + } + + void validateOperator(Operator op) { + if (op != Operator.EQ && op != Operator.NE && number == null) { + throw new FilterPatternException("Invalid filter pattern: ordering requires a numeric literal"); + } + } + + boolean test(Operator op, JsonNode node) { + if (!node.isTextual() && !node.isNumber()) { + return false; + } + if (number != null) { + if (node.isTextual()) { + return switch (op) { + case EQ -> text.equals(node.textValue()); + case NE -> !text.equals(node.textValue()); + default -> false; + }; + } + if (!Double.isFinite(node.doubleValue())) { + return false; + } + return compare(op, node.decimalValue().compareTo(number)); + } + return test(op, node.asText()); + } + + /** Whether the value from the event satisfies {@code op} against this literal. */ + boolean test(Operator op, String value) { + return switch (op) { + case EQ -> equalTo(value); + case NE -> !equalTo(value); + case LT, LE, GT, GE -> orders(op, value); + }; + } + + private boolean equalTo(String value) { + if (regex != null) { + return regex.find(value); + } + if (number != null) { + BigDecimal actual = parse(value); + return actual != null && actual.compareTo(number) == 0; + } + if (glob != null) { + return glob.matcher(value).matches(); + } + return text.equals(value); + } + + private boolean orders(Operator op, String value) { + BigDecimal actual = number == null ? null : parse(value); + if (actual == null) { + return false; + } + return compare(op, actual.compareTo(number)); + } + + private static boolean compare(Operator op, int comparison) { + return switch (op) { + case EQ -> comparison == 0; + case NE -> comparison != 0; + case LT -> comparison < 0; + case LE -> comparison <= 0; + case GT -> comparison > 0; + case GE -> comparison >= 0; + }; + } + + private static BigDecimal parse(String value) { + if (!NUMBER.matcher(value).matches()) { + return null; + } + try { + return new BigDecimal(value); + } catch (NumberFormatException e) { + return null; + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/PatternCursor.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/PatternCursor.java new file mode 100644 index 0000000000..e1ed6e71ab --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/PatternCursor.java @@ -0,0 +1,134 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +/** A position in the pattern text for the recursive descent parsers, with the small reads they share. */ +final class PatternCursor { + + private final String text; + private int position; + + PatternCursor(String text) { + this.text = text; + } + + boolean atEnd() { + return position >= text.length(); + } + + char peek() { + return atEnd() ? '\0' : text.charAt(position); + } + + boolean lookingAt(String token) { + return text.startsWith(token, position); + } + + char next() { + if (atEnd()) { + throw error("unexpected end of pattern"); + } + return text.charAt(position++); + } + + void skipWhitespace() { + while (!atEnd() && Character.isWhitespace(peek())) { + position++; + } + } + + void expect(char c) { + skipWhitespace(); + if (peek() != c) { + throw error("expected '" + c + "'"); + } + position++; + } + + /** Consumes {@code token} if it is next, reporting whether it was. */ + boolean consume(String token) { + if (lookingAt(token)) { + position += token.length(); + return true; + } + return false; + } + + boolean consumeKeyword(String keyword) { + int end = position + keyword.length(); + if (!lookingAt(keyword) || end < text.length() + && (Character.isLetterOrDigit(text.charAt(end)) || text.charAt(end) == '_')) { + return false; + } + position = end; + return true; + } + + /** Reads a double-quoted string whose opening quote is next; {@code \"} and {@code \\} are escapes. */ + String quoted() { + expect('"'); + StringBuilder value = new StringBuilder(); + while (true) { + if (atEnd()) { + throw error("unterminated quoted string"); + } + char c = next(); + if (c == '"') { + return value.toString(); + } + if (c == '\\' && !atEnd() && (peek() == '"' || peek() == '\\')) { + c = next(); + } + value.append(c); + } + } + + /** Reads a {@code %regex%} whose opening percent sign is next, returning the compiled regex. */ + AwsRegex regex() { + expect('%'); + int start = position; + while (!atEnd() && peek() != '%') { + position++; + } + if (atEnd()) { + throw error("unterminated regular expression, expected a closing '%'"); + } + String body = text.substring(start, position); + position++; + return AwsRegex.compile(body); + } + + /** Reads an identifier: a letter or underscore followed by letters, digits and underscores. */ + String identifier() { + skipWhitespace(); + int start = position; + if (atEnd() || !(Character.isLetter(peek()) || peek() == '_')) { + throw error("expected a field name"); + } + while (!atEnd() && (Character.isLetterOrDigit(peek()) || peek() == '_')) { + position++; + } + return text.substring(start, position); + } + + /** Reads a bare value up to whitespace or one of the delimiters the enclosing syntax uses. */ + String bareValue(String delimiters) { + int start = position; + while (!atEnd() && !Character.isWhitespace(peek()) && delimiters.indexOf(peek()) < 0) { + position++; + } + if (start == position) { + throw error("expected a value"); + } + return text.substring(start, position); + } + + void expectEnd() { + skipWhitespace(); + if (!atEnd()) { + throw error("unexpected '" + text.substring(position) + "'"); + } + } + + FilterPatternException error(String what) { + return new FilterPatternException("Invalid filter pattern at position " + position + ": " + what); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SpaceDelimitedPattern.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SpaceDelimitedPattern.java new file mode 100644 index 0000000000..e97c6baf13 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SpaceDelimitedPattern.java @@ -0,0 +1,237 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A pattern in square brackets over a space-delimited log event, one entry per field: a name, a + * name with conditions ({@code status_code = 4*}, {@code w1=ERROR || w1=WARNING}), an empty entry + * for a field without a name, or one {@code ...} standing for any number of fields. Text between + * double quotes or square brackets is one field. Without an ellipsis the last entry takes the rest + * of the line, which is why the reference asks for a blank indicator after the last term. + * + *

A match extracts every field: named ones as {@code $name}, the rest by position as + * {@code $1}, {@code $2} and so on, the shape {@code TestMetricFilter} reports. + */ +final class SpaceDelimitedPattern extends FilterPattern { + + /** The bound fields of one event, by name and by position. */ + record Fields(List values, Map byName) {} + + private record Entry(String name, Condition condition) {} + + private final List before; + private final List after; + private final boolean ellipsis; + private final Set names; + private final int regexes; + + private SpaceDelimitedPattern(List before, List after, boolean ellipsis, Set names, + int regexes) { + this.before = before; + this.after = after; + this.ellipsis = ellipsis; + this.names = names; + this.regexes = regexes; + } + + static SpaceDelimitedPattern of(String text) { + PatternCursor cursor = new PatternCursor(text); + cursor.expect('['); + List before = new ArrayList<>(); + List after = new ArrayList<>(); + boolean ellipsis = false; + Set declared = new HashSet<>(); + Set referenced = new HashSet<>(); + int[] regexes = {0}; + cursor.skipWhitespace(); + if (cursor.peek() != ']') { + while (true) { + cursor.skipWhitespace(); + if (cursor.lookingAt("...")) { + cursor.consume("..."); + if (ellipsis) { + throw cursor.error("only one '...' is allowed"); + } + ellipsis = true; + } else if (cursor.peek() == ',' || cursor.peek() == ']') { + (ellipsis ? after : before).add(new Entry(null, null)); + } else { + Entry entry = readEntry(cursor, referenced, regexes); + if (!declared.add(entry.name())) { + throw cursor.error("field '" + entry.name() + "' is declared twice"); + } + (ellipsis ? after : before).add(entry); + } + cursor.skipWhitespace(); + if (cursor.consume("]")) { + break; + } + cursor.expect(','); + } + } else { + cursor.next(); + ellipsis = true; + } + cursor.expectEnd(); + referenced.removeAll(declared); + if (!referenced.isEmpty()) { + throw cursor.error("condition refers to an undeclared field " + referenced.iterator().next()); + } + if (!ellipsis && before.isEmpty()) { + ellipsis = true; + } + return new SpaceDelimitedPattern(List.copyOf(before), List.copyOf(after), ellipsis, Set.copyOf(declared), + regexes[0]); + } + + /** Reads one entry; the first field name in it is the field the entry declares. */ + private static Entry readEntry(PatternCursor cursor, Set referenced, int[] regexes) { + String[] declared = {null}; + Condition condition = Condition.parse(cursor, c -> { + String name = c.identifier(); + if (declared[0] == null) { + declared[0] = name; + } else if (!declared[0].equals(name)) { + throw c.error("a field condition cannot reference another field"); + } + referenced.add(name); + c.skipWhitespace(); + if ("=!<>".indexOf(c.peek()) < 0) { + return fields -> true; + } + Literal.Operator op = Literal.Operator.read(c); + Literal literal = Literal.read(c, ",]&|)"); + literal.validateOperator(op); + if (literal.isRegex()) { + regexes[0]++; + } + return fields -> { + String value = fields.byName().get(name); + return value != null && literal.test(op, value); + }; + }); + return new Entry(declared[0], condition); + } + + @Override + public Kind kind() { + return Kind.SPACE_DELIMITED; + } + + @Override + public FilterMatch match(String message) { + List values = split(message == null ? "" : message, ellipsis ? -1 : before.size()); + int fixed = before.size() + after.size(); + if (values.size() < fixed || (!ellipsis && values.size() != fixed)) { + return FilterMatch.NONE; + } + Map byName = new HashMap<>(); + Map extracted = new LinkedHashMap<>(); + for (int position = 0; position < values.size(); position++) { + Entry entry = null; + if (position < before.size()) { + entry = before.get(position); + } else if (position >= values.size() - after.size()) { + entry = after.get(position - (values.size() - after.size())); + } + String value = values.get(position); + if (entry != null && entry.name() != null) { + byName.put(entry.name(), value); + extracted.put("$" + entry.name(), value); + } else { + extracted.put("$" + (position + 1), value); + } + } + Fields fields = new Fields(values, byName); + for (List entries : List.of(before, after)) { + for (Entry entry : entries) { + if (entry.condition() != null && !entry.condition().test(fields)) { + return FilterMatch.NONE; + } + } + } + return FilterMatch.of(extracted, reference -> value(fields, reference)); + } + + private static String value(Fields fields, String reference) { + if (reference == null || reference.length() < 2 || reference.charAt(0) != '$') { + return null; + } + String key = reference.substring(1); + Integer position = position(key); + if (position != null) { + return position >= 1 && position <= fields.values().size() ? fields.values().get(position - 1) : null; + } + return fields.byName().get(key); + } + + private static Integer position(String key) { + if (key.isEmpty() || !key.chars().allMatch(Character::isDigit)) { + return null; + } + try { + return Integer.parseInt(key); + } catch (NumberFormatException e) { + return null; + } + } + + /** + * Splits a message into fields on whitespace, keeping text between double quotes or square + * brackets together without the delimiters. With {@code limit} above zero, the field at that + * position takes the rest of the line as written. + */ + static List split(String message, int limit) { + List fields = new ArrayList<>(); + int i = 0; + int length = message.length(); + while (i < length) { + while (i < length && Character.isWhitespace(message.charAt(i))) { + i++; + } + if (i >= length) { + break; + } + char c = message.charAt(i); + char closing = c == '"' ? '"' : c == '[' ? ']' : '\0'; + int end = closing == '\0' ? -1 : message.indexOf(closing, i + 1); + boolean quoted = end >= 0; + if (quoted) { + end++; + } else { + end = i; + while (end < length && !Character.isWhitespace(message.charAt(end))) { + end++; + } + } + if (limit > 0 && fields.size() == limit - 1 && !message.substring(end).isBlank()) { + fields.add(message.substring(i)); + break; + } + fields.add(quoted ? message.substring(i + 1, end - 1) : message.substring(i, end)); + i = end; + } + return fields; + } + + @Override + public boolean declaresField(String reference) { + if (reference == null || reference.length() < 2 || reference.charAt(0) != '$') { + return false; + } + String key = reference.substring(1); + Integer position = position(key); + return position != null ? position >= 1 : names.contains(key); + } + + @Override + public int regexCount() { + return regexes; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SystemFieldSelection.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SystemFieldSelection.java new file mode 100644 index 0000000000..0adfe96c4d --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SystemFieldSelection.java @@ -0,0 +1,106 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import java.util.ArrayList; +import java.util.List; + +/** + * A metric filter's {@code fieldSelectionCriteria}: which log events the filter processes, decided + * from the system fields {@code @aws.account} and {@code @aws.region} rather than from the event's + * own text. The Logs API documents the operators {@code =}, {@code !=}, {@code IN}, {@code NOT IN}, + * {@code AND} and {@code OR}, as in {@code @aws.region = "us-east-1"} or + * {@code @aws.account IN ["123456789012", "987654321098"]}. + * + *

Both fields describe the batch rather than the individual event, so one evaluation decides a + * whole ingested batch. {@code &&} and {@code ||} are accepted beside {@code AND} and {@code OR}, + * and parentheses group, because the criteria share their shape with the filter-pattern syntax. + * + *

Instances are immutable and safe to share between threads. + */ +public final class SystemFieldSelection { + + private static final String ACCOUNT = "@aws.account"; + private static final String REGION = "@aws.region"; + + /** The system fields of one ingested batch. */ + private record Fields(String account, String region) {} + + private final Condition condition; + + private SystemFieldSelection(Condition condition) { + this.condition = condition; + } + + /** + * Parses a criterion, throwing {@link FilterPatternException} when it does not follow the + * syntax. A blank criterion selects everything, as an absent one does. + */ + public static SystemFieldSelection parse(String expression) { + String text = expression == null ? "" : expression.strip(); + if (text.isEmpty()) { + return new SystemFieldSelection(fields -> true); + } + PatternCursor cursor = new PatternCursor(text); + Condition condition = Condition.parse(cursor, SystemFieldSelection::atom, true); + cursor.expectEnd(); + return new SystemFieldSelection(condition); + } + + /** Whether a batch written for {@code account} in {@code region} is one this filter processes. */ + public boolean test(String account, String region) { + return condition.test(new Fields(account, region)); + } + + private static Condition atom(PatternCursor cursor) { + String field = field(cursor); + cursor.skipWhitespace(); + if (cursor.consume("!=")) { + String value = value(cursor); + return fields -> !value.equals(valueOf(field, fields)); + } + if (cursor.consume("=")) { + if (cursor.peek() == '=') { + throw cursor.error("'==' is not an operator, use '='"); + } + String value = value(cursor); + return fields -> value.equals(valueOf(field, fields)); + } + boolean negated = cursor.consumeKeyword("NOT") || cursor.consumeKeyword("not"); + cursor.skipWhitespace(); + if (!cursor.consumeKeyword("IN") && !cursor.consumeKeyword("in")) { + throw cursor.error("expected '=', '!=', IN or NOT IN after " + field); + } + List values = list(cursor); + return fields -> values.contains(valueOf(field, fields)) != negated; + } + + private static String field(PatternCursor cursor) { + cursor.skipWhitespace(); + if (cursor.consume(ACCOUNT)) { + return ACCOUNT; + } + if (cursor.consume(REGION)) { + return REGION; + } + throw cursor.error("expected " + ACCOUNT + " or " + REGION); + } + + private static String value(PatternCursor cursor) { + cursor.skipWhitespace(); + return cursor.peek() == '"' ? cursor.quoted() : cursor.bareValue(",]()=!<>|&"); + } + + private static List list(PatternCursor cursor) { + cursor.expect('['); + List values = new ArrayList<>(); + do { + values.add(value(cursor)); + cursor.skipWhitespace(); + } while (cursor.consume(",")); + cursor.expect(']'); + return List.copyOf(values); + } + + private static String valueOf(String field, Fields fields) { + return ACCOUNT.equals(field) ? fields.account() : fields.region(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/TermPattern.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/TermPattern.java new file mode 100644 index 0000000000..03d18d9de7 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/TermPattern.java @@ -0,0 +1,114 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Terms matched against an unstructured message: every plain term must be present (a + * case-sensitive substring, as the API reference's examples show), no {@code -term} may be, and + * {@code ?term}s make the pattern match when any of them is present, though only when no other + * kind of term is given, since AWS ignores them otherwise. A quoted phrase is one term. + * An unstructured {@code %regex%} must stand alone and is searched for. + */ +final class TermPattern extends FilterPattern { + + private enum Mode { INCLUDE, EXCLUDE, OPTIONAL } + + private record Term(Mode mode, String text, AwsRegex regex) { + boolean occursIn(String message) { + return regex != null ? regex.find(message) : message.contains(text); + } + } + + private final List includes = new ArrayList<>(); + private final List excludes = new ArrayList<>(); + private final List optionals = new ArrayList<>(); + private int regexes; + + static TermPattern of(String text) { + TermPattern pattern = new TermPattern(); + PatternCursor cursor = new PatternCursor(text); + if (cursor.peek() == '%') { + pattern.add(new Term(Mode.INCLUDE, null, cursor.regex())); + cursor.expectEnd(); + return pattern; + } + while (true) { + cursor.skipWhitespace(); + if (cursor.atEnd()) { + return pattern; + } + pattern.add(readTerm(cursor)); + if (!cursor.atEnd() && !Character.isWhitespace(cursor.peek()) + && cursor.peek() != '-' && cursor.peek() != '?') { + throw cursor.error("terms must be separated"); + } + } + } + + private static Term readTerm(PatternCursor cursor) { + Mode mode = Mode.INCLUDE; + if (cursor.peek() == '-' && !cursor.lookingAt("- ")) { + cursor.next(); + mode = Mode.EXCLUDE; + } else if (cursor.peek() == '?' && !cursor.lookingAt("? ")) { + cursor.next(); + mode = Mode.OPTIONAL; + } + if (cursor.peek() == '"') { + String value = cursor.quoted(); + if (value.isEmpty()) { + throw cursor.error("empty quoted term"); + } + return new Term(mode, value, null); + } + if (cursor.peek() == '%') { + throw cursor.error("a regex must be the entire unstructured pattern"); + } + String value = cursor.bareValue("-?"); + if (!value.matches("[\\p{L}\\p{N}_.]+")) { + throw cursor.error("invalid unquoted term '" + value + "'"); + } + return new Term(mode, value, null); + } + + private void add(Term term) { + if (term.regex() != null) { + regexes++; + } + switch (term.mode()) { + case INCLUDE -> includes.add(term); + case EXCLUDE -> excludes.add(term); + case OPTIONAL -> optionals.add(term); + } + } + + @Override + public Kind kind() { + return Kind.TERMS; + } + + @Override + public FilterMatch match(String message) { + String text = message == null ? "" : message; + boolean matched; + if (includes.isEmpty() && excludes.isEmpty()) { + matched = optionals.stream().anyMatch(term -> term.occursIn(text)); + } else { + matched = includes.stream().allMatch(term -> term.occursIn(text)) + && excludes.stream().noneMatch(term -> term.occursIn(text)); + } + return matched ? FilterMatch.of(Map.of(), reference -> null) : FilterMatch.NONE; + } + + @Override + public boolean declaresField(String reference) { + return false; + } + + @Override + public int regexCount() { + return regexes; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/MetricFilter.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/MetricFilter.java new file mode 100644 index 0000000000..b435906e47 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/MetricFilter.java @@ -0,0 +1,56 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.util.ArrayList; +import java.util.List; + +/** A metric filter on a log group, the AWS MetricFilter shape. */ +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class MetricFilter { + + private String filterName; + private String logGroupName; + private String filterPattern; + private List metricTransformations = new ArrayList<>(); + private long creationTime; + private Boolean applyOnTransformedLogs; + private String fieldSelectionCriteria; + private List emitSystemFieldDimensions; + + public MetricFilter() {} + + public String getFilterName() { return filterName; } + public void setFilterName(String filterName) { this.filterName = filterName; } + + public String getLogGroupName() { return logGroupName; } + public void setLogGroupName(String logGroupName) { this.logGroupName = logGroupName; } + + public String getFilterPattern() { return filterPattern; } + public void setFilterPattern(String filterPattern) { this.filterPattern = filterPattern; } + + public List getMetricTransformations() { return metricTransformations; } + public void setMetricTransformations(List metricTransformations) { + this.metricTransformations = metricTransformations != null ? metricTransformations : new ArrayList<>(); + } + + public long getCreationTime() { return creationTime; } + public void setCreationTime(long creationTime) { this.creationTime = creationTime; } + + public Boolean getApplyOnTransformedLogs() { return applyOnTransformedLogs; } + public void setApplyOnTransformedLogs(Boolean applyOnTransformedLogs) { + this.applyOnTransformedLogs = applyOnTransformedLogs; + } + + public String getFieldSelectionCriteria() { return fieldSelectionCriteria; } + public void setFieldSelectionCriteria(String fieldSelectionCriteria) { + this.fieldSelectionCriteria = fieldSelectionCriteria; + } + + public List getEmitSystemFieldDimensions() { return emitSystemFieldDimensions; } + public void setEmitSystemFieldDimensions(List emitSystemFieldDimensions) { + this.emitSystemFieldDimensions = emitSystemFieldDimensions; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/MetricTransformation.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/MetricTransformation.java new file mode 100644 index 0000000000..ab6ef44211 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/model/MetricTransformation.java @@ -0,0 +1,42 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** How a metric filter turns a matching log event into a metric value, the AWS MetricTransformation shape. */ +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class MetricTransformation { + + private String metricName; + private String metricNamespace; + private String metricValue; + private Double defaultValue; + private Map dimensions = new LinkedHashMap<>(); + private String unit; + + public MetricTransformation() {} + + public String getMetricName() { return metricName; } + public void setMetricName(String metricName) { this.metricName = metricName; } + + public String getMetricNamespace() { return metricNamespace; } + public void setMetricNamespace(String metricNamespace) { this.metricNamespace = metricNamespace; } + + public String getMetricValue() { return metricValue; } + public void setMetricValue(String metricValue) { this.metricValue = metricValue; } + + public Double getDefaultValue() { return defaultValue; } + public void setDefaultValue(Double defaultValue) { this.defaultValue = defaultValue; } + + public Map getDimensions() { return dimensions; } + public void setDimensions(Map dimensions) { + this.dimensions = dimensions != null ? dimensions : new LinkedHashMap<>(); + } + + public String getUnit() { return unit; } + public void setUnit(String unit) { this.unit = unit; } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java index 52fbe5eb22..61584f1842 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java @@ -153,7 +153,7 @@ public class CloudWatchMetricsQueryHandler { xml.elem("Unit", dp.unit()).end("member"); } xml.end("Datapoints"); - return Response.ok(AwsQueryResponse.envelope("GetMetricStatistics", null, xml.build())).build(); + return Response.ok(AwsQueryResponse.envelope("GetMetricStatistics", AwsNamespaces.CW, xml.build())).build(); } private Response handleGetMetricData(MultivaluedMap params, String region) { @@ -188,7 +188,7 @@ public class CloudWatchMetricsQueryHandler { xml.end("member"); } xml.end("MetricDataResults"); - return Response.ok(AwsQueryResponse.envelope("GetMetricData", null, xml.build())).build(); + return Response.ok(AwsQueryResponse.envelope("GetMetricData", AwsNamespaces.CW, xml.build())).build(); } private List parseMetricDataQueries( diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java index 7c85a1ab16..38c4a240e5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudwatch.metrics; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; @@ -48,6 +49,15 @@ public class CloudWatchMetricsService { } public void putMetricData(String namespace, List datums, String region) { + putMetricDataForAccount(null, namespace, datums, region); + } + + /** + * Stores the datums in {@code accountId}'s partition rather than the caller's, for writers that + * run outside a request, such as a metric filter publishing for a log batch a container + * streamed on another account's behalf. A null account is the caller's own. + */ + public void putMetricDataForAccount(String accountId, String namespace, List datums, String region) { long nowSeconds = Instant.now().getEpochSecond(); for (MetricDatum datum : datums) { datum.setNamespace(namespace); @@ -62,15 +72,50 @@ public class CloudWatchMetricsService { datum.setMaximum(datum.getValue()); } - String dimKey = buildDimKey(datum.getDimensions()); - String key = region + "::" + namespace + "::" + datum.getMetricName() - + "::" + dimKey + "::" - + String.format("%013d", datum.getTimestamp()) + "::" + UUID.randomUUID(); - metricStore.put(key, datum); + storeDatum(accountId, namespace, datum, region, UUID.randomUUID().toString()); } LOG.debugv("PutMetricData: {0} datums for namespace {1}", datums.size(), namespace); } + /** + * Internal scalar publication, not an AWS PutMetricData operation. The publisher supplies a + * canonical account, an explicit event timestamp (including epoch zero), and a stable ID for + * this one contribution. Retrying a partially/ambiguously committed write replaces the same + * key instead of appending another sample. Distinct contributions must have distinct IDs. + * The caller's snapshot is never mutated or retained by the store. + */ + public void publishMetricForAccount(String accountId, String namespace, MetricDatum datum, + String region, String publicationId) { + if (accountId == null || accountId.isBlank() || publicationId == null || publicationId.isBlank()) { + throw new IllegalArgumentException("Internal publication requires an explicit account and publication ID"); + } + MetricDatum sample = new MetricDatum(); + sample.setNamespace(namespace); + sample.setMetricName(datum.getMetricName()); + sample.setUnit(datum.getUnit()); + sample.setDimensions(List.copyOf(datum.getDimensions())); + sample.setTimestamp(datum.getTimestamp()); + sample.setValue(datum.getValue()); + sample.setSampleCount(1); + sample.setSum(datum.getValue()); + sample.setMinimum(datum.getValue()); + sample.setMaximum(datum.getValue()); + storeDatum(accountId, namespace, sample, region, "publication-" + publicationId); + } + + private void storeDatum(String accountId, String namespace, MetricDatum datum, String region, String id) { + String key = region + "::" + namespace + "::" + datum.getMetricName() + + "::" + buildDimKey(datum.getDimensions()) + "::" + + String.format("%013d", datum.getTimestamp()) + "::" + id; + if (accountId != null && metricStore instanceof AccountAwareStorageBackend rawAware) { + @SuppressWarnings("unchecked") + AccountAwareStorageBackend aware = (AccountAwareStorageBackend) rawAware; + aware.putForAccount(accountId, key, datum); + } else { + metricStore.put(key, datum); + } + } + public record MetricIdentity(String namespace, String metricName, List dimensions) {} public List listMetrics(String namespace, String metricName, diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java index 0dd276a3aa..527f2a00d0 100644 --- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java +++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java @@ -17,8 +17,11 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -84,4 +87,37 @@ class EmulatorInfoControllerTest { verify(sageMakerTeardown).stopManagedContainers(); verify(storageFactory).clearAll(); } + + @Test + @DisplayName("Should call beforeReset on every service before the wipe and afterReset after every clear") + void reset_bracketsTheWipeWithBeforeAndAfterHooks() { + Resettable second = mock(Resettable.class); + when(containerTeardowns.iterator()).thenReturn(List.of().iterator()); + when(resettables.iterator()).thenReturn(List.of(resettable, second).iterator()); + + controller.reset(); + + InOrder order = inOrder(storageFactory, resettable, second); + order.verify(resettable).beforeReset(); + order.verify(second).beforeReset(); + order.verify(storageFactory).clearAll(); + order.verify(resettable).clear(); + order.verify(second).clear(); + order.verify(second).afterReset(); + order.verify(resettable).afterReset(); + } + + @Test + @DisplayName("Should still call afterReset when the storage wipe fails, then rethrow") + void reset_runsAfterResetWhenTheWipeFails() { + doThrow(new IllegalStateException("wipe failed")).when(storageFactory).clearAll(); + when(containerTeardowns.iterator()).thenReturn(List.of().iterator()); + when(resettables.iterator()).thenReturn(List.of(resettable).iterator()); + + assertThrows(IllegalStateException.class, controller::reset); + + verify(resettable).beforeReset(); + verify(resettable).afterReset(); + verify(resettable, never()).clear(); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java index 2b9efc0ea2..8c0cd8688f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java @@ -67,6 +67,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaEve import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaEventSourceMappingCfnProvisioner; import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaVersionAliasCfnProvisioner; import io.github.hectorvent.floci.services.cloudformation.provisioners.LogsCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.LogsMetricFilterCfnProvisioner; import io.github.hectorvent.floci.services.cloudformation.provisioners.PipesCfnProvisioner; import io.github.hectorvent.floci.services.cloudformation.provisioners.RdsCfnProvisioner; import io.github.hectorvent.floci.services.cloudformation.provisioners.RedshiftClusterCfnProvisioner; @@ -82,6 +83,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudForm import io.github.hectorvent.floci.services.cloudfront.CloudFrontService; import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService; import io.github.hectorvent.floci.services.cloudwatch.dashboards.CloudWatchDashboardsService; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService; import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService; import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; import io.github.hectorvent.floci.services.cognito.CognitoService; @@ -182,6 +184,7 @@ final class CfnProvisionerFixture { // dispatcher. They exist only so inferredProvisioners() can wire their provisioner. private FlowLogService flowLogService; private CloudWatchDashboardsService cloudWatchDashboardsService; + private CloudWatchLogsMetricFilterService logsMetricFilterService; private IotDomainConfigurationService iotDomainConfigurationService; private IotService iotService; private LambdaMicrovmsService lambdaMicrovmsService; @@ -267,6 +270,9 @@ final class CfnProvisionerFixture { if (logsService != null) { discovered.add(new LogsCfnProvisioner(logsService)); } + if (logsMetricFilterService != null) { + discovered.add(new LogsMetricFilterCfnProvisioner(logsMetricFilterService)); + } if (kinesisService != null) { discovered.add(new KinesisCfnProvisioner(kinesisService)); } @@ -571,6 +577,11 @@ final class CfnProvisionerFixture { return this; } + public Builder logsMetricFilters(CloudWatchLogsMetricFilterService v) { + this.logsMetricFilterService = v; + return this; + } + public Builder iotDomainConfiguration(IotDomainConfigurationService v) { this.iotDomainConfigurationService = v; return this; diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogsMetricFilterIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogsMetricFilterIntegrationTest.java new file mode 100644 index 0000000000..f45bb8891f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogsMetricFilterIntegrationTest.java @@ -0,0 +1,758 @@ +package io.github.hectorvent.floci.services.cloudformation; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.XmlParser; +import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnRollback; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService.MutationOutcome; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService.MutationResult; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectSpy; +import jakarta.inject.Inject; +import io.restassured.response.Response; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.params.provider.CsvSource; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import static io.restassured.RestAssured.given; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doCallRealMethod; + +/** + * Provisions an {@code AWS::Logs::MetricFilter} through a CloudFormation stack and reads it back + * through the Logs API. Ref and physical id are filter names. A name change deletes before create; + * a failed update restores the complete backing definition; + * and the stack delete removes the filter. + */ +@QuarkusTest +class CloudFormationLogsMetricFilterIntegrationTest { + + @InjectSpy + CloudWatchLogsMetricFilterService metricFilterService; + + @Inject + CloudFormationService cloudFormationService; + + private static final String CFN_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/cloudformation/aws4_request"; + private static final String LOGS_CONTENT_TYPE = "application/x-amz-json-1.1"; + private static final String LOGS_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/logs/aws4_request"; + private static final Map> PRIOR_EVENTS = new ConcurrentHashMap<>(); + + private static final String TEMPLATE = """ + { + "Parameters": { + "FilterName": {"Type": "String"}, + "Pattern": {"Type": "String", "Default": "ERROR"} + }, + "Resources": { + "Filter": { + "Type": "AWS::Logs::MetricFilter", + "Properties": { + "LogGroupName": "%s", + "FilterName": {"Ref": "FilterName"}, + "FilterPattern": {"Ref": "Pattern"}, + "MetricTransformations": [{ + "MetricName": "ErrorCount", + "MetricNamespace": "Stack/%s", + "MetricValue": "1", + "DefaultValue": 0, + "Unit": "Count" + }] + } + }%s + }, + "Outputs": { + "FilterRef": {"Value": {"Ref": "Filter"}} + } + } + """; + + private static final String UNNAMED_TEMPLATE = """ + { + "Resources": { + "Filter": { + "Type": "AWS::Logs::MetricFilter", + "Properties": { + "LogGroupName": "%s", + "FilterPattern": "{ $.latency = * }", + "MetricTransformations": [{ + "MetricName": "Latency", + "MetricNamespace": "Stack/%s", + "MetricValue": "$.latency", + "Dimensions": [{"Key": "Route", "Value": "$.route"}] + }] + } + } + }, + "Outputs": { + "FilterRef": {"Value": {"Ref": "Filter"}} + } + } + """; + + /** The log group in a stack of its own, so the filter stacks hold nothing else. */ + private static final String GROUP_TEMPLATE = """ + { + "Resources": { + "LogGroup": { + "Type": "AWS::Logs::LogGroup", + "Properties": {"LogGroupName": "%s"} + } + } + } + """; + + /** A resource that fails after the filter, so the update rolls back. */ + private static final String FAILING_RESOURCE = """ + , + "BadSecret": { + "Type": "AWS::SecretsManager::Secret", + "DependsOn": "Filter", + "Properties": { + "Name": "metric-filter-rollback-%s", + "SecretString": "explicit", + "GenerateSecretString": {"PasswordLength": 32} + } + }"""; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void filterFollowsTheTemplateThroughCreateUpdateReplacementAndDelete() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-" + suffix; + String groupStack = "cfn-metric-filter-group-" + suffix; + String group = "/cfn/metric-filter/" + suffix; + createGroupStack(groupStack, group); + String template = TEMPLATE.formatted(group, suffix, ""); + + cloudFormation(stack, "CreateStack", template, Map.of("FilterName", "errors")); + assertEquals("errors", outputValue(describeStacks(stack, "CREATE_COMPLETE"), "FilterRef")); + describeFilters(group).then() + .statusCode(200) + .body("metricFilters", hasSize(1)) + .body("metricFilters[0].filterName", equalTo("errors")) + .body("metricFilters[0].filterPattern", equalTo("ERROR")) + .body("metricFilters[0].metricTransformations[0].metricName", equalTo("ErrorCount")) + .body("metricFilters[0].metricTransformations[0].metricNamespace", equalTo("Stack/" + suffix)) + .body("metricFilters[0].metricTransformations[0].metricValue", equalTo("1")) + .body("metricFilters[0].metricTransformations[0].defaultValue", equalTo(0.0f)) + .body("metricFilters[0].metricTransformations[0].unit", equalTo("Count")); + + cloudFormation(stack, "UpdateStack", template, Map.of("FilterName", "errors", "Pattern", "FATAL")); + assertEquals("errors", outputValue(describeStacks(stack, "UPDATE_COMPLETE"), "FilterRef"), + "a pattern change keeps the same filter"); + describeFilters(group).then() + .statusCode(200) + .body("metricFilters", hasSize(1)) + .body("metricFilters[0].filterPattern", equalTo("FATAL")); + + cloudFormation(stack, "UpdateStack", template, Map.of("FilterName", "fatal", "Pattern", "FATAL")); + assertEquals("fatal", outputValue(describeStacks(stack, "UPDATE_COMPLETE"), "FilterRef"), + "a name change is a replacement"); + describeFilters(group).then() + .statusCode(200) + .body("metricFilters", hasSize(1)) + .body("metricFilters[0].filterName", equalTo("fatal")); + assertResourceSequence(stack, List.of("errors:UPDATE_IN_PROGRESS", "errors:DELETE_IN_PROGRESS", + "errors:DELETE_COMPLETE", "fatal:CREATE_IN_PROGRESS", "fatal:CREATE_COMPLETE", "fatal:UPDATE_COMPLETE")); + assertTrue(describeEvents(stack).contains( + "Requested update requires the replacement of the existing resource; deleting existing resource, then creating a new one.")); + + cloudFormation(stack, "DeleteStack", null, Map.of()); + awaitStackDeleted(stack); + describeFilters(group).then() + .statusCode(200) + .body("metricFilters", hasSize(0)); + cloudFormation(groupStack, "DeleteStack", null, Map.of()); + awaitStackDeleted(groupStack); + } + + @Test + void anUnnamedFilterGetsAGeneratedNameAndItsDimensionsReachTheService() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-unnamed-" + suffix; + String groupStack = "cfn-metric-filter-unnamed-group-" + suffix; + String group = "/cfn/metric-filter/unnamed/" + suffix; + createGroupStack(groupStack, group); + String template = UNNAMED_TEMPLATE.formatted(group, suffix); + + cloudFormation(stack, "CreateStack", template, Map.of()); + String ref = outputValue(describeStacks(stack, "CREATE_COMPLETE"), "FilterRef"); + assertTrue(ref.startsWith(stack + "-Filter-"), ref); + describeFilters(group).then() + .statusCode(200) + .body("metricFilters", hasSize(1)) + .body("metricFilters[0].metricTransformations[0].metricValue", equalTo("$.latency")) + .body("metricFilters[0].metricTransformations[0].dimensions.Route", equalTo("$.route")); + + cloudFormation(stack, "UpdateStack", template.replace("$.latency = *", "$.latency > 10"), Map.of()); + assertEquals(ref, outputValue(describeStacks(stack, "UPDATE_COMPLETE"), "FilterRef"), + "an unnamed filter keeps its generated name"); + describeFilters(group).then().body("metricFilters[0].filterPattern", equalTo("{ $.latency > 10 }")); + + cloudFormation(stack, "DeleteStack", null, Map.of()); + awaitStackDeleted(stack); + cloudFormation(groupStack, "DeleteStack", null, Map.of()); + awaitStackDeleted(groupStack); + } + + @Test + void aFailedUpdateRestoresThePatternAnInPlaceUpdateChanged() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-rb-" + suffix; + String groupStack = "cfn-metric-filter-rb-group-" + suffix; + String group = "/cfn/metric-filter/rb/" + suffix; + createGroupStack(groupStack, group); + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(group, suffix, ""), Map.of("FilterName", "errors")); + describeStacks(stack, "CREATE_COMPLETE"); + + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(group, suffix, FAILING_RESOURCE.formatted(suffix)), + Map.of("FilterName", "errors", "Pattern", "FATAL")); + assertEquals("errors", outputValue(describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE"), "FilterRef")); + describeFilters(group).then() + .statusCode(200) + .body("metricFilters", hasSize(1)) + .body("metricFilters[0].filterPattern", equalTo("ERROR")); + assertMutationPrecedesBadSecret(stack, "errors"); + assertPublishedValue(group, "Stack/" + suffix, "ERROR", 1); + + cloudFormation(stack, "DeleteStack", null, Map.of()); + awaitStackDeleted(stack); + cloudFormation(groupStack, "DeleteStack", null, Map.of()); + awaitStackDeleted(groupStack); + } + + @Test + void aFailedUpdateRollsAReplacementBack() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-rb2-" + suffix; + String groupStack = "cfn-metric-filter-rb2-group-" + suffix; + String group = "/cfn/metric-filter/rb2/" + suffix; + createGroupStack(groupStack, group); + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(group, suffix, ""), Map.of("FilterName", "errors")); + describeStacks(stack, "CREATE_COMPLETE"); + + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(group, suffix, FAILING_RESOURCE.formatted(suffix)), + Map.of("FilterName", "fatal")); + assertEquals("errors", outputValue(describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE"), "FilterRef")); + describeFilters(group).then() + .statusCode(200) + .body("metricFilters", hasSize(1)) + .body("metricFilters[0].filterName", equalTo("errors")); + assertMutationPrecedesBadSecret(stack, "fatal"); + assertPublishedValue(group, "Stack/" + suffix, "ERROR", 1); + assertResourceSequence(stack, List.of("errors:UPDATE_IN_PROGRESS", "errors:DELETE_IN_PROGRESS", + "errors:DELETE_COMPLETE", "fatal:CREATE_IN_PROGRESS", "fatal:CREATE_COMPLETE", "fatal:UPDATE_COMPLETE", + "fatal:UPDATE_IN_PROGRESS", "fatal:DELETE_IN_PROGRESS", "fatal:DELETE_COMPLETE", + "fatal:CREATE_IN_PROGRESS", "errors:CREATE_COMPLETE", "errors:UPDATE_COMPLETE")); + + cloudFormation(stack, "DeleteStack", null, Map.of()); + awaitStackDeleted(stack); + cloudFormation(groupStack, "DeleteStack", null, Map.of()); + awaitStackDeleted(groupStack); + } + + @Test + void invalidReplacementDeletesThenRecreatesPriorDefinitionWithNameOnlyRefAndEvents() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-invalid-replace-" + suffix; + String groupStack = stack + "-group"; + String group = "/cfn/invalid-replace/" + suffix; + createGroupStack(groupStack, group); + String original = TEMPLATE.formatted(group, suffix, "").replace("\"MetricValue\": \"1\"", "\"MetricValue\": \"2\""); + cloudFormation(stack, "CreateStack", original, Map.of("FilterName", "old", "Pattern", "WARN")); + describeStacks(stack, "CREATE_COMPLETE"); + Map before = describeFilters(group).jsonPath().getMap("metricFilters[0]"); + cloudFormation(stack, "UpdateStack", original.replace("\"MetricValue\": \"2\"", "\"MetricValue\": \"9\""), + Map.of("FilterName", "new", "Pattern", "{")); + assertEquals("old", outputValue(describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE"), "FilterRef")); + Map after = describeFilters(group).jsonPath().getMap("metricFilters[0]"); + assertNotNull(after, "rollback must restore the deleted backing filter, not just its Ref"); + assertTrue(((Number) after.get("creationTime")).longValue() > ((Number) before.get("creationTime")).longValue()); + after.remove("creationTime"); + before.remove("creationTime"); + assertEquals(before, after); + assertResourceSequence(stack, List.of("old:UPDATE_IN_PROGRESS", "old:DELETE_IN_PROGRESS", "old:DELETE_COMPLETE", + "new:CREATE_IN_PROGRESS", "new:CREATE_FAILED", "new:UPDATE_FAILED", "new:UPDATE_IN_PROGRESS", + "new:DELETE_IN_PROGRESS", "new:DELETE_COMPLETE", "new:CREATE_IN_PROGRESS", + "old:CREATE_COMPLETE", "old:UPDATE_COMPLETE")); + assertPublishedValue(group, "Stack/" + suffix, "WARN", 2); + String template = given().contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH) + .formParam("Action", "GetTemplate").formParam("StackName", stack).post("/").asString(); + assertTrue(template.contains("MetricValue"), template); + assertTrue(template.contains(""2"") || template.contains("\"2\""), template); + cloudFormation(stack, "DeleteStack", null, Map.of()); + awaitStackDeleted(stack); + cloudFormation(groupStack, "DeleteStack", null, Map.of()); + awaitStackDeleted(groupStack); + } + + @Test + void resolvedArrayIntrinsicsAndNoValueUseCfnShapes() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-intrinsics-" + suffix; + String groupStack = stack + "-group"; + String group = "/cfn/intrinsics/" + suffix; + createGroupStack(groupStack, group); + String template = """ + {"Parameters":{"Enabled":{"Type":"String","Default":"false"}}, + "Conditions":{"UseDimensions":{"Fn::Equals":[{"Ref":"Enabled"},"true"]}}, + "Resources":{"Filter":{"Type":"AWS::Logs::MetricFilter","Properties":{ + "LogGroupName":"%s", "FilterName":{"Ref":"AWS::NoValue"}, "FilterPattern":"", + "ApplyOnTransformedLogs":{"Ref":"Enabled"}, + "MetricTransformations":{"Fn::If":["UseDimensions",[],[ + {"MetricName":"ErrorCount","MetricNamespace":"Stack/%s","MetricValue":"1", + "Dimensions":{"Fn::If":["UseDimensions",[],{"Ref":"AWS::NoValue"}]}}]]}, + "EmitSystemFieldDimensions":{"Fn::Split":[",","@aws.account,@aws.region"]}}}}, + "Outputs":{"FilterRef":{"Value":{"Ref":"Filter"}}}} + """.formatted(group, suffix); + cloudFormation(stack, "CreateStack", template, Map.of()); + String ref = outputValue(describeStacks(stack, "CREATE_COMPLETE"), "FilterRef"); + assertTrue(ref.startsWith(stack + "-Filter-"), ref); + describeFilters(group).then().body("metricFilters[0].applyOnTransformedLogs", equalTo(false)) + .body("metricFilters[0].emitSystemFieldDimensions", hasSize(2)); + cloudFormation(stack, "DeleteStack", null, Map.of()); + awaitStackDeleted(stack); + cloudFormation(groupStack, "DeleteStack", null, Map.of()); + awaitStackDeleted(groupStack); + } + + @ParameterizedTest + @ValueSource(strings = {"confirmedRestore", "uncertainRestore", "uncertainReplacement", "confirmedAddedDelete"}) + void deleteStackPreservesFailedRollbackOwnershipUntilResourceAwareCleanup(String fault) throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-delete-failed-" + suffix; + String group = "/cfn/external-delete-failed/" + suffix; + boolean uncertain = fault.startsWith("uncertain"); + boolean added = "confirmedAddedDelete".equals(fault); + String faultName = added ? "added" : "uncertainReplacement".equals(fault) ? "new" : "old"; + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.CreateLogGroup") + .body(Map.of("logGroupName", group)).post("/").then().statusCode(200); + try { + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.PutMetricFilter") + .body(Map.of("logGroupName", group, "filterName", "unrelated", "filterPattern", "UNRELATED", + "metricTransformations", List.of(Map.of("metricName", "Unrelated", + "metricNamespace", "Other/" + suffix, "metricValue", "7")))) + .post("/").then().statusCode(200); + Map unrelated = backingFilter(group, "unrelated"); + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(group, suffix, ""), + Map.of("FilterName", "old", "Pattern", "WARN")); + describeStacks(stack, "CREATE_COMPLETE"); + + doAnswer(invocation -> { + MetricFilter definition = invocation.getArgument(0); + if (added || !group.equals(definition.getLogGroupName()) || !faultName.equals(definition.getFilterName())) { + return invocation.callRealMethod(); + } + assertTrue(metricFilterService.findMetricFilter(group, faultName, "us-east-1").isEmpty()); + if (uncertain) { + // Persist the real definition but withhold the ownership acknowledgement, + // simulating a write followed by an unavailable ownership inspection. + metricFilterService.putMetricFilter(definition, "us-east-1"); + Consumer outcome = invocation.getArgument(2); + outcome.accept(new MutationResult(MutationOutcome.UNKNOWN, null)); + } else { + invocation.callRealMethod(); + } + throw new AwsException("ServiceUnavailableException", "injected " + fault + " after write", 500); + }).when(metricFilterService).createMetricFilter(any(), anyString(), any()); + + String additions = FAILING_RESOURCE.formatted(suffix); + if (added) { + doAnswer(invocation -> { + Consumer outcome = invocation.getArgument(3); + outcome.accept(new MutationResult(MutationOutcome.NOT_APPLIED, true)); + throw new AwsException("ServiceUnavailableException", "injected added-filter rollback deletion", 500); + }).doCallRealMethod().when(metricFilterService) + .deleteMetricFilter(eq(group), eq("added"), eq("us-east-1"), any()); + additions = """ + ,"AddedFilter":{"Type":"AWS::Logs::MetricFilter","Properties":{ + "LogGroupName":"%s","FilterName":"added","FilterPattern":"WARN", + "MetricTransformations":[{"MetricName":"Added","MetricNamespace":"Stack/%s","MetricValue":"1"}]}} + """.formatted(group, suffix) + + additions.replace("\"DependsOn\": \"Filter\"", "\"DependsOn\": \"AddedFilter\""); + } + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(group, suffix, additions), + Map.of("FilterName", added ? "old" : "new", "Pattern", "FATAL")); + describeStacks(stack, "UPDATE_ROLLBACK_FAILED"); + StackResource failed = cloudFormationService.describeStacks(stack, "us-east-1") + .getFirst().getResources().get(added ? "AddedFilter" : "Filter"); + assertEquals("UPDATE_FAILED", failed.getStatus()); + String snapshot = failed.getAttributes().get(CfnRollback.METRIC_FILTER_UPDATE_SNAPSHOT_ATTR); + if (added) { + assertNull(snapshot, "new resources carry create ownership, not an update snapshot"); + assertEquals("true", failed.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR)); + } else { + assertNotNull(snapshot); + String address = "uncertainReplacement".equals(fault) ? "target" : "prior"; + assertEquals(uncertain ? "UNKNOWN" : "OWNED", + new ObjectMapper().readTree(snapshot).path(address).path("ownership").asText(), snapshot); + } + assertNotNull(backingFilter(group, faultName), "the failed write really created a backing filter"); + + if (uncertain) { + for (int attempt = 0; attempt < 2; attempt++) { + assertThrows(ExecutionException.class, () -> cloudFormationService + .deleteStack(stack, "us-east-1", "000000000000").get(10, TimeUnit.SECONDS)); + describeStacks(stack, "DELETE_FAILED"); + StackResource retained = cloudFormationService.describeStacks(stack, "us-east-1") + .getFirst().getResources().get("Filter"); + assertEquals("DELETE_FAILED", retained.getStatus()); + assertEquals(snapshot, retained.getAttributes().get(CfnRollback.METRIC_FILTER_UPDATE_SNAPSHOT_ATTR)); + assertNotNull(backingFilter(group, faultName)); + assertEquals(unrelated, backingFilter(group, "unrelated")); + } + metricFilterService.deleteMetricFilter(group, faultName, "us-east-1"); + } + assertDoesNotThrow(() -> cloudFormationService.deleteStack(stack, "us-east-1", "000000000000") + .get(10, TimeUnit.SECONDS)); + assertTrue(metricFilterService.findMetricFilter(group, "old", "us-east-1").isEmpty(), + "DeleteStack must clean the owned restoration before reporting completion"); + assertTrue(metricFilterService.findMetricFilter(group, "new", "us-east-1").isEmpty()); + assertTrue(metricFilterService.findMetricFilter(group, "added", "us-east-1").isEmpty(), + "a failed rollback delete of a newly owned resource must also be retried"); + assertEquals(unrelated, backingFilter(group, "unrelated")); + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.DescribeLogGroups") + .body(Map.of("logGroupNamePrefix", group)).post("/").then().statusCode(200) + .body("logGroups[0].logGroupName", equalTo(group)); + assertThrows(AwsException.class, () -> cloudFormationService.describeStacks(stack, "us-east-1")); + } finally { + doCallRealMethod().when(metricFilterService).createMetricFilter(any(), anyString(), any()); + doCallRealMethod().when(metricFilterService).deleteMetricFilter(anyString(), anyString(), anyString(), any()); + for (String name : List.of("old", "new", "added")) { + if (metricFilterService.findMetricFilter(group, name, "us-east-1").isPresent()) { + metricFilterService.deleteMetricFilter(group, name, "us-east-1"); + } + } + cloudFormationService.deleteStack(stack, "us-east-1", "000000000000").get(10, TimeUnit.SECONDS); + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.DeleteLogGroup") + .body(Map.of("logGroupName", group)).post("/").then().statusCode(200); + } + } + + private static Map backingFilter(String group, String name) { + List> filters = describeFilters(group).jsonPath().getList("metricFilters"); + return filters.stream().filter(filter -> name.equals(filter.get("filterName"))).findFirst().orElse(null); + } + + @ParameterizedTest + @CsvSource({"forward,false", "forward,true", "stack,false", "stack,true"}) + void appliedAndUnknownDeletesDoNotClaimAConcurrentOwnersFilter(String phase, boolean unknown) throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-delete-outcome-" + suffix; + String group = "/cfn/external-delete-outcome/" + suffix; + ObjectMapper mapper = new ObjectMapper(); + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.CreateLogGroup") + .body(Map.of("logGroupName", group)).post("/").then().statusCode(200); + try { + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(group, suffix, ""), + Map.of("FilterName", "old", "Pattern", "WARN")); + describeStacks(stack, "CREATE_COMPLETE"); + MetricFilter original = metricFilterService.findMetricFilter(group, "old", "us-east-1").orElseThrow(); + MetricFilter stable = mapper.convertValue(original, MetricFilter.class); + stable.setFilterName("unrelated"); + metricFilterService.putMetricFilter(stable, "us-east-1"); + Map unrelated = backingFilter(group, "unrelated"); + AtomicReference otherOwner = new AtomicReference<>(); + AtomicBoolean armed = new AtomicBoolean(true); + doAnswer(invocation -> { + String requestedGroup = invocation.getArgument(0); + String requestedName = invocation.getArgument(1); + if (!group.equals(requestedGroup) || !"old".equals(requestedName) || !armed.compareAndSet(true, false)) { + return invocation.callRealMethod(); + } + if (unknown) { + metricFilterService.deleteMetricFilter(group, "old", "us-east-1"); + Consumer outcome = invocation.getArgument(3); + outcome.accept(new MutationResult(MutationOutcome.UNKNOWN, null)); + } else { + invocation.callRealMethod(); + } + MetricFilter b = mapper.convertValue(original, MetricFilter.class); + b.setFilterPattern("UNRELATED"); + b.getMetricTransformations().getFirst().setMetricValue("7"); + otherOwner.set(metricFilterService.putMetricFilter(b, "us-east-1")); + throw new AwsException("ServiceUnavailableException", "injected failure after delete", 500); + }).when(metricFilterService).deleteMetricFilter(anyString(), anyString(), anyString(), any()); + + if ("forward".equals(phase)) { + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(group, suffix, ""), + Map.of("FilterName", "new", "Pattern", "FATAL")); + describeStacks(stack, "UPDATE_ROLLBACK_FAILED"); + } else { + assertThrows(ExecutionException.class, () -> cloudFormationService + .deleteStack(stack, "us-east-1", "000000000000").get(10, TimeUnit.SECONDS)); + describeStacks(stack, "DELETE_FAILED"); + } + assertNotNull(otherOwner.get(), "B must be created after the actual delete and before A handles its failure"); + assertEquals(mapper.valueToTree(otherOwner.get()), + mapper.valueToTree(mapper.convertValue(backingFilter(group, "old"), MetricFilter.class))); + StackResource resource = cloudFormationService.describeStacks(stack, "us-east-1") + .getFirst().getResources().get("Filter"); + String metadataKey = "forward".equals(phase) ? CfnRollback.METRIC_FILTER_UPDATE_SNAPSHOT_ATTR + : "__FlociMetricFilterState"; + String metadata = resource.getAttributes().get(metadataKey); + assertNotNull(metadata); + JsonNode state = mapper.readTree(metadata); + if ("forward".equals(phase)) { + state = state.path("prior"); + } + assertEquals(unknown ? "UNKNOWN" : "UNOWNED", state.path("ownership").asText()); + if (unknown) { + for (int attempt = 0; attempt < 2; attempt++) { + assertThrows(ExecutionException.class, () -> cloudFormationService + .deleteStack(stack, "us-east-1", "000000000000").get(10, TimeUnit.SECONDS)); + describeStacks(stack, "DELETE_FAILED"); + assertEquals(metadata, cloudFormationService.describeStacks(stack, "us-east-1").getFirst() + .getResources().get("Filter").getAttributes().get(metadataKey)); + assertEquals(mapper.valueToTree(otherOwner.get()), + mapper.valueToTree(mapper.convertValue(backingFilter(group, "old"), MetricFilter.class))); + } + metricFilterService.deleteMetricFilter(group, "old", "us-east-1"); + } + assertDoesNotThrow(() -> cloudFormationService.deleteStack(stack, "us-east-1", "000000000000") + .get(10, TimeUnit.SECONDS)); + if (!unknown) { + assertEquals(mapper.valueToTree(otherOwner.get()), + mapper.valueToTree(mapper.convertValue(backingFilter(group, "old"), MetricFilter.class))); + } + assertEquals(unrelated, backingFilter(group, "unrelated")); + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.DescribeLogGroups") + .body(Map.of("logGroupNamePrefix", group)).post("/").then().statusCode(200) + .body("logGroups[0].logGroupName", equalTo(group)); + } finally { + doCallRealMethod().when(metricFilterService).deleteMetricFilter(anyString(), anyString(), anyString(), any()); + for (String name : List.of("old", "new")) { + if (metricFilterService.findMetricFilter(group, name, "us-east-1").isPresent()) { + metricFilterService.deleteMetricFilter(group, name, "us-east-1"); + } + } + cloudFormationService.deleteStack(stack, "us-east-1", "000000000000").get(10, TimeUnit.SECONDS); + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.DeleteLogGroup") + .body(Map.of("logGroupName", group)).post("/").then().statusCode(200); + } + } + + private static List> newEvents(String stack) { + List> events = new ArrayList<>(XmlParser.extractGroups(describeEvents(stack), "member")); + Collections.reverse(events); + return events.stream().filter(e -> !PRIOR_EVENTS.getOrDefault(stack, Set.of()).contains(e.get("EventId"))).toList(); + } + + private static void assertResourceSequence(String stack, List expected) { + List> events = newEvents(stack); + assertEquals(events.size(), events.stream().map(e -> e.get("EventId")).distinct().count()); + assertEquals(expected, events.stream().filter(e -> "Filter".equals(e.get("LogicalResourceId"))) + .map(e -> e.get("PhysicalResourceId") + ":" + e.get("ResourceStatus")).toList()); + } + + private static void assertMutationPrecedesBadSecret(String stack, String physicalId) { + List> events = newEvents(stack); + int mutation = -1; + int failure = -1; + for (int i = 0; i < events.size(); i++) { + Map e = events.get(i); + if ("Filter".equals(e.get("LogicalResourceId")) && physicalId.equals(e.get("PhysicalResourceId")) + && "UPDATE_COMPLETE".equals(e.get("ResourceStatus")) && mutation == -1) { + mutation = i; + } + if ("BadSecret".equals(e.get("LogicalResourceId")) && "UPDATE_FAILED".equals(e.get("ResourceStatus"))) { + failure = i; + } + } + assertTrue(mutation >= 0 && failure > mutation, events.toString()); + } + + private static void assertPublishedValue(String group, String namespace, String message, int value) { + long timestamp = System.currentTimeMillis(); + String stream = "verify-" + timestamp; + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.CreateLogStream") + .body(Map.of("logGroupName", group, "logStreamName", stream)).post("/").then().statusCode(200); + given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.PutLogEvents") + .body(Map.of("logGroupName", group, "logStreamName", stream, + "logEvents", List.of(Map.of("timestamp", timestamp, "message", message)))) + .post("/").then().statusCode(200); + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> given().contentType("application/x-amz-json-1.0") + .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/monitoring/aws4_request") + .header("X-Amz-Target", "GraniteServiceVersion20100801.GetMetricStatistics") + .body(Map.of("Namespace", namespace, "MetricName", "ErrorCount", "Period", 60, + "StartTime", (timestamp - 60_000) / 1000.0, "EndTime", (timestamp + 60_000) / 1000.0, + "Statistics", List.of("Sum", "SampleCount"))) + .post("/").then().statusCode(200) + .body("Datapoints", hasSize(1)).body("Datapoints[0].Sum", equalTo((float) value)) + .body("Datapoints[0].SampleCount", equalTo(1.0f))); + } + + @Test + void anInvalidPatternFailsTheResource() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-metric-filter-bad-" + suffix; + String groupStack = "cfn-metric-filter-bad-group-" + suffix; + String group = "/cfn/metric-filter/bad/" + suffix; + createGroupStack(groupStack, group); + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(group, suffix, ""), + Map.of("FilterName", "bad", "Pattern", "{ $.a = }")); + + String events = awaitStackStatus(stack, "ROLLBACK_COMPLETE"); + assertTrue(events.contains("Invalid filter pattern"), "the status reason carries the parse error: " + events); + cloudFormation(stack, "DeleteStack", null, Map.of()); + awaitStackDeleted(stack); + cloudFormation(groupStack, "DeleteStack", null, Map.of()); + awaitStackDeleted(groupStack); + } + + private static void createGroupStack(String stack, String group) { + cloudFormation(stack, "CreateStack", GROUP_TEMPLATE.formatted(group), Map.of()); + describeStacks(stack, "CREATE_COMPLETE"); + } + + private static Response describeFilters(String group) { + return given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328.DescribeMetricFilters") + .body("{\"logGroupName\":\"" + group + "\"}").post("/"); + } + + private static void cloudFormation(String stack, String action, String templateBody, + Map parameters) { + if ("UpdateStack".equals(action)) { + PRIOR_EVENTS.put(stack, XmlParser.extractGroups(describeEvents(stack), "member").stream() + .map(e -> e.get("EventId")).collect(Collectors.toSet())); + } + RequestSpecification request = given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", action) + .formParam("StackName", stack); + if (templateBody != null) { + request.formParam("TemplateBody", templateBody); + } + int index = 1; + for (Map.Entry parameter : parameters.entrySet()) { + request.formParam("Parameters.member." + index + ".ParameterKey", parameter.getKey()); + request.formParam("Parameters.member." + index + ".ParameterValue", parameter.getValue()); + index++; + } + request.when().post("/").then().statusCode(200); + } + + private static String describeStacks(String stack, String expectedStatus) { + String body = ""; + for (int i = 0; i < 100; i++) { + body = given().contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH) + .formParam("Action", "DescribeStacks").formParam("StackName", stack).post("/").asString(); + if (body.contains("" + expectedStatus + "") + && newEvents(stack).stream().anyMatch(e -> stack.equals(e.get("LogicalResourceId")) + && expectedStatus.equals(e.get("ResourceStatus")))) { + return body; + } + try { + Thread.sleep(50); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new AssertionError(e); + } + } + return fail("Expected new " + expectedStatus + " for " + stack + ": " + body + "\n" + describeEvents(stack)); + } + + private static String describeEvents(String stack) { + return given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DescribeStackEvents") + .formParam("StackName", stack) + .when().post("/").then().statusCode(200).extract().asString(); + } + + private static String awaitStackStatus(String stack, String expectedStatus) throws InterruptedException { + for (int i = 0; i < 100; i++) { + String body = given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DescribeStacks") + .formParam("StackName", stack) + .when().post("/").then().extract().asString(); + if (body.contains("" + expectedStatus + "")) { + return describeEvents(stack); + } + Thread.sleep(50); + } + return fail("stack " + stack + " did not reach " + expectedStatus + " within the timeout"); + } + + private static void awaitStackDeleted(String stack) throws InterruptedException { + for (int i = 0; i < 100; i++) { + String body = given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DescribeStacks") + .formParam("StackName", stack) + .when().post("/").then().extract().asString(); + if (body.contains("does not exist")) { + return; + } + if (body.contains("DELETE_FAILED")) { + fail("stack delete failed: " + body); + } + Thread.sleep(50); + } + fail("stack " + stack + " was not deleted within the timeout"); + } + + private static String outputValue(String xml, String key) { + return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LogsMetricFilterCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LogsMetricFilterCfnProvisionerTest.java new file mode 100644 index 0000000000..b307ca5668 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LogsMetricFilterCfnProvisionerTest.java @@ -0,0 +1,1079 @@ +package io.github.hectorvent.floci.services.cloudformation.provisioners; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; +import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterService.MutationOutcome; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.NullSource; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +/** Real backing filters, with storage faults at the mutation boundary. No live AWS calls. */ +class LogsMetricFilterCfnProvisionerTest { + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final String REGION = "us-east-1"; + private static final String GROUP = "/aws/lambda/orders"; + private static final String OTHER = "/aws/lambda/payments"; + private static final String SNAPSHOT = CfnRollback.METRIC_FILTER_UPDATE_SNAPSHOT_ATTR; + private static final String STATE = "__FlociMetricFilterState"; + private static final String GROUP_ATTR = "__FlociMetricFilterLogGroupName"; + private static final String NAME_MODE = "__FlociMetricFilterNameMode"; + private static final String FULL = """ + {"LogGroupName": "/aws/lambda/orders", "FilterName": "errors", + "FilterPattern": "{ $.level = \\"ERROR\\" }", + "MetricTransformations": [{"MetricName": "ErrorCount", "MetricNamespace": "Orders", + "MetricValue": "2", "DefaultValue": 7, "Unit": "Count"}], + "ApplyOnTransformedLogs": false, + "FieldSelectionCriteria": "@aws.region = \\"us-east-1\\"", + "EmitSystemFieldDimensions": ["@aws.account"]} + """; + + private RecordingStore store; + private AccountAwareStorageBackend canonicalStore; + private CloudWatchLogsService logs; + private CloudWatchLogsMetricFilterService service; + private LogsMetricFilterCfnProvisioner provisioner; + private CloudFormationTemplateEngine engine; + + @BeforeEach + void setUp() { + store = new RecordingStore(); + canonicalStore = new AccountAwareStorageBackend<>(store, null, "000000000000"); + store.monitor = canonicalStore; + RegionResolver resolver = new RegionResolver(REGION, "000000000000"); + StorageFactory storage = mock(StorageFactory.class); + when(storage.create(any(), any(), any())).thenAnswer(i -> + "cwlogs-metric-filters.json".equals(i.getArgument(1)) + ? canonicalStore + : AccountAwareStorageBackend.inMemory("000000000000")); + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().cloudwatchlogs().maxStoredEvents()).thenReturn(Integer.MAX_VALUE); + logs = new CloudWatchLogsService(storage, config, resolver, null, null); + logs.createLogGroup(GROUP, null, null, REGION); + logs.createLogGroup(OTHER, null, null, REGION); + service = new CloudWatchLogsMetricFilterService(logs, mock(CloudWatchMetricsService.class), resolver); + provisioner = new LogsMetricFilterCfnProvisioner(service); + engine = mock(CloudFormationTemplateEngine.class); + when(engine.resolve(any())).thenAnswer(i -> ((JsonNode) i.getArgument(0)).asText()); + when(engine.resolveNode(any())).thenAnswer(i -> i.getArgument(0)); + when(engine.resolveStringList(any())).thenCallRealMethod(); + } + + private static ObjectNode props() { + try { + return (ObjectNode) MAPPER.readTree(FULL); + } catch (Exception e) { + throw new AssertionError(e); + } + } + + private StackResource create(ObjectNode props) { + StackResource r = new StackResource(); + r.setLogicalId("Filter"); + r.setResourceType("AWS::Logs::MetricFilter"); + update(r, props); + return r; + } + + private void update(StackResource r, ObjectNode props) { + provisioner.provision(r, props, + new ProvisionContext(engine, REGION, "000000000000", "my-stack", r.getPhysicalId())); + } + + private MetricFilter filter(String group, String name) { + return service.findMetricFilter(group, name, REGION).orElseThrow(); + } + + private static JsonNode configuration(MetricFilter filter) { + ObjectNode node = MAPPER.valueToTree(filter); + node.remove("creationTime"); + return node; + } + + @Test + void createUsesNameOnlyPublicIdentityAndPreservesAllProperties() { + StackResource r = create(props()); + assertEquals("errors", r.getPhysicalId()); + assertEquals(GROUP, r.getAttributes().get(GROUP_ATTR)); + assertEquals("explicit", r.getAttributes().get(NAME_MODE)); + assertEquals(Set.of("AWS::Logs::MetricFilter"), provisioner.resourceTypes()); + MetricFilter f = filter(GROUP, "errors"); + assertEquals("{ $.level = \"ERROR\" }", f.getFilterPattern()); + assertEquals("2", f.getMetricTransformations().getFirst().getMetricValue()); + assertEquals(7.0, f.getMetricTransformations().getFirst().getDefaultValue()); + assertEquals("Count", f.getMetricTransformations().getFirst().getUnit()); + assertEquals(false, f.getApplyOnTransformedLogs()); + assertEquals(List.of("@aws.account"), f.getEmitSystemFieldDimensions()); + assertEquals("@aws.region = \"us-east-1\"", f.getFieldSelectionCriteria()); + } + + @Test + void generatedNameIsStableAcrossARealMutableUpdate() { + ObjectNode p = props(); + p.remove("FilterName"); + StackResource r = create(p); + String name = r.getPhysicalId(); + assertTrue(name.startsWith("my-stack-Filter-"), name); + assertEquals("generated", r.getAttributes().get(NAME_MODE)); + update(r, p.put("FilterPattern", "WARN")); + assertEquals(name, r.getPhysicalId()); + assertEquals("WARN", filter(GROUP, name).getFilterPattern()); + } + + @Test + void droppingAnExplicitNameDeletesItBeforeCreatingGeneratedName() { + StackResource r = create(props()); + ObjectNode p = props(); + p.remove("FilterName"); + store.operations.clear(); + update(r, p); + assertTrue(r.getPhysicalId().startsWith("my-stack-Filter-")); + assertEquals(List.of("delete:errors", "put:" + r.getPhysicalId()), store.operations); + } + + @Test + void replacementDeletesBeforeCreatingEvenWithPreviouslyInstalledUpdateReplaceRetain() { + StackResource r = create(props()); + r.setUpdateReplacePolicy("Retain"); + store.operations.clear(); + update(r, props().put("FilterName", "new")); + assertEquals(List.of("delete:errors", "put:new"), store.operations); + assertTrue(service.findMetricFilter(GROUP, "errors", REGION).isEmpty()); + assertFalse(provisioner.hasReplacementUpdate(r), "no post-commit deletion is owed"); + } + + @Test + void renameAtCapacityPreservesAllNinetyNineUnrelatedFilters() { + StackResource r = create(props()); + Map unrelated = new LinkedHashMap<>(); + for (int i = 0; i < 99; i++) { + String name = "unrelated-" + i; + create(props().put("FilterName", name)); + unrelated.put(name, MAPPER.valueToTree(filter(GROUP, name))); + } + assertDoesNotThrow(() -> update(r, props().put("FilterName", "renamed"))); + assertTrue(service.findMetricFilter(GROUP, "errors", REGION).isEmpty()); + assertNotNull(filter(GROUP, "renamed")); + assertEquals(100, store.keys().size()); + unrelated.forEach((name, expected) -> assertEquals(expected, MAPPER.valueToTree(filter(GROUP, name)))); + } + + @Test + void sameNameInAnotherGroupIsAReplacementAndDeleteOnlyUsesItsOwnGroup() { + StackResource r = create(props().put("FilterName", "same|name")); + StackResource other = create(props().put("LogGroupName", OTHER).put("FilterName", "same|name")); + provisioner.delete(other, REGION); + assertNotNull(filter(GROUP, "same|name")); + assertTrue(service.findMetricFilter(OTHER, "same|name", REGION).isEmpty()); + store.operations.clear(); + update(r, props().put("LogGroupName", OTHER).put("FilterName", "same|name")); + assertEquals(List.of("delete:same|name", "put:same|name"), store.operations); + assertEquals("same|name", r.getPhysicalId()); + assertTrue(service.findMetricFilter(GROUP, "same|name", REGION).isEmpty()); + provisioner.delete(r, REGION); + assertTrue(service.findMetricFilter(OTHER, "same|name", REGION).isEmpty()); + } + + @Test + void replacementPatternFailureDeletesOldThenRollbackRecreatesCompleteDefinition() { + StackResource r = create(props()); + MetricFilter old = filter(GROUP, "errors"); + old.setCreationTime(1L); + JsonNode expected = configuration(old); + Map attributes = Map.copyOf(r.getAttributes()); + store.operations.clear(); + assertThrows(AwsException.class, () -> update(r, props().put("FilterName", "bad").put("FilterPattern", "{"))); + assertEquals(List.of("delete:errors"), store.operations, "parsing failure happens after delete"); + assertTrue(service.findMetricFilter(GROUP, "errors", REGION).isEmpty()); + assertTrue(provisioner.rollbackUpdate(r)); + assertEquals(expected, configuration(filter(GROUP, "errors"))); + assertNotEquals(1L, filter(GROUP, "errors").getCreationTime()); + assertEquals("errors", r.getPhysicalId()); + assertEquals(attributes, r.getAttributes()); + assertTrue(service.findMetricFilter(GROUP, "bad", REGION).isEmpty()); + } + + @Test + void inPlaceRollbackRestoresEveryTransformationAndFlagWithoutRecreating() { + StackResource r = create(props()); + JsonNode expected = MAPPER.valueToTree(filter(GROUP, "errors")); + ObjectNode p = props().put("FilterPattern", "{ $.latency = * }").put("ApplyOnTransformedLogs", true); + p.remove(List.of("FieldSelectionCriteria", "EmitSystemFieldDimensions")); + ObjectNode t = (ObjectNode) p.path("MetricTransformations").get(0); + t.put("MetricName", "Latency").put("MetricNamespace", "Other").put("MetricValue", "$.latency"); + t.remove("DefaultValue"); + t.putArray("Dimensions").addObject().put("Key", "Route").put("Value", "$.route"); + update(r, p); + assertNotEquals(expected, MAPPER.valueToTree(filter(GROUP, "errors")), "mutation precedes rollback"); + assertTrue(provisioner.rollbackUpdate(r)); + assertEquals(expected, MAPPER.valueToTree(filter(GROUP, "errors"))); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void failedRestoreKeepsFullSnapshotForDeterministicRetry(boolean replacement) { + StackResource r = create(props()); + JsonNode expected = configuration(filter(GROUP, "errors")); + update(r, props().put("FilterName", replacement ? "new" : "errors").put("FilterPattern", "WARN")); + store.failBefore = "errors"; + assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r)); + assertTrue(r.getAttributes().containsKey(SNAPSHOT)); + store.failBefore = null; + assertTrue(provisioner.rollbackUpdate(r)); + assertEquals(expected, configuration(filter(GROUP, "errors"))); + assertFalse(r.getAttributes().containsKey(SNAPSHOT)); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void createWriteFailureTracksOnlyTheFilterActuallyWritten(boolean afterWrite) { + StackResource r = new StackResource(); + r.setLogicalId("Filter"); + r.setResourceType("AWS::Logs::MetricFilter"); + if (afterWrite) { + store.failAfter = "errors"; + } else { + store.failBefore = "errors"; + } + assertThrows(AwsException.class, () -> update(r, props())); + assertEquals(afterWrite ? "errors" : null, r.getPhysicalId()); + assertEquals(afterWrite, "true".equals(r.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR))); + provisioner.delete(r, REGION); + assertTrue(store.keys().isEmpty()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void anUninspectableCreateOutcomeCannotClaimSuccessfulRollbackOrDelete(boolean replacement) { + StackResource r = replacement ? create(props()) : new StackResource(); + r.setLogicalId("Filter"); + r.setResourceType("AWS::Logs::MetricFilter"); + store.failAfter = "bad"; + store.failInspection = true; + assertThrows(AwsException.class, () -> update(r, props().put("FilterName", "bad"))); + store.failReads = false; + assertNotNull(filter(GROUP, "bad")); + if (replacement) { + assertThrows(IllegalStateException.class, () -> provisioner.rollbackUpdate(r)); + assertTrue(r.getAttributes().containsKey(SNAPSHOT)); + } + assertThrows(IllegalStateException.class, () -> provisioner.delete(r, REGION)); + assertNotNull(filter(GROUP, "bad"), "unknown ownership must not be guessed"); + service.deleteMetricFilter(GROUP, "bad", REGION); + if (replacement) { + assertTrue(provisioner.rollbackUpdate(r)); + assertNotNull(filter(GROUP, "errors")); + } else { + assertDoesNotThrow(() -> provisioner.delete(r, REGION)); + } + } + + @Test + void anUninspectableRestorationRemainsVisibleToStackDeletion() { + StackResource r = create(props()); + update(r, props().put("FilterName", "new")); + store.failAfter = "errors"; + store.failInspection = true; + assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r)); + store.failReads = false; + assertThrows(IllegalStateException.class, () -> provisioner.delete(r, REGION)); + assertNotNull(filter(GROUP, "errors")); + assertTrue(r.getAttributes().containsKey(SNAPSHOT)); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void rollbackOfAnAlreadyAbsentPriorFilterRestoresAbsence(boolean replacement) { + StackResource r = create(props()); + service.deleteMetricFilter(GROUP, "errors", REGION); + update(r, props().put("FilterName", replacement ? "new" : "errors")); + assertTrue(provisioner.rollbackUpdate(r)); + assertTrue(store.keys().isEmpty()); + assertEquals("errors", r.getPhysicalId()); + create(props().put("FilterPattern", "UNRELATED")); + JsonNode otherOwner = MAPPER.valueToTree(filter(GROUP, "errors")); + assertEquals("AlreadyExistsException", assertThrows(AwsException.class, + () -> update(r, props().put("FilterPattern", "WARN"))).getErrorCode()); + assertTrue(provisioner.rollbackUpdate(r)); + provisioner.delete(r, REGION); + assertEquals(otherOwner, service.findMetricFilter(GROUP, "errors", REGION) + .map(MAPPER::valueToTree).orElse(null)); + } + + @ParameterizedTest + @ValueSource(strings = {"delete", "update", "replacement"}) + void absentPriorCollisionCannotBecomeOwnedAfterRollback(String laterOperation) throws Exception { + StackResource originalOwner = create(props()); + service.deleteMetricFilter(GROUP, "errors", REGION); + service = spy(service); + provisioner = new LogsMetricFilterCfnProvisioner(service); + CountDownLatch snapshotRead = new CountDownLatch(1); + CountDownLatch otherOwnerCreated = new CountDownLatch(1); + doAnswer(invocation -> { + Optional prior = (Optional) invocation.callRealMethod(); + assertTrue(prior.isEmpty(), "A snapshots the externally deleted filter as absent"); + snapshotRead.countDown(); + assertTrue(otherOwnerCreated.await(5, TimeUnit.SECONDS)); + return prior; + }).doCallRealMethod().when(service).findMetricFilter(GROUP, "errors", REGION); + JsonNode expected; + try (ExecutorService executor = Executors.newSingleThreadExecutor()) { + Future updateA = executor.submit(() -> assertThrows(AwsException.class, + () -> update(originalOwner, props().put("FilterPattern", "WARN")))); + try { + assertTrue(snapshotRead.await(5, TimeUnit.SECONDS)); + create(props().put("FilterPattern", "UNRELATED")); + expected = MAPPER.valueToTree(filter(GROUP, "errors")); + } finally { + otherOwnerCreated.countDown(); + } + assertEquals("AlreadyExistsException", updateA.get(5, TimeUnit.SECONDS).getErrorCode()); + } + JsonNode snapshot = MAPPER.readTree(originalOwner.getAttributes().get(SNAPSHOT)); + assertTrue(snapshot.path("absent").asBoolean()); + assertFalse(snapshot.path("replacement").asBoolean()); + assertEquals("UNOWNED", snapshot.path("prior").path("ownership").asText()); + assertTrue(provisioner.rollbackUpdate(originalOwner)); + assertEquals(expected, MAPPER.valueToTree(filter(GROUP, "errors"))); + originalOwner.setStatus("UPDATE_COMPLETE"); + provisioner.completeUpdate(originalOwner); + provisioner.clearUpdate(originalOwner); + + switch (laterOperation) { + case "delete" -> provisioner.delete(originalOwner, REGION); + case "update" -> { + assertEquals("AlreadyExistsException", assertThrows(AwsException.class, + () -> update(originalOwner, props().put("FilterPattern", "FATAL"))).getErrorCode()); + assertTrue(provisioner.rollbackUpdate(originalOwner)); + provisioner.delete(originalOwner, REGION); + } + case "replacement" -> { + update(originalOwner, props().put("FilterName", "new").put("FilterPattern", "WARN")); + assertEquals("WARN", filter(GROUP, "new").getFilterPattern()); + provisioner.completeUpdate(originalOwner); + assertFalse(originalOwner.getAttributes().containsKey(SNAPSHOT)); + provisioner.delete(originalOwner, REGION); + assertTrue(service.findMetricFilter(GROUP, "new", REGION).isEmpty()); + } + default -> throw new AssertionError(laterOperation); + } + assertEquals(expected, service.findMetricFilter(GROUP, "errors", REGION) + .map(MAPPER::valueToTree).orElse(null), "B's complete backing definition must remain untouched"); + assertEquals(1, store.keys().size()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void rollbackDoesNotUpsertOrDeleteACollidingUnownedTarget(boolean replacement) { + create(props().put("FilterName", "taken").put("FilterPattern", "UNRELATED")); + JsonNode unrelated = MAPPER.valueToTree(filter(GROUP, "taken")); + StackResource r = replacement ? create(props()) : new StackResource(); + if (!replacement) { + r.setLogicalId("Filter"); + r.setResourceType("AWS::Logs::MetricFilter"); + } + assertThrows(AwsException.class, () -> update(r, props().put("FilterName", "taken"))); + assertTrue(provisioner.rollbackUpdate(r)); + assertEquals(unrelated, MAPPER.valueToTree(filter(GROUP, "taken"))); + if (replacement) { + assertNotNull(filter(GROUP, "errors")); + } else { + assertNull(r.getPhysicalId()); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void replacementWriteFailureBeforeOrAfterStorageIsRecoverable(boolean afterWrite) { + StackResource r = create(props()); + JsonNode expected = configuration(filter(GROUP, "errors")); + if (afterWrite) { + store.failAfter = "new"; + } else { + store.failBefore = "new"; + } + assertThrows(AwsException.class, () -> update(r, props().put("FilterName", "new"))); + assertTrue(provisioner.rollbackUpdate(r)); + assertTrue(service.findMetricFilter(GROUP, "new", REGION).isEmpty()); + assertEquals(expected, configuration(filter(GROUP, "errors"))); + } + + @Test + void deleteToleratesOnlyAlreadyGoneErrors() { + StackResource r = create(props()); + store.failDelete = true; + assertThrows(AwsException.class, () -> provisioner.delete(r, REGION)); + store.failDelete = false; + provisioner.delete(r, REGION); + assertDoesNotThrow(() -> provisioner.delete(r, REGION)); + logs.deleteLogGroup(GROUP, REGION); + assertDoesNotThrow(() -> provisioner.delete(r, REGION)); + } + + @Test + void emptyPatternIsValidAndCommittedUpdateDropsSnapshot() { + StackResource r = create(props().put("FilterPattern", "")); + update(r, props()); + assertTrue(r.getAttributes().containsKey(SNAPSHOT)); + provisioner.clearUpdate(r); + assertFalse(r.getAttributes().containsKey(SNAPSHOT)); + store.operations.clear(); + assertTrue(provisioner.rollbackUpdate(r)); + assertTrue(store.operations.isEmpty()); + } + + @ParameterizedTest + @ValueSource(strings = {"LogGroupName", "FilterPattern", "MetricTransformations"}) + void eachRequiredPropertyIsValidatedBeforeMutation(String missing) { + ObjectNode p = props(); + p.remove(missing); + AwsException e = assertThrows(AwsException.class, () -> create(p)); + assertEquals("ValidationError", e.getErrorCode()); + assertTrue(store.operations.isEmpty()); + } + + @ParameterizedTest + @ValueSource(strings = {"MetricName", "MetricNamespace", "MetricValue"}) + void eachRequiredTransformationMemberIsValidatedByCfn(String missing) { + ObjectNode p = props(); + ((ObjectNode) p.path("MetricTransformations").get(0)).remove(missing); + AwsException e = assertThrows(AwsException.class, () -> create(p)); + assertEquals("ValidationError", e.getErrorCode()); + assertTrue(store.operations.isEmpty()); + } + + @ParameterizedTest + @ValueSource(strings = {"{}", "[]", "[{},{}]", "null", "\"wrong\""}) + void transformationMustBeExactlyOneObject(String json) throws Exception { + ObjectNode p = props(); + p.set("MetricTransformations", MAPPER.readTree(json)); + assertEquals("ValidationError", assertThrows(AwsException.class, () -> create(p)).getErrorCode()); + } + + @ParameterizedTest + @ValueSource(strings = {"{}", "[]", "[{}]", "[{\"Key\":\"K\"}]", "[{\"Value\":\"$.v\"}]", + "[{\"Key\":\"é\",\"Value\":\"$.v\"}]", "[{\"Key\":\":K\",\"Value\":\"$.v\"}]", + "[{\"Key\":\" \",\"Value\":\"$.v\"}]", "[{\"Key\":\"K\",\"Value\":\"\"}]", + "[{\"Key\":\"K\",\"Value\":\"$.v\"},{\"Key\":\"K\",\"Value\":\"$.v\"}]", + "[{\"Key\":\"A\",\"Value\":\"$.v\"},{\"Key\":\"B\",\"Value\":\"$.v\"},{\"Key\":\"C\",\"Value\":\"$.v\"},{\"Key\":\"D\",\"Value\":\"$.v\"}]"}) + void cfnDimensionsAreStrictKeyValueArrays(String json) throws Exception { + ObjectNode p = props(); + ObjectNode t = (ObjectNode) p.path("MetricTransformations").get(0); + t.remove("DefaultValue"); + t.set("Dimensions", MAPPER.readTree(json)); + assertEquals("ValidationError", assertThrows(AwsException.class, () -> create(p)).getErrorCode()); + assertTrue(store.operations.isEmpty()); + } + + @ParameterizedTest + @ValueSource(strings = {"NaN", "Infinity", "-Infinity", "1e999", "garbage"}) + void nonfiniteOrInvalidDefaultIsRejectedByCfn(String value) { + ObjectNode p = props(); + ((ObjectNode) p.path("MetricTransformations").get(0)).put("DefaultValue", value); + assertEquals("ValidationError", assertThrows(AwsException.class, () -> create(p)).getErrorCode()); + } + + @ParameterizedTest + @ValueSource(strings = {"garbage", "1", "TRUE", ""}) + void booleansDoNotSilentlyCoerceGarbageToFalse(String value) { + assertEquals("ValidationError", + assertThrows(AwsException.class, () -> create(props().put("ApplyOnTransformedLogs", value))).getErrorCode()); + } + + @ParameterizedTest + @ValueSource(strings = {"true", "false"}) + void resolvedCfnBooleanStringsAreAccepted(String value) { + create(props().put("ApplyOnTransformedLogs", value)); + assertEquals(Boolean.valueOf(value), filter(GROUP, "errors").getApplyOnTransformedLogs()); + } + + @Test + void cfnNamespace256PassesModelButLogsProviderStillRejectsIts255Limit() { + ObjectNode p = props(); + ((ObjectNode) p.path("MetricTransformations").get(0)).put("MetricNamespace", "A".repeat(256)); + assertEquals("InvalidParameterException", assertThrows(AwsException.class, () -> create(p)).getErrorCode()); + ((ObjectNode) p.path("MetricTransformations").get(0)).put("MetricNamespace", "A".repeat(257)); + assertEquals("ValidationError", assertThrows(AwsException.class, () -> create(p)).getErrorCode()); + } + + @ParameterizedTest + @ValueSource(strings = {"Unit", "UnknownTransformationProperty", "HexadecimalDefault"}) + void transformationModelErrorsAreRejectedBeforeReplacementDeletesAnything(String invalid) { + StackResource r = create(props()); + ObjectNode p = props().put("FilterName", "new"); + ObjectNode t = (ObjectNode) p.path("MetricTransformations").get(0); + switch (invalid) { + case "Unit" -> t.put("Unit", "Bananas"); + case "HexadecimalDefault" -> t.put("DefaultValue", "0x1.0p2"); + default -> t.put(invalid, "wrong"); + } + store.operations.clear(); + assertEquals("ValidationError", assertThrows(AwsException.class, () -> update(r, p)).getErrorCode()); + assertTrue(store.operations.isEmpty(), store.operations.toString()); + assertNotNull(filter(GROUP, "errors")); + } + + @Test + void priorDimensionsAreRestoredWhenUpdateRemovedThem() { + ObjectNode before = props().put("FilterPattern", "{ $.level = * }"); + ObjectNode t = (ObjectNode) before.path("MetricTransformations").get(0); + t.remove("DefaultValue"); + t.putArray("Dimensions").addObject().put("Key", "Level").put("Value", "$.level"); + StackResource r = create(before); + JsonNode expected = MAPPER.valueToTree(filter(GROUP, "errors")); + update(r, props().put("FilterPattern", "WARN")); + assertTrue(filter(GROUP, "errors").getMetricTransformations().getFirst().getDimensions().isEmpty()); + provisioner.rollbackUpdate(r); + assertEquals(expected, MAPPER.valueToTree(filter(GROUP, "errors"))); + } + + @Test + void restoreFailureAfterWriteRemainsRetryableWithoutAdoptingAnotherFilter() { + StackResource r = create(props()); + JsonNode expected = configuration(filter(GROUP, "errors")); + update(r, props().put("FilterName", "new")); + store.failAfter = "errors"; + assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r)); + assertTrue(r.getAttributes().containsKey(SNAPSHOT)); + store.failAfter = null; + assertTrue(provisioner.rollbackUpdate(r)); + assertEquals(expected, configuration(filter(GROUP, "errors"))); + assertEquals(1, store.keys().size()); + } + + @Test + void stackDeletionAfterFailedCollisionRollbackCannotDeleteTheUnownedTarget() { + create(props().put("FilterName", "taken")); + StackResource r = create(props()); + assertThrows(AwsException.class, () -> update(r, props().put("FilterName", "taken"))); + store.failBefore = "errors"; + assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r)); + JsonNode unrelated = MAPPER.valueToTree(filter(GROUP, "taken")); + provisioner.delete(r, REGION); + assertEquals(unrelated, service.findMetricFilter(GROUP, "taken", REGION) + .map(MAPPER::valueToTree).orElse(null)); + } + + @Test + void stackDeletionAfterAnAcknowledgedRestoreWriteFailureRemovesTheOwnedRestoration() { + StackResource r = create(props()); + update(r, props().put("FilterName", "new")); + store.failAfter = "errors"; + assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r)); + assertNotNull(filter(GROUP, "errors")); + provisioner.delete(r, REGION); + assertTrue(store.keys().isEmpty(), "delete cannot orphan the restoration in its retry snapshot"); + } + + @ParameterizedTest + @ValueSource(strings = {"Resource", "Dimension"}) + void unknownModelPropertiesAreRejectedBeforeMutation(String scope) { + ObjectNode p = props(); + if ("Resource".equals(scope)) { + p.put("Unknown", "value"); + } else { + ObjectNode t = (ObjectNode) p.path("MetricTransformations").get(0); + t.remove("DefaultValue"); + t.putArray("Dimensions").addObject().put("Key", "Level").put("Value", "$.level").put("Unknown", "value"); + } + assertEquals("ValidationError", assertThrows(AwsException.class, () -> create(p)).getErrorCode()); + assertTrue(store.keys().isEmpty()); + } + + @Test + void retainsFailedUpdateStateOnlyWhileAnUpdateSnapshotIsHeld() { + StackResource r = create(props()); + assertFalse(provisioner.retainsFailedUpdateState(r), "a committed create keeps nothing to roll back"); + store.failBefore = "new"; + assertThrows(AwsException.class, () -> update(r, props().put("FilterName", "new"))); + assertTrue(r.getAttributes().containsKey(SNAPSHOT)); + assertTrue(provisioner.retainsFailedUpdateState(r), "the failed attempt owns the snapshot the rollback needs"); + store.failBefore = null; + assertTrue(provisioner.rollbackUpdate(r)); + assertFalse(r.getAttributes().containsKey(SNAPSHOT)); + assertFalse(provisioner.retainsFailedUpdateState(r)); + } + + @Test + void concurrentNamedCreatesHaveExactlyOneOwner() throws Exception { + CountDownLatch start = new CountDownLatch(1); + try (ExecutorService executor = Executors.newFixedThreadPool(2)) { + Future first = executor.submit(() -> concurrentCreate(start)); + Future second = executor.submit(() -> concurrentCreate(start)); + start.countDown(); + assertEquals(1, first.get(5, TimeUnit.SECONDS) + + second.get(5, TimeUnit.SECONDS)); + } + assertEquals(List.of("put:errors"), store.operations); + assertEquals(1, store.keys().size()); + } + + private int concurrentCreate(CountDownLatch start) throws InterruptedException { + start.await(); + try { + create(props()); + return 1; + } catch (AwsException e) { + assertEquals("AlreadyExistsException", e.getErrorCode()); + return 0; + } + } + + @Test + void forwardDeleteAfterEffectCannotRestoreOverAConcurrentCreator() throws Exception { + StackResource original = create(props()); + CountDownLatch deleted = new CountDownLatch(1); + store.deleteFailureName = "errors"; + store.deleteFault = MutationFault.AFTER; + store.afterDeleteEffect = deleted::countDown; + JsonNode otherOwner; + boolean inspectedWhileLocked; + try (ExecutorService executor = Executors.newSingleThreadExecutor()) { + Future creator = executor.submit(() -> { + assertTrue(deleted.await(5, TimeUnit.SECONDS)); + create(props().put("FilterPattern", "UNRELATED")); + return null; + }); + try { + assertThrows(AwsException.class, () -> update(original, props().put("FilterName", "new"))); + } finally { + deleted.countDown(); + } + creator.get(5, TimeUnit.SECONDS); + inspectedWhileLocked = store.inspectionHeldMonitor; + otherOwner = MAPPER.valueToTree(filter(GROUP, "errors")); + } + store.clearMutationFaults(); + AwsException collision = null; + try { + provisioner.rollbackUpdate(original); + } catch (AwsException e) { + assertEquals("AlreadyExistsException", e.getErrorCode()); + collision = e; + } + assertEquals(otherOwner, MAPPER.valueToTree(filter(GROUP, "errors"))); + assertNotNull(collision, "restoration must use create-if-absent rather than adopt B"); + assertTrue(original.getAttributes().containsKey(SNAPSHOT)); + provisioner.delete(original, REGION); + assertEquals(otherOwner, service.findMetricFilter(GROUP, "errors", REGION) + .map(MAPPER::valueToTree).orElse(null)); + assertTrue(inspectedWhileLocked, "delete outcome inspection must precede releasing the canonical lock"); + } + + private static Stream deleteFaultMatrix() { + return Stream.of("forward", "rollback", "stack") + .flatMap(phase -> Arrays.stream(MutationFault.values()) + .map(fault -> Arguments.of(phase, fault))); + } + + @ParameterizedTest + @MethodSource("deleteFaultMatrix") + void deleteOutcomesControlEveryRetryWithoutAdoptingAnotherOwner(String phase, MutationFault fault) { + StackResource resource = create(props()); + String address = "rollback".equals(phase) ? "new" : "errors"; + if ("rollback".equals(phase)) { + update(resource, props().put("FilterName", "new")); + } + store.deleteFailureName = address; + store.deleteFault = fault; + assertThrows(AwsException.class, () -> { + switch (phase) { + case "forward" -> update(resource, props().put("FilterName", "new")); + case "rollback" -> provisioner.rollbackUpdate(resource); + case "stack" -> provisioner.delete(resource, REGION); + default -> throw new AssertionError(phase); + } + }); + assertTrue(store.inspectionHeldMonitor, phase + "/" + fault + " must inspect under the canonical monitor"); + ObjectNode recorded = mutationState(resource, "rollback".equals(phase)); + assertEquals("DELETE", recorded.path("operation").asText()); + assertEquals(fault.unknown() ? "UNKNOWN" : fault.after() ? "APPLIED" : "NOT_APPLIED", + recorded.path("outcome").asText()); + assertEquals(fault.unknown() ? "UNKNOWN" : fault.after() ? "UNOWNED" : "OWNED", + recorded.path("ownership").asText()); + store.clearMutationFaults(); + + if (fault == MutationFault.BEFORE) { + assertNotNull(filter(GROUP, address)); + resumeAfterDeleteFailure(phase, resource); + if (!"stack".equals(phase)) { + assertEquals("{ $.level = \"ERROR\" }", filter(GROUP, "errors").getFilterPattern()); + provisioner.delete(resource, REGION); + } + assertTrue(store.keys().isEmpty()); + return; + } + if (service.findMetricFilter(GROUP, address, REGION).isPresent()) { + service.deleteMetricFilter(GROUP, address, REGION); + } + create(props().put("FilterName", address).put("FilterPattern", "UNRELATED")); + JsonNode otherOwner = MAPPER.valueToTree(filter(GROUP, address)); + if (fault.unknown()) { + assertThrows(IllegalStateException.class, () -> resumeAfterDeleteFailure(phase, resource)); + assertThrows(IllegalStateException.class, () -> provisioner.delete(resource, REGION)); + assertEquals(otherOwner, MAPPER.valueToTree(filter(GROUP, address))); + service.deleteMetricFilter(GROUP, address, REGION); + resumeAfterDeleteFailure(phase, resource); + if (!"stack".equals(phase)) { + assertEquals("{ $.level = \"ERROR\" }", filter(GROUP, "errors").getFilterPattern()); + provisioner.delete(resource, REGION); + } + assertTrue(store.keys().isEmpty()); + } else { + if ("forward".equals(phase)) { + assertEquals("AlreadyExistsException", assertThrows(AwsException.class, + () -> provisioner.rollbackUpdate(resource)).getErrorCode()); + } else { + resumeAfterDeleteFailure(phase, resource); + } + provisioner.delete(resource, REGION); + assertEquals(otherOwner, service.findMetricFilter(GROUP, address, REGION) + .map(MAPPER::valueToTree).orElse(null), "known applied deletes must relinquish ownership before retry"); + assertEquals(1, store.keys().size()); + } + } + + private void resumeAfterDeleteFailure(String phase, StackResource resource) { + if ("stack".equals(phase)) { + provisioner.delete(resource, REGION); + } else { + assertTrue(provisioner.rollbackUpdate(resource)); + } + } + + private static Stream writeFaultMatrix() { + return Stream.of("create", "update", "restore", "restoreUpdate") + .flatMap(phase -> Arrays.stream(MutationFault.values()) + .map(fault -> Arguments.of(phase, fault))); + } + + @ParameterizedTest + @MethodSource("writeFaultMatrix") + void createUpdateAndRestoreUseTheSameMutationOutcomeRules(String phase, MutationFault fault) { + StackResource resource; + if ("create".equals(phase)) { + resource = new StackResource(); + resource.setLogicalId("Filter"); + resource.setResourceType("AWS::Logs::MetricFilter"); + } else { + resource = create(props()); + } + if ("restore".equals(phase)) { + update(resource, props().put("FilterName", "new")); + } else if ("restoreUpdate".equals(phase)) { + update(resource, props().put("FilterPattern", "WARN")); + } + store.failInspection = fault.unknown(); + if (fault.after()) { + store.failAfter = "errors"; + } else { + store.failBefore = "errors"; + } + assertThrows(AwsException.class, () -> { + if ("restore".equals(phase) || "restoreUpdate".equals(phase)) { + provisioner.rollbackUpdate(resource); + } else { + update(resource, props().put("FilterPattern", "WARN")); + } + }); + assertTrue(store.inspectionHeldMonitor, phase + "/" + fault + " must inspect under the canonical monitor"); + ObjectNode recorded = mutationState(resource, false); + boolean ownedUpdate = "update".equals(phase) || "restoreUpdate".equals(phase); + assertEquals(ownedUpdate ? "UPDATE" : "CREATE", recorded.path("operation").asText()); + assertEquals(fault.unknown() ? "UNKNOWN" : fault.after() ? "APPLIED" : "NOT_APPLIED", + recorded.path("outcome").asText()); + assertEquals(fault.unknown() ? "UNKNOWN" : fault.after() || ownedUpdate ? "OWNED" : "UNOWNED", + recorded.path("ownership").asText()); + store.clearMutationFaults(); + if (fault.unknown()) { + if (service.findMetricFilter(GROUP, "errors", REGION).isPresent()) { + service.deleteMetricFilter(GROUP, "errors", REGION); + } + create(props().put("FilterPattern", "UNRELATED")); + JsonNode otherOwner = MAPPER.valueToTree(filter(GROUP, "errors")); + if (!"create".equals(phase)) { + assertThrows(IllegalStateException.class, () -> provisioner.rollbackUpdate(resource)); + } + assertThrows(IllegalStateException.class, () -> provisioner.delete(resource, REGION)); + assertEquals(otherOwner, MAPPER.valueToTree(filter(GROUP, "errors"))); + service.deleteMetricFilter(GROUP, "errors", REGION); + } + if (!"create".equals(phase)) { + assertTrue(provisioner.rollbackUpdate(resource)); + assertEquals("{ $.level = \"ERROR\" }", filter(GROUP, "errors").getFilterPattern()); + } + provisioner.delete(resource, REGION); + assertTrue(store.keys().isEmpty()); + } + + @ParameterizedTest + @ValueSource(strings = {"success", "before", "after", "unknown"}) + void outcomeCallbackFailureNeverChangesStorageEvidence(String storageResult) { + create(props()); + MetricFilter definition = filter(GROUP, "errors"); + service.deleteMetricFilter(GROUP, "errors", REGION); + if ("before".equals(storageResult)) { + store.failBefore = "errors"; + } else if (!"success".equals(storageResult)) { + store.failAfter = "errors"; + } + store.failInspection = "unknown".equals(storageResult); + List recorded = new ArrayList<>(); + IllegalStateException callbackFailure = new IllegalStateException("outcome metadata recording failed"); + RuntimeException failure = assertThrows(RuntimeException.class, () -> service.createMetricFilter( + definition, REGION, result -> { + assertTrue(Thread.holdsLock(canonicalStore)); + recorded.add(result.outcome()); + throw callbackFailure; + })); + assertEquals(List.of(switch (storageResult) { + case "before" -> MutationOutcome.NOT_APPLIED; + case "unknown" -> MutationOutcome.UNKNOWN; + default -> MutationOutcome.APPLIED; + }), recorded, "a callback exception must not reclassify or repeat storage evidence"); + if (!"success".equals(storageResult)) { + assertSame(store.lastMutationFailure, failure); + assertTrue(List.of(failure.getSuppressed()).contains(callbackFailure)); + } else { + assertSame(callbackFailure, failure); + } + store.clearMutationFaults(); + assertEquals(!"before".equals(storageResult), service.findMetricFilter(GROUP, "errors", REGION).isPresent()); + } + + @Test + void reusedMutationAndInspectionExceptionStillReportsUnknownExactlyOnce() { + create(props()); + MetricFilter definition = filter(GROUP, "errors"); + service.deleteMetricFilter(GROUP, "errors", REGION); + store.failAfter = "errors"; + store.failInspection = true; + store.reuseMutationFailure = true; + List outcomes = new ArrayList<>(); + RuntimeException failure = assertThrows(RuntimeException.class, + () -> service.createMetricFilter(definition, REGION, result -> { + assertTrue(Thread.holdsLock(canonicalStore)); + assertNull(result.present()); + outcomes.add(result.outcome()); + })); + assertAll( + () -> assertSame(store.lastMutationFailure, failure), + () -> assertEquals(List.of(MutationOutcome.UNKNOWN), outcomes), + () -> assertEquals(0, failure.getSuppressed().length)); + } + + private static ObjectNode mutationState(StackResource resource, boolean target) { + try { + String snapshot = resource.getAttributes().get(SNAPSHOT); + if (snapshot == null) { + return (ObjectNode) MAPPER.readTree(resource.getAttributes().get(STATE)); + } + JsonNode root = MAPPER.readTree(snapshot); + return (ObjectNode) root.get(target && root.path("replacement").asBoolean() ? "target" : "prior"); + } catch (Exception e) { + throw new AssertionError("Invalid mutation evidence", e); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void failedOutcomeRecordingLeavesAConservativeStateForCleanup(boolean storageAlsoFailed) { + IllegalStateException recordingFailure = new IllegalStateException("metadata sink unavailable"); + AtomicBoolean rejecting = new AtomicBoolean(true); + StackResource resource = new StackResource(); + resource.setLogicalId("Filter"); + resource.setResourceType("AWS::Logs::MetricFilter"); + resource.setAttributes(new HashMap<>() { + @Override + public String put(String key, String value) { + if (rejecting.get() && STATE.equals(key) && value.contains("\"outcome\":\"APPLIED\"")) { + throw recordingFailure; + } + return super.put(key, value); + } + }); + if (storageAlsoFailed) { + store.failAfter = "errors"; + } + RuntimeException failure = assertThrows(RuntimeException.class, () -> update(resource, props())); + assertSame(storageAlsoFailed ? store.lastMutationFailure : recordingFailure, failure); + assertEquals("UNKNOWN", mutationState(resource, false).path("ownership").asText()); + assertEquals("errors", resource.getPhysicalId(), "failed metadata recording must still reach stack cleanup"); + assertEquals("true", resource.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR)); + rejecting.set(false); + store.clearMutationFaults(); + assertThrows(IllegalStateException.class, () -> provisioner.delete(resource, REGION)); + assertNotNull(filter(GROUP, "errors")); + service.deleteMetricFilter(GROUP, "errors", REGION); + assertDoesNotThrow(() -> provisioner.delete(resource, REGION)); + assertEquals("UNOWNED", mutationState(resource, false).path("ownership").asText()); + } + + @ParameterizedTest + @ValueSource(strings = {"DELETE_FAILED", "UPDATE_FAILED"}) + void anUntrackedFailedResourceCannotDeleteAnotherOwner(String status) { + StackResource resource = create(props()); + resource.getAttributes().remove(STATE); + resource.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR); + resource.setStatus(status); + service.deleteMetricFilter(GROUP, "errors", REGION); + create(props().put("FilterPattern", "UNRELATED")); + JsonNode otherOwner = MAPPER.valueToTree(filter(GROUP, "errors")); + assertTrue(provisioner.hasPendingRollbackCleanup(resource), "untracked identity must reach safe cleanup inspection"); + assertThrows(IllegalStateException.class, () -> provisioner.delete(resource, REGION)); + assertEquals("UNKNOWN", mutationState(resource, false).path("ownership").asText()); + assertEquals(otherOwner, MAPPER.valueToTree(filter(GROUP, "errors"))); + service.deleteMetricFilter(GROUP, "errors", REGION); + assertDoesNotThrow(() -> provisioner.delete(resource, REGION)); + assertEquals("UNOWNED", mutationState(resource, false).path("ownership").asText()); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = {"CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS", "unrecognized"}) + void missingOrUnprovenStatusWithoutTrackingCannotAuthorizeAnUpdate(String status) { + StackResource resource = create(props()); + resource.getAttributes().remove(STATE); + resource.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR); + resource.setStatus(status); + service.deleteMetricFilter(GROUP, "errors", REGION); + create(props().put("FilterPattern", "UNRELATED")); + JsonNode otherOwner = MAPPER.valueToTree(filter(GROUP, "errors")); + assertThrows(IllegalStateException.class, () -> update(resource, props().put("FilterPattern", "FATAL"))); + assertEquals("UNKNOWN", mutationState(resource, false).path("ownership").asText()); + assertThrows(IllegalStateException.class, () -> provisioner.delete(resource, REGION)); + assertEquals(otherOwner, MAPPER.valueToTree(filter(GROUP, "errors"))); + } + + private enum MutationFault { + BEFORE, AFTER, UNKNOWN_BEFORE, UNKNOWN_AFTER; + + boolean after() { + return name().endsWith("AFTER"); + } + + boolean unknown() { + return name().startsWith("UNKNOWN"); + } + } + + private static final class RecordingStore extends InMemoryStorage { + final List operations = new ArrayList<>(); + String failBefore; + String failAfter; + boolean failDelete; + boolean failInspection; + boolean failReads; + boolean reuseMutationFailure; + Object monitor; + Thread mutationThread; + boolean inspectionHeldMonitor; + RuntimeException lastMutationFailure; + String deleteFailureName; + MutationFault deleteFault; + Runnable afterDeleteEffect = () -> {}; + + void clearMutationFaults() { + failBefore = null; + failAfter = null; + failDelete = false; + failInspection = false; + failReads = false; + reuseMutationFailure = false; + deleteFailureName = null; + deleteFault = null; + mutationThread = null; + afterDeleteEffect = () -> {}; + } + + private AwsException mutationFailure(String message, boolean unknown) { + mutationThread = Thread.currentThread(); + inspectionHeldMonitor = false; + failReads = unknown; + AwsException failure = new AwsException("ServiceUnavailableException", message, 500); + lastMutationFailure = failure; + return failure; + } + + @Override + public void put(String key, MetricFilter value) { + if (value.getFilterName().equals(failBefore)) { + throw mutationFailure("before write", failInspection); + } + operations.add("put:" + value.getFilterName()); + super.put(key, value); + if (value.getFilterName().equals(failAfter)) { + throw mutationFailure("after write", failInspection); + } + } + + @Override + public Optional get(String key) { + if (Thread.currentThread() == mutationThread) { + inspectionHeldMonitor = Thread.holdsLock(monitor); + mutationThread = null; + } + if (failReads) { + if (reuseMutationFailure) { + throw lastMutationFailure; + } + throw new AwsException("ServiceUnavailableException", "ownership inspection unavailable", 500); + } + return super.get(key); + } + + @Override + public void delete(String key) { + if (failDelete) { + throw new AwsException("ServiceUnavailableException", "delete failed", 500); + } + MetricFilter before = super.get(key).orElse(null); + boolean inject = before != null && before.getFilterName().equals(deleteFailureName); + if (inject && !deleteFault.after()) { + throw mutationFailure("before delete", deleteFault.unknown()); + } + get(key).ifPresent(f -> operations.add("delete:" + f.getFilterName())); + super.delete(key); + if (inject) { + afterDeleteEffect.run(); + throw mutationFailure("after delete", deleteFault.unknown()); + } + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsFilterQuotaTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsFilterQuotaTest.java new file mode 100644 index 0000000000..19564d2372 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsFilterQuotaTest.java @@ -0,0 +1,380 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.RequestContext; +import io.github.hectorvent.floci.core.common.ServiceConfigAccess; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.SubscriptionFilter; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import jakarta.enterprise.inject.Instance; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +class CloudWatchLogsFilterQuotaTest { + + private static final String ACCOUNT = "000000000000"; + private static final String REGION = "us-east-1"; + private static final String GROUP = "/quota"; + private static final String TWO_REGEXES = "{ $.a = %ERROR% || $.a = %WARN% }"; + + @ParameterizedTest + @CsvSource({"false,true", "false,false", "true,true", "true,false"}) + void fiveRegexBearingFiltersCanEachHoldTwoExpressions(boolean factoryConstructor, boolean metric) { + Fixture fixture = new Fixture(factoryConstructor); + for (int i = 0; i < 5; i++) { + fixture.put(metric, "f" + i, TWO_REGEXES); + } + rejected(() -> fixture.put(metric, "sixth", "%INFO%"), "LimitExceededException"); + fixture.put(metric, "f0", TWO_REGEXES); + fixture.put(metric, "plain", "INFO"); + assertEquals(6, metric ? fixture.metricStore.keys().size() : fixture.subscriptions.keys().size()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void bothConstructorPathsShareThePoolAcrossFilterFamilies(boolean factoryConstructor) { + Fixture fixture = new Fixture(factoryConstructor); + for (int i = 0; i < 4; i++) { + fixture.put(true, "m" + i, "%ERROR%"); + } + fixture.put(false, "s0", "%ERROR%"); + rejected(() -> fixture.put(false, "s1", "%WARN%"), "LimitExceededException"); + rejected(() -> fixture.put(true, "m4", "%WARN%"), "LimitExceededException"); + } + + @Test + void replacementsAreFamilySpecificAndRejectedUpdatesLeaveTheStoredDefinitionUnchanged() { + Fixture fixture = new Fixture(false); + for (int i = 0; i < 3; i++) { + fixture.put(true, "m" + i, "%ERROR%"); + } + fixture.put(true, "shared", "%ERROR%"); + fixture.put(false, "shared", "%ERROR%"); + fixture.put(true, "shared", TWO_REGEXES); + fixture.put(false, "shared", TWO_REGEXES); + fixture.put(true, "plain", "ORIGINAL"); + fixture.put(false, "plain", "ORIGINAL"); + MetricFilter metric = fixture.metricStore.get(metricKey("plain")).orElseThrow(); + SubscriptionFilter subscription = fixture.subscriptions.get(subscriptionKey("plain")).orElseThrow(); + + rejected(() -> fixture.put(true, "plain", "%ERROR%"), "LimitExceededException"); + rejected(() -> fixture.put(false, "plain", "%ERROR%"), "LimitExceededException"); + assertSame(metric, fixture.metricStore.get(metricKey("plain")).orElseThrow()); + assertSame(subscription, fixture.subscriptions.get(subscriptionKey("plain")).orElseThrow()); + assertEquals("ORIGINAL", metric.getFilterPattern()); + assertEquals("ORIGINAL", subscription.getFilterPattern()); + + fixture.put(true, "shared", "PLAIN"); + fixture.put(false, "plain", "%ERROR%"); + rejected(() -> fixture.put(true, "plain", "%ERROR%"), "LimitExceededException"); + fixture.put(false, "shared", "PLAIN"); + fixture.put(true, "plain", "%ERROR%"); + fixture.metrics.deleteMetricFilter(GROUP, "plain", REGION); + fixture.put(false, "new-subscription", "%ERROR%"); + fixture.logs.deleteSubscriptionFilter(GROUP, "plain", REGION); + fixture.put(true, "new-metric", "%ERROR%"); + rejected(() -> fixture.put(true, "sixth", "%ERROR%"), "LimitExceededException"); + } + + @ParameterizedTest + @ValueSource(strings = {"%ERROR% %WARN%", "{ $.a = }", + "{ $.a = %ERROR% || $.a = %WARN% || $.a = %INFO% }"}) + void subscriptionPatternsUseTheSameGrammarAndPerPatternQuota(String pattern) { + Fixture fixture = new Fixture(false); + fixture.put(false, "original", "ORIGINAL"); + rejected(() -> fixture.put(false, "original", pattern), "InvalidParameterException"); + assertEquals("ORIGINAL", fixture.subscriptions.get(subscriptionKey("original")).orElseThrow().getFilterPattern()); + } + + @Test + void regexFiltersBeyondTheFirstSubscriptionPageStillCount() { + Fixture fixture = new Fixture(false); + for (int i = 0; i < 51; i++) { + fixture.put(false, "a" + i, "PLAIN"); + } + for (int i = 0; i < 5; i++) { + fixture.put(false, "z" + i, "%ERROR%"); + } + rejected(() -> fixture.put(true, "sixth", "%ERROR%"), "LimitExceededException"); + } + + @ParameterizedTest + @CsvSource({"true,true", "true,false", "false,true", "false,false"}) + void storageFailuresCannotSilentlyFreeQuota(boolean metricStorageFails, boolean metricWriter) { + Fixture fixture = new Fixture(false); + IllegalStateException failure = new IllegalStateException("quota storage unavailable"); + if (metricStorageFails) { + doThrow(failure).when(fixture.metricStore).scan(any()); + } else { + doThrow(failure).when(fixture.subscriptions).scan(any()); + } + assertSame(failure, assertThrows(IllegalStateException.class, + () -> fixture.put(metricWriter, "candidate", "%ERROR%"))); + assertTrue(fixture.metricStore.keys().isEmpty()); + assertTrue(fixture.subscriptions.keys().isEmpty()); + } + + @ParameterizedTest + @CsvSource({"true,true", "true,false", "false,true", "false,false"}) + void invalidStoredPatternsCannotSilentlyFreeQuota(boolean invalidMetric, boolean metricWriter) { + Fixture fixture = new Fixture(false); + if (invalidMetric) { + fixture.metricStore.put(metricKey("corrupt"), definition("corrupt", "{ $.a = }")); + } else { + SubscriptionFilter corrupt = new SubscriptionFilter(); + corrupt.setLogGroupName(GROUP); + corrupt.setFilterName("corrupt"); + corrupt.setFilterPattern("{ $.a = }"); + fixture.subscriptions.put(subscriptionKey("corrupt"), corrupt); + } + rejected(() -> fixture.put(metricWriter, "candidate", "%ERROR%"), "InvalidParameterException"); + assertTrue(fixture.metricStore.get(metricKey("candidate")).isEmpty()); + assertTrue(fixture.subscriptions.get(subscriptionKey("candidate")).isEmpty()); + } + + @Test + void quotaAndGroupDeletionAreIsolatedByAccountRegionAndGroup() { + RequestContext context = new RequestContext(); + context.setAccountId(ACCOUNT); + @SuppressWarnings("unchecked") + Instance contexts = mock(Instance.class); + when(contexts.get()).thenReturn(context); + Fixture fixture = new Fixture(contexts); + for (int i = 0; i < 5; i++) { + fixture.put(i % 2 == 0, "f" + i, "%ERROR%"); + } + fixture.logs.createLogGroup(GROUP, null, null, "eu-west-1"); + fixture.logs.putSubscriptionFilter(GROUP, "other-region", TWO_REGEXES, "destination", null, "eu-west-1"); + fixture.logs.createLogGroup("/other-group", null, null, REGION); + fixture.logs.putSubscriptionFilter("/other-group", "other-group", TWO_REGEXES, "destination", null, REGION); + + context.setAccountId("111111111111"); + fixture.logs.createLogGroup(GROUP, null, null, REGION); + for (int i = 0; i < 5; i++) { + fixture.put(i % 2 == 0, "f" + i, "%ERROR%"); + } + rejected(() -> fixture.put(false, "sixth", "%ERROR%"), "LimitExceededException"); + fixture.logs.deleteLogGroup(GROUP, REGION); + + context.setAccountId(ACCOUNT); + rejected(() -> fixture.put(true, "sixth", "%ERROR%"), "LimitExceededException"); + assertEquals(3, fixture.metrics.countMetricFilters(GROUP, REGION)); + assertEquals(2, fixture.logs.describeSubscriptionFilters(GROUP, null, null, 50, REGION) + .subscriptionFilters().size()); + assertEquals(1, fixture.logs.describeSubscriptionFilters(GROUP, null, null, 50, "eu-west-1") + .subscriptionFilters().size()); + assertEquals(1, fixture.logs.describeSubscriptionFilters("/other-group", null, null, 50, REGION) + .subscriptionFilters().size()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void deletingAGroupCascadesBothFamiliesWithoutRequiringAnEventObserver(boolean factoryConstructor) { + Fixture fixture = new Fixture(factoryConstructor); + fixture.put(true, "metric", "%ERROR%"); + fixture.put(false, "subscription", "%ERROR%"); + fixture.logs.deleteLogGroup(GROUP, REGION); + assertTrue(fixture.metricStore.keys().isEmpty()); + assertTrue(fixture.subscriptions.keys().isEmpty()); + rejected(() -> fixture.put(true, "orphan", "%ERROR%"), "ResourceNotFoundException"); + rejected(() -> fixture.put(false, "orphan", "%ERROR%"), "ResourceNotFoundException"); + fixture.logs.createLogGroup(GROUP, null, null, REGION); + for (int i = 0; i < 5; i++) { + fixture.put(i % 2 == 0, "new" + i, "%ERROR%"); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void crossFamilyWritersCannotBothClaimTheLastSlot(boolean metricFirst) throws Exception { + Fixture fixture = new Fixture(false); + for (int i = 0; i < 4; i++) { + fixture.put(true, "existing" + i, "%ERROR%"); + } + CountDownLatch inWrite = new CountDownLatch(1); + CountDownLatch finishWrite = new CountDownLatch(1); + if (metricFirst) { + gateWrite(fixture.metricStore, inWrite, finishWrite); + } else { + gateWrite(fixture.subscriptions, inWrite, finishWrite); + } + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future first = pool.submit(() -> fixture.put(metricFirst, "first", "%ERROR%")); + assertTrue(inWrite.await(5, TimeUnit.SECONDS)); + CyclicBarrier start = new CyclicBarrier(2); + Future second = pool.submit(() -> { + start.await(5, TimeUnit.SECONDS); + rejected(() -> fixture.put(!metricFirst, "second", "%ERROR%"), "LimitExceededException"); + return null; + }); + start.await(5, TimeUnit.SECONDS); + assertThrows(TimeoutException.class, () -> second.get(200, TimeUnit.MILLISECONDS), + "the second writer must wait until the first check-and-write completes"); + finishWrite.countDown(); + first.get(5, TimeUnit.SECONDS); + second.get(5, TimeUnit.SECONDS); + assertEquals(metricFirst ? 5 : 4, fixture.metricStore.keys().size()); + assertEquals(metricFirst ? 0 : 1, fixture.subscriptions.keys().size()); + } finally { + finishWrite.countDown(); + pool.shutdownNow(); + assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void groupDeletionCannotLeaveAConcurrentFilterWriteOrphaned(boolean metricWriter) throws Exception { + Fixture fixture = new Fixture(false); + CountDownLatch inWrite = new CountDownLatch(1); + CountDownLatch finishWrite = new CountDownLatch(1); + if (metricWriter) { + gateWrite(fixture.metricStore, inWrite, finishWrite); + } else { + gateWrite(fixture.subscriptions, inWrite, finishWrite); + } + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future writer = pool.submit(() -> fixture.put(metricWriter, "racing", "%ERROR%")); + assertTrue(inWrite.await(5, TimeUnit.SECONDS)); + CyclicBarrier start = new CyclicBarrier(2); + Future deletion = pool.submit(() -> { + start.await(5, TimeUnit.SECONDS); + fixture.logs.deleteLogGroup(GROUP, REGION); + return null; + }); + start.await(5, TimeUnit.SECONDS); + assertThrows(TimeoutException.class, () -> deletion.get(200, TimeUnit.MILLISECONDS), + "the cascade must wait for an in-flight filter write"); + finishWrite.countDown(); + writer.get(5, TimeUnit.SECONDS); + deletion.get(5, TimeUnit.SECONDS); + assertFalse(fixture.logs.logGroupExists(GROUP, REGION)); + assertTrue(fixture.metricStore.keys().isEmpty()); + assertTrue(fixture.subscriptions.keys().isEmpty()); + } finally { + finishWrite.countDown(); + pool.shutdownNow(); + assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + private static void gateWrite(StorageBackend store, CountDownLatch entered, + CountDownLatch release) { + doAnswer(invocation -> { + entered.countDown(); + assertTrue(release.await(5, TimeUnit.SECONDS)); + return invocation.callRealMethod(); + }).when(store).put(anyString(), any()); + } + + private static void rejected(Runnable operation, String code) { + assertEquals(code, assertThrows(AwsException.class, operation::run).getErrorCode()); + } + + private static String metricKey(String name) { + return REGION + "::" + GROUP + "::" + name; + } + + private static String subscriptionKey(String name) { + return REGION + "::" + GROUP + "::filter::" + name; + } + + private static MetricFilter definition(String name, String pattern) { + MetricTransformation transformation = new MetricTransformation(); + transformation.setMetricName("Errors"); + transformation.setMetricNamespace("Quota"); + transformation.setMetricValue("1"); + MetricFilter filter = new MetricFilter(); + filter.setLogGroupName(GROUP); + filter.setFilterName(name); + filter.setFilterPattern(pattern); + filter.setMetricTransformations(List.of(transformation)); + return filter; + } + + private static final class Fixture { + private final StorageBackend metricStore; + private final StorageBackend subscriptions; + private final CloudWatchLogsService logs; + private final CloudWatchLogsMetricFilterService metrics; + + private Fixture(boolean factoryConstructor) { + this(factoryConstructor, null); + } + + private Fixture(Instance contexts) { + this(false, contexts); + } + + private Fixture(boolean factoryConstructor, Instance contexts) { + RegionResolver resolver = new RegionResolver(REGION, ACCOUNT); + CloudWatchMetricsService publisher = mock(CloudWatchMetricsService.class); + if (factoryConstructor) { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.defaultAccountId()).thenReturn(ACCOUNT); + when(config.storage().persistentPath()).thenReturn("target/quota-test-storage"); + ServiceConfigAccess access = mock(ServiceConfigAccess.class); + when(access.storageMode("cloudwatchlogs")).thenReturn("memory"); + StorageFactory factory = new StorageFactory(config, access); + metricStore = factory.create("cloudwatchlogs", "cwlogs-metric-filters.json", + new TypeReference>() {}); + subscriptions = factory.create("cloudwatchlogs", "cwlogs-subscription-filters.json", + new TypeReference>() {}); + logs = new CloudWatchLogsService(factory, config, resolver, null, null); + metrics = new CloudWatchLogsMetricFilterService(logs, publisher, resolver); + } else { + metricStore = spy(new AccountAwareStorageBackend(new InMemoryStorage<>(), contexts, ACCOUNT)); + subscriptions = spy(new AccountAwareStorageBackend(new InMemoryStorage<>(), contexts, ACCOUNT)); + logs = new CloudWatchLogsService( + new AccountAwareStorageBackend<>(new InMemoryStorage<>(), contexts, ACCOUNT), + new InMemoryStorage<>(), new InMemoryStorage<>(), subscriptions, metricStore, 10_000, resolver); + metrics = new CloudWatchLogsMetricFilterService(logs, publisher, resolver); + } + logs.createLogGroup(GROUP, null, null, REGION); + } + + private void put(boolean metric, String name, String pattern) { + if (metric) { + metrics.putMetricFilter(definition(name, pattern), REGION); + } else { + logs.putSubscriptionFilter(GROUP, name, pattern, "destination", null, REGION); + } + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java index a93c52fba6..9f8f609c64 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandlerTest.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -15,6 +16,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.mockito.Mockito.mock; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -50,6 +52,7 @@ class CloudWatchLogsHandlerTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 10_000, new RegionResolver(REGION, ACCOUNT) ); @@ -58,6 +61,8 @@ class CloudWatchLogsHandlerTest { new CloudWatchLogsCrossAccountService( new InMemoryStorage<>(), new InMemoryStorage<>(), new RegionResolver(REGION, ACCOUNT), MAPPER), + new CloudWatchLogsMetricFilterService(service, + mock(CloudWatchMetricsService.class), new RegionResolver(REGION, ACCOUNT)), MAPPER); service.createLogGroup(GROUP, null, null, REGION); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.java index 60f004f376..5cf686dbd2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInputConstraintTest.java @@ -6,9 +6,11 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.mockito.Mockito.mock; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -35,6 +37,7 @@ class CloudWatchLogsInputConstraintTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 10_000, new RegionResolver(REGION, ACCOUNT) ); @@ -43,6 +46,8 @@ class CloudWatchLogsInputConstraintTest { new CloudWatchLogsCrossAccountService( new InMemoryStorage<>(), new InMemoryStorage<>(), new RegionResolver(REGION, ACCOUNT), MAPPER), + new CloudWatchLogsMetricFilterService(service, + mock(CloudWatchMetricsService.class), new RegionResolver(REGION, ACCOUNT)), MAPPER); service.createLogGroup(GROUP, null, null, REGION); service.createLogStream(GROUP, "s1", REGION); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.java index 1bb14f9167..4176d0aa5b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.java @@ -46,6 +46,7 @@ class CloudWatchLogsInsightsQueryTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 10000, new RegionResolver(REGION, "000000000000")); } @@ -674,7 +675,7 @@ class CloudWatchLogsInsightsQueryTest { private CloudWatchLogsService newAsyncService(AtomicLong clockMs, long delayMs, String group, String stream) { CloudWatchLogsService svc = new CloudWatchLogsService( new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), - 10000, new RegionResolver(REGION, "000000000000"), delayMs, clockMs::get); + new InMemoryStorage<>(), 10000, new RegionResolver(REGION, "000000000000"), delayMs, clockMs::get); createGroupStream(svc, group, stream); putRaw(svc, group, stream, BASE_MS + 2000, jobLog("INFO", "x", "JOB-1")); return svc; diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterFixturePackagingTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterFixturePackagingTest.java new file mode 100644 index 0000000000..63ebada8bc --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterFixturePackagingTest.java @@ -0,0 +1,27 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.*; + +class CloudWatchLogsMetricFilterFixturePackagingTest { + @ParameterizedTest + @ValueSource(strings = { + "compatibility-tests/sdk-test-java/src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json", + "compatibility-tests/sdk-test-python/tests/fixtures/metric-filter-publishing-aws.json" + }) + void sdkModuleFixtureIsAnExactCopyOfTheCanonicalAwsOracle(String modulePath) throws Exception { + Path copy = Path.of(modulePath); + assertTrue(Files.isRegularFile(copy), "SDK module must package its own fixture: " + modulePath); + try (InputStream canonical = getClass().getResourceAsStream("/cloudwatchlogs/metric-filter-publishing-aws.json")) { + assertNotNull(canonical); + assertArrayEquals(canonical.readAllBytes(), Files.readAllBytes(copy), + "Copy canonical bytes; never regenerate expected values from emulator output"); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterHandlerTest.java new file mode 100644 index 0000000000..d6a44a2a59 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterHandlerTest.java @@ -0,0 +1,185 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +/** + * The metric filter operations through the Logs handler: request members in the JSON 1.1 form the + * API takes, responses in the shape the API reference lists, and the real filter count on + * DescribeLogGroups. + */ +class CloudWatchLogsMetricFilterHandlerTest { + + private static final String REGION = "us-east-1"; + private static final String GROUP = "/app/api"; + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private CloudWatchLogsHandler handler; + + @BeforeEach + void setUp() { + CloudWatchLogsService service = new CloudWatchLogsService(new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + 10_000, new RegionResolver(REGION, "000000000000")); + handler = new CloudWatchLogsHandler(service, + new CloudWatchLogsCrossAccountService(new InMemoryStorage<>(), new InMemoryStorage<>(), + new RegionResolver(REGION, "000000000000"), MAPPER), + new CloudWatchLogsMetricFilterService(service, + mock(CloudWatchMetricsService.class), new RegionResolver(REGION, "000000000000")), + MAPPER); + service.createLogGroup(GROUP, null, null, REGION); + } + + private JsonNode call(String action, String json) { + try { + Response response = handler.handle(action, MAPPER.readTree(json), REGION); + assertEquals(200, response.getStatus()); + return MAPPER.valueToTree(response.getEntity()); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException(e); + } + } + + private static final String PUT = """ + {"logGroupName": "/app/api", "filterName": "errors", "filterPattern": "[..., status_code, size]", + "metricTransformations": [{"metricName": "Volume", "metricNamespace": "Web", "metricValue": "$size", + "dimensions": {"Status": "$status_code"}, "unit": "Bytes"}], + "applyOnTransformedLogs": false, "emitSystemFieldDimensions": ["@aws.region"]} + """; + + @Test + void putThenDescribeRoundTripsTheApiShape() { + JsonNode putResponse = call("PutMetricFilter", PUT); + assertTrue(putResponse.isObject() && putResponse.isEmpty(), "PutMetricFilter answers an empty object"); + + JsonNode described = call("DescribeMetricFilters", "{\"logGroupName\": \"/app/api\"}"); + assertEquals(1, described.path("metricFilters").size()); + JsonNode filter = described.path("metricFilters").get(0); + assertEquals("errors", filter.path("filterName").asText()); + assertEquals("/app/api", filter.path("logGroupName").asText()); + assertEquals("[..., status_code, size]", filter.path("filterPattern").asText()); + assertTrue(filter.path("creationTime").asLong() > 0); + assertFalse(filter.path("applyOnTransformedLogs").asBoolean()); + assertEquals("@aws.region", filter.path("emitSystemFieldDimensions").get(0).asText()); + JsonNode t = filter.path("metricTransformations").get(0); + assertEquals("Volume", t.path("metricName").asText()); + assertEquals("Web", t.path("metricNamespace").asText()); + assertEquals("$size", t.path("metricValue").asText()); + assertEquals("$status_code", t.path("dimensions").path("Status").asText()); + assertEquals("Bytes", t.path("unit").asText()); + assertFalse(t.has("defaultValue"), "an unset default value is left out"); + assertFalse(described.has("nextToken")); + } + + @Test + void defaultValueIsANumberOnTheWire() { + call("PutMetricFilter", """ + {"logGroupName": "/app/api", "filterName": "errors", "filterPattern": "ERROR", + "metricTransformations": [{"metricName": "Errors", "metricNamespace": "App", "metricValue": "1", + "defaultValue": 0}]} + """); + + JsonNode t = call("DescribeMetricFilters", "{\"logGroupName\": \"/app/api\"}") + .path("metricFilters").get(0).path("metricTransformations").get(0); + assertTrue(t.path("defaultValue").isNumber()); + assertEquals(0.0, t.path("defaultValue").asDouble()); + + AwsException e = assertThrows(AwsException.class, () -> call("PutMetricFilter", """ + {"logGroupName": "/app/api", "filterName": "bad", "filterPattern": "ERROR", + "metricTransformations": [{"metricName": "Errors", "metricNamespace": "App", "metricValue": "1", + "defaultValue": "zero"}]} + """)); + assertEquals("SerializationException", e.getErrorCode()); + } + + @Test + void describeHonoursLimitAndNextToken() { + for (int i = 0; i < 3; i++) { + call("PutMetricFilter", PUT.replace("\"errors\"", "\"f" + i + "\"")); + } + + JsonNode first = call("DescribeMetricFilters", "{\"logGroupName\": \"/app/api\", \"limit\": 2}"); + assertEquals(2, first.path("metricFilters").size()); + String token = first.path("nextToken").asText(); + JsonNode second = call("DescribeMetricFilters", + "{\"logGroupName\": \"/app/api\", \"limit\": 2, \"nextToken\": \"" + token + "\"}"); + assertEquals(1, second.path("metricFilters").size()); + assertFalse(second.has("nextToken")); + } + + @Test + void describeRequiresMetricNameAndNamespaceTogether() { + String message = "Describe Metric Filters request must contain both MetricName and MetricNamespace"; + + AwsException namespaceOnly = assertThrows(AwsException.class, + () -> call("DescribeMetricFilters", "{\"metricNamespace\": \"Web\"}")); + assertEquals("InvalidParameterException", namespaceOnly.getErrorCode()); + assertEquals(message, namespaceOnly.getMessage()); + AwsException nameOnly = assertThrows(AwsException.class, + () -> call("DescribeMetricFilters", "{\"metricName\": \"Volume\"}")); + assertEquals("InvalidParameterException", nameOnly.getErrorCode()); + assertEquals(message, nameOnly.getMessage()); + assertEquals(0, call("DescribeMetricFilters", "{\"metricName\": \"Volume\", \"metricNamespace\": \"Web\"}") + .path("metricFilters").size()); + } + + @Test + void describeLogGroupsCountsTheFilters() { + assertEquals(0, call("DescribeLogGroups", "{}").path("logGroups").get(0).path("metricFilterCount").asInt()); + call("PutMetricFilter", PUT); + call("PutMetricFilter", PUT.replace("\"errors\"", "\"more\"")); + + assertEquals(2, call("DescribeLogGroups", "{}").path("logGroups").get(0).path("metricFilterCount").asInt()); + } + + @Test + void deleteRemovesTheFilter() { + call("PutMetricFilter", PUT); + + JsonNode response = call("DeleteMetricFilter", "{\"logGroupName\": \"/app/api\", \"filterName\": \"errors\"}"); + + assertTrue(response.isObject() && response.isEmpty()); + assertEquals(0, call("DescribeMetricFilters", "{\"logGroupName\": \"/app/api\"}").path("metricFilters").size()); + AwsException e = assertThrows(AwsException.class, () -> call("DeleteMetricFilter", + "{\"logGroupName\": \"/app/api\", \"filterName\": \"errors\"}")); + assertEquals("ResourceNotFoundException", e.getErrorCode()); + } + + @Test + void testMetricFilterAnswersTheReferenceShape() { + JsonNode response = call("TestMetricFilter", """ + {"filterPattern": "[..., status_code=200, size]", + "logEventMessages": [ + "127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] \\"GET /apache_pb.gif HTTP/1.0\\" 200 1534", + "127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] \\"GET /apache_pb.gif HTTP/1.0\\" 500 5324"]} + """); + + assertEquals(1, response.path("matches").size()); + JsonNode match = response.path("matches").get(0); + assertEquals(1, match.path("eventNumber").asLong()); + assertTrue(match.path("eventMessage").asText().endsWith("200 1534")); + assertEquals("1534", match.path("extractedValues").path("$size").asText()); + assertEquals("200", match.path("extractedValues").path("$status_code").asText()); + assertEquals("127.0.0.1", match.path("extractedValues").path("$1").asText()); + + JsonNode terms = call("TestMetricFilter", + "{\"filterPattern\": \"\\\"[ERROR]\\\"\", \"logEventMessages\": [\"[INFO] up\", \"[ERROR] down\"]}"); + assertEquals(2, terms.path("matches").get(0).path("eventNumber").asLong()); + assertTrue(terms.path("matches").get(0).path("extractedValues").isObject()); + assertTrue(terms.path("matches").get(0).path("extractedValues").isEmpty()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterIntegrationTest.java new file mode 100644 index 0000000000..15cd8e2785 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterIntegrationTest.java @@ -0,0 +1,234 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Metric filters over the wire: PutMetricFilter, DescribeMetricFilters, TestMetricFilter and + * DeleteMetricFilter as the Logs API takes them, the metric a filter publishes when PutLogEvents + * stores matching events, read back through CloudWatch GetMetricStatistics, and the filters going + * with their log group. + */ +@QuarkusTest +class CloudWatchLogsMetricFilterIntegrationTest { + + private static final String LOGS_CONTENT_TYPE = "application/x-amz-json-1.1"; + private static final String LOGS_AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260908/us-east-1/logs/aws4_request"; + private static final String CW_CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String CW_AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260908/us-east-1/monitoring/aws4_request"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + private static Response logs(String action, String body) { + return given().contentType(LOGS_CONTENT_TYPE).header("Authorization", LOGS_AUTH) + .header("X-Amz-Target", "Logs_20140328." + action).body(body).post("/"); + } + + private static Response cloudWatch(String action, String body) { + return given().contentType(CW_CONTENT_TYPE).header("Authorization", CW_AUTH) + .header("X-Amz-Target", "GraniteServiceVersion20100801." + action).body(body).post("/"); + } + + private static void createGroupAndStream(String group, String stream) { + logs("CreateLogGroup", "{\"logGroupName\":\"" + group + "\"}").then().statusCode(200); + logs("CreateLogStream", "{\"logGroupName\":\"" + group + "\",\"logStreamName\":\"" + stream + "\"}") + .then().statusCode(200); + } + + private static void putLogEvents(String group, String stream, long timestamp, List messages) { + StringBuilder events = new StringBuilder(); + for (int i = 0; i < messages.size(); i++) { + events.append(i > 0 ? "," : "").append("{\"timestamp\":").append(timestamp + i) + .append(",\"message\":").append(quote(messages.get(i))).append("}"); + } + logs("PutLogEvents", "{\"logGroupName\":\"" + group + "\",\"logStreamName\":\"" + stream + + "\",\"logEvents\":[" + events + "]}").then().statusCode(200); + } + + private static String quote(String text) { + return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\""; + } + + private static Response statistics(String namespace, String metric, String dimensionName, String dimensionValue, + Instant from, Instant to) { + String dimensions = dimensionName == null ? "" + : ",\"Dimensions\":[{\"Name\":\"" + dimensionName + "\",\"Value\":\"" + dimensionValue + "\"}]"; + return cloudWatch("GetMetricStatistics", "{\"Namespace\":\"" + namespace + "\",\"MetricName\":\"" + metric + + "\",\"Period\":60,\"StartTime\":" + from.getEpochSecond() + ",\"EndTime\":" + to.getEpochSecond() + + ",\"Statistics\":[\"Sum\",\"SampleCount\"]" + dimensions + "}"); + } + + private static double sum(Response statistics) { + List sums = statistics.then().statusCode(200).extract().jsonPath().getList("Datapoints.Sum", Float.class); + return sums.stream().mapToDouble(Float::doubleValue).sum(); + } + + @Test + void aFilterPublishesTheFieldValueWithDimensionsForMatchingEvents() { + String group = "/it/metric-filter/access-" + System.nanoTime(); + createGroupAndStream(group, "web"); + logs("PutMetricFilter", """ + {"logGroupName":"%s","filterName":"volume","filterPattern":"[..., status_code, size]", + "metricTransformations":[{"metricName":"Volume","metricNamespace":"%s","metricValue":"$size", + "dimensions":{"Status":"$status_code"},"unit":"Bytes"}]} + """.formatted(group, group)).then().statusCode(200); + + long now = System.currentTimeMillis(); + putLogEvents(group, "web", now, List.of( + "127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] \"GET /index.html HTTP/1.0\" 200 1534", + "127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] \"GET /index.html HTTP/1.0\" 500 5324", + "127.0.0.1 - frank [10/Oct/2000:13:50:35 -0700] \"GET /index.html HTTP/1.0\" 200 4355", + "not an access log line")); + + Instant from = Instant.ofEpochMilli(now).minusSeconds(120); + Instant to = Instant.ofEpochMilli(now).plusSeconds(120); + // The 4355 sample is the batch's last write, so its sum arriving means the 500 sample is stored too. + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> + assertEquals(1534 + 4355, sum(statistics(group, "Volume", "Status", "200", from, to)), 0.001)); + assertEquals(5324, sum(statistics(group, "Volume", "Status", "500", from, to)), 0.001); + assertEquals(0, sum(statistics(group, "Volume", "Status", "404", from, to)), 0.001); + } + + @Test + void nonmatchDefaultsPublishPerEventWithoutSyntheticClosingEvents() { + String group = "/it/metric-filter/quiet-" + System.nanoTime(); + createGroupAndStream(group, "app"); + logs("PutMetricFilter", """ + {"logGroupName":"%s","filterName":"quiet","filterPattern":"ERROR", + "metricTransformations":[{"metricName":"ErrorCount","metricNamespace":"%s","metricValue":"3", + "defaultValue":7}]} + """.formatted(group, group)).then().statusCode(200); + + // Newly ingested historical events belong to their event minute, even before filter creation. + long time = (System.currentTimeMillis() / 60_000 - 10) * 60_000; + putLogEvents(group, "app", time, List.of("INFO one", "INFO two", "INFO three")); + Instant from = Instant.ofEpochMilli(time); + Instant to = from.plusSeconds(59); + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> + assertEquals(21, sum(statistics(group, "ErrorCount", null, null, from, to)), 0.001)); + for (int read = 0; read < 2; read++) { + Response result = statistics(group, "ErrorCount", null, null, from, to); + assertEquals(21, sum(result), 0.001); + result.then().body("Datapoints", hasSize(1)).body("Datapoints[0].SampleCount", equalTo(3.0f)); + } + statistics(group, "ErrorCount", null, null, to.plusSeconds(1), to.plusSeconds(60)).then() + .statusCode(200).body("Datapoints", hasSize(0)); + logs("DeleteLogGroup", "{\"logGroupName\":\"" + group + "\"}").then().statusCode(200); + } + + @Test + void putDescribeTestAndDeleteFollowTheApi() { + String group = "/it/metric-filter/api-" + System.nanoTime(); + createGroupAndStream(group, "s"); + logs("PutMetricFilter", """ + {"logGroupName":"%s","filterName":"latency","filterPattern":"{ $.latency = * }", + "metricTransformations":[{"metricName":"Latency","metricNamespace":"App","metricValue":"$.latency", + "unit":"Milliseconds"}]} + """.formatted(group)).then().statusCode(200); + + logs("DescribeMetricFilters", "{\"logGroupName\":\"" + group + "\"}").then() + .statusCode(200) + .body("metricFilters", hasSize(1)) + .body("metricFilters[0].filterName", equalTo("latency")) + .body("metricFilters[0].logGroupName", equalTo(group)) + .body("metricFilters[0].filterPattern", equalTo("{ $.latency = * }")) + .body("metricFilters[0].metricTransformations[0].metricName", equalTo("Latency")) + .body("metricFilters[0].metricTransformations[0].metricNamespace", equalTo("App")) + .body("metricFilters[0].metricTransformations[0].metricValue", equalTo("$.latency")) + .body("metricFilters[0].metricTransformations[0].unit", equalTo("Milliseconds")) + .body("nextToken", nullValue()); + logs("DescribeMetricFilters", "{\"metricName\":\"Latency\",\"metricNamespace\":\"App\"}").then() + .statusCode(200) + .body("metricFilters.logGroupName", org.hamcrest.Matchers.hasItem(group)); + logs("DescribeLogGroups", "{\"logGroupNamePrefix\":\"" + group + "\"}").then() + .statusCode(200) + .body("logGroups[0].metricFilterCount", equalTo(1)); + + logs("TestMetricFilter", """ + {"filterPattern":"{ $.latency = * }","logEventMessages":["{\\"latency\\": 50}","plain text"]} + """).then() + .statusCode(200) + .body("matches", hasSize(1)) + .body("matches[0].eventNumber", equalTo(1)) + .body("matches[0].eventMessage", equalTo("{\"latency\": 50}")) + .body("matches[0].extractedValues", equalTo(Map.of())); + + logs("PutMetricFilter", """ + {"logGroupName":"%s","filterName":"broken","filterPattern":"{ $.a = }", + "metricTransformations":[{"metricName":"A","metricNamespace":"App","metricValue":"1"}]} + """.formatted(group)).then() + .statusCode(400) + .body("__type", equalTo("InvalidParameterException")); + logs("PutMetricFilter", """ + {"logGroupName":"/it/metric-filter/missing","filterName":"x","filterPattern":"ERROR", + "metricTransformations":[{"metricName":"A","metricNamespace":"App","metricValue":"1"}]} + """).then() + .statusCode(400) + .body("__type", equalTo("ResourceNotFoundException")); + + logs("DeleteMetricFilter", "{\"logGroupName\":\"" + group + "\",\"filterName\":\"latency\"}").then() + .statusCode(200); + logs("DeleteMetricFilter", "{\"logGroupName\":\"" + group + "\",\"filterName\":\"latency\"}").then() + .statusCode(400) + .body("__type", equalTo("ResourceNotFoundException")); + logs("DescribeMetricFilters", "{\"logGroupName\":\"" + group + "\"}").then() + .statusCode(200) + .body("metricFilters", hasSize(0)); + } + + @Test + void deletingTheLogGroupDeletesItsFilters() { + String group = "/it/metric-filter/cascade-" + System.nanoTime(); + createGroupAndStream(group, "s"); + logs("PutMetricFilter", """ + {"logGroupName":"%s","filterName":"errors","filterPattern":"ERROR", + "metricTransformations":[{"metricName":"Errors","metricNamespace":"Cascade","metricValue":"1"}]} + """.formatted(group)).then().statusCode(200); + + logs("DeleteLogGroup", "{\"logGroupName\":\"" + group + "\"}").then().statusCode(200); + + logs("DescribeMetricFilters", "{\"metricName\":\"Errors\",\"metricNamespace\":\"Cascade\"}").then() + .statusCode(200) + .body("metricFilters.logGroupName", not(hasItem(group))); + logs("DescribeMetricFilters", "{\"logGroupName\":\"" + group + "\"}").then() + .statusCode(400) + .body("__type", equalTo("ResourceNotFoundException")); + } + + @Test + void describeRequiresMetricNameAndNamespaceTogether() { + String message = "Describe Metric Filters request must contain both MetricName and MetricNamespace"; + + logs("DescribeMetricFilters", "{\"metricNamespace\":\"ProbeNs\"}").then() + .statusCode(400) + .body("__type", equalTo("InvalidParameterException")) + .body("message", equalTo(message)); + logs("DescribeMetricFilters", "{\"metricName\":\"ProbeMetric\"}").then() + .statusCode(400) + .body("__type", equalTo("InvalidParameterException")) + .body("message", equalTo(message)); + logs("DescribeMetricFilters", "{\"metricName\":\"ProbeMetric\",\"metricNamespace\":\"ProbeNs\"}").then() + .statusCode(200) + .body("metricFilters", hasSize(0)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterPublisherTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterPublisherTest.java new file mode 100644 index 0000000000..37650bf274 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterPublisherTest.java @@ -0,0 +1,189 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterRetryTest.FaultStore; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterRetryTest.await; +import static io.github.hectorvent.floci.services.cloudwatch.logs.PublicationTestSupport.*; +import static org.junit.jupiter.api.Assertions.*; + +/** + * PutLogEvents evaluates the group's filters on the request thread and hands the resulting immutable + * samples to a bounded queue. The single publisher thread writes them; the request never waits on the + * Metrics sink. A full queue degrades to an inline write instead of dropping accepted samples. + */ +class CloudWatchLogsMetricFilterPublisherTest { + private final FaultStore sink = new FaultStore(); + private final PublicationTestSupport f = new PublicationTestSupport(sink); + + @AfterEach + void stop() { f.service.stop(); } + + @Test + void ingestionQueuesSamplesAndWritesNothingUntilTheQueueIsDrained() { + f.put("queued", "ERROR", "3", 7.0); + f.enqueue(TIME, "ERROR", "INFO", "ERROR"); + assertEquals(3, f.service.queuedSamples()); + assertTrue(sink.attempts.isEmpty(), "the ingestion thread must not write to the Metrics sink"); + f.stats("queued", TIME, 0, 0); + f.service.publishQueued(); + assertEquals(0, f.service.queuedSamples()); + f.stats("queued", TIME, 13, 3); + } + + @Test + void theStartedWorkerDrainsTheQueueWithoutFurtherIngestionOrReads() throws Exception { + f.put("worker", "ERROR", "3", 7.0); + CountDownLatch written = new CountDownLatch(2); + sink.after = (key, datum) -> written.countDown(); + f.service.start(true, true); + f.enqueue(TIME, "ERROR", "INFO"); + assertTrue(written.await(5, TimeUnit.SECONDS), "the worker must publish queued samples on its own"); + f.stats("worker", TIME, 10, 2); + assertEquals(0, f.service.queuedSamples()); + } + + @Test + void samplesQueuedBeforeStartAreDrainedWhenTheWorkerStarts() throws Exception { + f.put("early", "ERROR", "3", 7.0); + f.enqueue(TIME, "INFO"); + CountDownLatch written = new CountDownLatch(1); + sink.after = (key, datum) -> written.countDown(); + f.service.start(true, true); + assertTrue(written.await(5, TimeUnit.SECONDS)); + f.stats("early", TIME, 7, 1); + } + + @Test + void aFullQueueWritesTheBatchInlineInsteadOfDroppingIt() { + PublicationTestSupport bounded = new PublicationTestSupport(sink, 4); + try { + bounded.put("bounded", "ERROR", "3", 7.0); + bounded.enqueue(TIME, "INFO", "INFO", "INFO"); + assertEquals(3, bounded.service.queuedSamples()); + bounded.enqueue(TIME, "ERROR", "ERROR"); + assertEquals(3, bounded.service.queuedSamples(), "the batch that does not fit is written at once"); + bounded.stats("bounded", TIME, 6, 2); + bounded.service.publishQueued(); + bounded.stats("bounded", TIME, 27, 5); + } finally { + bounded.service.stop(); + } + } + + @Test + void failedFirstAttemptsMoveToTheRetryQueueWithTheirIds() { + f.put("failing", "ERROR", "3", 7.0); + sink.fail = true; + f.enqueue(TIME, "ERROR", "INFO"); + f.service.publishQueued(); + assertEquals(0, f.service.queuedSamples()); + assertEquals(2, f.service.pendingSamples()); + sink.fail = false; + f.service.retryPending(); + assertEquals(0, f.service.pendingSamples()); + f.stats("failing", TIME, 10, 2); + assertEquals(2, sink.keys().size(), "retries replace the same publication keys"); + } + + @Test + void resetDiscardsQueuedSamplesAndResumesTheSameInstance() { + f.put("reset", "ERROR", "3", 7.0); + f.enqueue(TIME, "INFO", "INFO"); + f.service.beforeReset(); + assertEquals(0, f.service.queuedSamples()); + f.service.publishQueued(); + f.service.afterReset(); + f.service.publishQueued(); + f.stats("reset", TIME, 0, 0); + f.ingest(TIME, "INFO"); + f.stats("reset", TIME, 7, 1); + } + + @Test + void resetWaitsForTheActiveWriteAndNothingIsWrittenAfterItReturns() throws Exception { + f.put("midway", "ERROR", "3", 7.0); + f.enqueue(TIME, "INFO", "INFO", "INFO"); + CountDownLatch inWrite = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + sink.before = (key, datum) -> { inWrite.countDown(); await(release); }; + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future drain = pool.submit(f.service::publishQueued); + assertTrue(inWrite.await(5, TimeUnit.SECONDS)); + Future reset = pool.submit(f.service::beforeReset); + assertThrows(TimeoutException.class, () -> reset.get(100, TimeUnit.MILLISECONDS), + "reset must wait for the write holding the monitor"); + sink.before = (key, datum) -> {}; + release.countDown(); + reset.get(5, TimeUnit.SECONDS); + int writtenWhenResetReturned = sink.attempts.size(); + drain.get(5, TimeUnit.SECONDS); + assertEquals(writtenWhenResetReturned, sink.attempts.size(), "no write may follow a completed beforeReset"); + assertEquals(0, f.service.queuedSamples()); + f.service.afterReset(); + f.service.publishQueued(); + assertEquals(writtenWhenResetReturned, sink.attempts.size()); + } finally { + release.countDown(); + pool.shutdownNow(); + assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + @Test + void aResetBetweenTwoSamplesOfABatchDiscardsTheRest() { + f.put("between", "ERROR", "3", 7.0); + f.enqueue(TIME, "INFO", "INFO", "INFO"); + // The monitor is reentrant, so a reset issued from inside the first write lands exactly + // between two samples of the batch, which unfair scheduling cannot guarantee from another thread. + sink.after = (key, datum) -> { + f.service.beforeReset(); + f.service.afterReset(); + }; + f.service.publishQueued(); + assertEquals(1, sink.attempts.size(), "the samples after the reset belong to the wiped state"); + assertEquals(0, f.service.queuedSamples()); + sink.after = (key, datum) -> {}; + f.ingest(TIME, "INFO"); + f.stats("between", TIME, 14, 2); + } + + @Test + void stopDrainsQueuedSamplesBeforeTheWorkerShutsDown() { + f.put("stopping", "ERROR", "3", 7.0); + f.enqueue(TIME, "ERROR", "INFO"); + f.service.stop(); + assertEquals(0, f.service.queuedSamples()); + f.stats("stopping", TIME, 10, 2); + } + + @Test + void deletingTheFilterDoesNotCancelSamplesAlreadyAccepted() { + f.put("deleted", "ERROR", "3", 7.0); + f.enqueue(TIME, "ERROR"); + f.service.deleteMetricFilter(GROUP, "deleted", REGION); + f.service.publishQueued(); + f.stats("deleted", TIME, 3, 1); + } + + @Test + void eventsIngestedWhileDisabledAreNeverQueued() { + f.service.start(true, false); + f.put("disabled", "ERROR", "3", 7.0); + f.enqueue(TIME, "ERROR"); + assertEquals(0, f.service.queuedSamples()); + f.service.publishQueued(); + f.stats("disabled", TIME, 0, 0); + assertEquals(List.of(), sink.attempts); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterPublishingTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterPublishingTest.java new file mode 100644 index 0000000000..81cdebe210 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterPublishingTest.java @@ -0,0 +1,225 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static io.github.hectorvent.floci.services.cloudwatch.logs.PublicationTestSupport.*; +import static org.junit.jupiter.api.Assertions.*; + +/** AWS observations are input data, never regenerated from emulator output. */ +class CloudWatchLogsMetricFilterPublishingTest { + private static final ObjectMapper JSON = new ObjectMapper(); + private PublicationTestSupport f; + + @BeforeEach + void setUp() { f = new PublicationTestSupport(); } + + private static JsonNode evidence(String section) throws IOException { + return JSON.readTree(CloudWatchLogsMetricFilterPublishingTest.class.getResourceAsStream( + "/cloudwatchlogs/metric-filter-publishing-aws.json")).get(section); + } + + @Test + void recordedMixedAndSeparateBatchesContributePerEvent() throws IOException { + JsonNode data = evidence("metricDefaults"); + int i = 0; + for (JsonNode scenario : data.get("cases")) { + String name = "case" + i; + f.put(name, data.get("filterPattern").asText(), data.get("metricValue").asText(), + data.get("defaultValue").asDouble()); + for (JsonNode batch : scenario.get("batches")) { + List messages = new ArrayList<>(); + batch.forEach(message -> messages.add(message.asText())); + f.ingest(TIME + i * 60_000L, messages.toArray(String[]::new)); + } + JsonNode expected = scenario.get("expected"); + f.stats(name, TIME + i * 60_000L, expected.get("Sum").asDouble(), expected.get("SampleCount").asDouble()); + assertEquals(expected.get("Minimum").asDouble(), f.points(name, List.of(), TIME + i * 60_000L, REGION) + .getFirst().minimum()); + assertEquals(expected.get("Maximum").asDouble(), f.points(name, List.of(), TIME + i * 60_000L, REGION) + .getFirst().maximum()); + i++; + } + } + + @Test + void threeQuietNonmatchesPublishImmediatelyWithoutClosingTraffic() { + f.put("quiet", "ERROR", "3", 7.0); + f.ingest(TIME, "INFO", "INFO", "INFO"); + f.stats("quiet", TIME, 21, 3); + f.stats("quiet", TIME, 21, 3); + f.stats("quiet", TIME + 60_000, 0, 0); + } + + @Test + void noEventsProduceNoPointsEvenWithANonzeroDefault() { + f.put("empty", "ERROR", "3", 7.0); + f.ingest(TIME); + assertTrue(f.metrics.listMetrics(null, null, null, REGION).isEmpty()); + } + + @Test + void lateMatchAppendsWithoutRetractingAlreadyPublishedDefault() { + f.put("late", "ERROR", "3", 11.0); + f.ingest(TIME, "INFO"); + f.stats("late", TIME, 11, 1); + f.ingest(TIME + 1_000, "ERROR"); + f.stats("late", TIME, 14, 2); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void recordedExtractionDistinguishesMissingNullAndRawNonnumeric(boolean fallback) throws IOException { + JsonNode data = evidence("extraction"); + f.put("extraction", data.get("filterPattern").asText(), data.get("metricValue").asText(), + fallback ? data.get("defaultValue").asDouble() : null); + int i = 0; + for (JsonNode scenario : data.get("cases")) { + long time = TIME + i++ * 60_000L; + f.ingest(time, scenario.get("message").toString()); + JsonNode expected = scenario.get("expected"); + boolean numeric = scenario.get("message").path("value").isNumber(); + boolean present = expected.has("Sum") && (fallback || numeric); + f.stats("extraction", time, present ? expected.get("Sum").asDouble() : 0, present ? 1 : 0); + } + f.ingest(TIME + i * 60_000L, "INFO"); + f.stats("extraction", TIME + i * 60_000L, fallback ? 7 : 0, fallback ? 1 : 0); + } + + @Test + void ordinaryDimensionsAreAllOrNoneAsRecorded() throws IOException { + JsonNode data = evidence("ordinaryDimensions"); + MetricFilter filter = definition("dimensions", data.get("filterPattern").asText(), "1", null); + filter.getMetricTransformations().getFirst().setDimensions(Map.of("A", "$.a", "B", "$.b")); + f.service.putMetricFilter(filter, REGION); + for (JsonNode message : data.get("messages")) { f.ingest(TIME, message.toString()); } + for (JsonNode series : data.get("expectedSeries")) { + List dims = new ArrayList<>(); + series.get("dimensions").properties().forEach(entry -> dims.add(new Dimension(entry.getKey(), entry.getValue().asText()))); + f.stats("dimensions", dims, TIME, series.get("Sum").asDouble(), series.get("SampleCount").asDouble()); + } + f.stats("dimensions", List.of(new Dimension("A", "alpha")), TIME, 0, 0); + f.stats("dimensions", List.of(new Dimension("B", "beta")), TIME, 0, 0); + } + + @Test + void recordedSystemDimensionsApplyToMatchesButNotPatternNonmatchDefaults() throws IOException { + JsonNode data = evidence("systemDimensions"); + int i = 0; + for (JsonNode scenario : data.get("cases")) { + String name = "system" + i++; + MetricFilter filter = definition(name, "ERROR", "3", 7.0); + List fields = new ArrayList<>(); + scenario.get("emitSystemFieldDimensions").forEach(field -> fields.add(field.asText())); + filter.setEmitSystemFieldDimensions(fields); + f.service.putMetricFilter(filter, REGION); + f.ingest(TIME, "ERROR", "INFO"); + for (JsonNode series : scenario.get("expectedSeries")) { + List dims = new ArrayList<>(); + series.get("dimensions").properties().forEach(entry -> dims.add(new Dimension(entry.getKey(), + entry.getValue().asText().replace("", ACCOUNT)))); + f.stats(name, dims, TIME, series.get("Sum").asDouble(), series.get("SampleCount").asDouble()); + } + f.service.deleteMetricFilter(GROUP, name, REGION); + } + } + + @Test + void inferredPolicyRetainsSystemDimensionsOnMatchingExtractionFallback() { + MetricFilter filter = definition("fallback", "{ $.probe = \"value\" || $.value = * }", "$.value", 7.0); + filter.setEmitSystemFieldDimensions(List.of("@aws.account")); + f.service.putMetricFilter(filter, REGION); + f.ingest(TIME, "{\"probe\":\"value\"}", "{\"probe\":\"value\",\"value\":null}"); + f.stats("fallback", List.of(new Dimension("@aws.account", ACCOUNT)), TIME, 14, 2); + f.stats("fallback", TIME, 0, 0); + } + + @Test + void inferredPolicyDropsIncompleteOrdinarySetButRetainsSystemDimensions() { + MetricFilter filter = definition("incomplete", "{ $.a = * || $.b = * }", "1", null); + filter.getMetricTransformations().getFirst().setDimensions(Map.of("A", "$.a", "B", "$.b")); + filter.setEmitSystemFieldDimensions(List.of("@aws.region")); + f.service.putMetricFilter(filter, REGION); + f.ingest(TIME, "{\"a\":\"alpha\"}"); + f.stats("incomplete", List.of(new Dimension("@aws.region", REGION)), TIME, 1, 1); + f.stats("incomplete", List.of(new Dimension("A", "alpha"), new Dimension("@aws.region", REGION)), TIME, 0, 0); + } + + @Test + void timestampsComeFromEventsIncludingBeforeCreationAndFutureNotIngestionTime() { + f.put("time", "ERROR", "3", 7.0); + long future = (System.currentTimeMillis() / 60_000 + 10) * 60_000; + f.ingest(TIME, "ERROR", "INFO"); + f.ingest(future, "ERROR", "INFO"); + f.stats("time", TIME, 10, 2); + f.stats("time", future, 10, 2); + assertEquals("Count", f.points("time", List.of(), TIME, REGION).getFirst().unit()); + } + + @Test + void spaceDelimitedFieldsStillFeedValuesAndDimensions() { + MetricFilter filter = definition("space", "[..., status_code, size]", "$size", null); + filter.getMetricTransformations().getFirst().setDimensions(Map.of("Status", "$status_code", "Method", "$1")); + f.service.putMetricFilter(filter, REGION); + f.ingest(TIME, "GET /index.html 200 1534"); + f.stats("space", List.of(new Dimension("Status", "200"), new Dimension("Method", "GET")), TIME, 1534, 1); + } + + @Test + void selectionRunsBeforeBothMatchesAndDefaults() { + MetricFilter filter = definition("selection", "ERROR", "3", 7.0); + filter.setFieldSelectionCriteria("@aws.account = \"" + OTHER_ACCOUNT + "\" AND @aws.region = \"" + REGION + "\""); + f.service.putMetricFilter(filter, REGION); + f.ingest(TIME, "ERROR", "INFO"); + f.stats("selection", TIME, 0, 0); + f.account.set(OTHER_ACCOUNT); + f.group(GROUP, REGION); + f.service.putMetricFilter(filter, REGION); + f.account.set(ACCOUNT); + f.ingest(OTHER_ACCOUNT, GROUP, REGION, TIME, List.of("ERROR", "INFO")); + f.stats("selection", TIME, 0, 0); + f.account.set(OTHER_ACCOUNT); + f.stats("selection", TIME, 10, 2); + } + + @Test + void selectionRegionExcludesBothDefaultAndMatchBeforePublication() { + MetricFilter filter = definition("region", "ERROR", "3", 7.0); + filter.setFieldSelectionCriteria("@aws.region = \"us-east-1\""); + f.service.putMetricFilter(filter, REGION); + f.ingest(TIME, "ERROR", "INFO"); + f.stats("region", TIME, 0, 0); + filter.setFieldSelectionCriteria("@aws.region = \"" + REGION + "\""); + f.service.putMetricFilter(filter, REGION); + f.ingest(TIME, "ERROR", "INFO"); + f.stats("region", TIME, 10, 2); + } + + @Test + void otherGroupsRegionsAndAccountsDoNotUseTheCallersFilters() { + f.put("isolation", "ERROR", "3", 7.0); + f.group("/other", REGION); + f.group(GROUP, "us-east-1"); + f.ingest(null, "/other", REGION, TIME, List.of("ERROR", "INFO")); + f.ingest(null, GROUP, "us-east-1", TIME, List.of("ERROR", "INFO")); + f.account.set(OTHER_ACCOUNT); + f.group(GROUP, REGION); + f.ingest(TIME, "ERROR", "INFO"); + f.stats("isolation", TIME, 0, 0); + f.account.set(ACCOUNT); + f.stats("isolation", TIME, 0, 0); + f.ingest(TIME, "ERROR"); + f.stats("isolation", TIME, 3, 1); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterReferenceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterReferenceTest.java new file mode 100644 index 0000000000..6a1d8c373b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterReferenceTest.java @@ -0,0 +1,144 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; + +class CloudWatchLogsMetricFilterReferenceTest { + + private static final String REGION = "eu-west-1"; + private static final String GROUP = "/synthetic/reference-validation"; + private static final String SCALAR_PATTERN = "{ $.probe = \"yes\" }"; + private static final String WILDCARD_PATTERN = "{ $.values[*] = * }"; + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private CloudWatchLogsMetricFilterService service; + private CloudWatchMetricsService metrics; + + @BeforeEach + void setUp() { + RegionResolver resolver = new RegionResolver(REGION, "000000000000"); + CloudWatchLogsService logs = new CloudWatchLogsService(new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), 10_000, resolver); + logs.createLogGroup(GROUP, null, null, REGION); + metrics = mock(CloudWatchMetricsService.class); + service = new CloudWatchLogsMetricFilterService(logs, metrics, resolver); + } + + static Stream verifiedLiveAwsCases() throws Exception { + try (InputStream input = CloudWatchLogsMetricFilterReferenceTest.class.getResourceAsStream( + "/cloudwatchlogs/metric-filter-reference-validation-aws.json")) { + JsonNode fixture = MAPPER.readTree(input); + assertEquals("Verified on Live AWS", fixture.path("provenance").path("verification").textValue()); + List cases = new ArrayList<>(); + for (JsonNode example : fixture.path("cases")) { + cases.add(Arguments.of(example.path("id").textValue(), example)); + } + return cases.stream(); + } + } + + @ParameterizedTest(name = "{0}") + @MethodSource("verifiedLiveAwsCases") + void putMatchesVerifiedLiveAwsReferenceValidation(String id, JsonNode example) { + MetricFilter definition = definition(example.path("filterPattern").textValue(), + example.path("metricValue").textValue()); + MetricTransformation transformation = definition.getMetricTransformations().getFirst(); + if (example.has("defaultValue")) { + transformation.setDefaultValue(example.path("defaultValue").doubleValue()); + } + if (example.has("dimensions")) { + transformation.setDimensions(MAPPER.convertValue(example.path("dimensions"), new TypeReference<>() {})); + } + JsonNode expected = example.path("expected"); + if (expected.path("accepted").booleanValue()) { + service.putMetricFilter(definition, REGION); + MetricFilter stored = service.findMetricFilter(GROUP, "reference", REGION).orElseThrow(); + assertEquals(definition.getFilterPattern(), stored.getFilterPattern(), id); + MetricTransformation saved = stored.getMetricTransformations().getFirst(); + assertEquals(transformation.getMetricValue(), saved.getMetricValue(), id); + assertEquals(transformation.getDefaultValue(), saved.getDefaultValue(), id); + assertEquals(transformation.getDimensions(), saved.getDimensions(), id); + } else { + AwsException error = assertThrows(AwsException.class, () -> service.putMetricFilter(definition, REGION)); + assertEquals(expected.path("errorCode").textValue(), error.getErrorCode(), id); + assertEquals(expected.path("message").textValue(), error.getMessage(), id); + assertEquals(400, error.getHttpStatus()); + assertTrue(service.findMetricFilter(GROUP, "reference", REGION).isEmpty()); + } + verifyNoInteractions(metrics); + } + + @ParameterizedTest + @ValueSource(strings = {"$.values[*]", "$.values.*", "$.values[*].amount"}) + void wildcardMetricReferencesCannotReplaceThePriorDefinition(String reference) { + service.putMetricFilter(definition(SCALAR_PATTERN, "$.value"), REGION); + + AwsException error = assertThrows(AwsException.class, + () -> service.putMetricFilter(definition(WILDCARD_PATTERN, reference), REGION)); + + assertEquals("InvalidParameterException", error.getErrorCode()); + MetricFilter retained = service.findMetricFilter(GROUP, "reference", REGION).orElseThrow(); + assertEquals(SCALAR_PATTERN, retained.getFilterPattern()); + assertEquals("$.value", retained.getMetricTransformations().getFirst().getMetricValue()); + verifyNoInteractions(metrics); + } + + @ParameterizedTest + @ValueSource(strings = {"$.['*']", "$.['foo*bar']", "$.['values[*]']", "$.values[0]"}) + void singlePropertyReferencesRemainValidIncludingLiteralAsterisks(String reference) { + MetricFilter stored = service.putMetricFilter(definition(SCALAR_PATTERN, reference), REGION); + assertEquals(reference, stored.getMetricTransformations().getFirst().getMetricValue()); + } + + @Test + void wildcardFilterPatternsRemainValidWithANumericMetricValue() { + MetricFilter stored = service.putMetricFilter(definition(WILDCARD_PATTERN, "1"), REGION); + assertEquals(WILDCARD_PATTERN, stored.getFilterPattern()); + } + + @Test + void unmeasuredDimensionWildcardValidationRemainsUnchanged() { + MetricFilter definition = definition(SCALAR_PATTERN, "1"); + definition.getMetricTransformations().getFirst().setDimensions(Map.of("A", "$.values[*]")); + MetricFilter stored = service.putMetricFilter(definition, REGION); + assertEquals(Map.of("A", "$.values[*]"), stored.getMetricTransformations().getFirst().getDimensions()); + } + + private static MetricFilter definition(String pattern, String value) { + MetricTransformation transformation = new MetricTransformation(); + transformation.setMetricName("Value"); + transformation.setMetricNamespace("Synthetic"); + transformation.setMetricValue(value); + MetricFilter filter = new MetricFilter(); + filter.setFilterName("reference"); + filter.setLogGroupName(GROUP); + filter.setFilterPattern(pattern); + filter.setMetricTransformations(List.of(transformation)); + return filter; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterResetTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterResetTest.java new file mode 100644 index 0000000000..f7698f91c6 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterResetTest.java @@ -0,0 +1,120 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.core.common.ContainerTeardown; +import io.github.hectorvent.floci.core.common.Resettable; +import io.github.hectorvent.floci.lifecycle.EmulatorInfoController; +import jakarta.enterprise.inject.Instance; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsMetricFilterRetryTest.await; +import static io.github.hectorvent.floci.services.cloudwatch.logs.PublicationTestSupport.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class CloudWatchLogsMetricFilterResetTest { + private final CloudWatchLogsMetricFilterRetryTest.FaultStore sink = new CloudWatchLogsMetricFilterRetryTest.FaultStore(); + private final PublicationTestSupport f = new PublicationTestSupport(sink); + + @AfterEach + void stop() { f.service.stop(); } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void actualResetControllerDrainsPublicationBeforeStorageWipe(boolean afterWrite) throws Exception { + f.put("reset", "ERROR", "3", 7.0); + sink.fail = true; + f.ingest(TIME, "INFO"); + CountDownLatch inWrite = new CountDownLatch(1); + CountDownLatch releaseWrite = new CountDownLatch(1); + if (afterWrite) { sink.after = (key, datum) -> { inWrite.countDown(); await(releaseWrite); }; } + else { sink.before = (key, datum) -> { inWrite.countDown(); await(releaseWrite); }; } + sink.fail = false; + CountDownLatch resetReachedService = new CountDownLatch(1); + Resettable resettable = new Resettable() { + @Override public void beforeReset() { resetReachedService.countDown(); f.service.beforeReset(); } + @Override public void clear() { resetReachedService.countDown(); f.service.clear(); } + @Override public void afterReset() { f.service.afterReset(); } + }; + EmulatorInfoController controller = controller(resettable); + doAnswer(call -> { f.metricStore.clear(); f.filters.clear(); return null; }).when(f.factory).clearAll(); + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future retry = pool.submit(f.service::retryPending); + assertTrue(inWrite.await(5, TimeUnit.SECONDS)); + Future reset = pool.submit(controller::reset); + assertTrue(resetReachedService.await(5, TimeUnit.SECONDS)); + assertFalse(reset.isDone(), "reset must drain the active store write"); + releaseWrite.countDown(); + retry.get(5, TimeUnit.SECONDS); + reset.get(5, TimeUnit.SECONDS); + f.service.retryPending(); + assertEquals(0, f.service.pendingSamples()); + f.stats("reset", TIME, 0, 0); + assertTrue(f.filters.keys().isEmpty()); + sink.before = (key, datum) -> {}; + sink.after = (key, datum) -> {}; + f.put("reset", "ERROR", "3", 11.0); + f.ingest(TIME, "INFO"); + f.stats("reset", TIME, 11, 1); + } finally { + releaseWrite.countDown(); pool.shutdownNow(); assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + @Test + void resetPausesConcurrentIngestionDuringWipeAndResumesSameInstance() throws Exception { + f.put("reset", "ERROR", "3", 7.0); + CountDownLatch wiping = new CountDownLatch(1); + CountDownLatch finishWipe = new CountDownLatch(1); + doAnswer(call -> { wiping.countDown(); await(finishWipe); f.metricStore.clear(); f.filters.clear(); return null; }) + .when(f.factory).clearAll(); + ExecutorService pool = Executors.newSingleThreadExecutor(); + try { + Future reset = pool.submit(controller(f.service)::reset); + assertTrue(wiping.await(5, TimeUnit.SECONDS)); + int attempts = sink.attempts.size(); + f.ingest(TIME, "INFO"); + assertEquals(attempts, sink.attempts.size(), "publication cannot enter the metric sink during reset"); + finishWipe.countDown(); + reset.get(5, TimeUnit.SECONDS); + f.put("reset", "ERROR", "3", 11.0); + f.ingest(TIME, "INFO"); + f.stats("reset", TIME, 11, 1); + } finally { + finishWipe.countDown(); pool.shutdownNow(); assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + @Test + void failedStorageResetStillResumesAndCancelsOldQueue() { + f.put("reset", "ERROR", "3", 7.0); + sink.fail = true; + f.ingest(TIME, "INFO"); + doThrow(new IllegalStateException("wipe failed")).when(f.factory).clearAll(); + assertThrows(IllegalStateException.class, () -> controller(f.service).reset()); + assertEquals(0, f.service.pendingSamples()); + sink.fail = false; + f.service.retryPending(); + f.ingest(TIME, "INFO"); + f.stats("reset", TIME, 7, 1); + } + + @SuppressWarnings("unchecked") + private EmulatorInfoController controller(Resettable resettable) { + Instance resettables = mock(Instance.class); + when(resettables.iterator()).thenAnswer(call -> List.of(resettable).iterator()); + Instance teardowns = mock(Instance.class); + when(teardowns.iterator()).thenAnswer(call -> List.of().iterator()); + return new EmulatorInfoController(null, null, f.factory, resettables, teardowns, null, null); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterRetryIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterRetryIntegrationTest.java new file mode 100644 index 0000000000..12a09aa8d4 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterRetryIntegrationTest.java @@ -0,0 +1,115 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.mockito.InjectSpy; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import io.restassured.response.Response; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static io.restassured.RestAssured.given; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +@QuarkusTest +@TestProfile(CloudWatchLogsMetricFilterRetryIntegrationTest.ResetIsolationProfile.class) +class CloudWatchLogsMetricFilterRetryIntegrationTest { + /** HTTP reset wipes every service; do not share this application with other test classes. */ + public static class ResetIsolationProfile implements QuarkusTestProfile {} + + @InjectSpy CloudWatchMetricsService metrics; + @Inject CloudWatchLogsMetricFilterService publications; + private static final String AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260916/eu-west-1/logs/aws4_request"; + private static final long TIME = 1_700_000_040_000L; + + @BeforeAll + static void contentTypes() { RestAssuredJsonUtils.configureAwsContentTypes(); } + + @AfterEach + void removeFault() { reset(metrics); } + + @Test + void managedWorkerSurvivesFailedRetryAndPublishesWithoutFurtherIngestion() throws Exception { + String group = "/it/publication/automatic-" + System.nanoTime(); + create(group); + AtomicInteger attempts = new AtomicInteger(); + CountDownLatch published = new CountDownLatch(1); + doAnswer(call -> { + if (attempts.incrementAndGet() <= 2) { throw new IllegalStateException("temporary metric sink failure"); } + Object result = call.callRealMethod(); + published.countDown(); + return result; + }).when(metrics).publishMetricForAccount(anyString(), eq(group), any(), eq("eu-west-1"), anyString()); + ingest(group); + assertTrue(published.await(10, TimeUnit.SECONDS), "no reads, ingestion or manual retry drive the worker"); + assertTrue(attempts.get() >= 3, "a failed retry must not suppress future scheduled ticks"); + statistics(group).then().statusCode(200).body("Datapoints", hasSize(1)) + .body("Datapoints[0].Sum", equalTo(7.0f)).body("Datapoints[0].SampleCount", equalTo(1.0f)); + publications.retryPending(); + statistics(group).then().body("Datapoints[0].SampleCount", equalTo(1.0f)); + logs("DeleteLogGroup", "{\"logGroupName\":\"" + group + "\"}").then().statusCode(200); + } + + @Test + void httpResetCancelsQueuedWorkOnTheLiveCdiInstance() { + String group = "/it/publication/reset-" + System.nanoTime(); + create(group); + doThrow(new IllegalStateException("outage through reset")).when(metrics) + .publishMetricForAccount(anyString(), eq(group), any(), eq("eu-west-1"), anyString()); + ingest(group); + await().atMost(Duration.ofSeconds(10)).until(() -> publications.pendingSamples() > 0); + given().post("/_floci/state/reset").then().statusCode(200).body("status", equalTo("OK")); + assertEquals(0, publications.pendingSamples()); + reset(metrics); + publications.retryPending(); + statistics(group).then().statusCode(200).body("Datapoints", hasSize(0)); + create(group); + ingest(group); + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> statistics(group).then().statusCode(200) + .body("Datapoints[0].Sum", equalTo(7.0f)).body("Datapoints[0].SampleCount", equalTo(1.0f))); + logs("DeleteLogGroup", "{\"logGroupName\":\"" + group + "\"}").then().statusCode(200); + } + + private static void create(String group) { + logs("CreateLogGroup", "{\"logGroupName\":\"" + group + "\"}").then().statusCode(200); + logs("CreateLogStream", "{\"logGroupName\":\"" + group + "\",\"logStreamName\":\"s\"}").then().statusCode(200); + logs("PutMetricFilter", """ + {"logGroupName":"%s","filterName":"retry","filterPattern":"ERROR", + "metricTransformations":[{"metricName":"Value","metricNamespace":"%s","metricValue":"3","defaultValue":7}]} + """.formatted(group, group)).then().statusCode(200); + } + + private static void ingest(String group) { + logs("PutLogEvents", """ + {"logGroupName":"%s","logStreamName":"s","logEvents":[{"timestamp":%d,"message":"INFO"}]} + """.formatted(group, TIME)).then().statusCode(200); + logs("GetLogEvents", "{\"logGroupName\":\"" + group + "\",\"logStreamName\":\"s\"}") + .then().statusCode(200).body("events", hasSize(1)); + } + + private static Response logs(String action, String body) { + return given().contentType("application/x-amz-json-1.1").header("Authorization", AUTH) + .header("X-Amz-Target", "Logs_20140328." + action).body(body).post("/"); + } + + private static Response statistics(String namespace) { + return given().contentType("application/x-amz-json-1.0").header("Authorization", AUTH.replace("/logs/", "/monitoring/")) + .header("X-Amz-Target", "GraniteServiceVersion20100801.GetMetricStatistics").body(""" + {"Namespace":"%s","MetricName":"Value","Period":60,"StartTime":%d,"EndTime":%d, + "Statistics":["Sum","SampleCount"]} + """.formatted(namespace, TIME / 1000, TIME / 1000 + 59)).post("/"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterRetryTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterRetryTest.java new file mode 100644 index 0000000000..5aa0694384 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterRetryTest.java @@ -0,0 +1,493 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.BiConsumer; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import static io.github.hectorvent.floci.services.cloudwatch.logs.PublicationTestSupport.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class CloudWatchLogsMetricFilterRetryTest { + private final FaultStore sink = new FaultStore(); + private final PublicationTestSupport f = new PublicationTestSupport(sink); + + @AfterEach + void stop() { f.service.stop(); } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void realPartialWritesRetryWithoutDuplicatesAndIngestionStillSucceeds(boolean after) { + f.put("broken", "ERROR", "3", 7.0); + AtomicBoolean armed = new AtomicBoolean(true); + BiConsumer fault = (key, datum) -> { + if (datum.getValue() == 7 && armed.compareAndSet(true, false)) { throw new IllegalStateException("sink failure"); } + }; + if (after) { sink.after = fault; } else { sink.before = fault; } + f.ingest(TIME, "ERROR", "INFO", "ERROR"); + assertEquals(1, f.service.pendingSamples()); + f.service.retryPending(); + f.service.retryPending(); + f.stats("broken", TIME, 13, 3); + assertEquals(0, f.service.pendingSamples()); + assertEquals(3, sink.keys().size()); + assertTrue(sink.attempts.size() > sink.attempts.stream().distinct().count()); + f.ingest(TIME, "ERROR", "INFO", "ERROR"); + f.stats("broken", TIME, 26, 6); // new identical accepted logs are legitimate contributions + } + + @Test + void confirmingAnIndeterminateDeleteCancelsOnlyTheAbsentOwnersPendingSamples() { + f.put("gone", "ERROR", "3", null); + sink.fail = true; + f.ingest(TIME, "ERROR"); + f.account.set(OTHER_ACCOUNT); + f.group(GROUP, REGION); + f.put("gone", "ERROR", "9", null); + f.ingest(TIME, "ERROR"); + assertEquals(2, f.service.pendingSamples()); + f.account.set(ACCOUNT); + synchronized (f.filters) { + f.filters.keys().forEach(f.filters::delete); + } + assertTrue(f.service.confirmMetricFilterAbsent(GROUP, "gone", REGION, + () -> assertTrue(Thread.holdsLock(f.filters)))); + assertEquals(1, f.service.pendingSamples(), "confirmed absence must finish deletion's publication cleanup"); + sink.fail = false; + f.service.retryPending(); + f.stats("gone", TIME, 0, 0); + f.account.set(OTHER_ACCOUNT); + f.stats("gone", TIME, 9, 1); + } + + @Test + void failedWorkRetainsCanonicalAccountAndImmutableTransformationAcrossUpdate() { + MetricFilter old = definition("filter", "{ $.value = * }", "$.value", null); + old.getMetricTransformations().getFirst().setDimensions(Map.of("A", "$.a")); + f.service.putMetricFilter(old, REGION); + sink.fail = true; + f.ingest(TIME, "{\"value\":3,\"a\":\"alpha\"}"); + MetricFilter replacement = definition("filter", "ERROR", "99", 17.0); + replacement.getMetricTransformations().getFirst().setMetricNamespace("New"); + replacement.getMetricTransformations().getFirst().setMetricName("NewName"); + replacement.getMetricTransformations().getFirst().setUnit("Bytes"); + f.service.putMetricFilter(replacement, REGION); + clearInvocations(f.resolver); + f.account.set(OTHER_ACCOUNT); // no request context survives on the worker + sink.fail = false; + f.service.retryPending(); + verify(f.resolver, never()).getAccountId(); + f.stats("filter", List.of(new Dimension("A", "alpha")), TIME, 0, 0); + f.account.set(ACCOUNT); + f.stats("filter", List.of(new Dimension("A", "alpha")), TIME, 3, 1); + assertEquals("Count", f.points("filter", List.of(new Dimension("A", "alpha")), TIME, REGION).getFirst().unit()); + assertTrue(f.metrics.listMetrics("New", null, null, REGION).isEmpty()); + f.ingest(TIME, "ERROR"); + assertEquals(1, f.metrics.listMetrics("New", "NewName", null, REGION).size()); + } + + @Test + void accountIsResolvedOnceForImmediateAndRetainedPublication() { + f.put("account", "ERROR", "3", 7.0); + clearInvocations(f.resolver); + sink.fail = true; + f.ingest(TIME, "ERROR", "INFO"); + verify(f.resolver, times(1)).getAccountId(); + f.account.set(OTHER_ACCOUNT); + sink.fail = false; + f.service.retryPending(); + verify(f.resolver, times(1)).getAccountId(); + f.stats("account", TIME, 0, 0); + f.account.set(ACCOUNT); + f.stats("account", TIME, 10, 2); + } + + @Test + void failingFilterDoesNotStopOtherFiltersOrSuccessfulLogsIngestion() { + f.put("broken", "ERROR", "3", null); + f.put("healthy", "ERROR", "5", null); + sink.before = (key, datum) -> { + if (datum.getNamespace().equals("broken")) { throw new IllegalStateException("one namespace unavailable"); } + }; + f.ingest(TIME, "ERROR"); + f.stats("healthy", TIME, 5, 1); + assertEquals(1, f.service.pendingSamples()); + sink.before = (key, datum) -> {}; + f.service.retryPending(); + f.stats("broken", TIME, 3, 1); + } + + @Test + void sampleBoundReportsOverflowAtErrorPreservesQueuedIdsAndReleasesCapacity() { + f.put("bound", "ERROR", "3", 7.0); + sink.fail = true; + List records = new CopyOnWriteArrayList<>(); + Logger logger = Logger.getLogger(CloudWatchLogsMetricFilterService.class.getName()); + Handler handler = new Handler() { + @Override public void publish(LogRecord record) { records.add(record); } + @Override public void flush() {} + @Override public void close() {} + }; + logger.addHandler(handler); + try { + f.ingest(null, GROUP, REGION, TIME, Collections.nCopies(10_003, "INFO")); + assertEquals(10_000, f.service.pendingSamples(), "bound counts samples, not batches"); + assertTrue(records.stream().anyMatch(r -> r.getLevel().intValue() >= Level.SEVERE.intValue() + && formatted(r).contains(ACCOUNT) && formatted(r).contains(GROUP) + && formatted(r).contains("region=" + REGION) && formatted(r).contains("filter=bound") + && formatted(r).contains("dropped=3")), "overflow must be ERROR with ownership and dropped count"); + List queuedKeys = List.copyOf(sink.attempts.subList(0, 10_000)); + sink.fail = false; + f.service.retryPending(); + f.stats("bound", TIME, 70_000, 10_000); + assertEquals(0, f.service.pendingSamples()); + assertEquals(Set.copyOf(queuedKeys), sink.keys()); + sink.fail = true; + f.ingest(TIME, "INFO"); + assertEquals(1, f.service.pendingSamples()); + f.service.deleteMetricFilter(GROUP, "bound", REGION); + assertEquals(0, f.service.pendingSamples()); + f.put("bound", "ERROR", "3", 7.0); + f.ingest(TIME, "INFO"); + assertEquals(1, f.service.pendingSamples()); + } finally { logger.removeHandler(handler); } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void deleteCancelsOnlyItsAccountRegionGroupAndFilter(boolean group) { + f.put("same", "ERROR", "3", null); + f.put("sibling", "ERROR", "5", null); + sink.fail = true; + f.ingest(TIME, "ERROR"); + f.group(GROUP, "us-east-1"); + f.service.putMetricFilter(definition("same", "ERROR", "3", null), "us-east-1"); + f.ingest(null, GROUP, "us-east-1", TIME, List.of("ERROR")); + f.group("/other", REGION); + MetricFilter other = definition("same", "ERROR", "3", null); + other.setLogGroupName("/other"); + f.service.putMetricFilter(other, REGION); + f.ingest(null, "/other", REGION, TIME, List.of("ERROR")); + f.account.set(OTHER_ACCOUNT); + f.group(GROUP, REGION); + f.put("same", "ERROR", "3", null); + f.ingest(TIME, "ERROR"); + f.account.set(ACCOUNT); + if (group) { f.logs.deleteLogGroup(GROUP, REGION); } + else { f.service.deleteMetricFilter(GROUP, "same", REGION); } + assertEquals(group ? 3 : 4, f.service.pendingSamples()); + sink.fail = false; + f.service.retryPending(); + f.stats("same", TIME, 3, 1); // other group only + f.stats("sibling", TIME, group ? 0 : 5, group ? 0 : 1); + assertEquals(3, f.points("same", List.of(), TIME, "us-east-1").getFirst().sum()); + f.account.set(OTHER_ACCOUNT); + f.stats("same", TIME, 3, 1); + } + + @Test + void clearOnTheSameInstanceReleasesQueuedWork() { + f.put("reset", "ERROR", "3", 7.0); + sink.fail = true; + f.ingest(TIME, "INFO"); + assertEquals(1, f.service.pendingSamples()); + f.service.clear(); + assertEquals(0, f.service.pendingSamples()); + sink.fail = false; + f.service.retryPending(); + f.stats("reset", TIME, 0, 0); + f.ingest(TIME, "INFO"); + f.stats("reset", TIME, 7, 1); + } + + @ParameterizedTest + @ValueSource(strings = {"filter", "group", "update", "clear", "stop"}) + void mutationRacingRetryWaitsForTheActiveWriteAndNeverReinterpretsIt(String mutation) throws Exception { + f.put("race", "ERROR", "3", 7.0); + sink.fail = true; + f.ingest(TIME, "INFO"); + sink.fail = false; + CountDownLatch entered = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + sink.before = (key, datum) -> { entered.countDown(); await(release); }; + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future retry = pool.submit(f.service::retryPending); + assertTrue(entered.await(5, TimeUnit.SECONDS)); + CountDownLatch started = new CountDownLatch(1); + Future change = pool.submit(() -> { + started.countDown(); + switch (mutation) { + case "filter" -> f.service.deleteMetricFilter(GROUP, "race", REGION); + case "group" -> f.logs.deleteLogGroup(GROUP, REGION); + case "clear" -> f.service.clear(); + case "stop" -> f.service.stop(); + case "update" -> { + MetricFilter next = definition("race", "ERROR", "99", 17.0); + next.getMetricTransformations().getFirst().setMetricNamespace("new"); + f.service.putMetricFilter(next, REGION); + } + default -> fail(mutation); + } + }); + assertTrue(started.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, () -> change.get(100, TimeUnit.MILLISECONDS)); + release.countDown(); + retry.get(5, TimeUnit.SECONDS); + change.get(5, TimeUnit.SECONDS); + sink.before = (key, datum) -> {}; + assertEquals(0, f.service.pendingSamples()); + f.service.retryPending(); + f.stats("race", TIME, 7, 1); + if (mutation.equals("stop")) { + f.ingest(TIME, "INFO"); + f.stats("race", TIME, 7, 1); + return; + } + if (mutation.equals("group")) { f.group(GROUP, REGION); } + if (!mutation.equals("update")) { f.put("race", "ERROR", "3", 11.0); } + f.ingest(TIME, "INFO"); + f.stats("race", TIME, mutation.equals("update") ? 7 : 18, mutation.equals("update") ? 1 : 2); + if (mutation.equals("update")) { f.stats("new", TIME, 17, 1); } + } finally { + release.countDown(); pool.shutdownNow(); assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + @Test + void simultaneousStreamsAndConcurrentRetryTicksKeepEveryAcceptedContribution() throws Exception { + f.put("streams", "ERROR", "3", 7.0); + f.logs.createLogStream(GROUP, "other", REGION); + sink.fail = true; + ExecutorService pool = Executors.newFixedThreadPool(2); + CountDownLatch start = new CountDownLatch(1); + try { + Future first = pool.submit(() -> { await(start); f.ingest(TIME, "ERROR", "INFO"); }); + Future second = pool.submit(() -> { + await(start); + f.logs.putLogEvents(GROUP, "other", List.of(Map.of("timestamp", TIME, "message", "INFO")), REGION); + }); + start.countDown(); + first.get(5, TimeUnit.SECONDS); + second.get(5, TimeUnit.SECONDS); + f.service.publishQueued(); + assertEquals(3, f.service.pendingSamples()); + sink.fail = false; + Future retryOne = pool.submit(f.service::retryPending); + Future retryTwo = pool.submit(f.service::retryPending); + retryOne.get(5, TimeUnit.SECONDS); + retryTwo.get(5, TimeUnit.SECONDS); + f.stats("streams", TIME, 17, 3); + assertEquals(0, f.service.pendingSamples()); + } finally { + start.countDown(); pool.shutdownNow(); assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void eitherDisabledServicePreventsPublicationAndWorkerStartup(boolean logsDisabled) { + f.service.start(!logsDisabled, logsDisabled); + f.put("disabled", "ERROR", "3", 7.0); + f.ingest(TIME, "ERROR"); + assertFalse(f.service.publisherRunning()); + f.stats("disabled", TIME, 0, 0); + assertEquals(0, f.service.pendingSamples()); + } + + @Test + void enabledWorkerStartsOnceAndStopIsIdempotentAndCancelsPending() { + f.service.start(true, true); + f.service.start(true, true); + assertTrue(f.service.publisherRunning()); + f.put("stop", "ERROR", "3", 7.0); + sink.fail = true; + f.ingest(TIME, "INFO"); + f.service.stop(); + f.service.stop(); + assertFalse(f.service.publisherRunning()); + assertEquals(0, f.service.pendingSamples()); + sink.fail = false; + f.service.retryPending(); + f.ingest(TIME, "INFO"); + f.stats("stop", TIME, 0, 0); + } + + @ParameterizedTest + @ValueSource(ints = {1, 100}) + void loggingOneHundredFailedTicksKeepOneDetailPerOutageAndBoundedSummaries(int filters) { + for (int i = 0; i < filters; i++) { f.put("outage-" + i, "ERROR", "3", 7.0); } + sink.fail = true; + try (FailureLogs logs = new FailureLogs()) { + f.ingest(TIME, "INFO"); + for (int tick = 0; tick < 100; tick++) { f.service.retryPending(); } + assertEquals(101 * filters, sink.attempts.size(), "diagnostic throttling must not throttle retries"); + assertEquals(filters, f.service.pendingSamples()); + assertEquals(filters, logs.details(), "one stack per owning filter outage, not per tick or sample"); + assertEquals(1, logs.summaries(), "one aggregate stack-free warning after 60 failed ticks, even for 100 filters"); + LogRecord summary = logs.warnings.stream().filter(record -> record.getThrown() == null).findFirst().orElseThrow(); + assertTrue(formatted(summary).contains("failedSamples=" + filters)); + assertTrue(formatted(summary).contains("filters=" + filters)); + } + } + + @Test + void loggingNewFailedIngestionDoesNotRepeatAnOutstandingOutagesStack() { + f.put("outage", "ERROR", "3", 7.0); + sink.fail = true; + try (FailureLogs logs = new FailureLogs()) { + for (int batch = 0; batch < 100; batch++) { f.ingest(TIME, "INFO"); } + assertEquals(100, f.service.pendingSamples()); + assertEquals(100, sink.attempts.size()); + assertEquals(1, logs.details()); + assertEquals(0, logs.summaries(), "new batches must not bypass retry summary throttling"); + } + } + + @Test + void loggingRecoveryThenNewOutageGetsFreshDetailAndSummaryWindow() { + f.put("outage", "ERROR", "3", 7.0); + sink.fail = true; + try (FailureLogs logs = new FailureLogs()) { + f.ingest(TIME, "INFO"); + for (int tick = 0; tick < 100; tick++) { f.service.retryPending(); } + sink.fail = false; + f.service.retryPending(); + assertEquals(0, f.service.pendingSamples()); + f.stats("outage", TIME, 7, 1); + sink.fail = true; + f.ingest(TIME, "INFO"); + assertEquals(2, logs.details(), "recovery must allow fresh detail for the next outage"); + for (int tick = 0; tick < 59; tick++) { f.service.retryPending(); } + assertEquals(1, logs.summaries(), "recovery must restart the summary window"); + f.service.retryPending(); + assertEquals(2, logs.summaries()); + assertEquals(2, logs.details()); + assertEquals(163, sink.attempts.size()); + } + } + + @ParameterizedTest + @ValueSource(strings = {"filter", "group", "clear", "reset"}) + void loggingLastCancellationOrResetReleasesSuppressionAndRestartsSummaryWindow(String operation) { + f.put("outage", "ERROR", "3", 7.0); + sink.fail = true; + try (FailureLogs logs = new FailureLogs()) { + f.ingest(TIME, "INFO"); + for (int tick = 0; tick < 59; tick++) { f.service.retryPending(); } + switch (operation) { + case "filter" -> f.service.deleteMetricFilter(GROUP, "outage", REGION); + case "group" -> f.logs.deleteLogGroup(GROUP, REGION); + case "clear" -> f.service.clear(); + case "reset" -> { + f.service.beforeReset(); + f.service.clear(); + f.service.afterReset(); + } + default -> fail(operation); + } + assertEquals(0, f.service.pendingSamples()); + if (operation.equals("group")) { f.group(GROUP, REGION); } + f.put("outage", "ERROR", "3", 7.0); + f.ingest(TIME, "INFO"); + f.service.retryPending(); + assertEquals(2, logs.details()); + assertEquals(0, logs.summaries(), "cancelling the final pending owner must restart the summary window"); + for (int tick = 0; tick < 59; tick++) { f.service.retryPending(); } + assertEquals(1, logs.summaries()); + assertEquals(2, logs.details()); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void loggingScopedCancellationDoesNotRearmAnUnrelatedPendingOutage(boolean group) { + f.put("outage", "ERROR", "3", 7.0); + f.group("/other", REGION); + MetricFilter sibling = definition("sibling", "ERROR", "3", 7.0); + sibling.setLogGroupName("/other"); + f.service.putMetricFilter(sibling, REGION); + sink.fail = true; + try (FailureLogs logs = new FailureLogs()) { + f.ingest(TIME, "INFO"); + f.ingest(null, "/other", REGION, TIME, List.of("INFO")); + for (int tick = 0; tick < 10; tick++) { f.service.retryPending(); } + if (group) { + f.logs.deleteLogGroup(GROUP, REGION); + f.group(GROUP, REGION); + } else { + f.service.deleteMetricFilter(GROUP, "outage", REGION); + } + assertEquals(1, f.service.pendingSamples()); + f.put("outage", "ERROR", "3", 7.0); + f.ingest(TIME, "INFO"); + f.ingest(null, "/other", REGION, TIME, List.of("INFO")); + for (int tick = 0; tick < 100; tick++) { f.service.retryPending(); } + assertEquals(3, f.service.pendingSamples()); + assertEquals(3, logs.details(), "only the cancelled owner gets new detail; its sibling stays suppressed"); + assertEquals(1, logs.summaries()); + } + } + + private static class FailureLogs extends Handler implements AutoCloseable { + private final Logger logger = Logger.getLogger(CloudWatchLogsMetricFilterService.class.getName()); + private final List warnings = new CopyOnWriteArrayList<>(); + + FailureLogs() { logger.addHandler(this); } + @Override public void publish(LogRecord record) { + if (record.getLevel().intValue() == Level.WARNING.intValue()) { warnings.add(record); } + } + long details() { return warnings.stream().filter(record -> record.getThrown() != null).count(); } + long summaries() { return warnings.stream().filter(record -> record.getThrown() == null).count(); } + @Override public void flush() {} + @Override public void close() { logger.removeHandler(this); } + } + + static void await(CountDownLatch latch) { + try { assertTrue(latch.await(5, TimeUnit.SECONDS)); } + catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException(e); } + } + + private static String formatted(LogRecord r) { + return MessageFormat.format(r.getMessage(), r.getParameters() == null ? new Object[0] : r.getParameters()); + } + + static class FaultStore extends InMemoryStorage { + volatile boolean fail; + volatile BiConsumer before = (key, value) -> {}; + volatile BiConsumer after = (key, value) -> {}; + final List attempts = Collections.synchronizedList(new ArrayList<>()); + @Override public void put(String key, MetricDatum value) { + attempts.add(key); + if (fail) { throw new IllegalStateException("storage offline"); } + before.accept(key, value); + super.put(key, value); + after.accept(key, value); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterServiceTest.java new file mode 100644 index 0000000000..2243e946e8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterServiceTest.java @@ -0,0 +1,520 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogEvent; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; + +/** + * The metric filter operations against the rules the Logs API documents. What a filter publishes + * for a stored batch is in {@link CloudWatchLogsMetricFilterPublishingTest}. + */ +class CloudWatchLogsMetricFilterServiceTest { + + private static final String REGION = "us-east-1"; + private static final String GROUP = "/app/api"; + private static final String OTHER_GROUP = "/app/worker"; + + private CloudWatchLogsMetricFilterService service; + private CloudWatchMetricsService metrics; + + @BeforeEach + void setUp() { + RegionResolver resolver = new RegionResolver(REGION, "000000000000"); + CloudWatchLogsService logs = new CloudWatchLogsService(new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), 10_000, resolver); + logs.createLogGroup(GROUP, null, null, REGION); + logs.createLogGroup(OTHER_GROUP, null, null, REGION); + metrics = mock(CloudWatchMetricsService.class); + service = new CloudWatchLogsMetricFilterService(logs, metrics, resolver); + } + + private static MetricTransformation transformation(String name, String namespace, String value) { + MetricTransformation t = new MetricTransformation(); + t.setMetricName(name); + t.setMetricNamespace(namespace); + t.setMetricValue(value); + return t; + } + + private static MetricFilter filter(String group, String name, String pattern, MetricTransformation transformation) { + MetricFilter filter = new MetricFilter(); + filter.setLogGroupName(group); + filter.setFilterName(name); + filter.setFilterPattern(pattern); + filter.setMetricTransformations(transformation == null ? List.of() : List.of(transformation)); + return filter; + } + + private static MetricFilter errorCounter(String group, String name) { + return filter(group, name, "ERROR", transformation("ErrorCount", "App", "1")); + } + + private static AwsException rejected(Runnable call, String code) { + AwsException e = assertThrows(AwsException.class, call::run); + assertEquals(code, e.getErrorCode(), e.getMessage()); + return e; + } + + // ──────────────────────────── PutMetricFilter ──────────────────────────── + + @Test + void putStoresTheFilterAndDescribeReturnsIt() { + MetricTransformation t = transformation("ErrorCount", "App", "1"); + t.setDefaultValue(0.0); + t.setUnit("Count"); + MetricFilter definition = filter(GROUP, "errors", "ERROR", t); + definition.setApplyOnTransformedLogs(false); + definition.setFieldSelectionCriteria("@aws.region = \"us-east-1\""); + definition.setEmitSystemFieldDimensions(List.of("@aws.account")); + + MetricFilter stored = service.putMetricFilter(definition, REGION); + + List described = service.describeMetricFilters(GROUP, null, null, null, null, null, REGION) + .metricFilters(); + assertEquals(1, described.size()); + MetricFilter f = described.getFirst(); + assertEquals("errors", f.getFilterName()); + assertEquals(GROUP, f.getLogGroupName()); + assertEquals("ERROR", f.getFilterPattern()); + assertEquals("ErrorCount", f.getMetricTransformations().getFirst().getMetricName()); + assertEquals("App", f.getMetricTransformations().getFirst().getMetricNamespace()); + assertEquals("1", f.getMetricTransformations().getFirst().getMetricValue()); + assertEquals(0.0, f.getMetricTransformations().getFirst().getDefaultValue()); + assertEquals("Count", f.getMetricTransformations().getFirst().getUnit()); + assertEquals(Boolean.FALSE, f.getApplyOnTransformedLogs()); + assertEquals("@aws.region = \"us-east-1\"", f.getFieldSelectionCriteria()); + assertEquals(List.of("@aws.account"), f.getEmitSystemFieldDimensions()); + assertTrue(f.getCreationTime() > 0); + assertEquals(stored.getCreationTime(), f.getCreationTime()); + assertEquals(1, service.countMetricFilters(GROUP, REGION)); + assertEquals(0, service.countMetricFilters(OTHER_GROUP, REGION)); + } + + @Test + void putReplacesAFilterOfTheSameNameAndKeepsItsCreationTime() throws InterruptedException { + long created = service.putMetricFilter(errorCounter(GROUP, "errors"), REGION).getCreationTime(); + Thread.sleep(2); + + service.putMetricFilter(filter(GROUP, "errors", "FATAL", transformation("FatalCount", "App", "2")), REGION); + + List described = service.describeMetricFilters(GROUP, null, null, null, null, null, REGION) + .metricFilters(); + assertEquals(1, described.size()); + assertEquals("FATAL", described.getFirst().getFilterPattern()); + assertEquals("FatalCount", described.getFirst().getMetricTransformations().getFirst().getMetricName()); + assertEquals(created, described.getFirst().getCreationTime(), "the creation time is the first put's"); + } + + @Test + void putNeedsAnExistingLogGroup() { + rejected(() -> service.putMetricFilter(errorCounter("/missing", "errors"), REGION), "ResourceNotFoundException"); + rejected(() -> service.putMetricFilter(errorCounter(GROUP, "errors"), "eu-west-1"), "ResourceNotFoundException"); + rejected(() -> service.putMetricFilter(errorCounter(null, "errors"), REGION), "InvalidParameterException"); + } + + @Test + void filterNameFollowsTheApiPattern() { + rejected(() -> service.putMetricFilter(errorCounter(GROUP, null), REGION), "InvalidParameterException"); + rejected(() -> service.putMetricFilter(errorCounter(GROUP, "a:b"), REGION), "InvalidParameterException"); + rejected(() -> service.putMetricFilter(errorCounter(GROUP, "a*"), REGION), "InvalidParameterException"); + rejected(() -> service.putMetricFilter(errorCounter(GROUP, "n".repeat(513)), REGION), "InvalidParameterException"); + service.putMetricFilter(errorCounter(GROUP, "spaces and | pipes are fine"), REGION); + } + + @Test + void filterPatternMustParse() { + AwsException e = rejected(() -> service.putMetricFilter( + filter(GROUP, "bad", "{ $.a = }", transformation("M", "N", "1")), REGION), "InvalidParameterException"); + assertTrue(e.getMessage().startsWith("Invalid filter pattern"), e.getMessage()); + rejected(() -> service.putMetricFilter(filter(GROUP, "none", null, transformation("M", "N", "1")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "long", "x".repeat(1025), transformation("M", "N", "1")), + REGION), "InvalidParameterException"); + service.putMetricFilter(filter(GROUP, "everything", "", transformation("All", "App", "1")), REGION); + } + + @Test + void exactlyOneTransformationIsRequired() { + rejected(() -> service.putMetricFilter(filter(GROUP, "none", "ERROR", null), REGION), "InvalidParameterException"); + MetricFilter two = filter(GROUP, "two", "ERROR", transformation("A", "N", "1")); + two.setMetricTransformations(List.of(transformation("A", "N", "1"), transformation("B", "N", "1"))); + rejected(() -> service.putMetricFilter(two, REGION), "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "noname", "ERROR", transformation(null, "N", "1")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "badname", "ERROR", transformation("a:b", "N", "1")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "nons", "ERROR", transformation("A", null, "1")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "badns", "ERROR", transformation("A", "n$s", "1")), REGION), + "InvalidParameterException"); + } + + @Test + void metricValueIsANumberOrAFieldThePatternCanSupply() { + service.putMetricFilter(filter(GROUP, "literal", "ERROR", transformation("A", "N", "1")), REGION); + service.putMetricFilter(filter(GROUP, "decimal", "ERROR", transformation("A", "N", "0.5")), REGION); + service.putMetricFilter(filter(GROUP, "json", "{ $.latency = * }", transformation("A", "N", "$.latency")), REGION); + service.putMetricFilter(filter(GROUP, "delimited", "[..., size]", transformation("A", "N", "$size")), REGION); + service.putMetricFilter(filter(GROUP, "position", "[..., size]", transformation("A", "N", "$3")), REGION); + + rejected(() -> service.putMetricFilter(filter(GROUP, "x", "ERROR", transformation("A", "N", null)), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "x", "ERROR", transformation("A", "N", "abc")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "x", "ERROR", transformation("A", "N", "$.latency")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "x", "[..., size]", transformation("A", "N", "$bytes")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "x", "[..., size]", transformation("A", "N", "$.size")), REGION), + "InvalidParameterException"); + rejected(() -> service.putMetricFilter(filter(GROUP, "x", "ERROR", transformation("A", "N", "1".repeat(101))), REGION), + "InvalidParameterException"); + } + + @ParameterizedTest + @ValueSource(strings = {"1e309", "-1e309", "1e9999999999"}) + void numericLiteralMustBeFinite(String value) { + service.putMetricFilter(errorCounter(GROUP, "existing"), REGION); + rejected(() -> service.putMetricFilter( + filter(GROUP, "existing", "WARN", transformation("A", "N", value)), REGION), + "InvalidParameterException"); + assertEquals("ERROR", service.findMetricFilter(GROUP, "existing", REGION).orElseThrow().getFilterPattern()); + } + + @ParameterizedTest + @ValueSource(doubles = {Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}) + void directServiceDefaultValueMustBeFinite(double value) { + MetricTransformation t = transformation("A", "N", "1"); + t.setDefaultValue(value); + rejected(() -> service.putMetricFilter(filter(GROUP, "bad-default", "ERROR", t), REGION), + "InvalidParameterException"); + assertTrue(service.findMetricFilter(GROUP, "bad-default", REGION).isEmpty()); + } + + @Test + void ingestionDoesNotPublishANonFiniteStoredLiteral() { + MetricFilter stored = service.putMetricFilter(errorCounter(GROUP, "legacy"), REGION); + stored.getMetricTransformations().getFirst().setMetricValue("1e309"); + LogEvent event = new LogEvent(); + event.setMessage("ERROR"); + event.setTimestamp(1_700_000_000_000L); + + service.onLogEventsIngested(new LogEventsIngested(null, REGION, GROUP, "s", List.of(event))); + assertEquals(0, service.queuedSamples()); + service.publishQueued(); + + verifyNoInteractions(metrics); + } + + @Test + void dimensionsFollowTheReferenceRules() { + MetricTransformation ok = transformation("A", "N", "1"); + ok.setDimensions(Map.of("eventType", "$.eventType", "ip", "$.sourceIPAddress")); + service.putMetricFilter(filter(GROUP, "ok", "{ $.eventType = \"*\" }", ok), REGION); + + MetricTransformation four = transformation("A", "N", "1"); + four.setDimensions(Map.of("a", "$.a", "b", "$.b", "c", "$.c", "d", "$.d")); + rejected(() -> service.putMetricFilter(filter(GROUP, "four", "{ $.a = 1 }", four), REGION), "InvalidParameterException"); + + MetricTransformation onTerms = transformation("A", "N", "1"); + onTerms.setDimensions(Map.of("a", "$.a")); + rejected(() -> service.putMetricFilter(filter(GROUP, "terms", "ERROR", onTerms), REGION), "InvalidParameterException"); + + MetricTransformation withDefault = transformation("A", "N", "1"); + withDefault.setDimensions(Map.of("a", "$.a")); + withDefault.setDefaultValue(0.0); + rejected(() -> service.putMetricFilter(filter(GROUP, "default", "{ $.a = 1 }", withDefault), REGION), + "InvalidParameterException"); + + MetricTransformation badReference = transformation("A", "N", "1"); + badReference.setDimensions(Map.of("a", "$bytes")); + rejected(() -> service.putMetricFilter(filter(GROUP, "ref", "[..., size]", badReference), REGION), + "InvalidParameterException"); + + MetricTransformation badName = transformation("A", "N", "1"); + badName.setDimensions(Map.of(":a", "$.a")); + rejected(() -> service.putMetricFilter(filter(GROUP, "name", "{ $.a = 1 }", badName), REGION), + "InvalidParameterException"); + } + + @Test + void unitMustBeAStandardUnit() { + MetricTransformation t = transformation("A", "N", "1"); + t.setUnit("Furlongs"); + rejected(() -> service.putMetricFilter(filter(GROUP, "unit", "ERROR", t), REGION), "InvalidParameterException"); + t.setUnit("Bytes/Second"); + service.putMetricFilter(filter(GROUP, "unit", "ERROR", t), REGION); + } + + @Test + void systemFieldDimensionsAreTheTwoDocumentedOnesAndCountTowardTheLimit() { + MetricFilter bad = errorCounter(GROUP, "sys"); + bad.setEmitSystemFieldDimensions(List.of("@aws.host")); + rejected(() -> service.putMetricFilter(bad, REGION), "InvalidParameterException"); + + MetricTransformation two = transformation("A", "N", "1"); + two.setDimensions(Map.of("a", "$.a", "b", "$.b")); + MetricFilter crowded = filter(GROUP, "sys", "{ $.a = 1 }", two); + crowded.setEmitSystemFieldDimensions(List.of("@aws.account", "@aws.region")); + rejected(() -> service.putMetricFilter(crowded, REGION), "InvalidParameterException"); + + crowded.setEmitSystemFieldDimensions(List.of("@aws.region")); + service.putMetricFilter(crowded, REGION); + } + + @Test + void aLogGroupHoldsAtMostOneHundredFilters() { + for (int i = 0; i < 100; i++) { + service.putMetricFilter(errorCounter(GROUP, "f" + i), REGION); + } + rejected(() -> service.putMetricFilter(errorCounter(GROUP, "f100"), REGION), "LimitExceededException"); + service.putMetricFilter(errorCounter(GROUP, "f0"), REGION); + service.putMetricFilter(errorCounter(OTHER_GROUP, "f100"), REGION); + assertEquals(100, service.countMetricFilters(GROUP, REGION)); + } + + /** Two creates racing for the last slot must not both get it. */ + @Test + void theLimitHoldsUnderConcurrentPuts() throws Exception { + for (int i = 0; i < 99; i++) { + service.putMetricFilter(errorCounter(GROUP, "f" + i), REGION); + } + int threads = 8; + CountDownLatch start = new CountDownLatch(1); + AtomicInteger accepted = new AtomicInteger(); + AtomicInteger refused = new AtomicInteger(); + ExecutorService pool = Executors.newFixedThreadPool(threads); + List> futures = new ArrayList<>(); + for (int i = 0; i < threads; i++) { + String name = "race" + i; + futures.add(pool.submit(() -> { + start.await(); + try { + service.putMetricFilter(errorCounter(GROUP, name), REGION); + accepted.incrementAndGet(); + } catch (AwsException e) { + assertEquals("LimitExceededException", e.getErrorCode()); + refused.incrementAndGet(); + } + return null; + })); + } + start.countDown(); + for (Future future : futures) { + future.get(); + } + pool.shutdown(); + + assertEquals(1, accepted.get()); + assertEquals(threads - 1, refused.get()); + assertEquals(100, service.countMetricFilters(GROUP, REGION)); + } + + // ──────────────────────────── DescribeMetricFilters ──────────────────────────── + + @Test + void describeSortsByNameAndFiltersByPrefixMetricAndNamespace() { + service.putMetricFilter(filter(GROUP, "beta", "B", transformation("Bees", "App", "1")), REGION); + service.putMetricFilter(filter(GROUP, "alpha", "A", transformation("Ants", "App", "1")), REGION); + service.putMetricFilter(filter(GROUP, "other", "O", transformation("Owls", "Zoo", "1")), REGION); + service.putMetricFilter(filter(OTHER_GROUP, "alpha", "A", transformation("Ants", "App", "1")), REGION); + + assertEquals(List.of("alpha", "beta", "other"), names(service.describeMetricFilters(GROUP, null, null, null, + null, null, REGION).metricFilters())); + assertEquals(List.of("alpha"), names(service.describeMetricFilters(GROUP, "al", null, null, null, null, REGION) + .metricFilters())); + assertEquals(List.of("alpha", "alpha"), names(service.describeMetricFilters(null, null, "Ants", "App", + null, null, REGION).metricFilters()), "a metric search spans the Region's groups"); + assertEquals(List.of("beta"), names(service.describeMetricFilters(null, null, "Bees", "App", null, null, REGION) + .metricFilters())); + assertEquals(List.of("alpha", "alpha", "beta", "other"), names(service.describeMetricFilters(null, null, null, + null, null, null, REGION).metricFilters())); + assertEquals(List.of(), names(service.describeMetricFilters(null, null, null, null, null, null, "eu-west-1") + .metricFilters())); + assertEquals(List.of("alpha", "alpha", "beta", "other"), names(service.describeMetricFilters(null, "al", null, + null, null, null, REGION).metricFilters()), "a prefix without a group is ignored, as on AWS"); + } + + @Test + void describeRejectsWhatTheApiRejects() { + rejected(() -> service.describeMetricFilters("/missing", null, null, null, null, null, REGION), + "ResourceNotFoundException"); + rejected(() -> service.describeMetricFilters(GROUP, null, null, null, null, 0, REGION), "InvalidParameterException"); + rejected(() -> service.describeMetricFilters(GROUP, null, null, null, null, 51, REGION), "InvalidParameterException"); + rejected(() -> service.describeMetricFilters(GROUP, null, null, null, "garbage", null, REGION), + "InvalidParameterException"); + rejected(() -> service.describeMetricFilters(GROUP, null, null, null, "99", null, REGION), + "InvalidParameterException"); + } + + @Test + void describeRequiresMetricNameAndNamespaceTogether() { + service.putMetricFilter(filter(GROUP, "beta", "B", transformation("Bees", "App", "1")), REGION); + String message = "Describe Metric Filters request must contain both MetricName and MetricNamespace"; + + AwsException nameOnly = rejected(() -> service.describeMetricFilters(null, null, "Bees", null, null, null, REGION), + "InvalidParameterException"); + assertEquals(message, nameOnly.getMessage()); + AwsException namespaceOnly = rejected(() -> service.describeMetricFilters(null, null, null, "App", null, null, + REGION), "InvalidParameterException"); + assertEquals(message, namespaceOnly.getMessage()); + assertEquals(List.of(), names(service.describeMetricFilters(null, null, "Ants", "App", null, null, REGION) + .metricFilters()), "both supplied with no match is an empty page, not an error"); + } + + @Test + void describePaginates() { + for (int i = 0; i < 5; i++) { + service.putMetricFilter(errorCounter(GROUP, "f" + i), REGION); + } + + CloudWatchLogsMetricFilterService.DescribeMetricFiltersResult first = + service.describeMetricFilters(GROUP, null, null, null, null, 2, REGION); + assertEquals(List.of("f0", "f1"), names(first.metricFilters())); + assertNotNull(first.nextToken()); + CloudWatchLogsMetricFilterService.DescribeMetricFiltersResult second = + service.describeMetricFilters(GROUP, null, null, null, first.nextToken(), 2, REGION); + assertEquals(List.of("f2", "f3"), names(second.metricFilters())); + CloudWatchLogsMetricFilterService.DescribeMetricFiltersResult last = + service.describeMetricFilters(GROUP, null, null, null, second.nextToken(), 2, REGION); + assertEquals(List.of("f4"), names(last.metricFilters())); + assertNull(last.nextToken()); + } + + private static List names(List filters) { + return filters.stream().map(MetricFilter::getFilterName).toList(); + } + + // ──────────────────────────── DeleteMetricFilter ──────────────────────────── + + @Test + void deleteRemovesTheFilterAndRejectsWhatIsNotThere() { + service.putMetricFilter(errorCounter(GROUP, "errors"), REGION); + + service.deleteMetricFilter(GROUP, "errors", REGION); + + assertEquals(0, service.countMetricFilters(GROUP, REGION)); + rejected(() -> service.deleteMetricFilter(GROUP, "errors", REGION), "ResourceNotFoundException"); + rejected(() -> service.deleteMetricFilter("/missing", "errors", REGION), "ResourceNotFoundException"); + rejected(() -> service.deleteMetricFilter(GROUP, null, REGION), "InvalidParameterException"); + } + + @Test + void deletingTheLogGroupTakesItsFiltersAlong() { + service.putMetricFilter(errorCounter(GROUP, "errors"), REGION); + service.putMetricFilter(errorCounter(OTHER_GROUP, "errors"), REGION); + + service.onLogGroupDeleted(new LogGroupDeleted(REGION, GROUP)); + + assertEquals(0, service.countMetricFilters(GROUP, REGION)); + assertEquals(1, service.countMetricFilters(OTHER_GROUP, REGION)); + } + + // ──────────────────────────── TestMetricFilter ──────────────────────────── + + @Test + void testMetricFilterReportsMatchesWithOneBasedNumbersAndExtractedValues() { + List matches = service.testMetricFilter( + "[..., status_code=200, size]", List.of( + "127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 1534", + "127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] \"GET /apache_pb.gif HTTP/1.0\" 500 5324", + "127.0.0.1 - frank [10/Oct/2000:13:50:35 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 4355")); + + assertEquals(2, matches.size()); + assertEquals(1, matches.get(0).eventNumber()); + assertEquals(3, matches.get(1).eventNumber()); + assertEquals("1534", matches.get(0).extractedValues().get("$size")); + assertEquals("200", matches.get(0).extractedValues().get("$status_code")); + assertEquals("frank", matches.get(0).extractedValues().get("$3")); + assertTrue(matches.get(0).eventMessage().endsWith("200 1534")); + + List terms = service.testMetricFilter( + "\"[ERROR]\"", List.of("[INFO] up", "[ERROR] down")); + assertEquals(1, terms.size()); + assertEquals(2, terms.getFirst().eventNumber()); + assertEquals(Map.of(), terms.getFirst().extractedValues()); + } + + @Test + void testMetricFilterDoesNotExposeJsonExtractions() { + List matches = service.testMetricFilter( + "{ $.latency = * }", List.of("plain text", "{\"latency\":50}")); + assertEquals(1, matches.size()); + assertEquals(2, matches.getFirst().eventNumber()); + assertEquals(Map.of(), matches.getFirst().extractedValues()); + } + + @Test + void testMetricFilterRejectsWhatTheApiRejects() { + rejected(() -> service.testMetricFilter("{ $.a = }", List.of("x")), "InvalidParameterException"); + rejected(() -> service.testMetricFilter(null, List.of("x")), "InvalidParameterException"); + rejected(() -> service.testMetricFilter("ERROR", List.of()), "InvalidParameterException"); + rejected(() -> service.testMetricFilter("ERROR", List.of("")), "InvalidParameterException"); + List tooMany = new ArrayList<>(); + for (int i = 0; i < 51; i++) { + tooMany.add("m"); + } + rejected(() -> service.testMetricFilter("ERROR", tooMany), "InvalidParameterException"); + } + + /** + * AWS allows five regex-bearing filters per log group, each holding up to two expressions. + * Replacing a filter reuses its slot, a pattern + * without a regular expression never counts, and the quota is per log group. + */ + @Test + void theRegexQuotaOfALogGroup() { + for (int i = 0; i < 5; i++) { + service.putMetricFilter(filter(GROUP, "r" + i, "{ $.a = %ERROR% || $.a = %WARN% }", + transformation("E", "App", "1")), REGION); + } + + rejected(() -> service.putMetricFilter(filter(GROUP, "r5", "%INFO%", transformation("E", "App", "1")), REGION), + "LimitExceededException"); + + service.putMetricFilter(filter(GROUP, "r2", "%NOTICE%", transformation("E", "App", "1")), REGION); + service.putMetricFilter(filter(GROUP, "plain", "ERROR", transformation("E", "App", "1")), REGION); + service.putMetricFilter(filter(OTHER_GROUP, "r3", "%INFO%", transformation("E", "App", "1")), REGION); + } + + /** The criterion has to parse and to fit the 2000 characters the API allows. */ + @Test + void aFieldSelectionCriterionIsCheckedBeforeItIsStored() { + MetricFilter selected = errorCounter(GROUP, "selected"); + selected.setFieldSelectionCriteria("@aws.region = \"us-east-1\" && @aws.account IN [\"000000000000\"]"); + assertNotNull(service.putMetricFilter(selected, REGION).getFieldSelectionCriteria()); + + MetricFilter unknownField = errorCounter(GROUP, "unknown"); + unknownField.setFieldSelectionCriteria("@aws.zone = \"us-east-1\""); + rejected(() -> service.putMetricFilter(unknownField, REGION), "InvalidParameterException"); + + MetricFilter tooLong = errorCounter(GROUP, "long"); + tooLong.setFieldSelectionCriteria("@aws.region = \"" + "x".repeat(2000) + "\""); + rejected(() -> service.putMetricFilter(tooLong, REGION), "InvalidParameterException"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterStorageReloadTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterStorageReloadTest.java new file mode 100644 index 0000000000..a6baaa9da1 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterStorageReloadTest.java @@ -0,0 +1,227 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.ServiceConfigAccess; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; +import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudformation.provisioners.LogsMetricFilterCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.ProvisionContext; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import jakarta.enterprise.event.Event; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.nio.file.Path; +import java.time.Instant; +import java.util.List; +import java.util.Map; + +import static io.github.hectorvent.floci.services.cloudwatch.logs.PublicationTestSupport.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +/** Graceful factory shutdown/reload, not a durability claim for the process-local retry queue. */ +class CloudWatchLogsMetricFilterStorageReloadTest { + @TempDir Path directory; + + @ParameterizedTest + @ValueSource(strings = {"memory", "persistent", "hybrid", "wal"}) + void definitionsQuotasDeletionIsolationAndPublishedSamplesSurviveAccordingToMode(String mode) { + long created; + try (Services first = open(mode, ACCOUNT)) { + first.group(GROUP, REGION); + first.group(GROUP, "us-east-1"); + MetricFilter definition = definition("kept", "{ $.kind = %ERROR% }", "$.value", 7.0); + definition.setApplyOnTransformedLogs(true); + definition.setFieldSelectionCriteria("@aws.region = \"" + REGION + "\""); + definition.setEmitSystemFieldDimensions(List.of("@aws.region")); + created = first.filters.putMetricFilter(definition, REGION).getCreationTime(); + for (int i = 0; i < 3; i++) { + first.filters.putMetricFilter(definition("regex" + i, "%WARN%", "1", null), REGION); + } + first.logs.putSubscriptionFilter(GROUP, "subscription", "%INFO%", "arn:aws:lambda:" + REGION + + ":" + ACCOUNT + ":function:target", null, REGION); + first.filters.putMetricFilter(definition("deleted", "ERROR", "3", 7.0), REGION); + first.filters.deleteMetricFilter(GROUP, "deleted", REGION); + MetricFilter dimensioned = definition("dimensioned", "{ $.kind = * }", "$.value", null); + dimensioned.getMetricTransformations().getFirst().setDimensions(Map.of("Kind", "$.kind")); + first.filters.putMetricFilter(dimensioned, REGION); + first.filters.putMetricFilter(definition("regional", "ERROR", "1", null), "us-east-1"); + first.logs.putLogEvents(GROUP, "s", List.of(Map.of("timestamp", TIME, "message", "INFO")), REGION); + assertEquals(5, first.filters.countMetricFilters(GROUP, REGION)); + assertEquals(1, first.logs.describeLogGroups(GROUP, REGION).size()); + first.factory.flushAll(); + } + // Different accounts use the very same factory path, with no concurrent writers. + try (Services other = open(mode, OTHER_ACCOUNT)) { + assertTrue(other.logs.describeLogGroups(GROUP, REGION).isEmpty()); + other.group(GROUP, REGION); + other.filters.putMetricFilter(definition("other", "ERROR", "9", null), REGION); + } + try (Services second = open(mode, ACCOUNT)) { + if (mode.equals("memory")) { + assertTrue(second.logs.describeLogGroups(null, REGION).isEmpty()); + assertTrue(second.metrics.listMetrics(null, null, null, REGION).isEmpty()); + return; + } + List loaded = second.filters.describeMetricFilters( + GROUP, null, null, null, null, 50, REGION).metricFilters(); + assertEquals(5, loaded.size()); + assertFalse(loaded.stream().anyMatch(f -> List.of("deleted", "other", "regional").contains(f.getFilterName()))); + MetricFilter kept = loaded.stream().filter(f -> f.getFilterName().equals("kept")).findFirst().orElseThrow(); + assertEquals(created, kept.getCreationTime()); + assertEquals("{ $.kind = %ERROR% }", kept.getFilterPattern()); + assertEquals(true, kept.getApplyOnTransformedLogs()); + assertEquals("@aws.region = \"" + REGION + "\"", kept.getFieldSelectionCriteria()); + assertEquals(List.of("@aws.region"), kept.getEmitSystemFieldDimensions()); + assertEquals("$.value", kept.getMetricTransformations().getFirst().getMetricValue()); + assertEquals("Value", kept.getMetricTransformations().getFirst().getMetricName()); + assertEquals("kept", kept.getMetricTransformations().getFirst().getMetricNamespace()); + assertEquals(7.0, kept.getMetricTransformations().getFirst().getDefaultValue()); + assertEquals("Count", kept.getMetricTransformations().getFirst().getUnit()); + assertEquals(Map.of("Kind", "$.kind"), loaded.stream().filter(f -> f.getFilterName().equals("dimensioned")) + .findFirst().orElseThrow().getMetricTransformations().getFirst().getDimensions()); + assertEquals(5, second.filters.countMetricFilters(GROUP, REGION)); + assertEquals(1, second.filters.countMetricFilters(GROUP, "us-east-1")); + AwsException quota = assertThrows(AwsException.class, () -> + second.filters.putMetricFilter(definition("over", "%FATAL%", "1", null), REGION)); + assertEquals("LimitExceededException", quota.getErrorCode()); + second.logs.deleteSubscriptionFilter(GROUP, "subscription", REGION); + second.filters.putMetricFilter(definition("over", "%FATAL%", "1", null), REGION); + assertEquals(6, second.filters.countMetricFilters(GROUP, REGION)); + List samples = second.metrics.getMetricStatistics("kept", "Value", + List.of(), Instant.ofEpochMilli(TIME), Instant.ofEpochMilli(TIME).plusSeconds(60), + 60, List.of("Sum", "SampleCount"), null, REGION); + assertEquals(1, samples.size()); + assertEquals(7, samples.getFirst().sum()); + assertEquals(1, samples.getFirst().sampleCount()); + assertEquals(Instant.ofEpochMilli(TIME), samples.getFirst().timestamp()); + assertTrue(second.metrics.listMetrics(null, null, null, "us-east-1").isEmpty()); + second.logs.deleteLogGroup(GROUP, REGION); + } + try (Services third = open(mode, ACCOUNT)) { + assertTrue(third.logs.describeLogGroups(GROUP, REGION).isEmpty()); + third.group(GROUP, REGION); + assertEquals(0, third.filters.countMetricFilters(GROUP, REGION), "group deletion cannot resurrect filters"); + } + try (Services other = open(mode, OTHER_ACCOUNT)) { + assertEquals(1, other.filters.countMetricFilters(GROUP, REGION)); + assertTrue(other.metrics.listMetrics(null, null, null, REGION).isEmpty()); + } + } + + @ParameterizedTest + @ValueSource(strings = {"persistent", "hybrid", "wal"}) + void reloadedMetricFilterCountStillEnforcesTheHundredDefinitionLimit(String mode) { + try (Services first = open(mode, ACCOUNT)) { + first.group(GROUP, REGION); + for (int i = 0; i < 100; i++) { + first.filters.putMetricFilter(definition("filter" + i, "ERROR", "1", null), REGION); + } + } + try (Services second = open(mode, ACCOUNT)) { + assertEquals(100, second.filters.countMetricFilters(GROUP, REGION)); + AwsException quota = assertThrows(AwsException.class, () -> + second.filters.putMetricFilter(definition("overflow", "ERROR", "1", null), REGION)); + assertEquals("LimitExceededException", quota.getErrorCode()); + second.filters.putMetricFilter(definition("filter0", "WARN", "2", null), REGION); + assertEquals(100, second.filters.countMetricFilters(GROUP, REGION)); + second.filters.deleteMetricFilter(GROUP, "filter1", REGION); + second.filters.putMetricFilter(definition("replacement", "ERROR", "1", null), REGION); + assertEquals(100, second.filters.countMetricFilters(GROUP, REGION)); + } + try (Services third = open(mode, ACCOUNT)) { + assertEquals(100, third.filters.countMetricFilters(GROUP, REGION)); + assertTrue(third.filters.describeMetricFilters(GROUP, "filter1", null, null, null, 50, REGION) + .metricFilters().stream().noneMatch(f -> f.getFilterName().equals("filter1"))); + } + } + + @ParameterizedTest + @ValueSource(strings = {"persistent", "hybrid", "wal"}) + void actualCfnOwnershipMetadataRoundTripsAndStillDeletesOnlyItsNamedFilter(String mode) throws Exception { + Map attributes; + try (Services first = open(mode, ACCOUNT)) { + first.group(GROUP, REGION); + StackResource resource = new StackResource(); + resource.setLogicalId("Filter"); + resource.setResourceType("AWS::Logs::MetricFilter"); + CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class); + when(engine.resolveNode(any())).thenAnswer(call -> call.getArgument(0)); + new LogsMetricFilterCfnProvisioner(first.filters).provision(resource, new ObjectMapper().readTree(""" + {"LogGroupName":"/publication","FilterName":"owned","FilterPattern":"ERROR", + "MetricTransformations":[{"MetricNamespace":"CfnReload","MetricName":"Count","MetricValue":"1"}]} + """), new ProvisionContext(engine, REGION, ACCOUNT, "reload")); + assertEquals("owned", resource.getPhysicalId()); + attributes = Map.copyOf(resource.getAttributes()); + assertTrue(attributes.get("__FlociMetricFilterState").contains("\"ownership\":\"OWNED\"")); + first.resources().put("filter", resource); + } + try (Services second = open(mode, ACCOUNT)) { + StackResource loaded = second.resources().get("filter").orElseThrow(); + assertEquals(attributes, loaded.getAttributes()); + assertEquals("owned", loaded.getPhysicalId()); + second.filters.putMetricFilter(definition("unrelated", "ERROR", "1", null), REGION); + new LogsMetricFilterCfnProvisioner(second.filters).delete(loaded, REGION); + assertEquals(List.of("unrelated"), second.filters.describeMetricFilters( + GROUP, null, null, null, null, 50, REGION).metricFilters().stream().map(MetricFilter::getFilterName).toList()); + } + } + + private Services open(String mode, String account) { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.defaultAccountId()).thenReturn(account); + when(config.storage().persistentPath()).thenReturn(directory.toString()); + when(config.storage().wal().compactionIntervalMs()).thenReturn(3_600_000L); + when(config.services().cloudwatchlogs().maxEventsPerQuery()).thenReturn(10_000); + when(config.services().cloudwatchlogs().maxStoredEvents()).thenReturn(10_000); + ServiceConfigAccess access = mock(ServiceConfigAccess.class); + when(access.storageMode(anyString())).thenReturn(mode); + when(access.storageFlushInterval(anyString())).thenReturn(3_600_000L); + return new Services(new StorageFactory(config, access), config, account); + } + + private static class Services implements AutoCloseable { + final StorageFactory factory; + final CloudWatchLogsService logs; + final CloudWatchMetricsService metrics; + final CloudWatchLogsMetricFilterService filters; + + @SuppressWarnings("unchecked") + Services(StorageFactory factory, EmulatorConfig config, String account) { + this.factory = factory; + RegionResolver resolver = new RegionResolver(REGION, account); + Event ingested = mock(Event.class); + Event deleted = mock(Event.class); + logs = new CloudWatchLogsService(factory, config, resolver, ingested, deleted); + metrics = new CloudWatchMetricsService(factory, resolver); + filters = new CloudWatchLogsMetricFilterService(logs, metrics, resolver); + doAnswer(call -> { filters.onLogEventsIngested(call.getArgument(0)); return null; }).when(ingested).fire(any()); + doAnswer(call -> { filters.onLogGroupDeleted(call.getArgument(0)); return null; }).when(deleted).fire(any()); + } + + void group(String group, String region) { + logs.createLogGroup(group, null, null, region); + logs.createLogStream(group, "s", region); + } + + AccountAwareStorageBackend resources() { + return factory.create("cloudformation", "reload-resources.json", new TypeReference<>() {}); + } + + /** Production order: the publisher drains and stops before storage shuts down. */ + @Override public void close() { + filters.stop(); + factory.shutdownAll(); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterWireTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterWireTest.java new file mode 100644 index 0000000000..77f318d198 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterWireTest.java @@ -0,0 +1,170 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +class CloudWatchLogsMetricFilterWireTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final String PUT = """ + {"logGroupName":"/synthetic","filterName":"synthetic","filterPattern":"", + "metricTransformations":[{"metricName":"Count","metricNamespace":"Synthetic","metricValue":"1"}]} + """; + private static final String DELETE = """ + {"logGroupName":"/synthetic","filterName":"synthetic"} + """; + private static final String TEST = """ + {"filterPattern":"","logEventMessages":["synthetic"]} + """; + private CloudWatchLogsMetricFilterService service; + private CloudWatchLogsMetricFilterHandler handler; + + @BeforeEach + void setUp() { + service = mock(CloudWatchLogsMetricFilterService.class); + when(service.describeMetricFilters(any(), any(), any(), any(), any(), any(), any())) + .thenReturn(new CloudWatchLogsMetricFilterService.DescribeMetricFiltersResult(List.of(), null)); + when(service.testMetricFilter(any(), any())).thenReturn(List.of()); + clearInvocations(service); + handler = new CloudWatchLogsMetricFilterHandler(service, MAPPER); + } + + static Stream invalidRequests() throws Exception { + List cases = new ArrayList<>(); + for (String action : List.of("PutMetricFilter", "DescribeMetricFilters", "DeleteMetricFilter", "TestMetricFilter")) { + for (String root : List.of("null", "[]", "\"text\"", "true", "1")) { + cases.add(Arguments.of(action, MAPPER.readTree(root))); + } + } + strings(cases, "PutMetricFilter", PUT, + List.of("logGroupName", "filterName", "filterPattern", "fieldSelectionCriteria")); + strings(cases, "DeleteMetricFilter", DELETE, List.of("logGroupName", "filterName")); + strings(cases, "DescribeMetricFilters", "{}", + List.of("logGroupName", "filterNamePrefix", "metricName", "metricNamespace", "nextToken")); + strings(cases, "TestMetricFilter", TEST, List.of("filterPattern")); + for (String member : List.of("metricName", "metricNamespace", "metricValue", "unit")) { + for (String value : List.of("null", "true", "1", "[]", "{}")) { + ObjectNode request = (ObjectNode) MAPPER.readTree(PUT); + ((ObjectNode) request.path("metricTransformations").get(0)).set(member, MAPPER.readTree(value)); + cases.add(Arguments.of("PutMetricFilter", request)); + } + } + replace(cases, "PutMetricFilter", PUT, "applyOnTransformedLogs", "null", "\"true\"", "1", "[]", "{}"); + replace(cases, "PutMetricFilter", PUT, "metricTransformations", + "null", "{}", "\"text\"", "1", "true", "[null]", "[[]]", "[1]", "[\"text\"]"); + replace(cases, "PutMetricFilter", PUT, "emitSystemFieldDimensions", + "null", "{}", "\"@aws.region\"", "1", "true", "[null]", "[[]]", "[1]"); + replace(cases, "TestMetricFilter", TEST, "logEventMessages", + "null", "{}", "\"text\"", "1", "true", "[null]", "[{}]", "[1]", "[true]", "[[]]"); + replace(cases, "DescribeMetricFilters", "{}", "limit", + "null", "\"1\"", "true", "[]", "{}", "1.5", "1.0", "2147483649", "4294967297", "0", "51"); + for (String value : List.of("null", "[]", "\"text\"", "1", "true", "{\"Name\":null}", "{\"Name\":1}", + "{\"Name\":true}", "{\"Name\":[]}", "{\"Name\":{}}")) { + ObjectNode request = (ObjectNode) MAPPER.readTree(PUT); + ((ObjectNode) request.path("metricTransformations").get(0)).set("dimensions", MAPPER.readTree(value)); + cases.add(Arguments.of("PutMetricFilter", request)); + } + for (String value : List.of("null", "[]", "{}", "true", "1e309")) { + ObjectNode request = (ObjectNode) MAPPER.readTree(PUT); + ((ObjectNode) request.path("metricTransformations").get(0)).set("defaultValue", MAPPER.readTree(value)); + cases.add(Arguments.of("PutMetricFilter", request)); + } + return cases.stream(); + } + + private static void strings(List cases, String action, String request, List members) + throws Exception { + for (String member : members) { + replace(cases, action, request, member, "null", "true", "1", "[]", "{}"); + } + } + + private static void replace(List cases, String action, String json, String member, String... values) + throws Exception { + for (String value : values) { + ObjectNode request = (ObjectNode) MAPPER.readTree(json); + request.set(member, MAPPER.readTree(value)); + cases.add(Arguments.of(action, request)); + } + } + + @ParameterizedTest + @MethodSource("invalidRequests") + void rejectsIncorrectNodeTypesBeforeCallingTheService(String action, JsonNode request) { + AwsException error = assertThrows(AwsException.class, () -> handler.handle(action, request, "eu-west-1"), + () -> action + " " + request); + assertEquals("InvalidParameterException", error.getErrorCode()); + assertEquals(400, error.getHttpStatus()); + verifyNoInteractions(service); + } + + @Test + void numericStringDefaultReportsTheSerializationErrorVerifiedOnLiveAws() throws Exception { + try (InputStream input = getClass().getResourceAsStream( + "/cloudwatchlogs/metric-filter-default-value-wire-aws.json")) { + JsonNode observation = MAPPER.readTree(input); + ObjectNode request = (ObjectNode) MAPPER.readTree(PUT); + ((ObjectNode) request.path("metricTransformations").get(0)) + .set("defaultValue", observation.get("requestValue")); + + AwsException error = assertThrows(AwsException.class, + () -> handler.handle("PutMetricFilter", request, "eu-west-1")); + + assertEquals("Verified on Live AWS", observation.path("provenance").path("verification").textValue()); + assertEquals(observation.path("response").path("status").intValue(), error.getHttpStatus()); + assertEquals(observation.path("response").path("body").path("__type").textValue(), error.getErrorCode()); + assertEquals(observation.path("response").path("body").path("Message").textValue(), error.getMessage()); + verifyNoInteractions(service); + } + } + + @Test + void otherStringsFollowTheSameNodeTypeRuleWithoutClaimingSeparateAwsObservations() throws Exception { + for (String value : List.of("NaN", "zero")) { + ObjectNode request = (ObjectNode) MAPPER.readTree(PUT); + ((ObjectNode) request.path("metricTransformations").get(0)).put("defaultValue", value); + AwsException error = assertThrows(AwsException.class, + () -> handler.handle("PutMetricFilter", request, "eu-west-1")); + assertEquals("SerializationException", error.getErrorCode()); + assertEquals(400, error.getHttpStatus()); + } + verifyNoInteractions(service); + } + + @Test + void validShapesKeepObjectNodeResponsesAndNumericDefaults() throws Exception { + for (String action : List.of("PutMetricFilter", "DescribeMetricFilters", "DeleteMetricFilter", "TestMetricFilter")) { + String json = switch (action) { + case "PutMetricFilter" -> PUT.replace("\"metricValue\":\"1\"", "\"metricValue\":\"1\",\"defaultValue\":0"); + case "DeleteMetricFilter" -> DELETE; + case "TestMetricFilter" -> TEST; + default -> "{\"limit\":50}"; + }; + Response response = handler.handle(action, MAPPER.readTree(json), "eu-west-1"); + assertEquals(200, response.getStatus()); + assertInstanceOf(ObjectNode.class, response.getEntity()); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java index f569ce3b57..0791a64778 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java @@ -32,6 +32,7 @@ class CloudWatchLogsServiceTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 10000, new RegionResolver("us-east-1", "000000000000") ); @@ -80,6 +81,7 @@ class CloudWatchLogsServiceTest { new AccountAwareStorageBackend<>(rawStreams, null, "000000000000"), new AccountAwareStorageBackend<>(rawEvents, null, "000000000000"), new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000"), + new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000"), 10_000, new RegionResolver(REGION, "000000000000")); String accountA = "111111111111"; String accountB = "222222222222"; @@ -120,6 +122,7 @@ class CloudWatchLogsServiceTest { new AccountAwareStorageBackend<>(rawStreams, null, accountB), new AccountAwareStorageBackend<>(rawEvents, null, accountB), new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB), + new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB), 10_000, new RegionResolver(REGION, accountB)); long now = System.currentTimeMillis(); long twoDaysAgo = now - 2 * 86_400_000L; @@ -154,6 +157,7 @@ class CloudWatchLogsServiceTest { new AccountAwareStorageBackend<>(rawStreams, null, accountB), new AccountAwareStorageBackend<>(rawEvents, null, accountB), new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB), + new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB), 10_000, 2, new RegionResolver(REGION, accountB)); accountService.createLogGroupForAccount(accountA, "/app/logs", null, null, REGION); @@ -244,6 +248,7 @@ class CloudWatchLogsServiceTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 10000, maxStoredEvents, new RegionResolver("us-east-1", "000000000000") @@ -820,6 +825,7 @@ class CloudWatchLogsServiceTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 2, new RegionResolver("us-east-1", "000000000000") ); @@ -904,6 +910,7 @@ class CloudWatchLogsServiceTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), Integer.MAX_VALUE, new RegionResolver("us-east-1", "000000000000") ); @@ -1199,6 +1206,7 @@ class CloudWatchLogsServiceTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 0, new RegionResolver("us-east-1", "000000000000") ); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/MetricFilterRulesTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/MetricFilterRulesTest.java new file mode 100644 index 0000000000..3b5772efe8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/MetricFilterRulesTest.java @@ -0,0 +1,54 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MetricFilterRulesTest { + + @ParameterizedTest + @ValueSource(strings = {"1e309", "-1e309", "NaN", "Infinity", " 1", "1x"}) + void numbersThatCannotBePublishedAreNotValues(String text) { + assertNull(MetricFilterRules.number(text)); + } + + @Test + void finiteNumbersRetainExistingGrammar() { + assertEquals(1000.0, MetricFilterRules.number("1e3")); + assertEquals(-0.5, MetricFilterRules.number("-.5")); + assertEquals(Double.MAX_VALUE, MetricFilterRules.number("1.7976931348623157e308")); + assertEquals(0.0, MetricFilterRules.number("1e-999")); + } + + @Test + void corruptedStoredCriteriaSkipRatherThanPublishAcrossAccounts() { + MetricFilter filter = new MetricFilter(); + filter.setFilterName("synthetic"); + filter.setLogGroupName("/synthetic"); + filter.setFieldSelectionCriteria("@aws.account === \"111111111111\""); + assertFalse(MetricFilterRules.selects(filter, "222222222222", "eu-west-1")); + filter.setFieldSelectionCriteria(null); + assertTrue(MetricFilterRules.selects(filter, "222222222222", "eu-west-1")); + } + + @Test + void nullSystemDimensionsAreDomainErrorsRatherThanNullPointerExceptions() { + AwsException error = assertThrows(AwsException.class, () -> MetricFilterRules.validateSystemFields( + Arrays.asList("@aws.account", null), new MetricTransformation())); + assertEquals("InvalidParameterException", error.getErrorCode()); + assertEquals(400, error.getHttpStatus()); + MetricFilterRules.validateSystemFields(List.of("@aws.account", "@aws.region"), new MetricTransformation()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/PublicationTestSupport.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/PublicationTestSupport.java new file mode 100644 index 0000000000..cf40e29e3c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/PublicationTestSupport.java @@ -0,0 +1,137 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs; + +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.RequestContext; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricFilter; +import io.github.hectorvent.floci.services.cloudwatch.logs.model.MetricTransformation; +import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum; +import jakarta.enterprise.event.Event; +import jakarta.enterprise.inject.Instance; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +/** Real Logs ingestion and Metrics aggregation, with only CDI wiring supplied by the fixture. */ +class PublicationTestSupport { + static final String ACCOUNT = "111111111111"; + static final String OTHER_ACCOUNT = "222222222222"; + static final String REGION = "eu-west-1"; + static final String GROUP = "/publication"; + static final long TIME = 1_700_000_040_000L; + final AtomicReference account = new AtomicReference<>(ACCOUNT); + final RegionResolver resolver = mock(RegionResolver.class); + final StorageFactory factory = mock(StorageFactory.class); + final AccountAwareStorageBackend metricStore; + final AccountAwareStorageBackend filters; + final CloudWatchMetricsService metrics; + final CloudWatchLogsService logs; + final CloudWatchLogsMetricFilterService service; + + PublicationTestSupport() { + this(new InMemoryStorage<>()); + } + + PublicationTestSupport(StorageBackend sink) { + this(sink, CloudWatchLogsMetricFilterService.MAX_QUEUED_SAMPLES); + } + + @SuppressWarnings("unchecked") + PublicationTestSupport(StorageBackend sink, int queueCapacity) { + Instance contexts = mock(Instance.class); + RequestContext context = mock(RequestContext.class); + when(contexts.get()).thenReturn(context); + when(context.getAccountId()).thenAnswer(call -> account.get()); + when(resolver.getAccountId()).thenAnswer(call -> account.get()); + metricStore = new AccountAwareStorageBackend<>(sink, contexts, ACCOUNT); + filters = new AccountAwareStorageBackend<>(new InMemoryStorage<>(), contexts, ACCOUNT); + doReturn(metricStore).when(factory).create(eq("cloudwatchmetrics"), eq("cwmetrics.json"), any()); + doReturn(AccountAwareStorageBackend.inMemory(ACCOUNT)).when(factory) + .create(eq("cloudwatchmetrics"), eq("cwalarms.json"), any()); + metrics = new CloudWatchMetricsService(factory, resolver); + Event ingested = mock(Event.class); + Event deleted = mock(Event.class); + logs = new CloudWatchLogsService(new AccountAwareStorageBackend<>(new InMemoryStorage<>(), contexts, ACCOUNT), + new AccountAwareStorageBackend<>(new InMemoryStorage<>(), contexts, ACCOUNT), + new AccountAwareStorageBackend<>(new InMemoryStorage<>(), contexts, ACCOUNT), + new AccountAwareStorageBackend<>(new InMemoryStorage<>(), contexts, ACCOUNT), filters, + new InMemoryStorage<>(), 10_000, Integer.MAX_VALUE, resolver, 0L, System::currentTimeMillis, + ingested, deleted); + service = new CloudWatchLogsMetricFilterService(logs, metrics, resolver, queueCapacity); + doAnswer(call -> { service.onLogEventsIngested(call.getArgument(0)); return null; }).when(ingested).fire(any()); + doAnswer(call -> { service.onLogGroupDeleted(call.getArgument(0)); return null; }).when(deleted).fire(any()); + group(GROUP, REGION); + } + + void group(String group, String region) { + logs.createLogGroup(group, null, null, region); + logs.createLogStream(group, "s", region); + } + + static MetricFilter definition(String name, String pattern, String value, Double fallback) { + MetricTransformation t = new MetricTransformation(); + t.setMetricName("Value"); + t.setMetricNamespace(name); + t.setMetricValue(value); + t.setDefaultValue(fallback); + t.setUnit("Count"); + MetricFilter f = new MetricFilter(); + f.setFilterName(name); + f.setLogGroupName(GROUP); + f.setFilterPattern(pattern); + f.setMetricTransformations(List.of(t)); + return f; + } + + void put(String name, String pattern, String value, Double fallback) { + service.putMetricFilter(definition(name, pattern, value, fallback), REGION); + } + + void ingest(long time, String... messages) { + ingest(null, GROUP, REGION, time, List.of(messages)); + } + + /** Ingests and drains the publication queue on this thread, as the worker would. */ + void ingest(String owner, String group, String region, long time, List messages) { + enqueue(owner, group, region, time, messages); + service.publishQueued(); + } + + /** Ingests without draining: the samples stay queued until publishQueued, start or stop. */ + void enqueue(long time, String... messages) { + enqueue(null, GROUP, REGION, time, List.of(messages)); + } + + void enqueue(String owner, String group, String region, long time, List messages) { + List> events = messages.stream() + .map(message -> Map.of("timestamp", time, "message", message)).toList(); + assertNotNull(logs.putLogEventsForAccount(owner, group, "s", events, region)); + } + + List points(String namespace, List dims, long time, String region) { + return metrics.getMetricStatistics(namespace, "Value", dims, Instant.ofEpochMilli(time), + Instant.ofEpochMilli(time).plusSeconds(59), 60, List.of("Sum", "SampleCount"), null, region); + } + + void stats(String namespace, long time, double sum, double count) { + stats(namespace, List.of(), time, sum, count); + } + + void stats(String namespace, List dims, long time, double sum, double count) { + List points = points(namespace, dims, time, REGION); + assertEquals(count == 0 ? 0 : 1, points.size()); + assertEquals(sum, points.stream().mapToDouble(CloudWatchMetricsService.Datapoint::sum).sum()); + assertEquals(count, points.stream().mapToDouble(CloudWatchMetricsService.Datapoint::sampleCount).sum()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternAwsCorpusTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternAwsCorpusTest.java new file mode 100644 index 0000000000..f2e1cda6a5 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternAwsCorpusTest.java @@ -0,0 +1,67 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class FilterPatternAwsCorpusTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @TestFactory + List observedAwsPatterns() throws Exception { + List tests = new ArrayList<>(); + for (String fixture : List.of("filter-pattern-aws.json", "filter-pattern-aws-edges.json", + "filter-pattern-aws-boundaries.json")) { + try (InputStream input = getClass().getResourceAsStream("/cloudwatchlogs/" + fixture)) { + JsonNode corpus = MAPPER.readTree(input); + for (JsonNode example : corpus.path("cases")) { + addCase(tests, example); + } + } + } + return tests; + } + + private static void addCase(List tests, JsonNode example) { + String id = example.path("id").textValue(); + String pattern = example.path("request").path("filterPattern").textValue(); + if (example.has("error")) { + tests.add(DynamicTest.dynamicTest(id, () -> + assertThrows(FilterPatternException.class, () -> FilterPattern.parse(pattern), pattern))); + return; + } + JsonNode messages = example.path("request").path("logEventMessages"); + for (int index = 0; index < messages.size(); index++) { + String message = messages.get(index).textValue(); + int eventNumber = index + 1; + JsonNode expected = null; + for (JsonNode match : example.path("response").path("matches")) { + if (match.path("eventNumber").intValue() == eventNumber) { + expected = match; + break; + } + } + JsonNode expectedMatch = expected; + tests.add(DynamicTest.dynamicTest(id + "/" + eventNumber, () -> { + FilterMatch actual = FilterPattern.parse(pattern).match(message); + assertEquals(expectedMatch != null, actual.matched(), pattern + " on " + message); + if (expectedMatch != null) { + Map extracted = MAPPER.convertValue(expectedMatch.path("extractedValues"), + new TypeReference<>() {}); + assertEquals(extracted, actual.extractedValues(), pattern + " on " + message); + } + })); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternTest.java new file mode 100644 index 0000000000..41a2faaf70 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/FilterPatternTest.java @@ -0,0 +1,503 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The filter pattern syntax CloudWatch Logs documents for metric filters, subscription filters and + * FilterLogEvents: plain terms, {@code %regex%}, JSON selectors and space-delimited fields. The + * examples are the ones on the AWS reference pages, with the matches those pages state. + */ +class FilterPatternTest { + + private static boolean matches(String pattern, String message) { + return FilterPattern.parse(pattern).match(message).matched(); + } + + private static FilterMatch match(String pattern, String message) { + return FilterPattern.parse(pattern).match(message); + } + + @Nested + class Terms { + + @Test + void aSingleTermMatchesMessagesContainingIt() { + assertTrue(matches("ERROR", "[ERROR 400] BAD REQUEST")); + assertTrue(matches("ERROR", "[ERROR 419] MISSING ARGUMENTS")); + assertFalse(matches("ERROR", "[INFO 200] OK")); + } + + @Test + void termsAreCaseSensitiveSubstrings() { + assertFalse(matches("ERROR", "an error happened")); + assertTrue(matches("Exception", "[ERROR] Unhanded exception: InvalidQueryException"), + "the API reference matches Exception inside InvalidQueryException"); + } + + @Test + void severalTermsMustAllBePresent() { + assertTrue(matches("ERROR ARGUMENTS", "[ERROR 419] MISSING ARGUMENTS")); + assertTrue(matches("ERROR ARGUMENTS", "[ERROR 420] INVALID ARGUMENTS")); + assertFalse(matches("ERROR ARGUMENTS", "[ERROR 400] BAD REQUEST")); + assertFalse(matches("ERROR ARGUMENTS", "[ERROR 401] UNAUTHORIZED REQUEST")); + } + + @Test + void questionMarkTermsMatchWhenAnyIsPresent() { + for (String message : List.of("[ERROR 400] BAD REQUEST", "[ERROR 401] UNAUTHORIZED REQUEST", + "[ERROR 419] MISSING ARGUMENTS", "[ERROR 420] INVALID ARGUMENTS")) { + assertTrue(matches("?ERROR ?ARGUMENTS", message), message); + } + assertFalse(matches("?ERROR ?ARGUMENTS", "[INFO 200] OK")); + } + + /** The reference: combined with other terms, the question mark terms are ignored. */ + @Test + void questionMarkTermsAreIgnoredNextToOtherTerms() { + assertTrue(matches("?ERROR ?ARGUMENTS REQUEST", "[INFO] REQUEST FAILED")); + assertTrue(matches("?ERROR ?ARGUMENTS REQUEST", "[WARN] UNAUTHORIZED REQUEST")); + assertTrue(matches("?ERROR ?ARGUMENTS REQUEST", "[ERROR] 400 BAD REQUEST")); + assertFalse(matches("?ERROR ?ARGUMENTS REQUEST", "[ERROR] MISSING ARGUMENTS")); + } + + @Test + void quotedPhrasesMatchExactly() { + assertTrue(matches("\"INTERNAL SERVER ERROR\"", "[ERROR 500] INTERNAL SERVER ERROR")); + assertFalse(matches("\"INTERNAL SERVER ERROR\"", "[ERROR 500] INTERNAL ERROR ON SERVER")); + assertTrue(matches("\"[ERROR]\"", "02 May 2014 00:34:16,142 [ERROR] Unhanded exception")); + assertFalse(matches("\"[ERROR]\"", "02 May 2014 00:34:12,525 [INFO] Starting the application")); + } + + @Test + void aQuotedPhraseAndATermCombine() { + assertTrue(matches("\"[ERROR]\" Exception", + "02 May 2014 00:34:16,142 [ERROR] Unhanded exception: InvalidQueryException")); + assertFalse(matches("\"[ERROR]\" Exception", "02 May 2014 00:34:16,224 [ERROR] Terminating the application")); + } + + @Test + void minusExcludesATerm() { + assertTrue(matches("ERROR -ARGUMENTS", "[ERROR 400] BAD REQUEST")); + assertTrue(matches("ERROR -ARGUMENTS", "[ERROR 401] UNAUTHORIZED REQUEST")); + assertFalse(matches("ERROR -ARGUMENTS", "[ERROR 419] MISSING ARGUMENTS")); + assertFalse(matches("ERROR -ARGUMENTS", "[ERROR 420] INVALID ARGUMENTS")); + } + + @Test + void aQuotedSpaceMatchesEverythingWithASpaceAndAnEmptyPatternMatchesEverything() { + assertTrue(matches("\" \"", "two words")); + assertTrue(matches("", "anything at all")); + assertTrue(matches(" ", "anything at all")); + assertTrue(matches("", "")); + } + + @Test + void aTermMatchHasNoExtractedValues() { + FilterMatch m = match("ERROR", "[ERROR 400] BAD REQUEST"); + assertTrue(m.matched()); + assertEquals(Map.of(), m.extractedValues()); + assertNull(m.value("$.anything")); + } + + @Test + void aNullMessageMatchesNothingButTheEmptyPattern() { + assertFalse(matches("ERROR", null)); + assertFalse(matches("%ERR%", null)); + assertTrue(matches("-ERROR", null), "an exclusion alone matches what lacks the term"); + assertTrue(matches("", null)); + assertFalse(matches("{ $.a = 1 }", null)); + assertFalse(matches("[a, b]", null)); + } + + @Test + void anUnterminatedQuoteIsRejected() { + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("\"INTERNAL SERVER")); + } + } + + @Nested + class Regex { + + @Test + void aStandaloneRegexMatchesAnywhereInTheMessage() { + assertTrue(matches("%AUTHORIZED%", "[ERROR 401] UNAUTHORIZED REQUEST")); + assertTrue(matches("%AUTHORIZED%", "[SUCCESS 200] AUTHORIZED REQUEST")); + assertFalse(matches("%AUTHORIZED%", "[SUCCESS 200] OK")); + } + + @Test + void theDocumentedOperatorsBehaveAsDocumented() { + assertTrue(matches("%^[hc]at%", "hat trick")); + assertFalse(matches("%^[hc]at%", "a hat")); + assertTrue(matches("%[hc]at$%", "the cat")); + assertTrue(matches("%colou?r%", "color")); + assertTrue(matches("%colou?r%", "colour")); + assertTrue(matches("%a{3,5}%", "aaaa")); + assertFalse(matches("%^a{3,5}$%", "aa")); + assertTrue(matches("%gra|ey%", "grey")); + assertTrue(matches("%^starting|^initializing|^shutting down%", "shutting down now")); + assertFalse(matches("%^starting|^initializing|^shutting down%", "skipping initializing ...")); + assertTrue(matches("%\\[.\\]%", "value [7] here")); + assertTrue(matches("%ab*c%", "ac")); + assertTrue(matches("%ab+c%", "abbc")); + assertFalse(matches("%ab+c%", "ac")); + assertTrue(matches("%.at%", "4at")); + assertTrue(matches("%\\d%", "port 80")); + assertTrue(matches("%\\D%", "port 80")); + assertTrue(matches("%\\s%", "a b")); + assertTrue(matches("%\\w%", "_")); + assertTrue(matches("%\\x3A%", "key:value"), "\\xhh matches the ASCII character"); + assertTrue(matches("%10\\.10\\.0\\.1%", "from 10.10.0.1")); + assertFalse(matches("%10\\.10\\.0\\.1%", "from 10010,051")); + } + + @Test + void unsupportedCharactersAreRejected() { + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%something!%")); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%(ab)+%"), + "parentheses are not supported"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%café%"), + "multi-byte characters are not supported"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%\\p%"), + "only the documented escapes are supported"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%\\(ab\\)+%"), + "escaping does not admit parentheses"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%\\!%")); + assertTrue(matches("%a\\-b%", "a-b"), "an escaped symbol from the allowed list is fine"); + assertTrue(matches("%\\\\%", "back\\slash"), "an escaped backslash is a backslash"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%%")); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%abc")); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("%a{3,%")); + } + + @Test + void aPatternMayHoldAtMostTwoRegexes() { + FilterPattern.parse("[a=%x%, b=%y%, c]"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("[a=%x%, b=%y%, c=%z%]")); + } + } + + @Nested + class Json { + + private static final String EVENT = """ + { + "eventType": "UpdateTrail", + "sourceIPAddress": "111.111.111.111", + "arrayKey": ["value", "another value"], + "objectList": [{"name": "a", "id": 1}, {"name": "b", "id": 2}], + "SomeObject": null, + "cluster.name": "c" + } + """; + + private static final String COMPOUND = """ + { + "user": {"id": 1, "email": "John.Stiles@example.com"}, + "users": [{"id": 2, "email": "John.Doe@example.com"}, {"id": 3, "email": "Jane.Doe@example.com"}], + "actions": ["GET", "PUT", "DELETE"], + "coordinates": [[0, 1, 2], [4, 5, 6], [7, 8, 9]] + } + """; + + /** + * AWS allows one wildcard in a property selector and three wildcard selectors in a whole + * pattern, the quotas the syntax page states beside its JSON examples. + */ + @Test + void theWildcardQuotas() { + assertTrue(matches("{ $.* = %111\\.111\\.111\\.1[0-9]{1,2}% }", EVENT), + "the reference example uses one wildcard in the selector"); + assertTrue(matches("{ $.objectList[*].name = \"a\" }", EVENT)); + assertEquals(FilterPattern.Kind.JSON, FilterPattern.parse( + "{ $.users[*].id = 2 || $.actions[*] = \"GET\" || $.coordinates[*][0] = 4 }").kind(), + "three wildcard selectors are within the quota"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse("{ $.*[*] = \"a\" }"), + "two wildcards in one property selector are more than AWS allows"); + assertThrows(FilterPatternException.class, () -> FilterPattern.parse( + "{ $.users[*].id = 2 || $.actions[*] = \"GET\" || $.coordinates[*][0] = 4" + + " || $.objectList[*].id = 1 }"), + "four wildcard selectors are more than AWS allows in one pattern"); + } + + @Test + void theReferenceStringExamples() { + assertTrue(matches("{ $.eventType = \"UpdateTrail\" }", EVENT)); + assertTrue(matches("{ $.sourceIPAddress != 123.123.* }", EVENT)); + assertFalse(matches("{ $.sourceIPAddress != 111.111.* }", EVENT)); + assertTrue(matches("{ $.arrayKey[0] = \"value\" }", EVENT)); + assertFalse(matches("{ $.arrayKey[1] = \"value\" }", EVENT)); + assertTrue(matches("{ $.eventType = %Trail% }", EVENT)); + assertTrue(matches("{ $.arrayKey[*] = %val.{2}% }", EVENT)); + assertTrue(matches("{ $.* = %111\\.111\\.111\\.1[0-9]{1,2}% }", EVENT)); + assertTrue(matches("{ $.['cluster.name'] = \"c\" }", EVENT)); + assertTrue(matches("{ $.SomeObject IS NULL }", EVENT)); + assertFalse(matches("{ $.eventType IS NULL }", EVENT)); + assertTrue(matches("{ $.SomeOtherObject NOT EXISTS }", EVENT)); + assertFalse(matches("{ $.eventType NOT EXISTS }", EVENT)); + assertTrue(matches("{ $.objectList[1].name = \"b\" }", EVENT)); + assertTrue(matches("{ $.objectList[*].id = 2 }", EVENT)); + } + + @Test + void theReferenceNumericExamples() { + assertTrue(matches("{ $.bandwidth > 75 }", "{\"bandwidth\": 80}")); + assertFalse(matches("{ $.bandwidth > 75 }", "{\"bandwidth\": 75}")); + assertTrue(matches("{ $.latency < 50 }", "{\"latency\": 49.5}")); + assertTrue(matches("{ $.refreshRate >= 60 }", "{\"refreshRate\": 60}")); + assertTrue(matches("{ $.responseTime <= 5 }", "{\"responseTime\": 5}")); + assertTrue(matches("{ $.errorCode = 400}", "{\"errorCode\": 400}")); + assertTrue(matches("{ $.errorCode != 500 }", "{\"errorCode\": 400}")); + assertFalse(matches("{ $.errorCode != 500 }", "{\"errorCode\": 500}")); + assertTrue(matches("{ $.number[0] = 1e+3 }", "{\"number\": [1000]}")); + assertTrue(matches("{ $.number[0] != 1e-3 }", "{\"number\": [1000]}")); + assertTrue(matches("{ $.errorCode = 4* }", "{\"errorCode\": 404}"), "the asterisk also matches numbers"); + } + + @Test + void theReferenceCompoundExamples() { + assertTrue(matches("{ ($.user.id = 1) && ($.users[0].email = \"John.Doe@example.com\") }", COMPOUND)); + assertTrue(matches("{ $.user.email = \"John.Stiles@example.com\" || $.coordinates[0][1] = \"nonmatch\"" + + " && $.actions[2] = \"nonmatch\" }", COMPOUND)); + assertFalse(matches("{ ($.user.email = \"John.Stiles@example.com\" || $.coordinates[0][1] = \"nonmatch\")" + + " && $.actions[2] = \"nonmatch\" }", COMPOUND)); + assertFalse(matches("{ ($.user.id = 2 && $.users[0].email = \"nonmatch\") || $.actions[2] = \"GET\" }", + COMPOUND)); + assertTrue(matches("{ $.coordinates[1][2] = 6 }", COMPOUND)); + } + + @Test + void aMessageThatIsNotJsonDoesNotMatch() { + assertFalse(matches("{ $.eventType = \"UpdateTrail\" }", "eventType=UpdateTrail")); + assertFalse(matches("{ $.eventType = \"UpdateTrail\" }", "2024-01-01 INFO {\"eventType\": \"UpdateTrail\"}")); + assertFalse(matches("{ $.eventType NOT EXISTS }", "not json"), "an unparseable message matches nothing"); + assertFalse(matches("{ $.eventType = \"UpdateTrail\" }", "")); + } + + @Test + void aSelectorPointingAtAnObjectOrArrayDoesNotMatch() { + assertFalse(matches("{ $.arrayKey = \"value\" }", EVENT)); + assertFalse(matches("{ $.objectList[0] != \"x\" }", EVENT)); + assertFalse(matches("{ $.missing != \"x\" }", EVENT), "a missing field satisfies no comparison"); + } + + @Test + void bareWordsQuotedStringsAndBooleans() { + assertTrue(matches("{ $.eventType = UpdateTrail }", EVENT), "alphanumeric strings may be unquoted"); + assertTrue(matches("{ $.flag IS TRUE }", "{\"flag\": true}")); + assertTrue(matches("{ $.flag IS FALSE }", "{\"flag\": false}")); + assertFalse(matches("{ $.flag IS TRUE }", "{\"flag\": \"true\"}"), "IS TRUE wants a boolean"); + assertFalse(matches("{ $.flag = true }", "{\"flag\": true}"), + "AWS comparisons exclude booleans; IS TRUE tests boolean values"); + assertTrue(matches("{ $.flag = true }", "{\"flag\": \"true\"}")); + assertTrue(matches("{ $.eventType = \"*\" }", EVENT), "a wildcard string matches any value"); + assertFalse(matches("{ $.SomeObject = \"*\" }", EVENT), "but not null"); + assertTrue(matches("{ $.count = \"3\" }", "{\"count\": 3}"), "a number compares by its text too"); + assertTrue(matches("{ $.count = 3 }", "{\"count\": \"3\"}"), "and a numeric string by its value"); + assertFalse(matches("{ $.count > 2 }", "{\"count\": \"many\"}")); + } + + @Test + void jsonMatchesExposeValuesInternallyButNotInTheTestMetricFilterExtractionMap() { + FilterMatch m = match("{ $.eventType = \"UpdateTrail\" && $.objectList[1].id = 2 }", EVENT); + assertTrue(m.matched()); + assertEquals(Map.of(), m.extractedValues(), "observed AWS JSON TestMetricFilter response"); + assertEquals("UpdateTrail", m.value("$.eventType")); + assertEquals("111.111.111.111", m.value("$.sourceIPAddress"), "any field can be read off a match"); + assertEquals("c", m.value("$.['cluster.name']")); + assertNull(m.value("$.missing")); + assertNull(m.value("$.objectList"), "objects and arrays have no value"); + assertNull(m.value("$server"), "a space-delimited reference means nothing here"); + } + + @Test + void malformedPatternsAreRejected() { + for (String bad : List.of("{ $.a = }", "{ $.a }", "{ $.a = 1", "{ $.a = 1 } trailing", "{ a = 1 }", + "{ $.a == 1 }", "{ $.a = 1 && }", "{ ($.a = 1 }", "{ $.a IS MAYBE }", "{ $.a EXISTS }", + "{ $.a[x] = 1 }", "{ $.a = \"unterminated }", "{ $.x[99999999999999999999] = 1 }")) { + assertThrows(FilterPatternException.class, () -> FilterPattern.parse(bad), bad); + } + } + } + + @Nested + class SpaceDelimited { + + private static final String APACHE = + "127.0.0.1 Prod frank [10/Oct/2000:13:25:15 -0700] \"GET /index.html HTTP/1.0\" 404 1534"; + private static final String ACCESS_200 = + "127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 1534"; + + @Test + void quotedAndBracketedRunsAreSingleFields() { + FilterMatch m = match("[ip, user, username, timestamp, request, status_code, bytes]", APACHE); + assertTrue(m.matched()); + assertEquals("127.0.0.1", m.value("$ip")); + assertEquals("Prod", m.value("$user")); + assertEquals("10/Oct/2000:13:25:15 -0700", m.value("$timestamp")); + assertEquals("GET /index.html HTTP/1.0", m.value("$request")); + assertEquals("404", m.value("$status_code")); + assertEquals("1534", m.value("$bytes")); + assertEquals("1534", m.value("$7"), "positions can always be read"); + } + + /** + * The reference says {@code [w1=ERROR, w2]} matches lines of any length and that a blank + * indicator must follow the last term, which holds when the last field takes the rest of + * the line. An ellipsis stands for any number of fields, before, between or after the named ones. + */ + @Test + void fieldCountRules() { + FilterMatch rest = match("[ip, user, rest]", APACHE); + assertTrue(rest.matched()); + assertEquals("frank [10/Oct/2000:13:25:15 -0700] \"GET /index.html HTTP/1.0\" 404 1534", rest.value("$rest")); + assertFalse(matches("[w1=ERROR]", "ERROR 09/25/2014 12:00:02 Failed"), "without a blank indicator w1 is the whole line"); + assertFalse(matches("[a, b, c, d, e, f, g, h]", APACHE), "eight fields need eight words"); + assertFalse(matches("[w1=ERROR, w2]", "ERROR"), "a blank indicator still needs a word"); + assertTrue(matches("[..., bytes]", APACHE)); + assertTrue(matches("[ip, ..., bytes]", APACHE)); + assertTrue(matches("[ip, user, ...]", APACHE)); + assertFalse(matches("[a, b, c, d, e, f, g, h, ...]", APACHE), "eight named fields need eight fields"); + assertTrue(matches("[]", APACHE), "an empty field list matches any event"); + assertTrue(matches("[]", ""), "even an empty one"); + assertFalse(matches("[a]", "")); + } + + @Test + void theReferenceConditions() { + assertTrue(matches("[ip=%127\\.0\\.0\\.[1-9]%, user, username, timestamp, request =*.html*," + + " status_code = 4*, bytes]", APACHE)); + assertTrue(matches("[..., request =*.html*, status_code = 4*, bytes]", APACHE)); + assertTrue(matches("[ip, user, username, timestamp, request =*.html*, status_code = 404" + + " || status_code = 410, bytes]", APACHE)); + assertTrue(matches("[ip, server, username, timestamp, request, status_code, bytes > 1000]", APACHE)); + assertFalse(matches("[ip, server, username, timestamp, request, status_code, bytes > 2000]", APACHE)); + assertTrue(matches("[..., status_code=200, size]", ACCESS_200)); + assertFalse(matches("[..., status_code=200, size]", APACHE)); + } + + @Test + void theReferencePatternMatchingExamples() { + assertTrue(matches("[w1=ERROR, w2]", "ERROR 09/25/2014 12:00:02 Failed to process request")); + assertFalse(matches("[w1=ERROR, w2]", "INFO 09/25/2014 12:00:00 GET /service/resource/67 1200")); + assertTrue(matches("[w1=ERROR || w1=WARNING, w2]", "WARNING 09/25/2014 12:00:02 Invalid user request")); + assertTrue(matches("[w1!=ERROR && w1!=WARNING, w2]", "INFO 09/25/2014 12:00:00 GET /service/resource/67 1200")); + assertFalse(matches("[w1!=ERROR && w1!=WARNING, w2]", "ERROR 09/25/2014 12:00:02 Failed to process request")); + assertTrue(matches("[logLevel, date, time, method, url=%/service/resource/[0-9]+$%, response_time]", + "INFO 09/25/2014 12:00:00 GET /service/resource/67 1200")); + assertFalse(matches("[logLevel, date, time, method, url=%/service/resource/[0-9]+$%, response_time]", + "INFO 09/25/2014 12:00:01 POST /service/resource/67/part/111 1310")); + assertTrue(matches("[logLevel, date, time, method, url=%/service/resource/[0-9]+/part/[0-9]+$%, response_time]", + "INFO 09/25/2014 12:00:01 POST /service/resource/67/part/111 1310")); + assertTrue(matches("[w1=ERROR || w1=%WARN%, w2]", "WARNING 09/25/2014 12:00:02 Invalid user request")); + } + + /** The extracted values of the TestMetricFilter reference examples, verbatim. */ + @Test + void extractedValuesNameFieldsAndNumberTheRest() { + FilterMatch all = match("[ip, identity, user_id, timestamp, request, status_code, size]", ACCESS_200); + assertEquals(Map.of("$ip", "127.0.0.1", "$identity", "-", "$user_id", "frank", + "$timestamp", "10/Oct/2000:13:25:15 -0700", "$request", "GET /apache_pb.gif HTTP/1.0", + "$status_code", "200", "$size", "1534"), all.extractedValues()); + + FilterMatch ellipsis = match("[..., size]", ACCESS_200); + assertEquals(Map.of("$1", "127.0.0.1", "$2", "-", "$3", "frank", "$4", "10/Oct/2000:13:25:15 -0700", + "$5", "GET /apache_pb.gif HTTP/1.0", "$6", "200", "$size", "1534"), ellipsis.extractedValues()); + + FilterMatch none = match("[]", ACCESS_200); + assertEquals("1534", none.extractedValues().get("$7")); + assertEquals(7, none.extractedValues().size()); + + FilterMatch mixed = match("[..., status_code=200, size]", ACCESS_200); + assertEquals("200", mixed.extractedValues().get("$status_code")); + assertEquals("1534", mixed.extractedValues().get("$size")); + assertEquals("frank", mixed.extractedValues().get("$3")); + + FilterMatch trailingComma = match("[..., request=*.html*, status_code=4*,]", APACHE); + assertTrue(trailingComma.matched()); + assertEquals("1534", trailingComma.extractedValues().get("$7"), "an empty entry is an unnamed field"); + assertEquals("404", trailingComma.extractedValues().get("$status_code")); + } + + @Test + void valuesOfNamedAndNumberedFieldsCanBeReadOffAMatch() { + FilterMatch m = match("[..., status_code, bytes]", APACHE); + assertEquals("1534", m.value("$bytes")); + assertEquals("Prod", m.value("$2")); + assertNull(m.value("$missing")); + assertNull(m.value("$9")); + assertNull(m.value("$.bytes"), "a JSON reference means nothing here"); + } + + @Test + void quotedValuesAndNumbersCompare() { + assertTrue(matches("[..., request=\"GET /index.html HTTP/1.0\", status_code, bytes]", APACHE)); + assertTrue(matches("[..., status_code >= 400 && status_code < 500, bytes]", APACHE)); + assertTrue(matches("[..., status_code, bytes = 1534]", APACHE)); + assertFalse(matches("[..., status_code, bytes < 100]", APACHE)); + assertFalse(matches("[..., status_code, bytes > 100]", "a b c d e f text"), "a word is not a number"); + } + + @Test + void malformedPatternsAreRejected() { + for (String bad : List.of("[a, ..., b, ..., c]", "[a, a]", "[a = ]", "[a, b", "[a, b] trailing", + "[a = 1 ||]", "[1a]", "[a, b=x || c=y]")) { + assertThrows(FilterPatternException.class, () -> FilterPattern.parse(bad), bad); + } + } + } + + @Nested + class References { + + @ParameterizedTest + @ValueSource(strings = {"$ip", "$1", "$7"}) + void aDelimitedPatternDeclaresItsNamesAndAnyPosition(String reference) { + assertTrue(FilterPattern.parse("[ip, ..., bytes]").declaresField(reference)); + } + + @Test + void aDelimitedPatternDoesNotDeclareOtherNames() { + FilterPattern pattern = FilterPattern.parse("[ip, ..., bytes]"); + assertFalse(pattern.declaresField("$size")); + assertFalse(pattern.declaresField("$.ip")); + assertFalse(pattern.declaresField("ip")); + } + + @Test + void aJsonPatternDeclaresAnySelector() { + FilterPattern pattern = FilterPattern.parse("{ $.a = 1 }"); + assertTrue(pattern.declaresField("$.latency")); + assertTrue(pattern.declaresField("$.['a.b'][0]")); + assertFalse(pattern.declaresField("$latency")); + assertFalse(pattern.declaresField("$.a[")); + } + + @Test + void termPatternsDeclareNothing() { + assertFalse(FilterPattern.parse("ERROR").declaresField("$.a")); + assertFalse(FilterPattern.parse("").declaresField("$1")); + } + + @Test + void kindsAreReported() { + assertEquals(FilterPattern.Kind.MATCH_ALL, FilterPattern.parse(" ").kind()); + assertEquals(FilterPattern.Kind.TERMS, FilterPattern.parse("ERROR").kind()); + assertEquals(FilterPattern.Kind.TERMS, FilterPattern.parse("%ERR%").kind()); + assertEquals(FilterPattern.Kind.JSON, FilterPattern.parse("{ $.a = 1 }").kind()); + assertEquals(FilterPattern.Kind.SPACE_DELIMITED, FilterPattern.parse("[a, b]").kind()); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SystemFieldSelectionTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SystemFieldSelectionTest.java new file mode 100644 index 0000000000..1f558a41ea --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/filter/SystemFieldSelectionTest.java @@ -0,0 +1,94 @@ +package io.github.hectorvent.floci.services.cloudwatch.logs.filter; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SystemFieldSelectionTest { + + private static final String ACCOUNT = "@aws.account = \"111111111111\""; + private static final String REGION = "@aws.region = \"eu-west-1\""; + + @Test + void keywordOperatorsFollowTheirTruthTables() { + for (String account : List.of("111111111111", "222222222222")) { + for (String region : List.of("eu-west-1", "us-east-1")) { + boolean a = account.equals("111111111111"); + boolean b = region.equals("eu-west-1"); + for (String and : List.of("AND", "&&")) { + assertEquals(a && b, SystemFieldSelection.parse(ACCOUNT + " " + and + " " + REGION) + .test(account, region)); + } + for (String or : List.of("OR", "||")) { + assertEquals(a || b, SystemFieldSelection.parse(ACCOUNT + " " + or + " " + REGION) + .test(account, region)); + } + } + } + } + + @Test + void andBindsMoreTightlyThanOrAndParenthesesOverridePrecedence() { + for (String account : List.of("111111111111", "222222222222")) { + for (String region : List.of("eu-west-1", "us-east-1", "ap-south-1")) { + boolean a = account.equals("111111111111"); + boolean b = region.equals("eu-west-1"); + boolean c = !region.equals("us-east-1"); + String third = "@aws.region != \"us-east-1\""; + assertEquals(a || b && c, SystemFieldSelection.parse(ACCOUNT + " OR " + REGION + " AND " + third) + .test(account, region)); + assertEquals((a || b) && c, SystemFieldSelection.parse("(" + ACCOUNT + " OR " + REGION + ") AND " + third) + .test(account, region)); + } + } + } + + @Test + void membershipComposesWithoutRewritingKeywordsInsideValues() { + assertTrue(SystemFieldSelection.parse( + "@aws.account IN [\"111111111111\", \"222222222222\"] AND " + + "(@aws.region NOT IN [\"us-east-1\"] OR @aws.region = \"AND OR\")") + .test("111111111111", "AND OR")); + } + + @ParameterizedTest + @ValueSource(strings = { + "AND", "OR", "&", "|", "&&&", "|||", "AND AND", "OR OR", "XOR", "ANDROID", "ORDER" + }) + void malformedOperatorsAreRejected(String operator) { + assertThrows(FilterPatternException.class, () -> SystemFieldSelection.parse(ACCOUNT + " " + operator)); + if (!operator.equals("AND") && !operator.equals("OR")) { + assertThrows(FilterPatternException.class, + () -> SystemFieldSelection.parse(ACCOUNT + " " + operator + " " + REGION)); + } + } + + @ParameterizedTest + @ValueSource(strings = { + "@aws.account == \"111111111111\"", + "@aws.account = =", + "@aws.account = ||", + "@aws.account != &&", + "@aws.account NOTIN [\"111111111111\"]", + "@aws.account IN [\"111111111111\",]", + "@aws.account IN []", + "@aws.account = \"111111111111\" AND ()", + "(@aws.account = \"111111111111\"" + }) + void malformedComparisonsAreRejected(String expression) { + assertThrows(FilterPatternException.class, () -> SystemFieldSelection.parse(expression)); + } + + @ParameterizedTest + @ValueSource(strings = {"AND", "OR"}) + void keywordsDoNotBecomeJsonLogPatternOperators(String operator) { + assertThrows(FilterPatternException.class, + () -> FilterPattern.parse("{ $.a = 1 " + operator + " $.b = 2 }")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricFilterQueryNamespaceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricFilterQueryNamespaceTest.java new file mode 100644 index 0000000000..b3c04e2aec --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricFilterQueryNamespaceTest.java @@ -0,0 +1,34 @@ +package io.github.hectorvent.floci.services.cloudwatch.metrics; + +import io.github.hectorvent.floci.core.common.AwsNamespaces; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.XmlParser; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import jakarta.ws.rs.core.MultivaluedHashMap; +import jakarta.ws.rs.core.MultivaluedMap; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CloudWatchMetricFilterQueryNamespaceTest { + @ParameterizedTest + @ValueSource(strings = {"GetMetricStatistics", "GetMetricData"}) + void metricFilterReadApisUseTheCloudWatchQueryNamespace(String action) throws Exception { + CloudWatchMetricsService metrics = new CloudWatchMetricsService(new InMemoryStorage<>(), + new InMemoryStorage<>(), new RegionResolver("us-east-1", "000000000000")); + CloudWatchMetricsQueryHandler handler = new CloudWatchMetricsQueryHandler(metrics, null, null); + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("Namespace", "MetricFilters"); + params.putSingle("MetricName", "Count"); + params.putSingle("Period", "60"); + params.putSingle("StartTime", "2026-09-16T00:00:00Z"); + params.putSingle("EndTime", "2026-09-16T00:01:00Z"); + try (Response response = handler.handle(action, params, "us-east-1")) { + assertEquals(200, response.getStatus()); + assertEquals(AwsNamespaces.CW, XmlParser.parseDocument((String) response.getEntity()) + .getDocumentElement().getNamespaceURI()); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricPublicationStorageFaultTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricPublicationStorageFaultTest.java new file mode 100644 index 0000000000..a229b2d082 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricPublicationStorageFaultTest.java @@ -0,0 +1,102 @@ +package io.github.hectorvent.floci.services.cloudwatch.metrics; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.HybridStorage; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.PersistentStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.WalStorage; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.nio.file.Path; +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.*; + +/** Injects failures around real backend writes, not mock invocation counts. */ +class CloudWatchMetricPublicationStorageFaultTest { + private static final String ACCOUNT = "111111111111"; + private static final String REGION = "eu-west-1"; + private static final TypeReference> TYPE = new TypeReference<>() {}; + @TempDir Path directory; + + @ParameterizedTest + @CsvSource({"memory,false", "memory,true", "persistent,false", "persistent,true", + "hybrid,false", "hybrid,true", "wal,false", "wal,true"}) + void beforeAndAfterPartialCommitRemainSingleSamplesInEveryBackend(String mode, boolean after) { + StorageBackend actual = backend(mode); + AtomicBoolean armed = new AtomicBoolean(true); + AccountAwareStorageBackend faulting = new AccountAwareStorageBackend<>(actual, null, ACCOUNT) { + @Override + public void putForAccount(String account, String key, MetricDatum value) { + boolean fail = value.getValue() == 7 && armed.compareAndSet(true, false); + if (fail && !after) { throw new IllegalStateException("before actual backend write"); } + super.putForAccount(account, key, value); + if (fail) { throw new IllegalStateException("after actual backend write"); } + } + }; + try { + actual.load(); + CloudWatchMetricsService metrics = metrics(faulting); + assertThrows(IllegalStateException.class, () -> batch(metrics)); + assertEquals(after ? 2 : 1, actual.keys().size(), "the fault surrounds the second actual write"); + batch(metrics); + batch(metrics); + statistics(metrics); + assertEquals(3, actual.keys().size()); + actual.flush(); + } finally { close(actual); } + if (!mode.equals("memory")) { + StorageBackend reloaded = backend(mode); + try { + reloaded.load(); + statistics(metrics(new AccountAwareStorageBackend<>(reloaded, null, ACCOUNT))); + assertEquals(3, reloaded.keys().size()); + } finally { close(reloaded); } + } + } + + private StorageBackend backend(String mode) { + return switch (mode) { + case "memory" -> new InMemoryStorage<>(); + case "persistent" -> new PersistentStorage<>(directory.resolve("metrics.json"), TYPE); + case "hybrid" -> new HybridStorage<>(directory.resolve("metrics.json"), TYPE, 3_600_000); + case "wal" -> new WalStorage<>(directory.resolve("metrics-snapshot.json"), directory.resolve("metrics.wal"), TYPE, 3_600_000); + default -> throw new IllegalArgumentException(mode); + }; + } + + private static void close(StorageBackend backend) { + if (backend instanceof HybridStorage hybrid) { hybrid.shutdown(); } + if (backend instanceof WalStorage wal) { wal.shutdown(); } + } + + private static CloudWatchMetricsService metrics(AccountAwareStorageBackend store) { + return new CloudWatchMetricsService(store, new InMemoryStorage<>(), new RegionResolver(REGION, ACCOUNT)); + } + + private static void batch(CloudWatchMetricsService metrics) { + double[] values = {3, 7, 3}; + for (int i = 0; i < values.length; i++) { + MetricDatum datum = new MetricDatum(); + datum.setMetricName("Value"); datum.setValue(values[i]); datum.setTimestamp(120); datum.setUnit("Count"); + metrics.publishMetricForAccount(ACCOUNT, "App", datum, REGION, "stable-" + i); + } + } + + private static void statistics(CloudWatchMetricsService metrics) { + List points = metrics.getMetricStatistics("App", "Value", List.of(), + Instant.ofEpochSecond(120), Instant.ofEpochSecond(179), 60, List.of("Sum", "SampleCount"), null, REGION); + assertEquals(1, points.size()); + assertEquals(13, points.getFirst().sum()); + assertEquals(3, points.getFirst().sampleCount()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricPublicationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricPublicationTest.java new file mode 100644 index 0000000000..ba498a2d71 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricPublicationTest.java @@ -0,0 +1,93 @@ +package io.github.hectorvent.floci.services.cloudwatch.metrics; + +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; +import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.*; + +class CloudWatchMetricPublicationTest { + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void partialStorageFailureBeforeOrAfterWriteIsIdempotentWithStableIds(boolean after) { + AtomicBoolean armed = new AtomicBoolean(true); + List secondKeys = new ArrayList<>(); + InMemoryStorage store = new InMemoryStorage<>() { + @Override + public void put(String key, MetricDatum value) { + if (value.getValue() == 7) { secondKeys.add(key); } + boolean fail = value.getValue() == 7 && armed.compareAndSet(true, false); + if (fail && !after) { throw new IllegalStateException("before store.put"); } + super.put(key, value); + if (fail) { throw new IllegalStateException("after store.put"); } + } + }; + CloudWatchMetricsService service = service(store); + assertThrows(IllegalStateException.class, () -> publishBatch(service)); + publishBatch(service); + publishBatch(service); + List points = service.getMetricStatistics("App", "Value", List.of(), + Instant.ofEpochSecond(120), Instant.ofEpochSecond(179), 60, List.of("Sum", "SampleCount"), null, "eu-west-1"); + assertEquals(1, points.size()); + assertEquals(13, points.getFirst().sum()); + assertEquals(3, points.getFirst().sampleCount()); + assertEquals(1, secondKeys.stream().distinct().count(), "ambiguous writes must reuse the exact storage key"); + } + + private static void publishBatch(CloudWatchMetricsService service) { + double[] values = {3, 7, 3}; + for (int i = 0; i < values.length; i++) { + service.publishMetricForAccount("111111111111", "App", datum(values[i], 120), "eu-west-1", "batch-" + i); + } + } + + @Test + void ordinaryPutMetricDataStillAppendsIdenticalInputsAndSuppliesMissingTime() { + CloudWatchMetricsService service = service(new InMemoryStorage<>()); + service.putMetricDataForAccount("111111111111", "App", List.of(datum(7, 120)), "eu-west-1"); + service.putMetricDataForAccount("111111111111", "App", List.of(datum(7, 120)), "eu-west-1"); + List points = service.getMetricStatistics("App", "Value", List.of(), + Instant.ofEpochSecond(120), Instant.ofEpochSecond(179), 60, List.of("Sum"), null, "eu-west-1"); + assertEquals(14, points.getFirst().sum()); + assertEquals(2, points.getFirst().sampleCount()); + MetricDatum missingTime = datum(1, 0); + service.putMetricData("App", List.of(missingTime), "eu-west-1"); + assertTrue(missingTime.getTimestamp() > 0); + } + + @Test + void internalPublicationPreservesExplicitEpochZeroAndDoesNotMutateCallerDatum() { + CloudWatchMetricsService service = service(new InMemoryStorage<>()); + MetricDatum input = datum(7, 0); + input.setDimensions(new ArrayList<>(List.of(new Dimension("A", "a")))); + service.publishMetricForAccount("111111111111", "App", input, "eu-west-1", "epoch"); + assertEquals(0, input.getTimestamp()); + assertNull(input.getNamespace()); + input.getDimensions().clear(); + List points = service.getMetricStatistics("App", "Value", + List.of(new Dimension("A", "a")), Instant.EPOCH, Instant.ofEpochSecond(59), 60, List.of("Sum"), null, "eu-west-1"); + assertEquals(1, points.size()); + assertEquals(7, points.getFirst().sum()); + } + + private static MetricDatum datum(double value, long timestamp) { + MetricDatum d = new MetricDatum(); + d.setMetricName("Value"); d.setValue(value); d.setTimestamp(timestamp); d.setUnit("Count"); + return d; + } + + private static CloudWatchMetricsService service(InMemoryStorage store) { + return new CloudWatchMetricsService(new AccountAwareStorageBackend<>(store, null, "111111111111"), + new InMemoryStorage<>(), new RegionResolver("eu-west-1", "111111111111")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java index ec23f2084c..438c8b9a21 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudwatch.metrics; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricAlarm; @@ -253,4 +254,20 @@ class CloudWatchMetricsServiceTest { assertEquals("", CloudWatchMetricsService.buildDimKey(List.of())); assertEquals("", CloudWatchMetricsService.buildDimKey(null)); } + + /** A writer outside any request can store datums in the account it acts for, not the default one. */ + @Test + void putMetricDataForAccountStoresInThatAccountsPartition() { + AccountAwareStorageBackend store = AccountAwareStorageBackend.inMemory("000000000000"); + CloudWatchMetricsService accountAware = new CloudWatchMetricsService(store, new InMemoryStorage<>(), + new RegionResolver("us-east-1", "000000000000")); + + accountAware.putMetricDataForAccount("111111111111", NAMESPACE, List.of(datum("Requests", 1)), REGION); + accountAware.putMetricData(NAMESPACE, List.of(datum("Requests", 2)), REGION); + + assertEquals(1, store.scanForAccount("111111111111", k -> true).size()); + assertEquals(1.0, store.scanForAccount("111111111111", k -> true).getFirst().getValue()); + assertEquals(1, store.scan(k -> true).size(), "the default partition holds only the ambient put"); + assertEquals(2.0, store.scan(k -> true).getFirst().getValue()); + } } diff --git a/src/test/resources/cloudformation/supported-resource-types.tsv b/src/test/resources/cloudformation/supported-resource-types.tsv index 5ed7678bed..f893ec6940 100644 --- a/src/test/resources/cloudformation/supported-resource-types.tsv +++ b/src/test/resources/cloudformation/supported-resource-types.tsv @@ -111,6 +111,7 @@ AWS::Lambda::Permission LambdaAddressingCfnProvisioner AWS::Lambda::Url LambdaAddressingCfnProvisioner AWS::Lambda::Version LambdaVersionAliasCfnProvisioner AWS::Logs::LogGroup LogsCfnProvisioner +AWS::Logs::MetricFilter LogsMetricFilterCfnProvisioner AWS::Organizations::Account OrganizationsCfnProvisioner AWS::Organizations::Organization OrganizationsCfnProvisioner AWS::Organizations::OrganizationalUnit OrganizationsCfnProvisioner diff --git a/src/test/resources/cloudwatchlogs/filter-pattern-aws-boundaries.json b/src/test/resources/cloudwatchlogs/filter-pattern-aws-boundaries.json new file mode 100644 index 0000000000..86ceedd86a --- /dev/null +++ b/src/test/resources/cloudwatchlogs/filter-pattern-aws-boundaries.json @@ -0,0 +1,271 @@ +{ + "provenance": { + "source": "AWS CloudWatch Logs TestMetricFilter", + "capturedAt": "2026-09-16T08:35:06.272542+00:00", + "region": "eu-west-1", + "endpoint": "https://logs.eu-west-1.amazonaws.com", + "inputs": "Synthetic only. No resource writes or user logs.", + "eventNumberBase": 1, + "reference": "https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_TestMetricFilter.html" + }, + "cases": [ + { + "id": "boundary-array-true", + "request": { + "filterPattern": "{ $.a IS TRUE }", + "logEventMessages": [ + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":[true,false]}", + "{\"a\":[null]}", + "{\"a\":[[null]]}", + "{\"a\":[{\"b\":2},{\"b\":1}]}", + "{\"a\":[{}]}", + "{}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":true}", + "extractedValues": {} + } + ] + } + }, + { + "id": "boundary-array-false", + "request": { + "filterPattern": "{ $.a IS FALSE }", + "logEventMessages": [ + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":[true,false]}", + "{\"a\":[null]}", + "{\"a\":[[null]]}", + "{\"a\":[{\"b\":2},{\"b\":1}]}", + "{\"a\":[{}]}", + "{}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "{\"a\":false}", + "extractedValues": {} + } + ] + } + }, + { + "id": "boundary-array-null", + "request": { + "filterPattern": "{ $.a IS NULL }", + "logEventMessages": [ + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":[true,false]}", + "{\"a\":[null]}", + "{\"a\":[[null]]}", + "{\"a\":[{\"b\":2},{\"b\":1}]}", + "{\"a\":[{}]}", + "{}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 4, + "eventMessage": "{\"a\":[null]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "boundary-nested-negative", + "request": { + "filterPattern": "{ $.a[*].b != 2 }", + "logEventMessages": [ + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":[true,false]}", + "{\"a\":[null]}", + "{\"a\":[[null]]}", + "{\"a\":[{\"b\":2},{\"b\":1}]}", + "{\"a\":[{}]}", + "{}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":true}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":false}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":[true,false]}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":[null]}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":[[null]]}", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "{\"a\":[{}]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "boundary-wildcard-null", + "request": { + "filterPattern": "{ $.a[*] IS NULL }", + "logEventMessages": [ + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":[true,false]}", + "{\"a\":[null]}", + "{\"a\":[[null]]}", + "{\"a\":[{\"b\":2},{\"b\":1}]}", + "{\"a\":[{}]}", + "{}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 4, + "eventMessage": "{\"a\":[null]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "boundary-term-slash", + "request": { + "filterPattern": "a/b", + "logEventMessages": [ + "a/b", + "a b" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '/'" + } + }, + { + "id": "boundary-term-at", + "request": { + "filterPattern": "a@b", + "logEventMessages": [ + "a@b", + "a b" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '@'" + } + }, + { + "id": "boundary-term-unicode", + "request": { + "filterPattern": "caf\u00e9", + "logEventMessages": [ + "caf\u00e9", + "cafe" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "caf\u00e9", + "extractedValues": {} + } + ] + } + }, + { + "id": "boundary-quoted-space", + "request": { + "filterPattern": "\" \"", + "logEventMessages": [ + "x", + "two words", + " " + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "two words", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": " ", + "extractedValues": {} + } + ] + } + }, + { + "id": "boundary-huge-exponent", + "request": { + "filterPattern": "{ $.a = 1e2147483648 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1e2147483648\"}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid metric filter pattern" + } + }, + { + "id": "boundary-bare-negative", + "request": { + "filterPattern": "a-b", + "logEventMessages": [ + "a", + "b", + "a-b", + "ab", + "a b" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "a", + "extractedValues": {} + } + ] + } + } + ] +} diff --git a/src/test/resources/cloudwatchlogs/filter-pattern-aws-edges.json b/src/test/resources/cloudwatchlogs/filter-pattern-aws-edges.json new file mode 100644 index 0000000000..f4b27f1365 --- /dev/null +++ b/src/test/resources/cloudwatchlogs/filter-pattern-aws-edges.json @@ -0,0 +1,1394 @@ +{ + "provenance": { + "source": "AWS CloudWatch Logs TestMetricFilter", + "capturedAt": "2026-09-16T08:29:15.333461+00:00", + "region": "eu-west-1", + "endpoint": "https://logs.eu-west-1.amazonaws.com", + "inputs": "Synthetic only. No resource writes or user logs.", + "eventNumberBase": 1, + "reference": "https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_TestMetricFilter.html" + }, + "cases": [ + { + "id": "edge-decimal", + "request": { + "filterPattern": "{ $.a = 1.0 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1e0}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"1.0\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-zero", + "request": { + "filterPattern": "{ $.a = 0 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 7, + "eventMessage": "{\"a\":0.0}", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "{\"a\":-0.0}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e-999}", + "extractedValues": {} + }, + { + "eventNumber": 17, + "eventMessage": "{\"a\":0}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-negative-zero", + "request": { + "filterPattern": "{ $.a = -0.0 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 7, + "eventMessage": "{\"a\":0.0}", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "{\"a\":-0.0}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e-999}", + "extractedValues": {} + }, + { + "eventNumber": 17, + "eventMessage": "{\"a\":0}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-precise-long", + "request": { + "filterPattern": "{ $.a = 9007199254740993 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 11, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-large-finite", + "request": { + "filterPattern": "{ $.a = 1e308 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 12, + "eventMessage": "{\"a\":1e308}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-large-ordered", + "request": { + "filterPattern": "{ $.a > 1e307 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 12, + "eventMessage": "{\"a\":1e308}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-underflow-ordered", + "request": { + "filterPattern": "{ $.a > 1e-999 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1e0}", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "{\"a\":9007199254740992}", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + }, + { + "eventNumber": 12, + "eventMessage": "{\"a\":1e308}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-string-exponent", + "request": { + "filterPattern": "{ $.a = \"1e0\" }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":1.0}", + "{\"a\":1e0}", + "{\"a\":\"1\"}", + "{\"a\":\"1.0\"}", + "{\"a\":\"01\"}", + "{\"a\":0.0}", + "{\"a\":-0.0}", + "{\"a\":-1}", + "{\"a\":9007199254740992}", + "{\"a\":9007199254740993}", + "{\"a\":1e308}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":\"1e-999\"}", + "{\"a\":0}", + "{\"a\":true}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{\"a\":[1,2]}", + "{\"a\":[null,2]}" + ] + }, + "response": { + "matches": [] + } + }, + { + "id": "edge-is-null", + "request": { + "filterPattern": "{ $.a IS NULL }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-equals", + "request": { + "filterPattern": "{ $.a = 2 }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "{\"a\":2}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"2\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-not-equals", + "request": { + "filterPattern": "{ $.a != 2 }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 4, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-wildcard-not-equals", + "request": { + "filterPattern": "{ $.a[*] != 2 }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":2}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"2\"}", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "{\"a\":[]}", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "{\"a\":[1]}", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "{\"a\":{}}", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "{\"a\":[{\"b\":1},{\"b\":2}]}", + "extractedValues": {} + }, + { + "eventNumber": 14, + "eventMessage": "{\"a\":[{\"b\":null}]}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":[[1,2]]}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":[true,false]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-wildcard-null", + "request": { + "filterPattern": "{ $.a[*] IS NULL }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 10, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-wildcard-missing", + "request": { + "filterPattern": "{ $.a[*] NOT EXISTS }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":2}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"2\"}", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "{\"a\":[]}", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "{\"a\":{}}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-missing", + "request": { + "filterPattern": "{ $.a NOT EXISTS }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-child", + "request": { + "filterPattern": "{ $.a.b = 2 }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 12, + "eventMessage": "{\"a\":{\"b\":2}}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-child-missing", + "request": { + "filterPattern": "{ $.a.b NOT EXISTS }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":2}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"2\"}", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "{\"a\":[]}", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "{\"a\":[1,2]}", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "{\"a\":[2,1]}", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "{\"a\":[1]}", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "{\"a\":{}}", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "{\"a\":[{\"b\":1},{\"b\":2}]}", + "extractedValues": {} + }, + { + "eventNumber": 14, + "eventMessage": "{\"a\":[{\"b\":null}]}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":[[1,2]]}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":[true,false]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-star", + "request": { + "filterPattern": "{ $.a = * }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "{\"a\":2}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"2\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-quoted-number", + "request": { + "filterPattern": "{ $.a = \"2\" }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "{\"a\":2}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"2\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-bare-null", + "request": { + "filterPattern": "{ $.a = null }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid metric filter pattern" + } + }, + { + "id": "edge-is-not-null", + "request": { + "filterPattern": "{ $.a IS NOT NULL }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term 'NOT'" + } + }, + { + "id": "edge-selector-root", + "request": { + "filterPattern": "{ $ = 2 }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '$ '" + } + }, + { + "id": "edge-json-negative-regex", + "request": { + "filterPattern": "{ $.a != %2% }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 4, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-regex-text", + "request": { + "filterPattern": "ERROR %WARN%", + "logEventMessages": [ + "ERROR WARN", + "ERROR", + "WARN" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%'" + } + }, + { + "id": "edge-regex-optional", + "request": { + "filterPattern": "?%WARN%", + "logEventMessages": [ + "WARN", + "WARN INFO" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%'" + } + }, + { + "id": "edge-term-punctuation", + "request": { + "filterPattern": "a-b", + "logEventMessages": [ + "a-b", + "a b" + ] + }, + "response": { + "matches": [] + } + }, + { + "id": "edge-term-underscore", + "request": { + "filterPattern": "a_b", + "logEventMessages": [ + "a_b", + "a b" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "a_b", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-term-dot", + "request": { + "filterPattern": "a.b", + "logEventMessages": [ + "a.b", + "a b" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "a.b", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-term-question", + "request": { + "filterPattern": "a?b", + "logEventMessages": [ + "a?b", + "a b" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "a?b", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "a b", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-regex-structured-two", + "request": { + "filterPattern": "{ $.a = %1% || $.a = %2% }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "{\"a\":2}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":\"2\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-regex-structured-three", + "request": { + "filterPattern": "{ $.a = %1% || $.a = %2% || $.a = %3% }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Your input has exceeded the maximum of 2 regular expressions per filter pattern." + } + }, + { + "id": "edge-quoted-order-space", + "request": { + "filterPattern": "[a>\"1\", b]", + "logEventMessages": [ + "2 x", + "0 x" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid metric filter pattern" + } + }, + { + "id": "edge-json-keyword-case", + "request": { + "filterPattern": "{ $.a is null }", + "logEventMessages": [ + "{}", + "{\"a\":null}", + "{\"a\":2}", + "{\"a\":1}", + "{\"a\":\"2\"}", + "{\"a\":[]}", + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[1]}", + "{\"a\":[null,2]}", + "{\"a\":{}}", + "{\"a\":{\"b\":2}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a\":[{\"b\":null}]}", + "{\"a\":[[1,2]]}", + "{\"a\":[true,false]}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "edge-json-unquoted-symbol", + "request": { + "filterPattern": "{ $.a = a:b }", + "logEventMessages": [ + "{\"a\":\"a:b\"}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term ':'" + } + }, + { + "id": "edge-space-quote-trailing", + "request": { + "filterPattern": "[a, b]", + "logEventMessages": [ + "x \"y\" ", + "x [y] ", + "x y ", + "x ", + "x \"\"", + "x []" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "x \"y\" ", + "extractedValues": { + "$a": "x", + "$b": "y" + } + }, + { + "eventNumber": 2, + "eventMessage": "x [y] ", + "extractedValues": { + "$a": "x", + "$b": "y" + } + }, + { + "eventNumber": 3, + "eventMessage": "x y ", + "extractedValues": { + "$a": "x", + "$b": "y" + } + }, + { + "eventNumber": 5, + "eventMessage": "x \"\"", + "extractedValues": { + "$a": "x", + "$b": "" + } + }, + { + "eventNumber": 6, + "eventMessage": "x []", + "extractedValues": { + "$a": "x", + "$b": "" + } + } + ] + } + }, + { + "id": "edge-space-incomplete", + "request": { + "filterPattern": "[a, b, c]", + "logEventMessages": [ + "x \"y z", + "x [y z", + "x \"y z q", + "x [y z q" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "x \"y z", + "extractedValues": { + "$a": "x", + "$b": "\"y", + "$c": "z" + } + }, + { + "eventNumber": 2, + "eventMessage": "x [y z", + "extractedValues": { + "$a": "x", + "$b": "[y", + "$c": "z" + } + }, + { + "eventNumber": 3, + "eventMessage": "x \"y z q", + "extractedValues": { + "$a": "x", + "$b": "\"y", + "$c": "z q" + } + }, + { + "eventNumber": 4, + "eventMessage": "x [y z q", + "extractedValues": { + "$a": "x", + "$b": "[y", + "$c": "z q" + } + } + ] + } + } + ] +} diff --git a/src/test/resources/cloudwatchlogs/filter-pattern-aws.json b/src/test/resources/cloudwatchlogs/filter-pattern-aws.json new file mode 100644 index 0000000000..57ffd32051 --- /dev/null +++ b/src/test/resources/cloudwatchlogs/filter-pattern-aws.json @@ -0,0 +1,5242 @@ +{ + "provenance": { + "source": "AWS CloudWatch Logs TestMetricFilter", + "capturedAt": "2026-09-16T08:26:23.333750+00:00", + "region": "eu-west-1", + "endpoint": "https://logs.eu-west-1.amazonaws.com", + "inputs": "Synthetic only. No resource writes or user logs.", + "eventNumberBase": 1, + "reference": "https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_TestMetricFilter.html" + }, + "cases": [ + { + "id": "terms-substring", + "request": { + "filterPattern": "ERROR", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "ERRORS", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "xERROR", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "[ERROR]", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "ERROR_code", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "ERROR timeout", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-case", + "request": { + "filterPattern": "error", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 4, + "eventMessage": "error", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-and", + "request": { + "filterPattern": "ERROR timeout", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 7, + "eventMessage": "ERROR timeout", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-negative", + "request": { + "filterPattern": "ERROR -timeout", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "ERRORS", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "xERROR", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "[ERROR]", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "ERROR_code", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-negative-only", + "request": { + "filterPattern": "-ERROR", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 4, + "eventMessage": "error", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "INFO timeout", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "WARN", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "two words", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "two words", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-optional", + "request": { + "filterPattern": "?ERROR ?WARN", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "ERRORS", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "xERROR", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "[ERROR]", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "ERROR_code", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "ERROR timeout", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "WARN", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-optional-ignored", + "request": { + "filterPattern": "?ERROR timeout", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 7, + "eventMessage": "ERROR timeout", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "INFO timeout", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-optional-negative", + "request": { + "filterPattern": "?ERROR -timeout", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "ERRORS", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "xERROR", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "error", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "[ERROR]", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "ERROR_code", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "WARN", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "two words", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "two words", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-phrase", + "request": { + "filterPattern": "\"two words\"", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 10, + "eventMessage": "two words", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-negative-phrase", + "request": { + "filterPattern": "-\"two words\"", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "ERRORS", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "xERROR", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "error", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "[ERROR]", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "ERROR_code", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "ERROR timeout", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "INFO timeout", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "WARN", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "two words", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-optional-phrase", + "request": { + "filterPattern": "?\"two words\" ?WARN", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 9, + "eventMessage": "WARN", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "two words", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-wildcard", + "request": { + "filterPattern": "ERR*", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '*'" + } + }, + { + "id": "terms-quoted-wildcard", + "request": { + "filterPattern": "\"ERR*\"", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [] + } + }, + { + "id": "terms-bare-symbol", + "request": { + "filterPattern": "ERROR!", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '!'" + } + }, + { + "id": "terms-bare-colon", + "request": { + "filterPattern": "ERROR:", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term ':'" + } + }, + { + "id": "terms-empty", + "request": { + "filterPattern": "", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "ERRORS", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "xERROR", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "error", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "[ERROR]", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "ERROR_code", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "ERROR timeout", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "INFO timeout", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "WARN", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "two words", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "two words", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-blank", + "request": { + "filterPattern": " ", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "ERRORS", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "xERROR", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "error", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "[ERROR]", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "ERROR_code", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "ERROR timeout", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "INFO timeout", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "WARN", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "two words", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "two words", + "extractedValues": {} + } + ] + } + }, + { + "id": "terms-quoted-empty", + "request": { + "filterPattern": "\"\"", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '\"\"'" + } + }, + { + "id": "terms-missing-quote", + "request": { + "filterPattern": "\"ERROR", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '\"ERROR'" + } + }, + { + "id": "terms-adjacent-quote", + "request": { + "filterPattern": "\"ERROR\"WARN", + "logEventMessages": [ + "ERROR", + "ERRORS", + "xERROR", + "error", + "[ERROR]", + "ERROR_code", + "ERROR timeout", + "INFO timeout", + "WARN", + "two words", + "two words" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid metric filter pattern" + } + }, + { + "id": "regex-alternation", + "request": { + "filterPattern": "%ERROR|WARN%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "WARN", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-anchors", + "request": { + "filterPattern": "%^[hc]at$%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 5, + "eventMessage": "cat", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "hat", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-quantifier", + "request": { + "filterPattern": "%^a{3,5}$%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "aaaa", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-optional", + "request": { + "filterPattern": "%colou?r%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 8, + "eventMessage": "color", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "colour", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-digits", + "request": { + "filterPattern": "%\\d+%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 13, + "eventMessage": "123", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-word", + "request": { + "filterPattern": "%\\w+%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "WARN", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "aaaa", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "aa", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "cat", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "a cat", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "hat", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "color", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "colour", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "a-b", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "a b", + "extractedValues": {} + }, + { + "eventNumber": 12, + "eventMessage": "a\nb", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "123", + "extractedValues": {} + }, + { + "eventNumber": 14, + "eventMessage": "_", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "caf\u00e9", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "key:value", + "extractedValues": {} + }, + { + "eventNumber": 17, + "eventMessage": "ab", + "extractedValues": {} + }, + { + "eventNumber": 18, + "eventMessage": "back\\slash", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-space", + "request": { + "filterPattern": "%a\\sb%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 11, + "eventMessage": "a b", + "extractedValues": {} + }, + { + "eventNumber": 12, + "eventMessage": "a\nb", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-hex", + "request": { + "filterPattern": "%\\x3A%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 16, + "eventMessage": "key:value", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-dot-newline", + "request": { + "filterPattern": "%a.b%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 10, + "eventMessage": "a-b", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "a b", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-escaped-symbol", + "request": { + "filterPattern": "%a\\-b%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 10, + "eventMessage": "a-b", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-group-rejected", + "request": { + "filterPattern": "%(ab)+%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%('" + } + }, + { + "id": "regex-unicode-rejected", + "request": { + "filterPattern": "%caf\u00e9%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%caf\u00e9'" + } + }, + { + "id": "regex-symbol-rejected", + "request": { + "filterPattern": "%!%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%!'" + } + }, + { + "id": "regex-escape-rejected", + "request": { + "filterPattern": "%\\p%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Failed to compile regular expression." + } + }, + { + "id": "regex-escaped-group", + "request": { + "filterPattern": "%\\(ab\\)%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%\\('" + } + }, + { + "id": "regex-empty", + "request": { + "filterPattern": "%%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%%'" + } + }, + { + "id": "regex-unclosed", + "request": { + "filterPattern": "%abc", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "If a filter pattern starts with '%' it must end with '%'" + } + }, + { + "id": "regex-bad-range", + "request": { + "filterPattern": "%[z-a]%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Failed to compile regular expression." + } + }, + { + "id": "regex-lazy", + "request": { + "filterPattern": "%a+?%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "aaaa", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "aa", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "cat", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "a cat", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "hat", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "a-b", + "extractedValues": {} + }, + { + "eventNumber": 11, + "eventMessage": "a b", + "extractedValues": {} + }, + { + "eventNumber": 12, + "eventMessage": "a\nb", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "caf\u00e9", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "key:value", + "extractedValues": {} + }, + { + "eventNumber": 17, + "eventMessage": "ab", + "extractedValues": {} + }, + { + "eventNumber": 18, + "eventMessage": "back\\slash", + "extractedValues": {} + } + ] + } + }, + { + "id": "regex-possessive", + "request": { + "filterPattern": "%a++%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Failed to compile regular expression." + } + }, + { + "id": "regex-class-intersection", + "request": { + "filterPattern": "%[a&&b]%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '%[a&'" + } + }, + { + "id": "regex-two", + "request": { + "filterPattern": "%ERROR% %WARN%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term ' '" + } + }, + { + "id": "regex-three", + "request": { + "filterPattern": "%ERROR% %WARN% %INFO%", + "logEventMessages": [ + "ERROR", + "WARN", + "aaaa", + "aa", + "cat", + "a cat", + "hat", + "color", + "colour", + "a-b", + "a b", + "a\nb", + "123", + "_", + "caf\u00e9", + "key:value", + "ab", + "back\\slash", + "!" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term ' '" + } + }, + { + "id": "json-numeric", + "request": { + "filterPattern": "{ $.a = 1 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":\"1\"}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-quoted-number", + "request": { + "filterPattern": "{ $.a = \"1\" }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":\"1\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-numeric-ne", + "request": { + "filterPattern": "{ $.a != 1 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 4, + "eventMessage": "{\"a\":\"01\"}", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "{\"a\":\"true\"}", + "extractedValues": {} + }, + { + "eventNumber": 12, + "eventMessage": "{\"a\":\"foo\"}", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "{\"a\":\"foobar\"}", + "extractedValues": {} + }, + { + "eventNumber": 14, + "eventMessage": "{\"a\":\"FOO\"}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e3}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":\"1e3\"}", + "extractedValues": {} + }, + { + "eventNumber": 18, + "eventMessage": "{\"a\":\"1e309\"}", + "extractedValues": {} + }, + { + "eventNumber": 19, + "eventMessage": "{\"a\":1e-999}", + "extractedValues": {} + }, + { + "eventNumber": 20, + "eventMessage": "{\"a\":-0.0}", + "extractedValues": {} + }, + { + "eventNumber": 21, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-string-ne", + "request": { + "filterPattern": "{ $.a != \"foo\" }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":\"1\"}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":\"01\"}", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "{\"a\":\"true\"}", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "{\"a\":\"foobar\"}", + "extractedValues": {} + }, + { + "eventNumber": 14, + "eventMessage": "{\"a\":\"FOO\"}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e3}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":\"1e3\"}", + "extractedValues": {} + }, + { + "eventNumber": 17, + "eventMessage": "{\"a\":1e309}", + "extractedValues": {} + }, + { + "eventNumber": 18, + "eventMessage": "{\"a\":\"1e309\"}", + "extractedValues": {} + }, + { + "eventNumber": 19, + "eventMessage": "{\"a\":1e-999}", + "extractedValues": {} + }, + { + "eventNumber": 20, + "eventMessage": "{\"a\":-0.0}", + "extractedValues": {} + }, + { + "eventNumber": 21, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-ordering", + "request": { + "filterPattern": "{ $.a > 0 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e3}", + "extractedValues": {} + }, + { + "eventNumber": 21, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-quoted-order", + "request": { + "filterPattern": "{ $.a > \"0\" }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '\"0\"'" + } + }, + { + "id": "json-bare-word", + "request": { + "filterPattern": "{ $.a = foo }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 12, + "eventMessage": "{\"a\":\"foo\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-bare-boolean", + "request": { + "filterPattern": "{ $.a = true }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 7, + "eventMessage": "{\"a\":\"true\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-quoted-boolean", + "request": { + "filterPattern": "{ $.a = \"true\" }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 7, + "eventMessage": "{\"a\":\"true\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-is-true", + "request": { + "filterPattern": "{ $.a IS TRUE }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 5, + "eventMessage": "{\"a\":true}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-is-false", + "request": { + "filterPattern": "{ $.a IS FALSE }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 6, + "eventMessage": "{\"a\":false}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-null", + "request": { + "filterPattern": "{ $.a IS NULL }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 8, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-missing", + "request": { + "filterPattern": "{ $.a NOT EXISTS }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 9, + "eventMessage": "{}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-star", + "request": { + "filterPattern": "{ $.a = * }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":\"1\"}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":\"01\"}", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "{\"a\":\"true\"}", + "extractedValues": {} + }, + { + "eventNumber": 12, + "eventMessage": "{\"a\":\"foo\"}", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "{\"a\":\"foobar\"}", + "extractedValues": {} + }, + { + "eventNumber": 14, + "eventMessage": "{\"a\":\"FOO\"}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e3}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":\"1e3\"}", + "extractedValues": {} + }, + { + "eventNumber": 17, + "eventMessage": "{\"a\":1e309}", + "extractedValues": {} + }, + { + "eventNumber": 18, + "eventMessage": "{\"a\":\"1e309\"}", + "extractedValues": {} + }, + { + "eventNumber": 19, + "eventMessage": "{\"a\":1e-999}", + "extractedValues": {} + }, + { + "eventNumber": 20, + "eventMessage": "{\"a\":-0.0}", + "extractedValues": {} + }, + { + "eventNumber": 21, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-quoted-star", + "request": { + "filterPattern": "{ $.a = \"*\" }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":\"1\"}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":\"01\"}", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "{\"a\":\"true\"}", + "extractedValues": {} + }, + { + "eventNumber": 12, + "eventMessage": "{\"a\":\"foo\"}", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "{\"a\":\"foobar\"}", + "extractedValues": {} + }, + { + "eventNumber": 14, + "eventMessage": "{\"a\":\"FOO\"}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e3}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":\"1e3\"}", + "extractedValues": {} + }, + { + "eventNumber": 17, + "eventMessage": "{\"a\":1e309}", + "extractedValues": {} + }, + { + "eventNumber": 18, + "eventMessage": "{\"a\":\"1e309\"}", + "extractedValues": {} + }, + { + "eventNumber": 19, + "eventMessage": "{\"a\":1e-999}", + "extractedValues": {} + }, + { + "eventNumber": 20, + "eventMessage": "{\"a\":-0.0}", + "extractedValues": {} + }, + { + "eventNumber": 21, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-glob", + "request": { + "filterPattern": "{ $.a = foo* }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 12, + "eventMessage": "{\"a\":\"foo\"}", + "extractedValues": {} + }, + { + "eventNumber": 13, + "eventMessage": "{\"a\":\"foobar\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-exponent", + "request": { + "filterPattern": "{ $.a = 1e3 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e3}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":\"1e3\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-plus", + "request": { + "filterPattern": "{ $.a = +1 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-overflow", + "request": { + "filterPattern": "{ $.a = 1e309 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 18, + "eventMessage": "{\"a\":\"1e309\"}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-underflow", + "request": { + "filterPattern": "{ $.a = 1e-999 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [] + } + }, + { + "id": "json-precision", + "request": { + "filterPattern": "{ $.a = 9007199254740992 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [] + } + }, + { + "id": "json-regex-number", + "request": { + "filterPattern": "{ $.a = %1% }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":1}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":\"1\"}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":1.0}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":\"01\"}", + "extractedValues": {} + }, + { + "eventNumber": 15, + "eventMessage": "{\"a\":1e3}", + "extractedValues": {} + }, + { + "eventNumber": 16, + "eventMessage": "{\"a\":\"1e3\"}", + "extractedValues": {} + }, + { + "eventNumber": 18, + "eventMessage": "{\"a\":\"1e309\"}", + "extractedValues": {} + }, + { + "eventNumber": 21, + "eventMessage": "{\"a\":9007199254740993}", + "extractedValues": {} + } + ] + } + }, + { + "id": "json-keyword-and", + "request": { + "filterPattern": "{ $.a = 1 AND $.b = 2 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid metric filter pattern" + } + }, + { + "id": "json-double-equal", + "request": { + "filterPattern": "{ $.a == 1 }", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid metric filter pattern" + } + }, + { + "id": "json-trailing", + "request": { + "filterPattern": "{ $.a = 1 } x", + "logEventMessages": [ + "{\"a\":1}", + "{\"a\":\"1\"}", + "{\"a\":1.0}", + "{\"a\":\"01\"}", + "{\"a\":true}", + "{\"a\":false}", + "{\"a\":\"true\"}", + "{\"a\":null}", + "{}", + "{\"a\":{}}", + "{\"a\":[]}", + "{\"a\":\"foo\"}", + "{\"a\":\"foobar\"}", + "{\"a\":\"FOO\"}", + "{\"a\":1e3}", + "{\"a\":\"1e3\"}", + "{\"a\":1e309}", + "{\"a\":\"1e309\"}", + "{\"a\":1e-999}", + "{\"a\":-0.0}", + "{\"a\":9007199254740993}" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "If a filter pattern starts with '{' it must end with '}'" + } + }, + { + "id": "selector-index", + "request": { + "filterPattern": "{ $.a[1] = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":[1,2]}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-wildcard", + "request": { + "filterPattern": "{ $.a[*] = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":[1,2]}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":[2,1]}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":{\"x\":1,\"y\":2}}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":{\"x\":2,\"y\":1}}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-object-wildcard", + "request": { + "filterPattern": "{ $.a.* = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":[1,2]}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":[2,1]}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":{\"x\":1,\"y\":2}}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":{\"x\":2,\"y\":1}}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-wildcard-child", + "request": { + "filterPattern": "{ $.a[*].b = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 6, + "eventMessage": "{\"a\":[{\"b\":1},{\"b\":2}]}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-quoted-property", + "request": { + "filterPattern": "{ $.['a.b'] = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 7, + "eventMessage": "{\"a.b\":2}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-bracket-property", + "request": { + "filterPattern": "{ $['a.b'] = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '$['" + } + }, + { + "id": "selector-double-property", + "request": { + "filterPattern": "{ $.[\"a.b\"] = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '$.[\"'" + } + }, + { + "id": "selector-hyphen", + "request": { + "filterPattern": "{ $.a-b = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 8, + "eventMessage": "{\"a-b\":2}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-root-index", + "request": { + "filterPattern": "{ $[1] = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '$['" + } + }, + { + "id": "selector-negative-index", + "request": { + "filterPattern": "{ $.a[-1] = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '[-'" + } + }, + { + "id": "selector-null-extraction", + "request": { + "filterPattern": "{ $.a IS NULL }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-or-extraction", + "request": { + "filterPattern": "{ $.a = 9 || $.missing NOT EXISTS }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "{\"a\":[1,2]}", + "extractedValues": {} + }, + { + "eventNumber": 2, + "eventMessage": "{\"a\":[2,1]}", + "extractedValues": {} + }, + { + "eventNumber": 3, + "eventMessage": "{\"a\":[null,2]}", + "extractedValues": {} + }, + { + "eventNumber": 4, + "eventMessage": "{\"a\":{\"x\":1,\"y\":2}}", + "extractedValues": {} + }, + { + "eventNumber": 5, + "eventMessage": "{\"a\":{\"x\":2,\"y\":1}}", + "extractedValues": {} + }, + { + "eventNumber": 6, + "eventMessage": "{\"a\":[{\"b\":1},{\"b\":2}]}", + "extractedValues": {} + }, + { + "eventNumber": 7, + "eventMessage": "{\"a.b\":2}", + "extractedValues": {} + }, + { + "eventNumber": 8, + "eventMessage": "{\"a-b\":2}", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + }, + { + "eventNumber": 10, + "eventMessage": "{}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-array-ne", + "request": { + "filterPattern": "{ $.a[*] != 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 6, + "eventMessage": "{\"a\":[{\"b\":1},{\"b\":2}]}", + "extractedValues": {} + }, + { + "eventNumber": 9, + "eventMessage": "{\"a\":null}", + "extractedValues": {} + } + ] + } + }, + { + "id": "selector-nested-wildcard", + "request": { + "filterPattern": "{ $.a[*].* = 2 }", + "logEventMessages": [ + "{\"a\":[1,2]}", + "{\"a\":[2,1]}", + "{\"a\":[null,2]}", + "{\"a\":{\"x\":1,\"y\":2}}", + "{\"a\":{\"x\":2,\"y\":1}}", + "{\"a\":[{\"b\":1},{\"b\":2}]}", + "{\"a.b\":2}", + "{\"a-b\":2}", + "{\"a\":null}", + "{}", + "[1,2]", + "{\"a\":2} trailing" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Your filter pattern has a JSON path selector that exceeds the maximum of 1 wildcard allowed per json selector." + } + }, + { + "id": "space-fields", + "request": { + "filterPattern": "[a, b, c]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$a": "ERROR", + "$b": "one", + "$c": "two" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$a": "a", + "$b": "two words", + "$c": "c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$a": "a", + "$b": "two words", + "$c": "c" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$a": "a", + "$b": "b", + "$c": "c" + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$a": "a", + "$b": "b", + "$c": "c" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$a": "a", + "$b": "b", + "$c": "c d" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$a": "a", + "$b": "b", + "$c": "c d" + } + } + ] + } + }, + { + "id": "space-last-rest", + "request": { + "filterPattern": "[a, rest]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$rest": "one", + "$a": "ERROR" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$rest": "one two", + "$a": "ERROR" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$rest": "one", + "$a": "WARN" + } + }, + { + "eventNumber": 5, + "eventMessage": "INFO one", + "extractedValues": { + "$rest": "one", + "$a": "INFO" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$rest": "\"two words\" c", + "$a": "a" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$rest": "[two words] c", + "$a": "a" + } + }, + { + "eventNumber": 8, + "eventMessage": "a \"two words\"", + "extractedValues": { + "$rest": "two words", + "$a": "a" + } + }, + { + "eventNumber": 9, + "eventMessage": "a [two words]", + "extractedValues": { + "$rest": "two words", + "$a": "a" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$rest": "b c ", + "$a": "a" + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$rest": "b\tc", + "$a": "a" + } + }, + { + "eventNumber": 12, + "eventMessage": "a \"unclosed", + "extractedValues": { + "$rest": "\"unclosed", + "$a": "a" + } + }, + { + "eventNumber": 13, + "eventMessage": "a [unclosed", + "extractedValues": { + "$rest": "[unclosed", + "$a": "a" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$rest": "\"b\"c d", + "$a": "a" + } + }, + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$rest": "2", + "$a": "1" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$rest": "2", + "$a": "1.0" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$rest": "b c d", + "$a": "a" + } + } + ] + } + }, + { + "id": "space-single", + "request": { + "filterPattern": "[a]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": { + "$a": "ERROR" + } + }, + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$a": "ERROR one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$a": "ERROR one two" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$a": "WARN one" + } + }, + { + "eventNumber": 5, + "eventMessage": "INFO one", + "extractedValues": { + "$a": "INFO one" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$a": "a \"two words\" c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$a": "a [two words] c" + } + }, + { + "eventNumber": 8, + "eventMessage": "a \"two words\"", + "extractedValues": { + "$a": "a \"two words\"" + } + }, + { + "eventNumber": 9, + "eventMessage": "a [two words]", + "extractedValues": { + "$a": "a [two words]" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$a": "a b c " + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$a": "a\tb\tc" + } + }, + { + "eventNumber": 12, + "eventMessage": "a \"unclosed", + "extractedValues": { + "$a": "a \"unclosed" + } + }, + { + "eventNumber": 13, + "eventMessage": "a [unclosed", + "extractedValues": { + "$a": "a [unclosed" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$a": "a \"b\"c d" + } + }, + { + "eventNumber": 15, + "eventMessage": "one", + "extractedValues": { + "$a": "one" + } + }, + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$a": "1 2" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$a": "1.0 2" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$a": "a b c d" + } + } + ] + } + }, + { + "id": "space-empty", + "request": { + "filterPattern": "[]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": { + "$1": "ERROR" + } + }, + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$1": "ERROR", + "$2": "one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$1": "ERROR", + "$2": "one", + "$3": "two" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$1": "WARN", + "$2": "one" + } + }, + { + "eventNumber": 5, + "eventMessage": "INFO one", + "extractedValues": { + "$1": "INFO", + "$2": "one" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$1": "a", + "$2": "two words", + "$3": "c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$1": "a", + "$2": "two words", + "$3": "c" + } + }, + { + "eventNumber": 8, + "eventMessage": "a \"two words\"", + "extractedValues": { + "$1": "a", + "$2": "two words" + } + }, + { + "eventNumber": 9, + "eventMessage": "a [two words]", + "extractedValues": { + "$1": "a", + "$2": "two words" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$1": "a", + "$2": "b", + "$3": "c" + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$1": "a", + "$2": "b", + "$3": "c" + } + }, + { + "eventNumber": 12, + "eventMessage": "a \"unclosed", + "extractedValues": { + "$1": "a", + "$2": "\"unclosed" + } + }, + { + "eventNumber": 13, + "eventMessage": "a [unclosed", + "extractedValues": { + "$1": "a", + "$2": "[unclosed" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$4": "d", + "$1": "a", + "$2": "b", + "$3": "c" + } + }, + { + "eventNumber": 15, + "eventMessage": "one", + "extractedValues": { + "$1": "one" + } + }, + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$1": "1", + "$2": "2" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$1": "1.0", + "$2": "2" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$4": "d", + "$1": "a", + "$2": "b", + "$3": "c" + } + } + ] + } + }, + { + "id": "space-leading-ellipsis", + "request": { + "filterPattern": "[..., last]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": { + "$last": "ERROR" + } + }, + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$1": "ERROR", + "$last": "one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$1": "ERROR", + "$2": "one", + "$last": "two" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$1": "WARN", + "$last": "one" + } + }, + { + "eventNumber": 5, + "eventMessage": "INFO one", + "extractedValues": { + "$1": "INFO", + "$last": "one" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$1": "a", + "$2": "two words", + "$last": "c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$1": "a", + "$2": "two words", + "$last": "c" + } + }, + { + "eventNumber": 8, + "eventMessage": "a \"two words\"", + "extractedValues": { + "$1": "a", + "$last": "two words" + } + }, + { + "eventNumber": 9, + "eventMessage": "a [two words]", + "extractedValues": { + "$1": "a", + "$last": "two words" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$1": "a", + "$2": "b", + "$last": "c" + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$1": "a", + "$2": "b", + "$last": "c" + } + }, + { + "eventNumber": 12, + "eventMessage": "a \"unclosed", + "extractedValues": { + "$1": "a", + "$last": "\"unclosed" + } + }, + { + "eventNumber": 13, + "eventMessage": "a [unclosed", + "extractedValues": { + "$1": "a", + "$last": "[unclosed" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$1": "a", + "$2": "b", + "$last": "d", + "$3": "c" + } + }, + { + "eventNumber": 15, + "eventMessage": "one", + "extractedValues": { + "$last": "one" + } + }, + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$1": "1", + "$last": "2" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$1": "1.0", + "$last": "2" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$1": "a", + "$2": "b", + "$last": "d", + "$3": "c" + } + } + ] + } + }, + { + "id": "space-middle-ellipsis", + "request": { + "filterPattern": "[first, ..., last]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$first": "ERROR", + "$last": "one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$first": "ERROR", + "$2": "one", + "$last": "two" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$first": "WARN", + "$last": "one" + } + }, + { + "eventNumber": 5, + "eventMessage": "INFO one", + "extractedValues": { + "$first": "INFO", + "$last": "one" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$first": "a", + "$2": "two words", + "$last": "c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$first": "a", + "$2": "two words", + "$last": "c" + } + }, + { + "eventNumber": 8, + "eventMessage": "a \"two words\"", + "extractedValues": { + "$first": "a", + "$last": "two words" + } + }, + { + "eventNumber": 9, + "eventMessage": "a [two words]", + "extractedValues": { + "$first": "a", + "$last": "two words" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$first": "a", + "$2": "b", + "$last": "c" + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$first": "a", + "$2": "b", + "$last": "c" + } + }, + { + "eventNumber": 12, + "eventMessage": "a \"unclosed", + "extractedValues": { + "$first": "a", + "$last": "\"unclosed" + } + }, + { + "eventNumber": 13, + "eventMessage": "a [unclosed", + "extractedValues": { + "$first": "a", + "$last": "[unclosed" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$first": "a", + "$2": "b", + "$last": "d", + "$3": "c" + } + }, + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$first": "1", + "$last": "2" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$first": "1.0", + "$last": "2" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$first": "a", + "$2": "b", + "$last": "d", + "$3": "c" + } + } + ] + } + }, + { + "id": "space-trailing-ellipsis", + "request": { + "filterPattern": "[first, ...]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 1, + "eventMessage": "ERROR", + "extractedValues": { + "$first": "ERROR" + } + }, + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$first": "ERROR", + "$2": "one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$first": "ERROR", + "$2": "one", + "$3": "two" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$first": "WARN", + "$2": "one" + } + }, + { + "eventNumber": 5, + "eventMessage": "INFO one", + "extractedValues": { + "$first": "INFO", + "$2": "one" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$first": "a", + "$2": "two words", + "$3": "c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$first": "a", + "$2": "two words", + "$3": "c" + } + }, + { + "eventNumber": 8, + "eventMessage": "a \"two words\"", + "extractedValues": { + "$first": "a", + "$2": "two words" + } + }, + { + "eventNumber": 9, + "eventMessage": "a [two words]", + "extractedValues": { + "$first": "a", + "$2": "two words" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$first": "a", + "$2": "b", + "$3": "c" + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$first": "a", + "$2": "b", + "$3": "c" + } + }, + { + "eventNumber": 12, + "eventMessage": "a \"unclosed", + "extractedValues": { + "$first": "a", + "$2": "\"unclosed" + } + }, + { + "eventNumber": 13, + "eventMessage": "a [unclosed", + "extractedValues": { + "$first": "a", + "$2": "[unclosed" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$4": "d", + "$first": "a", + "$2": "b", + "$3": "c" + } + }, + { + "eventNumber": 15, + "eventMessage": "one", + "extractedValues": { + "$first": "one" + } + }, + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$first": "1", + "$2": "2" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$first": "1.0", + "$2": "2" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$4": "d", + "$first": "a", + "$2": "b", + "$3": "c" + } + } + ] + } + }, + { + "id": "space-unnamed", + "request": { + "filterPattern": "[, b,]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$1": "ERROR", + "$b": "one", + "$3": "two" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$1": "a", + "$b": "two words", + "$3": "c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$1": "a", + "$b": "two words", + "$3": "c" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$1": "a", + "$b": "b", + "$3": "c" + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$1": "a", + "$b": "b", + "$3": "c" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$1": "a", + "$b": "b", + "$3": "c d" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$1": "a", + "$b": "b", + "$3": "c d" + } + } + ] + } + }, + { + "id": "space-trailing-comma", + "request": { + "filterPattern": "[a,]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$a": "ERROR", + "$2": "one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$a": "ERROR", + "$2": "one two" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$a": "WARN", + "$2": "one" + } + }, + { + "eventNumber": 5, + "eventMessage": "INFO one", + "extractedValues": { + "$a": "INFO", + "$2": "one" + } + }, + { + "eventNumber": 6, + "eventMessage": "a \"two words\" c", + "extractedValues": { + "$a": "a", + "$2": "\"two words\" c" + } + }, + { + "eventNumber": 7, + "eventMessage": "a [two words] c", + "extractedValues": { + "$a": "a", + "$2": "[two words] c" + } + }, + { + "eventNumber": 8, + "eventMessage": "a \"two words\"", + "extractedValues": { + "$a": "a", + "$2": "two words" + } + }, + { + "eventNumber": 9, + "eventMessage": "a [two words]", + "extractedValues": { + "$a": "a", + "$2": "two words" + } + }, + { + "eventNumber": 10, + "eventMessage": "a b c ", + "extractedValues": { + "$a": "a", + "$2": "b c " + } + }, + { + "eventNumber": 11, + "eventMessage": "a\tb\tc", + "extractedValues": { + "$a": "a", + "$2": "b\tc" + } + }, + { + "eventNumber": 12, + "eventMessage": "a \"unclosed", + "extractedValues": { + "$a": "a", + "$2": "\"unclosed" + } + }, + { + "eventNumber": 13, + "eventMessage": "a [unclosed", + "extractedValues": { + "$a": "a", + "$2": "[unclosed" + } + }, + { + "eventNumber": 14, + "eventMessage": "a \"b\"c d", + "extractedValues": { + "$a": "a", + "$2": "\"b\"c d" + } + }, + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$a": "1", + "$2": "2" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$a": "1.0", + "$2": "2" + } + }, + { + "eventNumber": 18, + "eventMessage": "a b c d", + "extractedValues": { + "$a": "a", + "$2": "b c d" + } + } + ] + } + }, + { + "id": "space-constraint", + "request": { + "filterPattern": "[level=ERROR, rest]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$level": "ERROR", + "$rest": "one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$level": "ERROR", + "$rest": "one two" + } + } + ] + } + }, + { + "id": "space-or", + "request": { + "filterPattern": "[level=ERROR || level=WARN, rest]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$level": "ERROR", + "$rest": "one" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$level": "ERROR", + "$rest": "one two" + } + }, + { + "eventNumber": 4, + "eventMessage": "WARN one", + "extractedValues": { + "$level": "WARN", + "$rest": "one" + } + } + ] + } + }, + { + "id": "space-numeric", + "request": { + "filterPattern": "[a=1, rest]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$rest": "2", + "$a": "1" + } + }, + { + "eventNumber": 17, + "eventMessage": "1.0 2", + "extractedValues": { + "$rest": "2", + "$a": "1.0" + } + } + ] + } + }, + { + "id": "space-quoted-numeric", + "request": { + "filterPattern": "[a=\"1\", rest]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 16, + "eventMessage": "1 2", + "extractedValues": { + "$rest": "2", + "$a": "1" + } + } + ] + } + }, + { + "id": "space-cross-field", + "request": { + "filterPattern": "[a=ERROR || b=one, b]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Multiple variables used in a single filter. Var1 : a, Var2 : b" + } + }, + { + "id": "space-duplicate", + "request": { + "filterPattern": "[a, a]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Duplicate field 'a'" + } + }, + { + "id": "space-invalid-name", + "request": { + "filterPattern": "[1a]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid character(s) in term '1a'" + } + }, + { + "id": "space-double-ellipsis", + "request": { + "filterPattern": "[a, ..., b, ...]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Duplicate field '...'" + } + }, + { + "id": "space-bad-operator", + "request": { + "filterPattern": "[a == 1]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "error": { + "code": "InvalidParameterException", + "message": "Invalid metric filter pattern" + } + }, + { + "id": "space-regex", + "request": { + "filterPattern": "[a=%ERR%, rest]", + "logEventMessages": [ + "ERROR", + "ERROR one", + "ERROR one two", + "WARN one", + "INFO one", + "a \"two words\" c", + "a [two words] c", + "a \"two words\"", + "a [two words]", + "a b c ", + "a\tb\tc", + "a \"unclosed", + "a [unclosed", + "a \"b\"c d", + "one", + "1 2", + "1.0 2", + "a b c d" + ] + }, + "response": { + "matches": [ + { + "eventNumber": 2, + "eventMessage": "ERROR one", + "extractedValues": { + "$rest": "one", + "$a": "ERROR" + } + }, + { + "eventNumber": 3, + "eventMessage": "ERROR one two", + "extractedValues": { + "$rest": "one two", + "$a": "ERROR" + } + } + ] + } + } + ] +} diff --git a/src/test/resources/cloudwatchlogs/metric-filter-cfn-aws.json b/src/test/resources/cloudwatchlogs/metric-filter-cfn-aws.json new file mode 100644 index 0000000000..b1ca66b38d --- /dev/null +++ b/src/test/resources/cloudwatchlogs/metric-filter-cfn-aws.json @@ -0,0 +1,70 @@ +{ + "verification": "Verified on Live AWS", + "observedDate": "2026-09-16", + "region": "eu-west-1", + "awsCliVersion": "2.36.45", + "description": "Normalized observations from isolated CloudFormation updates of one explicitly named AWS::Logs::MetricFilter. Physical roles replace temporary resource names. No log events or new metric series were published.", + "identity": { + "Ref": "filter name only", + "PhysicalResourceId": "filter name only" + }, + "validReplacement": { + "priorDefinition": {"FilterPattern": "ERROR", "MetricValue": "1"}, + "newDefinition": {"FilterPattern": "WARN", "MetricValue": "2"}, + "events": [ + {"physicalRole": "prior", "status": "UPDATE_IN_PROGRESS"}, + {"physicalRole": "prior", "status": "DELETE_IN_PROGRESS"}, + {"physicalRole": "prior", "status": "DELETE_COMPLETE"}, + {"physicalRole": "replacement", "status": "CREATE_IN_PROGRESS"}, + {"physicalRole": "replacement", "status": "CREATE_COMPLETE"}, + {"physicalRole": "replacement", "status": "UPDATE_COMPLETE"} + ], + "deletionReason": "Requested update requires the replacement of the existing resource; deleting existing resource, then creating a new one.", + "stackStatus": "UPDATE_COMPLETE" + }, + "failedReplacement": { + "priorDefinition": {"FilterPattern": "WARN", "MetricValue": "2"}, + "attemptedDefinition": {"FilterPattern": "{", "MetricValue": "9"}, + "events": [ + {"physicalRole": "prior", "status": "UPDATE_IN_PROGRESS"}, + {"physicalRole": "prior", "status": "DELETE_IN_PROGRESS"}, + {"physicalRole": "prior", "status": "DELETE_COMPLETE"}, + {"physicalRole": "attempted", "status": "CREATE_IN_PROGRESS"}, + {"physicalRole": "attempted", "status": "CREATE_FAILED"}, + {"physicalRole": "attempted", "status": "UPDATE_FAILED"}, + {"physicalRole": "attempted", "status": "UPDATE_IN_PROGRESS"}, + {"physicalRole": "attempted", "status": "DELETE_IN_PROGRESS"}, + {"physicalRole": "attempted", "status": "DELETE_COMPLETE"}, + {"physicalRole": "attempted", "desiredDefinitionRole": "prior", "status": "CREATE_IN_PROGRESS"}, + {"physicalRole": "prior", "status": "CREATE_COMPLETE"}, + {"physicalRole": "prior", "status": "UPDATE_COMPLETE"} + ], + "stackStatus": "UPDATE_ROLLBACK_COMPLETE", + "restoredDefinition": {"FilterPattern": "WARN", "MetricValue": "2"}, + "priorInstanceWasRecreated": true, + "attemptedFilterSurvived": false, + "storedTemplateRestored": true, + "qualification": "The intermediate rollback CREATE_IN_PROGRESS event retained the attempted physical ID while ResourceProperties already specified the restored definition. Completion carried the prior name. Direct Logs reads and a changed creationTime confirmed recreation." + }, + "retainReplacement": { + "policyInstalledBeforeRename": "Retain", + "oldFilterSurvived": false, + "deleteSkippedEventObserved": false, + "emptyFilterListObservedBetweenDeleteAndCreate": true, + "finalSurvivorCount": 1, + "finalDefinition": {"FilterPattern": "INFO", "MetricValue": "3"}, + "qualification": "This resource-specific experiment still deleted the old filter before creation despite UpdateReplacePolicy Retain. Do not generalize to other resource types or stack DeletionPolicy." + }, + "notMeasured": [ + "replacement at the 100-filter limit", + "collisions with a filter owned by another stack", + "all generated-name and changed-log-group combinations", + "all deletion and snapshot policies" + ], + "cleanup": { + "verifiedAt": "2026-09-16T10:25:44Z", + "stackAbsent": true, + "remainingOwnedLogGroups": 0, + "newMetricSeriesPublished": 0 + } +} diff --git a/src/test/resources/cloudwatchlogs/metric-filter-default-value-wire-aws.json b/src/test/resources/cloudwatchlogs/metric-filter-default-value-wire-aws.json new file mode 100644 index 0000000000..080a925e04 --- /dev/null +++ b/src/test/resources/cloudwatchlogs/metric-filter-default-value-wire-aws.json @@ -0,0 +1,26 @@ +{ + "provenance": { + "verification": "Verified on Live AWS", + "source": "Signed raw PutMetricFilter request reported by the coordinated AWS research agent", + "observationDate": "2026-09-16", + "region": "eu-west-1", + "endpoint": "https://logs.eu-west-1.amazonaws.com", + "reference": "https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutMetricFilter.html", + "scope": "Only the observed defaultValue member and response are reproduced. Other request fields and resource identities are omitted. CLI client-side type validation was bypassed." + }, + "requestMember": "metricTransformations[0].defaultValue", + "requestValue": "7", + "response": { + "status": 400, + "body": { + "__type": "SerializationException", + "Message": "STRING_VALUE cannot be converted to Double" + } + }, + "positiveControl": { + "requestValue": 7, + "responseStatus": 200, + "responseBody": "" + }, + "unobserved": "Other string contents and null, boolean, object or array defaultValue nodes were not probed by this observation." +} diff --git a/src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json b/src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json new file mode 100644 index 0000000000..bf828c42b6 --- /dev/null +++ b/src/test/resources/cloudwatchlogs/metric-filter-publishing-aws.json @@ -0,0 +1,112 @@ +{ + "verification": "Verified on Live AWS", + "observedDate": "2026-09-16", + "region": "eu-west-1", + "awsCliVersion": "2.36.45", + "description": "Normalized observations from isolated synthetic PutMetricFilter/PutLogEvents/GetMetricStatistics probes. Values are independent AWS results, not emulator-generated expectations.", + "period": 60, + "observedThrough": "2026-09-16T09:06:51Z", + "metricDefaults": { + "filterPattern": "ERROR", + "metricValue": "3", + "defaultValue": 7, + "cases": [ + { + "name": "mixed events in one batch", + "batches": [["ERROR", "INFO", "ERROR"]], + "expected": {"Sum": 13, "SampleCount": 3, "Minimum": 3, "Maximum": 7} + }, + { + "name": "hit then miss in separate batches", + "batches": [["ERROR"], ["INFO"]], + "expected": {"Sum": 10, "SampleCount": 2, "Minimum": 3, "Maximum": 7} + }, + { + "name": "miss then hit in separate batches", + "batches": [["INFO"], ["ERROR"]], + "expected": {"Sum": 10, "SampleCount": 2, "Minimum": 3, "Maximum": 7} + } + ], + "qualification": "Each case used one isolated event-time minute. Results converged through repeated reads over approximately 107 seconds. No minute-wide suppression or default deduplication was observed." + }, + "extraction": { + "filterPattern": "{ $.probe = \"value\" || $.value = * }", + "metricValue": "$.value", + "defaultValue": 7, + "cases": [ + { + "name": "matching event with missing value", + "message": {"probe": "value", "case": "missing"}, + "expected": {"Sum": 7, "SampleCount": 1} + }, + { + "name": "matching event with null value", + "message": {"probe": "value", "value": null, "case": "null"}, + "expected": {"Sum": 7, "SampleCount": 1} + }, + { + "name": "matching event with nonnumeric string", + "message": {"probe": "value", "value": "not-a-number", "case": "nonnumeric"}, + "expected": {"datapoints": []} + }, + { + "name": "matching numeric control", + "message": {"probe": "value", "value": 3, "case": "numeric-control"}, + "expected": {"Sum": 3, "SampleCount": 1} + } + ], + "qualification": "Cases occupied distinct event-time minutes. Matching did not depend on the value field being present. The numeric control succeeded; negative results remained absent through the completed observation window." + }, + "ordinaryDimensions": { + "filterPattern": "{ $.probe = \"dimension\" || $.a = * || $.b = * }", + "metricValue": "1", + "dimensions": {"A": "$.a", "B": "$.b"}, + "messages": [ + {"probe": "dimension", "a": "alpha", "b": "beta"}, + {"probe": "dimension", "a": "alpha"}, + {"probe": "dimension", "b": "beta"}, + {"probe": "dimension"} + ], + "expectedSeries": [ + {"dimensions": {"A": "alpha", "B": "beta"}, "Sum": 1, "SampleCount": 1}, + {"dimensions": {}, "Sum": 3, "SampleCount": 3} + ], + "absentSeries": [ + {"dimensions": {"A": "alpha"}}, + {"dimensions": {"B": "beta"}} + ], + "qualification": "No default configured. All events occupied one minute. ListMetrics confirmed complete and dimensionless series, not partial sets. Null or empty dimension values were not measured." + }, + "systemDimensions": { + "filterPattern": "ERROR", + "metricValue": "3", + "defaultValue": 7, + "messages": ["ERROR", "INFO"], + "accountTransformerPolicyCount": 0, + "groupTransformerConfig": [], + "cases": [ + { + "emitSystemFieldDimensions": ["@aws.account"], + "expectedSeries": [ + {"dimensions": {"@aws.account": ""}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + }, + { + "emitSystemFieldDimensions": ["@aws.region"], + "expectedSeries": [ + {"dimensions": {"@aws.region": "eu-west-1"}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + }, + { + "emitSystemFieldDimensions": ["@aws.account", "@aws.region"], + "expectedSeries": [ + {"dimensions": {"@aws.account": "", "@aws.region": "eu-west-1"}, "Sum": 3, "SampleCount": 1}, + {"dimensions": {}, "Sum": 7, "SampleCount": 1} + ] + } + ], + "qualification": "Regular noncentralized group. Matching events receive requested system dimensions; pattern-nonmatch defaults are dimensionless. This does not measure extraction-fallback defaults or centralized source provenance." + } +} diff --git a/src/test/resources/cloudwatchlogs/metric-filter-reference-validation-aws.json b/src/test/resources/cloudwatchlogs/metric-filter-reference-validation-aws.json new file mode 100644 index 0000000000..1314114e45 --- /dev/null +++ b/src/test/resources/cloudwatchlogs/metric-filter-reference-validation-aws.json @@ -0,0 +1,49 @@ +{ + "provenance": { + "verification": "Verified on Live AWS", + "operation": "PutMetricFilter", + "observedFrom": "2026-09-16T12:16:08Z", + "observedThrough": "2026-09-16T12:16:10Z", + "cleanupVerifiedAt": "2026-09-16T12:16:11Z", + "region": "eu-west-1", + "endpoint": "https://logs.eu-west-1.amazonaws.com", + "source": "Four synthetic validation probes reported by the parent coordinator", + "reference": "https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutMetricFilter.html", + "normalization": "Only reported filter/metric transformation members and acceptance/error details are retained. Resource names, account identifiers and unrelated request members are omitted. These are not raw HTTP requests or responses.", + "publication": "None. Validation only.", + "unmeasured": "Dimension wildcard references and literal asterisks inside quoted property names were not probed." + }, + "cases": [ + { + "id": "A-mentioned-scalar", + "filterPattern": "{ $.probe = \"yes\" || $.value = * }", + "metricValue": "$.value", + "defaultValue": 7, + "expected": { "accepted": true } + }, + { + "id": "B-unmentioned-scalar", + "filterPattern": "{ $.probe = \"yes\" }", + "metricValue": "$.value", + "defaultValue": 7, + "expected": { "accepted": true } + }, + { + "id": "C-unmentioned-dimension", + "filterPattern": "{ $.probe = \"yes\" }", + "metricValue": "1", + "dimensions": { "A": "$.a" }, + "expected": { "accepted": true } + }, + { + "id": "D-wildcard-metric-value", + "filterPattern": "{ $.values[*] = * }", + "metricValue": "$.values[*]", + "expected": { + "accepted": false, + "errorCode": "InvalidParameterException", + "message": "Invalid metric transformation: metric value $.values[*] must be a valid number" + } + } + ] +} diff --git a/tools/aws-capture/cloudwatchlogs/capture_filter_patterns.py b/tools/aws-capture/cloudwatchlogs/capture_filter_patterns.py new file mode 100644 index 0000000000..2b5fbe0507 --- /dev/null +++ b/tools/aws-capture/cloudwatchlogs/capture_filter_patterns.py @@ -0,0 +1,241 @@ +"""Opt-in AWS oracle capture. Only synthetic, read-only TestMetricFilter requests.""" + +import argparse +import datetime +import json +from pathlib import Path +import re +import subprocess +import time + + +def cases(): + result = [] + + def add(name, pattern, messages): + result.append({"id": name, "request": { + "filterPattern": pattern, "logEventMessages": messages}}) + + terms = ["ERROR", "ERRORS", "xERROR", "error", "[ERROR]", "ERROR_code", + "ERROR timeout", "INFO timeout", "WARN", "two words", "two words"] + for name, pattern in [ + ("substring", "ERROR"), ("case", "error"), ("and", "ERROR timeout"), + ("negative", "ERROR -timeout"), ("negative-only", "-ERROR"), + ("optional", "?ERROR ?WARN"), ("optional-ignored", "?ERROR timeout"), + ("optional-negative", "?ERROR -timeout"), ("phrase", '"two words"'), + ("negative-phrase", '-"two words"'), ("optional-phrase", '?"two words" ?WARN'), + ("wildcard", "ERR*"), ("quoted-wildcard", '"ERR*"'), + ("bare-symbol", "ERROR!"), ("bare-colon", "ERROR:"), + ("empty", ""), ("blank", " "), ("quoted-empty", '""'), + ("missing-quote", '"ERROR'), ("adjacent-quote", '"ERROR"WARN'), + ]: + add("terms-" + name, pattern, terms) + + regex_messages = ["ERROR", "WARN", "aaaa", "aa", "cat", "a cat", "hat", + "color", "colour", "a-b", "a b", "a\nb", "123", "_", "café", + "key:value", "ab", "back\\slash", "!"] + for name, pattern in [ + ("alternation", "%ERROR|WARN%"), ("anchors", "%^[hc]at$%"), + ("quantifier", "%^a{3,5}$%"), ("optional", "%colou?r%"), + ("digits", "%\\d+%"), ("word", "%\\w+%"), ("space", "%a\\sb%"), + ("hex", "%\\x3A%"), ("dot-newline", "%a.b%"), + ("escaped-symbol", "%a\\-b%"), ("group-rejected", "%(ab)+%"), + ("unicode-rejected", "%café%"), ("symbol-rejected", "%!%"), + ("escape-rejected", "%\\p%"), ("escaped-group", "%\\(ab\\)%"), + ("empty", "%%"), ("unclosed", "%abc"), ("bad-range", "%[z-a]%"), + ("lazy", "%a+?%"), ("possessive", "%a++%"), + ("class-intersection", "%[a&&b]%"), ("two", "%ERROR% %WARN%"), + ("three", "%ERROR% %WARN% %INFO%"), + ]: + add("regex-" + name, pattern, regex_messages) + + json_messages = [ + '{"a":1}', '{"a":"1"}', '{"a":1.0}', '{"a":"01"}', '{"a":true}', + '{"a":false}', '{"a":"true"}', '{"a":null}', '{}', '{"a":{}}', + '{"a":[]}', '{"a":"foo"}', '{"a":"foobar"}', '{"a":"FOO"}', + '{"a":1e3}', '{"a":"1e3"}', '{"a":1e309}', '{"a":"1e309"}', + '{"a":1e-999}', '{"a":-0.0}', '{"a":9007199254740993}', + ] + for name, pattern in [ + ("numeric", "{ $.a = 1 }"), ("quoted-number", '{ $.a = "1" }'), + ("numeric-ne", "{ $.a != 1 }"), ("string-ne", '{ $.a != "foo" }'), + ("ordering", "{ $.a > 0 }"), ("quoted-order", '{ $.a > "0" }'), + ("bare-word", "{ $.a = foo }"), ("bare-boolean", "{ $.a = true }"), + ("quoted-boolean", '{ $.a = "true" }'), ("is-true", "{ $.a IS TRUE }"), + ("is-false", "{ $.a IS FALSE }"), ("null", "{ $.a IS NULL }"), + ("missing", "{ $.a NOT EXISTS }"), ("star", "{ $.a = * }"), + ("quoted-star", '{ $.a = "*" }'), ("glob", "{ $.a = foo* }"), + ("exponent", "{ $.a = 1e3 }"), ("plus", "{ $.a = +1 }"), + ("overflow", "{ $.a = 1e309 }"), ("underflow", "{ $.a = 1e-999 }"), + ("precision", "{ $.a = 9007199254740992 }"), + ("regex-number", "{ $.a = %1% }"), ("keyword-and", "{ $.a = 1 AND $.b = 2 }"), + ("double-equal", "{ $.a == 1 }"), ("trailing", "{ $.a = 1 } x"), + ]: + add("json-" + name, pattern, json_messages) + + selectors = [ + '{"a":[1,2]}', '{"a":[2,1]}', '{"a":[null,2]}', + '{"a":{"x":1,"y":2}}', '{"a":{"x":2,"y":1}}', + '{"a":[{"b":1},{"b":2}]}', '{"a.b":2}', '{"a-b":2}', + '{"a":null}', '{}', '[1,2]', '{"a":2} trailing', + ] + for name, pattern in [ + ("index", "{ $.a[1] = 2 }"), ("wildcard", "{ $.a[*] = 2 }"), + ("object-wildcard", "{ $.a.* = 2 }"), ("wildcard-child", "{ $.a[*].b = 2 }"), + ("quoted-property", "{ $.['a.b'] = 2 }"), ("bracket-property", "{ $['a.b'] = 2 }"), + ("double-property", '{ $.["a.b"] = 2 }'), ("hyphen", "{ $.a-b = 2 }"), + ("root-index", "{ $[1] = 2 }"), ("negative-index", "{ $.a[-1] = 2 }"), + ("null-extraction", "{ $.a IS NULL }"), ("or-extraction", "{ $.a = 9 || $.missing NOT EXISTS }"), + ("array-ne", "{ $.a[*] != 2 }"), ("nested-wildcard", "{ $.a[*].* = 2 }"), + ]: + add("selector-" + name, pattern, selectors) + + spaced = ["ERROR", "ERROR one", "ERROR one two", "WARN one", "INFO one", + 'a "two words" c', "a [two words] c", 'a "two words"', + "a [two words]", "a b c ", "a\tb\tc", 'a "unclosed', "a [unclosed", + 'a "b"c d', "one", "1 2", "1.0 2", "a b c d"] + for name, pattern in [ + ("fields", "[a, b, c]"), ("last-rest", "[a, rest]"), ("single", "[a]"), + ("empty", "[]"), ("leading-ellipsis", "[..., last]"), + ("middle-ellipsis", "[first, ..., last]"), ("trailing-ellipsis", "[first, ...]"), + ("unnamed", "[, b,]"), ("trailing-comma", "[a,]"), + ("constraint", "[level=ERROR, rest]"), + ("or", "[level=ERROR || level=WARN, rest]"), + ("numeric", "[a=1, rest]"), ("quoted-numeric", '[a="1", rest]'), + ("cross-field", "[a=ERROR || b=one, b]"), + ("duplicate", "[a, a]"), ("invalid-name", "[1a]"), + ("double-ellipsis", "[a, ..., b, ...]"), + ("bad-operator", "[a == 1]"), ("regex", "[a=%ERR%, rest]"), + ]: + add("space-" + name, pattern, spaced) + return result + + +def edge_cases(): + numbers = ['{"a":' + value + '}' for value in [ + '1', '1.0', '1e0', '"1"', '"1.0"', '"01"', '0.0', '-0.0', '-1', + '9007199254740992', '9007199254740993', '1e308', '1e309', '"1e309"', + '1e-999', '"1e-999"', '0', 'true', '"true"', 'null', '[1,2]', '[null,2]', + ]] + structures = ['{}', '{"a":null}', '{"a":2}', '{"a":1}', '{"a":"2"}', + '{"a":[]}', '{"a":[1,2]}', '{"a":[2,1]}', '{"a":[1]}', + '{"a":[null,2]}', '{"a":{}}', '{"a":{"b":2}}', + '{"a":[{"b":1},{"b":2}]}', '{"a":[{"b":null}]}', + '{"a":[[1,2]]}', '{"a":[true,false]}'] + result = [] + for name, pattern, messages in [ + ("decimal", "{ $.a = 1.0 }", numbers), + ("zero", "{ $.a = 0 }", numbers), + ("negative-zero", "{ $.a = -0.0 }", numbers), + ("precise-long", "{ $.a = 9007199254740993 }", numbers), + ("large-finite", "{ $.a = 1e308 }", numbers), + ("large-ordered", "{ $.a > 1e307 }", numbers), + ("underflow-ordered", "{ $.a > 1e-999 }", numbers), + ("string-exponent", '{ $.a = "1e0" }', numbers), + ("is-null", "{ $.a IS NULL }", structures), + ("equals", "{ $.a = 2 }", structures), + ("not-equals", "{ $.a != 2 }", structures), + ("wildcard-not-equals", "{ $.a[*] != 2 }", structures), + ("wildcard-null", "{ $.a[*] IS NULL }", structures), + ("wildcard-missing", "{ $.a[*] NOT EXISTS }", structures), + ("missing", "{ $.a NOT EXISTS }", structures), + ("child", "{ $.a.b = 2 }", structures), + ("child-missing", "{ $.a.b NOT EXISTS }", structures), + ("star", "{ $.a = * }", structures), + ("quoted-number", '{ $.a = "2" }', structures), + ("bare-null", "{ $.a = null }", structures), + ("is-not-null", "{ $.a IS NOT NULL }", structures), + ("selector-root", "{ $ = 2 }", structures), + ("json-negative-regex", "{ $.a != %2% }", structures), + ("regex-text", "ERROR %WARN%", ["ERROR WARN", "ERROR", "WARN"]), + ("regex-optional", "?%WARN%", ["WARN", "WARN INFO"]), + ("term-punctuation", "a-b", ["a-b", "a b"]), + ("term-underscore", "a_b", ["a_b", "a b"]), + ("term-dot", "a.b", ["a.b", "a b"]), + ("term-question", "a?b", ["a?b", "a b"]), + ("regex-structured-two", "{ $.a = %1% || $.a = %2% }", structures), + ("regex-structured-three", "{ $.a = %1% || $.a = %2% || $.a = %3% }", structures), + ("quoted-order-space", '[a>"1", b]', ["2 x", "0 x"]), + ("json-keyword-case", "{ $.a is null }", structures), + ("json-unquoted-symbol", "{ $.a = a:b }", ['{"a":"a:b"}']), + ("space-quote-trailing", "[a, b]", ['x "y" ', "x [y] ", "x y ", "x ", 'x ""', "x []"]), + ("space-incomplete", "[a, b, c]", ['x "y z', "x [y z", 'x "y z q', "x [y z q"]), + ]: + result.append({"id": "edge-" + name, "request": { + "filterPattern": pattern, "logEventMessages": messages}}) + return result + + +def boundary_cases(): + messages = [ + '{"a":true}', '{"a":false}', '{"a":[true,false]}', '{"a":[null]}', + '{"a":[[null]]}', '{"a":[{"b":2},{"b":1}]}', '{"a":[{}]}', '{}', + ] + result = [] + for name, pattern, inputs in [ + ("array-true", "{ $.a IS TRUE }", messages), + ("array-false", "{ $.a IS FALSE }", messages), + ("array-null", "{ $.a IS NULL }", messages), + ("nested-negative", "{ $.a[*].b != 2 }", messages), + ("wildcard-null", "{ $.a[*] IS NULL }", messages), + ("term-slash", "a/b", ["a/b", "a b"]), + ("term-at", "a@b", ["a@b", "a b"]), + ("term-unicode", "café", ["café", "cafe"]), + ("quoted-space", '" "', ["x", "two words", " "]), + ("huge-exponent", "{ $.a = 1e2147483648 }", ['{"a":1}', '{"a":"1e2147483648"}']), + ("bare-negative", "a-b", ["a", "b", "a-b", "ab", "a b"]), + ]: + result.append({"id": "boundary-" + name, "request": { + "filterPattern": pattern, "logEventMessages": inputs}}) + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--profile", required=True) + parser.add_argument("--region", required=True) + parser.add_argument("--endpoint-url", required=True) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--suite", choices=("base", "edges", "boundaries"), default="base") + args = parser.parse_args() + if args.endpoint_url != f"https://logs.{args.region}.amazonaws.com": + parser.error("capture requires the explicit regional AWS Logs endpoint") + corpus = {"base": cases, "edges": edge_cases, "boundaries": boundary_cases}[args.suite]() + assert len(cases()) + len(edge_cases()) + len(boundary_cases()) <= 149 + for case in corpus: + for attempt in range(4): + command = ["aws", "--profile", args.profile, "--region", args.region, + "--endpoint-url", args.endpoint_url, "--no-cli-pager", + "logs", "test-metric-filter", "--output", "json", + "--cli-input-json", json.dumps(case["request"])] + completed = subprocess.run(command, capture_output=True, text=True) + if "Throttling" not in completed.stderr: + break + time.sleep(2 ** attempt) + if completed.returncode == 0: + case["response"] = json.loads(completed.stdout) + else: + error = re.search(r"An error occurred \((\w+)\).*?: (.*)", completed.stderr) + if not error or error.group(1) != "InvalidParameterException": + raise RuntimeError("AWS capture blocked: " + completed.stderr) + case["error"] = {"code": error.group(1), "message": error.group(2)} + print(case["id"], "error" if "error" in case else + str(len(case["response"]["matches"])) + " matches", flush=True) + time.sleep(0.15) + fixture = { + "provenance": { + "source": "AWS CloudWatch Logs TestMetricFilter", + "capturedAt": datetime.datetime.now(datetime.timezone.utc).isoformat(), + "region": args.region, + "endpoint": args.endpoint_url, + "inputs": "Synthetic only. No resource writes or user logs.", + "eventNumberBase": 1, + "reference": "https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_TestMetricFilter.html", + }, + "cases": corpus, + } + args.output.write_text(json.dumps(fixture, indent=2) + "\n") + + +if __name__ == "__main__": + main() diff --git a/tools/docs/service_matrix.yaml b/tools/docs/service_matrix.yaml index 78122bd95f..4550e86884 100644 --- a/tools/docs/service_matrix.yaml +++ b/tools/docs/service_matrix.yaml @@ -65,4 +65,5 @@ aliases: deferred: [] # docs/services pages that intentionally have no Service Matrix row of their own. -facet_pages: [] +facet_pages: + - cloudwatch-metric-filters-verification diff --git a/tools/docs/services.yaml b/tools/docs/services.yaml index c8f417b70a..9d926d6a87 100644 --- a/tools/docs/services.yaml +++ b/tools/docs/services.yaml @@ -396,6 +396,7 @@ deferred_handlers: - { path: src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlJsonHandler.java } # no standalone action table yet - { path: src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapHandler.java } # non-tabular doc (grouped/inline) - { path: src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsHandler.java } # multi-table doc (nested handler) + - { path: src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsMetricFilterHandler.java } # multi-table doc (nested handler, reached through CloudWatchLogsHandler) - { path: src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsJsonHandler.java } # multi-table doc (nested handler) - { path: src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java } # multi-table doc (nested handler) - { path: src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildJsonHandler.java } # non-tabular doc (grouped/inline) diff --git a/tools/docs/test_service_inventory.py b/tools/docs/test_service_inventory.py index 65d5986884..77bd0cc716 100644 --- a/tools/docs/test_service_inventory.py +++ b/tools/docs/test_service_inventory.py @@ -280,19 +280,36 @@ def test_deferral_beyond_the_warning_window_is_not_flagged(): assert r["docs"]["deferred_expiring"] == [] +def test_row_exempt_facet_pages_are_counted_without_requiring_a_matrix_row(): + r = si.reconcile(_data(doc_pages=["iam", "s3", "iam-verification"], + facet_pages=["iam-verification"])) + assert r["counts"]["matrix_pages"] == 2 + assert r["counts"]["doc_pages"] == 3 + assert r["docs"]["pages_without_row"] == [] + assert r["docs"]["rows_without_page"] == [] + + +def test_facet_exemption_does_not_hide_an_unregistered_page(): + r = si.reconcile(_data(doc_pages=["iam", "s3", "iam-verification", "unregistered"], + facet_pages=["iam-verification"])) + assert r["docs"]["pages_without_row"] == ["unregistered"] + + # --------------------------------------------------------------------------- # # End to end, against the real repo # --------------------------------------------------------------------------- # def test_real_sources_parse_and_render(): - result = si.reconcile(si.collect(si._repo_root())) + data = si.collect(si._repo_root()) + result = si.reconcile(data) counts = result["counts"] # Not pinned to exact values: they move every time a service lands. What must hold # is that every source parsed to something plausible and the two gated equalities # this tool reports as green really are green. assert all(v > 50 for v in counts.values()) - assert counts["matrix_pages"] == counts["doc_pages"] + assert counts["matrix_pages"] == len(set(data["doc_pages"]) - set(data["facet_pages"])) assert counts["config_accessors"] == counts["yaml_keys"] assert result["docs"]["pages_without_row"] == [] + assert result["docs"]["rows_without_page"] == [] assert result["packages"]["unregistered"] == [] assert "THE ANSWER" in si.render(result)