55package software .amazon .awssdk .crt .s3 ;
66
77import software .amazon .awssdk .crt .CRT ;
8+ import software .amazon .awssdk .crt .CrtRuntimeException ;
89
910/**
1011 * An Request is any HTTP request made to the S3 Server. Within CRT,
11- * a large enough request (S3MetaRequest) from the customer might be divided
12- * into smaller, parallelised Requests for better performance. These are considered
12+ * a large enough request (S3MetaRequest) from the customer might be divided
13+ * into smaller, parallelised Requests for better performance. These are considered
1314 * independent requests to S3 but CRT abstracts away the implementation details.
1415 *
1516 * S3RequestMetrics are asynchronously collected upon completion of a Request.
1920 * objects (assuming all requests succeed in the first attempt, more if some fail).
2021 */
2122public class S3RequestMetrics {
22- private long s3RequestFirstAttemptStartTimestampNs ;
23- private long s3RequestLastAttemptEndTimestampNs ;
24- private long startTimestampNs ;
25- private long endTimestampNs ;
26- private long totalDurationNs ;
27- private long sendStartTimestampNs ;
28- private long sendEndTimestampNs ;
29- private long sendingDurationNs ;
30- private long receiveStartTimestampNs ;
31- private long receiveEndTimestampNs ;
32- private long receivingDurationNs ;
33- private long signStartTimestampNs ;
34- private long signEndTimestampNs ;
35- private long signingDurationNs ;
36- private long memAcquireStartTimestampNs ;
37- private long memAcquireEndTimestampNs ;
38- private long memAcquireDurationNs ;
39- private long deliverStartTimestampNs ;
40- private long deliverEndTimestampNs ;
41- private long deliverDurationNs ;
42- private long retryDelayStartTimestampNs ;
43- private long retryDelayEndTimestampNs ;
44- private long retryDelayDurationNs ;
45- private long serviceCallDurationNs ;
23+ // AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE = 14358
24+ private static final int AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE = 14358 ;
25+
26+ // Required timestamp metrics - always available (default to 0)
27+ private long s3RequestFirstAttemptStartTimestampNs = 0 ;
28+ private long startTimestampNs = 0 ;
29+ private long endTimestampNs = 0 ;
30+ private long totalDurationNs = 0 ;
31+
32+ // Optional timestamp metrics - may not be available (default to -1)
33+ // These fields will throw CrtRuntimeException(14358) if accessed when unavailable
34+ private long s3RequestLastAttemptEndTimestampNs = -1 ;
35+ private long sendStartTimestampNs = -1 ;
36+ private long sendEndTimestampNs = -1 ;
37+ private long sendingDurationNs = -1 ;
38+ private long receiveStartTimestampNs = -1 ;
39+ private long receiveEndTimestampNs = -1 ;
40+ private long receivingDurationNs = -1 ;
41+ private long signStartTimestampNs = -1 ;
42+ private long signEndTimestampNs = -1 ;
43+ private long signingDurationNs = -1 ;
44+ private long memAcquireStartTimestampNs = -1 ;
45+ private long memAcquireEndTimestampNs = -1 ;
46+ private long memAcquireDurationNs = -1 ;
47+ private long deliverStartTimestampNs = -1 ;
48+ private long deliverEndTimestampNs = -1 ;
49+ private long deliverDurationNs = -1 ;
50+ private long retryDelayStartTimestampNs = -1 ;
51+ private long retryDelayEndTimestampNs = -1 ;
52+ private long retryDelayDurationNs = -1 ;
53+ private long serviceCallDurationNs = -1 ;
4654
4755 // Request/Response info metrics
48- private int responseStatus ;
49- private String requestId ;
50- private String extendedRequestId ;
51- private String operationName ;
52- private String requestPathQuery ;
53- private String hostAddress ;
54- private int requestType ;
56+ // Optional: may not be available (defaults to -1 for int, null for String)
57+ private int responseStatus = -1 ;
58+ private String requestId = null ;
59+ private String extendedRequestId = null ;
60+ private String operationName = null ;
5561
56- // CRT info metrics
57- private String ipAddress ;
62+ // Required: always available (default to null, will be set by native code)
63+ private String requestPathQuery = null ;
64+ private String hostAddress = null ;
65+
66+ // Required: always available (default to 0)
67+ private int requestType = 0 ;
68+
69+ // CRT info metrics - optional, may not be available (default to null or -1)
70+ private String ipAddress = null ;
5871
5972 // Request ptr and connection id are internal metrics for crt that directly points to the
6073 // connection's and S3 request's addresses for this request attempt. This does not need to be exposed,
6174 // but it might prove useful for logs.
62- private long connectionId ;
63- private long requestPtr ;
64-
65- private long threadId ;
66- private int streamId ;
67- private int errorCode ;
68- private int retryAttempt ;
69-
70- public long getApiCallDurationNs () {
75+ // Optional: may not be available (default to -1)
76+ private long connectionId = -1 ;
77+ private long requestPtr = -1 ;
78+ private long threadId = -1 ;
79+ private int streamId = -1 ;
80+
81+ // Required: always available (default to 0)
82+ private int errorCode = 0 ;
83+ private int retryAttempt = 0 ;
84+
85+ public long getApiCallDurationNs () throws CrtRuntimeException {
86+ if (this .s3RequestLastAttemptEndTimestampNs == -1 ) {
87+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
88+ }
7189 return this .s3RequestLastAttemptEndTimestampNs - this .s3RequestFirstAttemptStartTimestampNs ;
72- }
90+ }
7391
7492 public boolean isApiCallSuccessful () {
7593 return this .errorCode == 0 ;
@@ -91,31 +109,52 @@ public String getServiceEndpoint() {
91109 return this .hostAddress ;
92110 }
93111
94- public String getAwsExtendedRequestId () {
112+ public String getAwsExtendedRequestId () throws CrtRuntimeException {
113+ if (this .extendedRequestId == null ) {
114+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
115+ }
95116 return this .extendedRequestId ;
96117 }
97118
98- public String getAwsRequestId () {
119+ public String getAwsRequestId () throws CrtRuntimeException {
120+ if (this .requestId == null ) {
121+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
122+ }
99123 return this .requestId ;
100124 }
101125
102- public long getBackoffDelayDurationNs () {
126+ public long getBackoffDelayDurationNs () throws CrtRuntimeException {
127+ if (this .retryDelayDurationNs == -1 ) {
128+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
129+ }
103130 return this .retryDelayDurationNs ;
104131 }
105132
106- public long getServiceCallDurationNs () {
133+ public long getServiceCallDurationNs () throws CrtRuntimeException {
134+ if (this .serviceCallDurationNs == -1 ) {
135+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
136+ }
107137 return this .serviceCallDurationNs ;
108138 }
109139
110- public long getSigningDurationNs () {
140+ public long getSigningDurationNs () throws CrtRuntimeException {
141+ if (this .signingDurationNs == -1 ) {
142+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
143+ }
111144 return this .signingDurationNs ;
112145 }
113146
114- public long getTimeToFirstByte () {
147+ public long getTimeToFirstByte () throws CrtRuntimeException {
148+ if (this .receiveStartTimestampNs == -1 ) {
149+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
150+ }
115151 return this .receiveStartTimestampNs ;
116152 }
117153
118- public long getTimeToLastByte () {
154+ public long getTimeToLastByte () throws CrtRuntimeException {
155+ if (this .receiveEndTimestampNs == -1 ) {
156+ throw new CrtRuntimeException (AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE );
157+ }
119158 return this .receiveEndTimestampNs ;
120159 }
121160
0 commit comments