Skip to content

Commit 739682f

Browse files
committed
Metrics: Add interfaces and initial implementations
1 parent f8e7fb6 commit 739682f

File tree

62 files changed

+3649
-57
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3649
-57
lines changed

core/aws-core/src/main/java/software/amazon/awssdk/awscore/client/handler/AwsClientHandlerUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
4040
import software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain;
4141
import software.amazon.awssdk.core.interceptor.InterceptorContext;
42+
import software.amazon.awssdk.core.interceptor.MetricExecutionAttribute;
4243
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
4344
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
4445
import software.amazon.awssdk.core.signer.Signer;
@@ -81,7 +82,12 @@ static <InputT extends SdkRequest, OutputT extends SdkResponse> ExecutionContext
8182
.putAttribute(SdkInternalExecutionAttribute.IS_FULL_DUPLEX, executionParams.isFullDuplex())
8283
.putAttribute(SdkExecutionAttribute.CLIENT_TYPE, clientConfig.option(SdkClientOption.CLIENT_TYPE))
8384
.putAttribute(SdkExecutionAttribute.SERVICE_NAME, clientConfig.option(SdkClientOption.SERVICE_NAME))
84-
.putAttribute(SdkExecutionAttribute.OPERATION_NAME, executionParams.getOperationName());
85+
.putAttribute(SdkExecutionAttribute.OPERATION_NAME, executionParams.getOperationName())
86+
.putAttribute(MetricExecutionAttribute.METRIC_CONFIGURATION_PROVIDER,
87+
clientConfig.option(SdkClientOption.METRIC_CONFIGURATION_PROVIDER))
88+
.putAttribute(MetricExecutionAttribute.METRIC_PUBLISHER_CONFIGURATION,
89+
clientConfig.option(SdkClientOption.METRIC_PUBLISHER_CONFIGURATION))
90+
;
8591

8692
ExecutionInterceptorChain executionInterceptorChain =
8793
new ExecutionInterceptorChain(clientConfig.option(SdkClientOption.EXECUTION_INTERCEPTORS));

