Skip to content
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

feat: inject user agent in Lambda runtime API calls #363

Merged
merged 3 commits into from
Nov 15, 2021

Conversation

nmoutschen
Copy link
Contributor

@nmoutschen nmoutschen commented Nov 11, 2021

Issue #, if available:

Description of changes:

This injects a User-Agent header into the Lambda runtime API calls to track usage of Rust for provided/provided.al2 Lambda runtimes.

This uses aws-lambda-rust/%s, with %s being the crate version, to mimick the behaviour of other runtimes. E.g. for Go, Java RIC. This is slightly different as we cannot get the rustc version directly from the environment variables set by Cargo.

By submitting this pull request

  • I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • I confirm that I've made a best effort attempt to update all relevant documentation.

Sorry, something went wrong.

@@ -20,6 +20,7 @@ impl IntoRequest for NextEventRequest {
fn into_req(self) -> Result<Request<Body>, Error> {
let req = Request::builder()
.method(Method::GET)
.header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format!() is 3 times slower than [].concat(). See https://github.com/hoodie/concatenation_benchmarks-rs. I've just re-run the benches on the latest nightly and it gave me 23ns vs 79ns on my laptop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've changed it to ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat().

Question from me: since both of these values are known at compile time, wouldn't the compiler already optimize them to prevent concatenation at run time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! I actually ran into this recently in another project. I think it comes down to concat being able to call through to an implementation that is specifically optimized for combining the two &str inputs. format! doesn't have this due to the need for being extremely flexible.

@SebastianEdwards
Copy link

@nmoutschen anything those of us using custom Rust runtimes (not this crate) can do to signal our usage and support of Rust to AWS?

@nmoutschen
Copy link
Contributor Author

@SebastianEdwards If you use a similar pattern (/) for the User-Agent, and you let me know what pattern you'll use, we can track it. 😁 I'll just ask that you don't prefix your runtime name with "aws-lambda".

E.g. if you made a runtime called "sebs-awesome-runtime", you can pass a "sebs-awesome-runtime/0.1.0" User-Agent to the Lambda runtime API.

@SebastianEdwards
Copy link

E.g. if you made a runtime called "sebs-awesome-runtime", you can pass a "sebs-awesome-runtime/0.1.0" User-Agent to the Lambda runtime API.

Haha, alright, I guess the user-agent is just going to have to be "sebs-awesome-runtime" then. Will add the header next week

Copy link
Contributor

@simonvandel simonvandel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid allocations for User-Agent by defining it once as a const:

const USER_AGENT: &'static str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION"));

See https://godbolt.org/z/8fPdxWKrn for a comparison on the generated ASM.

@coltonweaver
Copy link
Contributor

coltonweaver commented Nov 14, 2021

You can avoid allocations for User-Agent by defining it once as a const:

const USER_AGENT: &'static str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION"));

See https://godbolt.org/z/8fPdxWKrn for a comparison on the generated ASM.

Change overall looks good to me. I agree with @simonvandel that we could probably get away with making this a const using the above. This way we avoid all of the additional allocations on request and response. Thoughts @nmoutschen?

@nmoutschen
Copy link
Contributor Author

Sounds good! I'll make the changes tomorrow morning.

Copy link
Contributor

@coltonweaver coltonweaver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Thanks for addressing the feedback!

@coltonweaver coltonweaver merged commit c948939 into awslabs:master Nov 15, 2021
@nmoutschen nmoutschen deleted the user-agent branch November 15, 2021 16:59
@nmoutschen nmoutschen added the 0.5 label Feb 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants