From 7619866d05879bfde4d2f21e70c5c24a9c7869da Mon Sep 17 00:00:00 2001 From: Stephen-Bao Date: Tue, 9 Aug 2022 17:34:05 -0400 Subject: [PATCH] Added logic for replacing colon in log group name and unit test --- .../emf/environment/AgentBasedEnvironment.java | 12 +++++++++++- .../emf/environment/ECSEnvironmentTest.java | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/software/amazon/cloudwatchlogs/emf/environment/AgentBasedEnvironment.java b/src/main/java/software/amazon/cloudwatchlogs/emf/environment/AgentBasedEnvironment.java index f401dd1e..ef27d4ac 100644 --- a/src/main/java/software/amazon/cloudwatchlogs/emf/environment/AgentBasedEnvironment.java +++ b/src/main/java/software/amazon/cloudwatchlogs/emf/environment/AgentBasedEnvironment.java @@ -45,7 +45,17 @@ public String getName() { @Override public String getLogGroupName() { - return config.getLogGroupName().orElse(getName() + "-metrics"); + if (config.getLogGroupName().isPresent()) { + return config.getLogGroupName().get(); + } else { + String serviceName = getName(); + // for ECS services, replace "repo:tag" format with "repo-tag" to satisfy + // log group regex + if (serviceName.indexOf(':') != -1) { + serviceName = serviceName.replaceAll(":", "-"); + } + return serviceName + "-metrics"; + } } public String getLogStreamName() { diff --git a/src/test/java/software/amazon/cloudwatchlogs/emf/environment/ECSEnvironmentTest.java b/src/test/java/software/amazon/cloudwatchlogs/emf/environment/ECSEnvironmentTest.java index 475648fd..eef757c7 100644 --- a/src/test/java/software/amazon/cloudwatchlogs/emf/environment/ECSEnvironmentTest.java +++ b/src/test/java/software/amazon/cloudwatchlogs/emf/environment/ECSEnvironmentTest.java @@ -150,6 +150,14 @@ public void testGetLogGroupNameReturnNonEmpty() { assertEquals(environment.getLogGroupName(), Constants.UNKNOWN + "-metrics"); } + @Test + public void testGetLogGroupNameReplaceColon() { + String serviceName = "testRepo:testTag"; + when(config.getServiceName()).thenReturn(Optional.of(serviceName)); + + assertEquals(environment.getLogGroupName(), "testRepo-testTag-metrics"); + } + @Test public void testConfigureContext() throws UnknownHostException { PowerMockito.mockStatic(SystemWrapper.class);