Skip to content

Commit 4c959a5

Browse files
committed
Address pr comments
1 parent f7d8996 commit 4c959a5

File tree

35 files changed

+242
-336
lines changed

35 files changed

+242
-336
lines changed

core/metrics-spi/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
<artifactId>utils</artifactId>
2727
<version>${awsjavasdk.version}</version>
2828
</dependency>
29-
<dependency>
30-
<groupId>org.slf4j</groupId>
31-
<artifactId>slf4j-api</artifactId>
32-
</dependency>
3329

3430
<dependency>
3531
<groupId>software.amazon.awssdk</groupId>

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/MetricCategory.java

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

2121
/**
2222
* A enum class representing the different types of metric categories in the SDK.
23-
*
23+
* <p>
2424
* A metric can be tagged with multiple categories. Clients can enable/disable metric collection
2525
* at a {@link MetricCategory} level.
2626
*
@@ -48,7 +48,7 @@ public enum MetricCategory {
4848
/**
4949
* This is an umbrella category (provided for convenience) that records metrics belonging to every category
5050
* defined in this enum. Clients who wish to collect lot of SDK metrics data should use this.
51-
*
51+
* <p>
5252
* Note: Enabling this option is verbose and can be expensive based on the platform the metrics are uploaded to.
5353
* Please make sure you need all this data before using this category.
5454
*/

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/internal/MetricSystemSetting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import software.amazon.awssdk.metrics.MetricCategory;
2020
import software.amazon.awssdk.utils.SystemSetting;
2121

22-
/**
22+
/**
2323
* System properties to configure metrics options in the SDK.
2424
*/
2525
@SdkInternalApi

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/internal/SdkMetric.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package software.amazon.awssdk.metrics.internal;
1717

1818
import static software.amazon.awssdk.metrics.MetricCategory.DEFAULT;
19-
import static software.amazon.awssdk.metrics.MetricCategory.HTTP_CLIENT;
2019

2120
import java.util.Arrays;
2221
import java.util.Collections;
@@ -25,6 +24,7 @@
2524
import software.amazon.awssdk.annotations.SdkInternalApi;
2625
import software.amazon.awssdk.metrics.MetricCategory;
2726
import software.amazon.awssdk.metrics.meter.Metric;
27+
import software.amazon.awssdk.utils.ToString;
2828

2929
/**
3030
* A enum representing the metrics supported by the SDK. All metrics collected by the SDK will be declared in this
@@ -95,11 +95,6 @@ public enum SdkMetric implements Metric {
9595
*/
9696
ExtendedRequestId("ExtendedRequestId", DEFAULT),
9797

98-
/**
99-
* Maximum number of streams allowed on a http2 connection
100-
*/
101-
MaxStreamCount("MaxStreamCount", DEFAULT, HTTP_CLIENT)
102-
10398
;
10499