core/metrics-spi/pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>core</artifactId>
7+
<groupId>software.amazon.awssdk</groupId>
8+
<version>2.7.12-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>metrics-spi</artifactId>
13+
<name>AWS Java SDK :: Metrics SPI</name>
14+
<description> This is the base module for SDK metrics feature. It contains the interfaces used for metrics feature
15+
that are used by other modules in the library.
16+
</description>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>software.amazon.awssdk</groupId>
21+
<artifactId>annotations</artifactId>
22+
<version>${awsjavasdk.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>software.amazon.awssdk</groupId>
26+
<artifactId>utils</artifactId>
27+
<version>${awsjavasdk.version}</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>software.amazon.awssdk</groupId>
32+
<artifactId>test-utils</artifactId>
33+
<version>${awsjavasdk.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.github.tomakehurst</groupId>
43+
<artifactId>wiremock</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.assertj</groupId>
48+
<artifactId>assertj-core</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.mockito</groupId>
53+
<artifactId>mockito-core</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-jar-plugin</artifactId>
64+
<configuration>
65+
<archive>
66+
<manifestEntries>
67+
<Automatic-Module-Name>software.amazon.awssdk.metrics</Automatic-Module-Name>
68+
</manifestEntries>
69+
</archive>
70+
</configuration>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-compiler-plugin</artifactId>
75+
<configuration>
76+
<source>1.8</source>
77+
<target>1.8</target>
78+
</configuration>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.metrics;
17+
18+
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.metrics.metrics.SdkDefaultMetric;
20+
21+
/**
22+
* A enum class representing the different types of metric categories in the SDK.
23+
* <p>
24+
* A metric can be tagged with multiple categories. Clients can enable/disable metric collection
25+
* at a {@link MetricCategory} level.
26+
*
27+
* @see SdkDefaultMetric
28+
*/
29+
@SdkPublicApi
30+
public enum MetricCategory {
31+
32+
/**
33+
* All metrics defined by the SDK are classified under this category at a minimum. If the metrics feature is enabled
34+
* but the category to collect is not, only metrics that are classified under this category are collected by the SDK
35+
*/
36+
DEFAULT("default"),
37+
38+
/**
39+
* Metrics collected at the http client level are classified under this category.
40+
*/
41+
HTTP_CLIENT("httpclient"),
42+
43+
/**
44+
* Metrics specific to streaming, eventStream APIs are classified under this category.
45+
*/
46+
STREAMING("streaming"),
47+
48+
/**
49+
* This is an umbrella category (provided for convenience) that records metrics belonging to every category
50+
* defined in this enum. Clients who wish to collect lot of SDK metrics data should use this.
51+
* <p>
52+
* Note: Enabling this option is verbose and can be expensive based on the platform the metrics are uploaded to.
53+
* Please make sure you need all this data before using this category.
54+
*/
55+
ALL("all")
56+
57+
;
58+
59+
private final String value;
60+
61+
MetricCategory(String value) {
62+
this.value = value;
63+
}
64+
65+
public String getValue() {
66+
return value;
67+
}
68+
69+
/**
70+
* Create a {@link MetricCategory} from the given String value. This method is case insensitive.
71+
*
72+
* @param value the value to create the {@link MetricCategory} from
73+
* @return A {@link MetricCategory} if the given {@link #value} matches one of the enum values.
74+
* Otherwise throws {@link IllegalArgumentException}
75+
*/
76+
public static MetricCategory fromString(String value) {
77+
for (MetricCategory mc : MetricCategory.values()) {
78+
if (mc.value.equalsIgnoreCase(value)) {
79+
return mc;
80+
}
81+
}
82+
83+
throw new IllegalArgumentException("MetricCategory cannot be created from value: " + value);
84+
}
85+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.metrics.internal;
17+
18+
import software.amazon.awssdk.annotations.SdkInternalApi;
19+
import software.amazon.awssdk.metrics.MetricCategory;
20+
import software.amazon.awssdk.utils.SystemSetting;
21+
22+
/**
23+
* System properties to configure metrics options in the SDK.
24+
*/
25+
@SdkInternalApi
26+
public enum MetricSystemSetting implements SystemSetting {
27+
28+
/**
29+
* Enable the Java SDK Metrics by setting this property. Metrics feature is disabled if this feature is not specified or
30+
* specified to anything other than "true"
31+
*/
32+
AWS_JAVA_SDK_METRICS_ENABLED("aws.javasdk2x.metrics.enabled", null),
33+
34+
/**
35+
* Specify comma separated {@link MetricCategory} values to enable the categories for metrics collection.
36+
* Only metrics belonging to these categories are collected by the SDK.
37+
*
38+
* <p>
39+
* This value is defaulted to {@link MetricCategory#DEFAULT}. If this property is not set but metrics are enabled,
40+
* then metrics belonging to {@link MetricCategory#DEFAULT} category are collected.
41+
* </p>
42+
*/
43+
AWS_JAVA_SDK_METRICS_CATEGORY("aws.javasdk2x.metrics.category", "Default")
44+
45+
;
46+
47+
private final String systemProperty;
48+
private final String defaultValue;
49+
50+
MetricSystemSetting(String systemProperty, String defaultValue) {
51+
this.systemProperty = systemProperty;
52+
this.defaultValue = defaultValue;
53+
}
54+
55+
@Override
56+
public String property() {
57+
return systemProperty;
58+
}
59+
60+
@Override
61+
public String environmentVariable() {
62+
return name();
63+
}
64+
65+
@Override
66+
public String defaultValue() {
67+
return defaultValue;
68+
}
69+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.metrics.meter;
17+
18+
import java.util.Collections;
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
import software.amazon.awssdk.annotations.SdkPublicApi;
22+
import software.amazon.awssdk.metrics.MetricCategory;
23+
import software.amazon.awssdk.utils.Validate;
24+
25+
/**
26+
* A {@link Gauge} implementation that stores a constant value for a metric.
27+
* The value stored cannot be changed after object creation
28+
*
29+
* @param <TypeT> the type of the value recorded in the Gauge
30+
*/
31+
@SdkPublicApi
32+
public final class ConstantGauge<TypeT> implements Gauge<TypeT> {
33+
34+
private final TypeT value;
35+
private final Set<MetricCategory> categories;
36+
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();
54+
}
55+
56+
/**
57+
* @param value the value to store in the guage
58+
* @param <T> type of the value
59+
* @return An instance of {@link ConstantGauge} with the given {@link #value} stored in the gauge.
60+
*/
61+
public static <T> ConstantGauge<T> create(T value) {
62+
return builder().value(value).build();
63+
}
64+
65+
public static final class Builder<BuilderT> {
66+
private BuilderT value;
67+
private final Set<MetricCategory> categories = new HashSet<>();
68+
69+
private Builder() {
70+
}
71+
72+
/**
73+
* @param value the value to store in the gauge
74+
* @return This object for method chaining
75+
*/
76+
public Builder<BuilderT> value(BuilderT value) {
77+
this.value = value;
78+
return this;
79+
}
80+
81+
/**
82+
* Register the given categories in this metric
83+
* @param categories the set of {@link MetricCategory} this metric belongs to
84+
* @return This object for method chaining
85+
*/
86+
public Builder<BuilderT> categories(Set<MetricCategory> categories) {
87+
this.categories.addAll(categories);
88+
return this;
89+
}
90+
91+
/**
92+
* Register the given {@link MetricCategory} in this metric
93+
* @param category the {@link MetricCategory} to tag the metric with
94+
* @return This object for method chaining
95+
*/
96+
public Builder<BuilderT> addCategory(MetricCategory category) {
97+
this.categories.add(category);
98+
return this;
99+
}
100+
101+
public ConstantGauge<BuilderT> build() {
102+
return new ConstantGauge(this);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)