Skip to content

Commit 0fcb1fb

Browse files
committed
Add few more metrics
1 parent d5241e3 commit 0fcb1fb

26 files changed

+819
-121
lines changed

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

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

1818
import java.util.Arrays;
19+
import java.util.Collections;
1920
import java.util.HashSet;
2021
import java.util.Set;
2122
import software.amazon.awssdk.annotations.SdkProtectedApi;
@@ -40,9 +41,9 @@ public enum SdkMetrics implements Metric {
4041
Api("Api", MetricCategory.Default),
4142

4243
/**
43-
* The http status code returned in the response
44+
* The total time taken to finish (success or fail) a request (inclusive of all retries)
4445
*/
45-
HttpStatusCode("HttpStatusCode", MetricCategory.Default),
46+
ApiCallLatency("ApiCallLatency", MetricCategory.Default),
4647

4748
/**
4849
* The time taken to marshall the request
@@ -55,6 +56,41 @@ public enum SdkMetrics implements Metric {
5556
*/
5657
ApiCallAttemptCount("ApiCallAttemptCount", MetricCategory.Default),
5758

59+
/**
60+
* The time taken to sign the request
61+
*/
62+
SigningLatency("SigningLatency", MetricCategory.Default),
63+
64+
/**
65+
* The time taken by the underlying http client to start the Api call attempt and return the response
66+
*/
67+
HttpRequestRoundTripLatency("HttpRequestRoundTripLatency", MetricCategory.Default),
68+
69+
/**
70+
* The time taken to unmarshall the response (either successful and failed response)
71+
*/
72+
UnmarshallingLatency("UnmarshallingLatency", MetricCategory.Default),
73+
74+
/**
75+
* The total time taken for an Api call attempt
76+
*/
77+
ApiCallAttemptLatency("ApiCallAttemptLatency", MetricCategory.Default),
78+
79+
/**
80+
* The http status code returned in the response
81+
*/
82+
HttpStatusCode("HttpStatusCode", MetricCategory.Default),
83+
84+
/**
85+
* The request Id for the request. Represented by x-amz-request-id header in response
86+
*/
87+
AwsRequestId("AwsRequestId", MetricCategory.Default),
88+
89+
/**
90+
* The extended request Id for the request. Represented by x-amz-id-2 header in response
91+
*/
92+
ExtendedRequestId("ExtendedRequestId", MetricCategory.Default),
93+
5894
/**
5995
* Maximum number of streams allowed on a http2 connection
6096
*/
@@ -64,27 +100,27 @@ public enum SdkMetrics implements Metric {
64100

65101
private final String value;
66102

67-
private final Set<MetricCategory> tags;
103+
private final Set<MetricCategory> categories;
68104

69-
SdkMetrics(String value, MetricCategory... tags) {
105+
SdkMetrics(String value, MetricCategory... categories) {
70106
this.value = value;
71-
this.tags = new HashSet<>(Arrays.asList(tags));
107+
this.categories = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(categories)));
72108
}
73109

74110
public String value() {
75111
return value;
76112
}
77113

78114
@Override
79-
public Set<MetricCategory> tags() {
80-
return tags;
115+
public Set<MetricCategory> categories() {
116+
return categories;
81117
}
82118

83119
@Override
84120
public String toString() {
85121
return "{" +
86122
"value='" + value + '\'' +
87-
", tags=" + tags +
123+
", categories=" + categories +
88124
'}';
89125
}
90126
}

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

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515

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

18+
import java.util.Collections;
19+
import java.util.HashSet;
20+
import java.util.Set;
1821
import software.amazon.awssdk.annotations.SdkPublicApi;
22+
import software.amazon.awssdk.metrics.MetricCategory;
23+
import software.amazon.awssdk.utils.Validate;
1924

2025
/**
2126
* A {@link Gauge} implementation that stores a constant value for a metric.
@@ -27,9 +32,25 @@
2732
public final class ConstantGauge<TypeT> implements Gauge<TypeT> {
2833

2934
private final TypeT value;
35+
private final Set<MetricCategory> categories;
3036

31-
private ConstantGauge(TypeT value) {
32-
this.value = value;
37+
private ConstantGauge(Builder<TypeT> builder) {
38+
this.value = Validate.notNull(builder.value, "Value cannot be null");
39+
this.categories = Collections.unmodifiableSet(builder.categories);
40+
}
41+
42+
@Override
43+
public TypeT value() {
44+
return value;
45+
}
46+
47+
@Override
48+
public Set<MetricCategory> categories() {
49+
return categories;
50+
}
51+
52+
public static Builder builder() {
53+
return new Builder();
3354
}
3455

3556
/**
@@ -38,11 +59,44 @@ private ConstantGauge(TypeT value) {
3859
* @return An instance of {@link ConstantGauge} with the given {@link #value} stored in the gauge.
3960
*/
4061
public static <T> ConstantGauge<T> create(T value) {
41-
return new ConstantGauge<>(value);
62+
return builder().value(value).build();
4263
}
4364

44-
@Override
45-
public TypeT value() {
46-
return value;
65+
public static final class Builder<BuilderT> {
66+
private BuilderT value;
67+
private final Set<MetricCategory> categories = new HashSet<>();
68+
69+
/**
70+
* @param value the value to store in the gauge
71+
* @return This object for method chaining
72+
*/
73+
public Builder<BuilderT> value(BuilderT value) {
74+
this.value = value;
75+
return this;
76+
}
77+
78+
/**
79+
* Register the given categories in this metric
80+
* @param categories the set of {@link MetricCategory} this metric belongs to
81+
* @return This object for method chaining
82+
*/
83+
public Builder<BuilderT> categories(Set<MetricCategory> categories) {
84+
this.categories.addAll(categories);
85+
return this;
86+
}
87+
88+
/**
89+
* Register the given {@link MetricCategory} in this metric
90+
* @param category the {@link MetricCategory} to tag the metric with
91+
* @return This object for method chaining
92+
*/
93+
public Builder<BuilderT> addCategory(MetricCategory category) {
94+
this.categories.add(category);
95+
return this;
96+
}
97+
98+
public ConstantGauge<BuilderT> build() {
99+
return new ConstantGauge(this);
100+
}
47101
}
48102
}

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

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515

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

18+
import java.util.Collections;
19+
import java.util.HashSet;
20+
import java.util.Set;
1821
import java.util.concurrent.atomic.AtomicReference;
1922
import software.amazon.awssdk.annotations.SdkPublicApi;
23+
import software.amazon.awssdk.metrics.MetricCategory;
24+
import software.amazon.awssdk.utils.Validate;
2025

2126
/**
2227
* A basic implementation of {@link Gauge} that has ability to set, update and return a single value.
@@ -27,19 +32,12 @@
2732
public final class DefaultGauge<TypeT> implements Gauge<TypeT> {
2833

2934
private final AtomicReference<TypeT> atomicReference;
35+
private final Set<MetricCategory> categories;
3036

31-
private DefaultGauge(TypeT initialValue) {
32-
this.atomicReference = new AtomicReference<>(initialValue);
33-
}
34-
35-
/**
36-
*
37-
* @param initialValue the value to stored in the gauge instance when its created
38-
* @param <T> type of the value
39-
* @return An instance of {@link DefaultGauge} with the given #initialValue stored in the gauge.
40-
*/
41-
public static <T> DefaultGauge<T> create(T initialValue) {
42-
return new DefaultGauge<>(initialValue);
37+
private DefaultGauge(Builder<TypeT> builder) {
38+
Validate.notNull(builder.value, "Value cannot be null");
39+
this.atomicReference = new AtomicReference<>(builder.value);
40+
this.categories = Collections.unmodifiableSet(builder.categories);
4341
}
4442

4543
/**
@@ -54,4 +52,60 @@ public void value(TypeT value) {
5452
public TypeT value() {
5553
return atomicReference.get();
5654
}
55+
56+
@Override
57+
public Set<MetricCategory> categories() {
58+
return categories;
59+
}
60+
61+
public static Builder builder() {
62+
return new Builder();
63+
}
64+
65+
/**
66+
* @param initialValue the value to stored in the gauge instance when its created
67+
* @param <T> type of the value
68+
* @return A new instance of {@link DefaultGauge} with the given #initialValue stored in the gauge.
69+
*/
70+
public static <T> DefaultGauge<T> create(T initialValue) {
71+
return builder().value(initialValue).build();
72+
}
73+
74+
public static final class Builder<BuilderT> {
75+
private BuilderT value;
76+
private final Set<MetricCategory> categories = new HashSet<>();
77+
78+
/**
79+
* @param value the initial value to store in the gauge
80+
* @return This object for method chaining
81+
*/
82+
public Builder<BuilderT> value(BuilderT value) {
83+
this.value = value;
84+
return this;
85+
}
86+
87+
/**
88+
* Register the given categories in this metric
89+
* @param categories the set of {@link MetricCategory} this metric belongs to
90+
* @return This object for method chaining
91+
*/
92+
public Builder<BuilderT> categories(Set<MetricCategory> categories) {
93+
this.categories.addAll(categories);
94+
return this;
95+
}
96+
97+
/**
98+
* Register the given {@link MetricCategory} in this metric
99+
* @param category the {@link MetricCategory} to tag the metric with
100+
* @return This object for method chaining
101+
*/
102+
public Builder<BuilderT> addCategory(MetricCategory category) {
103+
this.categories.add(category);
104+
return this;
105+
}
106+
107+
public DefaultGauge<BuilderT> build() {
108+
return new DefaultGauge(this);
109+
}
110+
}
57111
}

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,31 @@
1818
import java.time.Clock;
1919
import java.time.Duration;
2020
import java.time.Instant;
21+
import java.util.Collections;
22+
import java.util.HashSet;
23+
import java.util.Set;
2124
import java.util.concurrent.Callable;
2225
import java.util.concurrent.TimeUnit;
2326
import software.amazon.awssdk.annotations.SdkPublicApi;
27+
import software.amazon.awssdk.metrics.MetricCategory;
2428
import software.amazon.awssdk.utils.Validate;
25-
import software.amazon.awssdk.utils.builder.CopyableBuilder;
26-
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
2729

2830
/**
2931
* A default implementation of {@link Timer} metric interface.
3032
*/
3133
@SdkPublicApi
32-
public final class DefaultTimer implements Timer, ToCopyableBuilder<DefaultTimer.Builder, DefaultTimer> {
34+
public final class DefaultTimer implements Timer {
3335

3436
private final Clock clock;
3537
private Instant startTime;
3638
private Instant endTime;
39+
private final Set<MetricCategory> categories;
3740

3841
private DefaultTimer(Builder builder) {
3942
this.clock = Validate.notNull(builder.clock, "Clock");
4043
this.startTime = clock.instant();
41-
this.endTime = null;
44+
this.endTime = clock.instant();
45+
this.categories = Collections.unmodifiableSet(builder.categories);
4246
}
4347

4448
@Override
@@ -89,6 +93,11 @@ public Duration totalTime() {
8993
return Duration.between(startTime, endTime);
9094
}
9195

96+
@Override
97+
public Set<MetricCategory> categories() {
98+
return categories;
99+
}
100+
92101
private void validateEndTime() {
93102
if (endTime == null) {
94103
throw new IllegalStateException("End time was never updated. You need to call one of the "
@@ -103,23 +112,19 @@ public static Builder builder() {
103112
return new Builder();
104113
}
105114

106-
@Override
107-
public Builder toBuilder() {
108-
return builder().clock(clock);
109-
}
110-
111115
/**
112-
* Builder to configure the params for construction
116+
* Builder class to create instances of {@link DefaultTimer}
113117
*/
114-
public static final class Builder implements CopyableBuilder<Builder, DefaultTimer> {
118+
public static final class Builder {
115119

116-
private Clock clock;
120+
private Clock clock = Clock.systemUTC();
121+
private final Set<MetricCategory> categories = new HashSet<>();
117122

118123
private Builder() {
119124
}
120125

121126
/**
122-
* Sets the {@link Clock} implementation the timer should use
127+
* Sets the {@link Clock} implementation the timer should use. The default value is {@link Clock#systemUTC()}
123128
*
124129
* @param clock the {@link Clock} implementation used by the time
125130
* @return This object for method chaining
@@ -129,7 +134,26 @@ Builder clock(Clock clock) {
129134
return this;
130135
}
131136

132-
@Override
137+
/**
138+
* Register the given categories in this metric
139+
* @param categories the set of {@link MetricCategory} this metric belongs to
140+
* @return This object for method chaining
141+
*/
142+
public Builder categories(Set<MetricCategory> categories) {
143+
this.categories.addAll(categories);
144+
return this;
145+
}
146+
147+
/**
148+
* Register the given {@link MetricCategory} in this metric
149+
* @param category the {@link MetricCategory} to tag the metric with
150+
* @return This object for method chaining
151+
*/
152+
public Builder addCategory(MetricCategory category) {
153+
this.categories.add(category);
154+
return this;
155+
}
156+
133157
public DefaultTimer build() {
134158
return new DefaultTimer(this);
135159
}

0 commit comments

Comments
 (0)