Skip to content

Commit c9ff702

Browse files
author
Mark Kuhn
committed
fixes for java target version downgrade
1 parent 746eef7 commit c9ff702

File tree

10 files changed

+54
-28
lines changed

10 files changed

+54
-28
lines changed

examples/agent/src/main/java/agent/App.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public static void main(String[] args) {
2323
} catch (InvalidMetricException | InvalidDimensionException | DimensionSetExceededException e) {
2424
System.out.println(e);
2525
}
26-
environment.getSink().shutdown().orTimeout(360_000L, TimeUnit.MILLISECONDS);
26+
27+
try {
28+
environment.getSink().shutdown().get(360_000L, TimeUnit.MILLISECONDS);
29+
} catch (Exception ignored) {
30+
}
2731
}
2832

2933
private static void emitMetric(Environment environment)

examples/ecs-firelens/src/main/java/App.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public static void main(String[] args) throws Exception {
5151
private static void registerShutdownHook() {
5252
// https://aws.amazon.com/blogs/containers/graceful-shutdowns-with-ecs/
5353
Signal.handle(new Signal("TERM"), sig -> {
54-
env.getSink().shutdown().orTimeout(1_000L, TimeUnit.MILLISECONDS);
54+
try {
55+
env.getSink().shutdown().get(1_000L, TimeUnit.MILLISECONDS);
56+
} catch (Exception ignored) {
57+
}
5558
System.exit(0);
5659
});
5760
}

src/main/java/software/amazon/cloudwatchlogs/emf/environment/AgentBasedEnvironment.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package software.amazon.cloudwatchlogs.emf.environment;
1818

19+
import java.util.Optional;
1920
import lombok.extern.slf4j.Slf4j;
2021
import software.amazon.cloudwatchlogs.emf.Constants;
2122
import software.amazon.cloudwatchlogs.emf.config.Configuration;
@@ -36,11 +37,14 @@ protected AgentBasedEnvironment(Configuration config) {
3637

3738
@Override
3839
public String getName() {
39-
if (config.getServiceName().isEmpty()) {
40-
log.warn("Unknown ServiceName.");
41-
return Constants.UNKNOWN;
40+
Optional<String> serviceName = config.getServiceName();
41+
42+
if (serviceName.isPresent()) {
43+
return serviceName.get();
4244
}
43-
return config.getServiceName().get();
45+
46+
log.warn("Unknown ServiceName.");
47+
return Constants.UNKNOWN;
4448
}
4549

4650
@Override
@@ -64,13 +68,13 @@ public String getLogStreamName() {
6468
public ISink getSink() {
6569
if (sink == null) {
6670
Endpoint endpoint;
67-
if (config.getAgentEndpoint().isEmpty()) {
71+
if (config.getAgentEndpoint().isPresent()) {
72+
endpoint = Endpoint.fromURL(config.getAgentEndpoint().get());
73+
} else {
6874
log.info(
6975
"Endpoint is not defined. Using default: {}",
7076
Endpoint.DEFAULT_TCP_ENDPOINT);
7177
endpoint = Endpoint.DEFAULT_TCP_ENDPOINT;
72-
} else {
73-
endpoint = Endpoint.fromURL(config.getAgentEndpoint().get());
7478
}
7579
sink =
7680
new AgentSink(

src/main/java/software/amazon/cloudwatchlogs/emf/environment/DefaultEnvironment.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package software.amazon.cloudwatchlogs.emf.environment;
1818

19+
import java.util.Optional;
1920
import lombok.extern.slf4j.Slf4j;
2021
import software.amazon.cloudwatchlogs.emf.Constants;
2122
import software.amazon.cloudwatchlogs.emf.config.Configuration;
@@ -37,11 +38,14 @@ public boolean probe() {
3738

3839
@Override
3940
public String getType() {
40-
if (config.getServiceType().isEmpty()) {
41-
log.info("Unknown ServiceType");
42-
return Constants.UNKNOWN;
41+
Optional<String> serviceType = config.getServiceType();
42+
43+
if (serviceType.isPresent()) {
44+
return serviceType.get();
4345
}
44-
return config.getServiceType().get();
46+
47+
log.info("Unknown ServiceType");
48+
return Constants.UNKNOWN;
4549
}
4650

4751
@Override

src/test/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLoggerTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ void whenSetDimension_withInvalidValue_thenThrowInvalidDimensionException(
126126

127127
@Test
128128
void whenSetDimension_withNameTooLong_thenThrowDimensionException() {
129-
String dimensionName = "a".repeat(Constants.MAX_DIMENSION_NAME_LENGTH + 1);
129+
String dimensionName =
130+
new String(new char[Constants.MAX_DIMENSION_NAME_LENGTH + 1]).replace("\0", "a");
130131
String dimensionValue = "dimValue";
131132
assertThrows(
132133
InvalidDimensionException.class,
@@ -136,7 +137,8 @@ void whenSetDimension_withNameTooLong_thenThrowDimensionException() {
136137
@Test
137138
void whenSetDimension_withValueTooLong_thenThrowDimensionException() {
138139
String dimensionName = "dim";
139-
String dimensionValue = "a".repeat(Constants.MAX_DIMENSION_VALUE_LENGTH + 1);
140+
String dimensionValue =
141+
new String(new char[Constants.MAX_DIMENSION_VALUE_LENGTH + 1]).replace("\0", "a");
140142
assertThrows(
141143
InvalidDimensionException.class,
142144
() -> DimensionSet.of(dimensionName, dimensionValue));
@@ -315,7 +317,8 @@ void whenSetDimensions_withMultipleFlush_thenClearsDimensions()
315317

316318
@Test
317319
void whenPutMetric_withTooLongName_thenThrowInvalidMetricException() {
318-
String name1 = "a".repeat(Constants.MAX_METRIC_NAME_LENGTH + 1);
320+
String name1 =
321+
new String(new char[Constants.MAX_METRIC_NAME_LENGTH + 1]).replace("\0", "a");
319322
assertThrows(InvalidMetricException.class, () -> logger.putMetric(name1, 1));
320323
}
321324

@@ -362,7 +365,8 @@ void whenSetNamespace_withInvalidValue_thenThrowInvalidNamespaceException(String
362365

363366
@Test
364367
void whenSetNamespace_withNameTooLong_thenThrowInvalidNamespaceException() {
365-
String namespace = "a".repeat(Constants.MAX_NAMESPACE_LENGTH + 1);
368+
String namespace =
369+
new String(new char[Constants.MAX_NAMESPACE_LENGTH + 1]).replace("\0", "a");
366370
assertThrows(InvalidNamespaceException.class, () -> logger.setNamespace(namespace));
367371
}
368372

@@ -479,7 +483,7 @@ void testUseDefaultEnvironmentOnResolverException() throws DimensionSetExceededE
479483
@Test
480484
void flush_doesNotPreserveMetrics()
481485
throws InvalidMetricException, InvalidDimensionException,
482-
DimensionSetExceededException {
486+
DimensionSetExceededException {
483487
MetricsLogger logger = new MetricsLogger(envProvider);
484488
logger.setDimensions(DimensionSet.of("Name", "Test"));
485489
logger.putMetric("Count", 1.0);

src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricDefinitionTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
import com.fasterxml.jackson.core.JsonProcessingException;
2222
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import java.util.List;
23+
import java.util.Arrays;
24+
import java.util.Collections;
2425
import org.junit.Test;
2526

2627
public class MetricDefinitionTest {
@@ -51,9 +52,9 @@ public void testSerializeMetricDefinition() throws JsonProcessingException {
5152
@Test
5253
public void testAddValue() {
5354
MetricDefinition md = new MetricDefinition("Time", Unit.MICROSECONDS, 10);
54-
assertEquals(List.of(10d), md.getValues());
55+
assertEquals(Collections.singletonList(10d), md.getValues());
5556

5657
md.addValue(20);
57-
assertEquals(List.of(10d, 20d), md.getValues());
58+
assertEquals(Arrays.asList(10d, 20d), md.getValues());
5859
}
5960
}

src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricsContextTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.databind.json.JsonMapper;
2222
import java.time.Instant;
2323
import java.util.ArrayList;
24+
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Map;
2627
import org.junit.jupiter.api.Assertions;
@@ -101,7 +102,7 @@ void testSerializeAMetricWith101DataPoints()
101102
expectedValues.add((double) i);
102103
}
103104
Assertions.assertEquals(expectedValues, allMetrics.get(0).getValues());
104-
Assertions.assertEquals(List.of(100.0), allMetrics.get(1).getValues());
105+
Assertions.assertEquals(Collections.singletonList(100.0), allMetrics.get(1).getValues());
105106
}
106107

107108
@Test
@@ -128,10 +129,12 @@ void testSerializeMetricsWith101DataPoints()
128129
expectedValues.add((double) i);
129130
}
130131
Assertions.assertEquals(expectedValues, metricsFromEvent1.get(0).getValues());
131-
Assertions.assertEquals(List.of(2.0), metricsFromEvent1.get(1).getValues());
132+
Assertions.assertEquals(
133+
Collections.singletonList(2.0), metricsFromEvent1.get(1).getValues());
132134

133135
Assertions.assertEquals(1, metricsFromEvent2.size());
134-
Assertions.assertEquals(List.of(100.0), metricsFromEvent2.get(0).getValues());
136+
Assertions.assertEquals(
137+
Collections.singletonList(100.0), metricsFromEvent2.get(0).getValues());
135138
}
136139

137140
@Test

src/test/java/software/amazon/cloudwatchlogs/emf/model/RootNodeTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
import com.fasterxml.jackson.core.type.TypeReference;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import java.util.Arrays;
2223
import java.util.List;
2324
import java.util.Map;
2425
import org.junit.jupiter.api.Assertions;
@@ -72,7 +73,8 @@ void testGetTargetMembers()
7273

7374
mc.putProperty("Prop1", "PropValue1");
7475

75-
Assertions.assertEquals(List.of(10.0, 20.0), rootNode.getTargetMembers().get("Count"));
76+
Assertions.assertEquals(
77+
Arrays.asList(10.0, 20.0), rootNode.getTargetMembers().get("Count"));
7678
Assertions.assertEquals(100.0, rootNode.getTargetMembers().get("Latency"));
7779
Assertions.assertEquals("DimVal1", rootNode.getTargetMembers().get("Dim1"));
7880
Assertions.assertEquals("PropValue1", rootNode.getTargetMembers().get("Prop1"));

src/test/java/software/amazon/cloudwatchlogs/emf/sinks/AgentSinkTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public void testEmptyLogGroupName() throws JsonProcessingException, InvalidMetri
103103
ObjectMapper objectMapper = new ObjectMapper();
104104
Map<String, Object> emf_map =
105105
objectMapper.readValue(
106-
fixture.client.getMessages().get(0), new TypeReference<>() {});
106+
fixture.client.getMessages().get(0),
107+
new TypeReference<Map<String, Object>>() {});
107108
Map<String, Object> metadata = (Map<String, Object>) emf_map.get("_aws");
108109

109110
assertFalse(metadata.containsKey("LogGroupName"));

src/test/java/software/amazon/cloudwatchlogs/emf/sinks/MultiSinkTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public void shutdownClosesAllComponentSinks() {
2828
@Test
2929
public void shutdownCompletesExceptionallyIfComponentSinkCompletesExceptionally() {
3030
// arrange
31-
CompletableFuture<Void> failedResult =
32-
CompletableFuture.failedFuture(new RuntimeException());
31+
CompletableFuture<Void> failedResult = new CompletableFuture<>();
32+
failedResult.completeExceptionally(new RuntimeException());
3333
TestSink sink1 = new TestSink();
3434
TestSink sink2 = new TestSink(failedResult);
3535
MultiSink multiSink = MultiSink.builder().sink(sink1).sink(sink2).build();

0 commit comments

Comments
 (0)