Unverified Commit 87e45f60 authored by 82marbag's avatar 82marbag Committed by GitHub
Browse files

Request IDs (#2054)



* Request IDs

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>
parent 42f9ad98
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
RFC: RequestID in business logic handlers
=============

> Status: RFC
> Status: Implemented
>
> Applies to: server

@@ -85,7 +85,7 @@ where
    }

    fn call(&mut self, mut req: http::Request<R>) -> Self::Future {
        request.extensions_mut().insert(ServerRequestId::new());
        req.extensions_mut().insert(ServerRequestId::new());
        self.inner.call(req)
    }
}
@@ -131,7 +131,12 @@ Although the generated ID is opaque, this will give guarantees to customers as t
Changes checklist
-----------------

- [ ] Implement `ServerRequestId`: a `new()` function that generates a UUID, with `Display`, `Debug` and `ToStr` implementations
- [x] Implement `ServerRequestId`: a `new()` function that generates a UUID, with `Display`, `Debug` and `ToStr` implementations
- [ ] Implement `ClientRequestId`: `new()` that wraps a string (the header value) and the header in which the value could be found, with `Display`, `Debug` and `ToStr` implementations
- [x] Implement `FromParts` for `Extension<ServerRequestId>`
- [x] Implement `FromParts` for `Extension<ClientRequestId>`
- [ ] Implement `FromParts` for `Extension<ClientRequestId>`

Changes since the RFC has been approved
---------------------------------------

This RFC has been changed to only implement `ServerRequestId`.
+2 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ publish = true
[features]
aws-lambda = ["dep:lambda_http"]
unredacted-logging = []
request-id = ["dep:uuid"]

[dependencies]
aws-smithy-http = { path = "../aws-smithy-http", features = ["rt-tokio"] }
@@ -40,6 +41,7 @@ tracing = "0.1.35"
tokio = { version = "1.8.4", features = ["full"] }
tower = { version = "0.4.11", features = ["util", "make"], default-features = false }
tower-http = { version = "0.3", features = ["add-extension", "map-response-body"] }
uuid = { version = "1", features = ["v4", "fast-rng"], optional = true }

[dev-dependencies]
pretty_assertions = "1"
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ futures-util = "0.3"
lambda_http = "0.7.1"

# Local paths
aws-smithy-http-server = { path = "../../", features = ["aws-lambda"] }
aws-smithy-http-server = { path = "../../", features = ["aws-lambda", "request-id"] }
pokemon-service-server-sdk = { path = "../pokemon-service-server-sdk/" }

[dev-dependencies]
+18 −7
Original line number Diff line number Diff line
@@ -5,15 +5,16 @@

use std::net::{IpAddr, SocketAddr};

use aws_smithy_http_server::request::connect_info::ConnectInfo;
use clap::Parser;
use pokemon_service::{
    capture_pokemon, check_health, do_nothing, get_pokemon_species, get_server_statistics, setup_tracing,
use aws_smithy_http_server::{
    request::connect_info::ConnectInfo, request::request_id::ServerRequestId,
    request::request_id::ServerRequestIdProviderLayer,
};
use clap::Parser;
use pokemon_service::{capture_pokemon, check_health, get_pokemon_species, get_server_statistics, setup_tracing};
use pokemon_service_server_sdk::{
    error::{GetStorageError, NotAuthorized},
    input::GetStorageInput,
    output::GetStorageOutput,
    input::{DoNothingInput, GetStorageInput},
    output::{DoNothingOutput, GetStorageOutput},
    PokemonService,
};

@@ -55,6 +56,14 @@ pub async fn get_storage_with_local_approved(
    Err(GetStorageError::NotAuthorized(NotAuthorized {}))
}

pub async fn do_nothing_but_log_request_ids(
    _input: DoNothingInput,
    server_request_id: ServerRequestId,
) -> DoNothingOutput {
    tracing::debug!("This request has this server ID: {}", server_request_id);
    DoNothingOutput {}
}

#[tokio::main]
async fn main() {
    let args = Args::parse();
@@ -64,11 +73,13 @@ async fn main() {
        .get_storage(get_storage_with_local_approved)
        .get_server_statistics(get_server_statistics)
        .capture_pokemon(capture_pokemon)
        .do_nothing(do_nothing)
        .do_nothing(do_nothing_but_log_request_ids)
        .check_health(check_health)
        .build()
        .expect("failed to build an instance of PokemonService");

    let app = app.layer(&ServerRequestIdProviderLayer::new());

    // Start the [`hyper::Server`].
    let bind: SocketAddr = format!("{}:{}", args.address, args.port)
        .parse()
+3 −0
Original line number Diff line number Diff line
@@ -67,6 +67,9 @@ pub mod extension;
#[cfg(feature = "aws-lambda")]
#[cfg_attr(docsrs, doc(cfg(feature = "aws-lambda")))]
pub mod lambda;
#[cfg(feature = "request-id")]
#[cfg_attr(docsrs, doc(cfg(feature = "request-id")))]
pub mod request_id;

fn internal_server_error() -> http::Response<BoxBody> {
    let mut response = http::Response::new(empty());
Loading