105100
private final String value;
@@ -122,9 +117,8 @@ public Set<MetricCategory> categories() {
122117

123118
@Override
124119
public String toString() {
125-
return "{" +
126-
"value='" + value + '\'' +
127-
", categories=" + categories +
128-
'}';
120+
return ToString.builder(value)
121+
.add("categories", categories)
122+
.build();
129123
}
130124
}

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/meter/ConstantGauge.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public static final class Builder<BuilderT> {
6666
private BuilderT value;
6767
private final Set<MetricCategory> categories = new HashSet<>();
6868

69+
private Builder() {
70+
}
71+
6972
/**
7073
* @param value the value to store in the gauge
7174
* @return This object for method chaining

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/meter/DefaultGauge.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public static final class Builder<BuilderT> {
7373
private BuilderT value;
7474
private final Set<MetricCategory> categories = new HashSet<>();
7575

76+
private Builder() {
77+
}
78+
7679
/**
7780
* @param value the initial value to store in the gauge
7881
* @return This object for method chaining

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/meter/Gauge.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
package software.amazon.awssdk.metrics.meter;
1717

18+
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
1820
/**
1921
* A gauge metric is an instantaneous value recorded at a point in time.
2022
*
2123
* @param <T> the type of the value recorded in the Gauge
2224
*/
2325
@FunctionalInterface
26+
@SdkPublicApi
2427
public interface Gauge<T> extends Metric {
2528

2629
/**

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/meter/LongCounter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.atomic.LongAdder;
2222
import software.amazon.awssdk.annotations.SdkPublicApi;
2323
import software.amazon.awssdk.metrics.MetricCategory;
24+
import software.amazon.awssdk.utils.Validate;
2425

2526
/**
2627
* A {@link Counter} implementation that stores {@link Long} values.
@@ -43,6 +44,7 @@ public void increment() {
4344

4445
@Override
4546
public void increment(Long value) {
47+
Validate.isNotNegative(value, "Value should not be negative. Use decrement(...) method to reduce count.");
4648
count.add(value);
4749
}
4850

@@ -53,6 +55,7 @@ public void decrement() {
5355

5456
@Override
5557
public void decrement(Long value) {
58+
Validate.isNotNegative(value, "Value should be positive. Use increment(...) method to increase count.");
5659
count.add(-value);
5760
}
5861

@@ -78,9 +81,11 @@ public static LongCounter create() {
7881
* Builder class to create instances of {@link LongCounter}
7982
*/
8083
public static final class Builder {
81-
8284
private final Set<MetricCategory> categories = new HashSet<>();
8385

86+
private Builder() {
87+
}
88+
8489
/**
8590
* Register the given categories in this metric
8691
* @param categories the set of {@link MetricCategory} this metric belongs to

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/meter/NoOpCounter.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package software.amazon.awssdk.metrics.meter;
2-
31
/*
42
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
53
*
@@ -15,15 +13,19 @@
1513
* permissions and limitations under the License.
1614
*/
1715

16+
package software.amazon.awssdk.metrics.meter;
17+
1818
import software.amazon.awssdk.annotations.SdkPublicApi;
1919

2020
/**
2121
* A NoOp implementation of the {@link Counter} metric
2222
*/
2323
@SdkPublicApi
24-
public enum NoOpCounter implements Counter<Number> {
25-
/** Singleton instance **/
26-
INSTANCE;
24+
public final class NoOpCounter implements Counter<Number> {
25+
private static final NoOpCounter INSTANCE = new NoOpCounter();
26+
27+
private NoOpCounter() {
28+
}
2729

2830
@Override
2931
public void increment() {
@@ -49,4 +51,8 @@ public void decrement(Number value) {
4951
public Number count() {
5052
return 0;
5153
}
54+
55+
public static NoOpCounter instance() {
56+
return INSTANCE;
57+
}
5258
}

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/meter/NoOpGauge.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
* A NoOp implementation of the {@link Gauge} metric
2222
*/
2323
@SdkPublicApi
24-
public enum NoOpGauge implements Gauge {
25-
/** Singleton instance **/
26-
INSTANCE;
27-
24+
public final class NoOpGauge implements Gauge {
25+
private static final NoOpGauge INSTANCE = new NoOpGauge();
2826
private static final Object EMPTY_OBJECT = new Object();
2927

28+
private NoOpGauge() {
29+
}
30+
3031
@Override
3132
public Object value() {
3233
return EMPTY_OBJECT;
3334
}
35+
36+
public static NoOpGauge instance() {
37+
return INSTANCE;
38+
}
3439
}

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/meter/NoOpTimer.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323

2424
/**
2525
* A NoOp implementation of the {@link Timer} metric.
26-
*
26+
* <p>
2727
* Always returns {@link Duration#ZERO} as the total duration.
2828
*/
2929
@SdkPublicApi
30-
public enum NoOpTimer implements Timer {
31-
/** Singleton instance **/
32-
INSTANCE;
30+
public final class NoOpTimer implements Timer {
31+
private static final NoOpTimer INSTANCE = new NoOpTimer();
32+
33+
private NoOpTimer() {
34+
}
3335

3436
@Override
3537
public void record(long duration, TimeUnit timeUnit) {
@@ -71,4 +73,8 @@ public Instant endTime() {
7173
public Duration totalTime() {
7274
return Duration.ZERO;
7375
}
76+
77+
public static NoOpTimer instance() {
78+
return INSTANCE;
79+
}
7480
}

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/provider/DefaultMetricConfigurationProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222
import software.amazon.awssdk.annotations.SdkPublicApi;
2323
import software.amazon.awssdk.metrics.MetricCategory;
24+
import software.amazon.awssdk.utils.CollectionUtils;
2425

2526
/**
2627
* The default implementation of {@link MetricConfigurationProvider} interface.
@@ -54,7 +55,7 @@ public static Builder builder() {
5455
}
5556

5657
private Set<MetricCategory> resolveCategories(Set<MetricCategory> categories) {
57-
if (categories == null || categories.isEmpty()) {
58+
if (CollectionUtils.isNullOrEmpty(categories)) {
5859
return new HashSet<>(Arrays.asList(MetricCategory.DEFAULT));
5960
}
6061

@@ -67,6 +68,9 @@ public static final class Builder {
6768

6869
private final Set<MetricCategory> categories = new HashSet<>();
6970

71+
private Builder() {
72+
}
73+
7074
/**
7175
* @param enabled set true to enable metrics. Set to false by default
7276
* @return

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/provider/MetricConfigurationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* Interface to configure the options in metrics feature.
24-
*
24+
* <p>
2525
* This interface acts as a feature flag for metrics. The methods in the interface are called for each request.
2626
* This gives flexibility for metrics feature to be enabled/disabled at runtime and configuration changes
2727
* can be picked up at runtime without need for deploying the application (depending on the implementation).

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/provider/MetricConfigurationProviderChain.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515

1616
package software.amazon.awssdk.metrics.provider;
1717

18-
import java.util.ArrayList;
19-
import java.util.Collections;
20-
import java.util.List;
2118
import java.util.Set;
22-
import org.slf4j.Logger;
23-
import org.slf4j.LoggerFactory;
2419
import software.amazon.awssdk.annotations.SdkProtectedApi;
2520
import software.amazon.awssdk.metrics.MetricCategory;
21+
import software.amazon.awssdk.utils.Logger;
22+
import software.amazon.awssdk.utils.Validate;
2623

2724
/**
2825
* Composite {@link MetricConfigurationProvider} that lets you specify a list of providers to get metric configuration.
29-
*
26+
* <p>
3027
* This chain decides the {@link MetricConfigurationProvider} to use at object construction time and cannot be changed
3128
* later. The resolution policy is as follows:
3229
*
@@ -36,44 +33,35 @@
3633
*/
3734
@SdkProtectedApi
3835
public class MetricConfigurationProviderChain implements MetricConfigurationProvider {
39-
40-
private static final Logger log = LoggerFactory.getLogger(MetricConfigurationProviderChain.class);
41-
42-
private final List<MetricConfigurationProvider> providers;
36+
private static final Logger log = Logger.loggerFor(MetricConfigurationProviderChain.class);
4337

4438
private final MetricConfigurationProvider resolvedProvider;
4539

4640
public MetricConfigurationProviderChain(MetricConfigurationProvider... providers) {
47-
this.providers = new ArrayList<>(providers.length);
48-
Collections.addAll(this.providers, providers);
49-
50-
this.resolvedProvider = resolve();
41+
Validate.isPositive(providers.length, "Atleast one provider should be set");
42+
this.resolvedProvider = resolve(providers);
5143
}
5244

5345
/**
5446
* Determine the provider to use from the chain. This is determined at object construction time and cannot
5547
* be changed later.
5648
*/
57-
private MetricConfigurationProvider resolve() {
49+
private MetricConfigurationProvider resolve(MetricConfigurationProvider... providers) {
5850
for (MetricConfigurationProvider provider : providers) {
5951
try {
6052
if (provider.enabled()) {
6153
return provider;
6254
}
6355
} catch (Exception e) {
6456
// Ignore any exceptions and move onto the next provider
65-
if (log.isDebugEnabled()) {
66-
log.debug("Metrics are not enabled in {}:{}", provider.toString(), e.getMessage());
67-
}
57+
log.debug(() -> "Metrics are not enabled in " + provider.toString(), e);
6858
}
6959
}
7060

71-
if (log.isDebugEnabled()) {
72-
log.debug("Metrics are not enabled in any of the providers in the chain. So using the first provider in"
73-
+ " the chain: ", providers.get(0).toString());
74-
}
61+
log.debug(() -> "Metrics are not enabled in any of the providers in the chain. "
62+
+ "So using the first provider in the chain: " + providers[0]);
7563

76-
return providers.get(0);
64+
return providers[0];
7765
}
7866

7967
@Override

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/provider/SystemSettingsMetricConfigurationProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* A {@link MetricConfigurationProvider} implementation that uses system properties to configure the metrics feature.
26-
*
26+
* <p>
2727
* As Java system properties can only be set during application start time or using application code,
2828
* it is not possible to change the behavior at runtime without code changes.
2929
*/
@@ -50,8 +50,8 @@ public Set<MetricCategory> metricCategories() {
5050

5151
private boolean resolveEnabled() {
5252
return MetricSystemSetting.AWS_JAVA_SDK_METRICS_ENABLED.getStringValue()
53-
.filter(s -> Boolean.TRUE.toString().equals(s.toLowerCase()))
54-
.map(s -> Boolean.TRUE.booleanValue())
53+
.filter(s -> Boolean.TRUE.toString().equalsIgnoreCase(s))
54+
.map(s -> true)
5555
.orElse(false);
5656
}
5757

0 commit comments

Comments
 (0)