-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for metrics in stream response handler #738
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
209d1aa
Add support for metrics in stream response handler
ianbotsf 48ab79c
addressing PR feedback: release JNI metrics reference after callback;…
ianbotsf f4f3f65
addressing PR feedback: add test to verify metrics callback; switch o…
ianbotsf 3fb9256
Merge branch 'main' into main
TingDaoK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/software/amazon/awssdk/crt/http/HttpStreamMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.awssdk.crt.http; | ||
|
||
/** | ||
* Holds tracing metrics for an HTTP stream. Maps to `struct aws_http_stream_metrics` in **aws-c-http**'s | ||
* **request_response.h**. | ||
*/ | ||
public class HttpStreamMetrics { | ||
private final long sendStartTimestampNs; | ||
private final long sendEndTimestampNs; | ||
private final long sendingDurationNs; | ||
private final long receiveStartTimestampNs; | ||
private final long receiveEndTimestampNs; | ||
private final long receivingDurationNs; | ||
private final int streamId; | ||
|
||
HttpStreamMetrics( | ||
long sendStartTimestampNs, | ||
long sendEndTimestampNs, | ||
long sendingDurationNs, | ||
long receiveStartTimestampNs, | ||
long receiveEndTimestampNs, | ||
long receivingDurationNs, | ||
int streamId | ||
) { | ||
this.sendStartTimestampNs = sendStartTimestampNs; | ||
this.sendEndTimestampNs = sendEndTimestampNs; | ||
this.sendingDurationNs = sendingDurationNs; | ||
this.receiveStartTimestampNs = receiveStartTimestampNs; | ||
this.receiveEndTimestampNs = receiveEndTimestampNs; | ||
this.receivingDurationNs = receivingDurationNs; | ||
this.streamId = streamId; | ||
} | ||
|
||
public long getSendStartTimestampNs() { | ||
return sendStartTimestampNs; | ||
} | ||
|
||
public long getSendEndTimestampNs() { | ||
return sendEndTimestampNs; | ||
} | ||
|
||
public long getSendingDurationNs() { | ||
return sendingDurationNs; | ||
} | ||
|
||
public long getReceiveStartTimestampNs() { | ||
return receiveStartTimestampNs; | ||
} | ||
|
||
public long getReceiveEndTimestampNs() { | ||
return receiveEndTimestampNs; | ||
} | ||
|
||
public long getReceivingDurationNs() { | ||
return receivingDurationNs; | ||
} | ||
|
||
public int getStreamId() { | ||
return streamId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,6 +311,57 @@ void aws_java_http_stream_on_stream_destroy_fn(void *user_data) { | |
/********** JNI ENV RELEASE **********/ | ||
} | ||
|
||
void aws_java_http_stream_on_stream_metrics_fn( | ||
struct aws_http_stream *stream, | ||
const struct aws_http_stream_metrics *metrics, | ||
void *user_data) { | ||
struct http_stream_binding *binding = (struct http_stream_binding *)user_data; | ||
|
||
/********** JNI ENV ACQUIRE **********/ | ||
JNIEnv *env = aws_jni_acquire_thread_env(binding->jvm); | ||
if (env == NULL) { | ||
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */ | ||
return; | ||
} | ||
|
||
/* Convert metrics to Java HttpStreamMetrics obj */ | ||
jobject jni_metrics = (*env)->NewObject( | ||
env, | ||
http_stream_metrics_properties.http_stream_metrics_class, | ||
http_stream_metrics_properties.constructor_id, | ||
(jlong)metrics->send_start_timestamp_ns, | ||
(jlong)metrics->send_end_timestamp_ns, | ||
(jlong)metrics->sending_duration_ns, | ||
(jlong)metrics->receive_start_timestamp_ns, | ||
(jlong)metrics->receive_end_timestamp_ns, | ||
(jlong)metrics->receiving_duration_ns, | ||
|
||
/* Stream IDs are 31-bit unsigned integers, which fits into Java's regular (signed) 32-bit int */ | ||
TingDaoK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(jint)metrics->stream_id | ||
); | ||
|
||
(*env)->CallVoidMethod( | ||
env, | ||
binding->java_http_response_stream_handler, | ||
http_stream_response_handler_properties.onMetrics, | ||
binding->java_http_stream_base, | ||
jni_metrics); | ||
|
||
/* Delete local reference to metrics object */ | ||
(*env)->DeleteLocalRef(env, jni_metrics); | ||
|
||
if (aws_jni_check_and_clear_exception(env)) { | ||
/* Close the Connection if the Java Callback throws an Exception */ | ||
aws_http_connection_close(aws_http_stream_get_connection(stream)); | ||
|
||
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Received Exception from onMetrics", (void *)stream); | ||
aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to manually delete the local reference created when it's called from a C thread, |
||
aws_jni_release_thread_env(binding->jvm, env); | ||
/********** JNI ENV RELEASE **********/ | ||
} | ||
|
||
jobjectArray aws_java_http_headers_from_native(JNIEnv *env, struct aws_http_headers *headers) { | ||
(void)headers; | ||
jobjectArray ret; | ||
|
@@ -383,6 +434,7 @@ static jobject s_make_request_general( | |
.on_response_body = aws_java_http_stream_on_incoming_body_fn, | ||
.on_complete = aws_java_http_stream_on_stream_complete_fn, | ||
.on_destroy = aws_java_http_stream_on_stream_destroy_fn, | ||
.on_metrics = aws_java_http_stream_on_stream_metrics_fn, | ||
.user_data = stream_binding, | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: can we move the metrics callback before the complete callback? So, that it's the same order as it gets invoked