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);