Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade to hyper v1.0
Browse files Browse the repository at this point in the history
fundon committed Nov 20, 2023
1 parent e69c3e5 commit 71f3d18
Showing 15 changed files with 75 additions and 53 deletions.
21 changes: 12 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -61,12 +61,13 @@ thiserror = "1.0"
path-tree = "0.7"

# http
headers = "0.3"
http = "0.2"
http-body = "=1.0.0-rc.2"
http-body-util = "=0.1.0-rc.3"
hyper = { version = "=1.0.0-rc.4", features = ["server"] }
hyper-util = { git = "https://github.com/hyperium/hyper-util", rev = "63e84bf", features = ["auto"] }
# TODO: wait headers-v1.0
headers = { git = "https://github.com/hyperium/headers.git", rev = "4400aa9" }
http = "1"
http-body = "1"
http-body-util = "0.1"
hyper = { version = "1", features = ["server"] }
hyper-util = { version = "0.1", features = ["server-auto", "tokio"] }

futures-util = "0.3"
tokio = { version = "1.33", features = ["net"] }
@@ -86,11 +87,12 @@ hex = "0.4"
rust-embed = "8"

# OpenTelemetry
opentelemetry = { version = "0.20", default-features = false }
opentelemetry-prometheus = { version = "0.13", features = [
opentelemetry = { version = "0.21", default-features = false }
opentelemetry_sdk = { version = "0.21", default-features = false }
opentelemetry-prometheus = { version = "0.14", features = [
"prometheus-encoding",
] }
opentelemetry-semantic-conventions = { version = "0.12" }
opentelemetry-semantic-conventions = { version = "0.13" }
prometheus = "0.13"

# Tracing
@@ -102,6 +104,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[profile.dev]
opt-level = 1
split-debuginfo = "unpacked"

[profile.dev.package."*"]
3 changes: 2 additions & 1 deletion examples/otel/metrics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -8,4 +8,5 @@ publish = false
viz = { workspace = true, features = ["otel-metrics", "otel-prometheus"] }

tokio = { workspace = true, features = [ "rt-multi-thread", "macros" ] }
opentelemetry = { workspace = true, default-features = false, features = ["metrics"]}
opentelemetry = { workspace = true, features = ["metrics"]}
opentelemetry_sdk = { workspace = true, features = ["metrics"] }
11 changes: 4 additions & 7 deletions examples/otel/metrics/src/main.rs
Original file line number Diff line number Diff line change
@@ -4,13 +4,10 @@
use std::{net::SocketAddr, sync::Arc};
use tokio::net::TcpListener;

use opentelemetry::{
global,
sdk::{
metrics::{self, Aggregation, Instrument, MeterProvider, Stream},
Resource,
},
KeyValue,
use opentelemetry::{global, KeyValue};
use opentelemetry_sdk::{
metrics::{self, Aggregation, Instrument, MeterProvider, Stream},
Resource,
};

use viz::{
3 changes: 2 additions & 1 deletion examples/otel/tracing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -9,4 +9,5 @@ viz = { workspace = true, features = ["otel-tracing"] }

tokio = { workspace = true, features = [ "rt-multi-thread", "macros" ] }
opentelemetry.workspace = true
opentelemetry-jaeger = { version = "0.19.0", features = ["rt-tokio-current-thread"]}
opentelemetry_sdk = { workspace = true, features = ["trace", "rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.20", features = ["rt-tokio-current-thread"]}
6 changes: 3 additions & 3 deletions examples/otel/tracing/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![deny(warnings)]
#![allow(clippy::unused_async)]

use opentelemetry::{
global,
use opentelemetry::global;
use opentelemetry_sdk::{
runtime::TokioCurrentThread,
sdk::{propagation::TraceContextPropagator, trace::Tracer},
{propagation::TraceContextPropagator, trace::Tracer},
};
use std::{net::SocketAddr, sync::Arc};
use tokio::net::TcpListener;
4 changes: 2 additions & 2 deletions examples/routing/openapi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -12,5 +12,5 @@ serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

utoipa = "4.0"
utoipa-swagger-ui = "4.0"
utoipa = "4"
utoipa-swagger-ui = "4"
2 changes: 1 addition & 1 deletion examples/templates/markup/Cargo.toml
Original file line number Diff line number Diff line change
@@ -9,5 +9,5 @@ viz.workspace = true

tokio = { workspace = true, features = [ "rt-multi-thread", "macros" ] }

markup = "0.13"
markup = "0.14"
v_htmlescape = "0.15"
2 changes: 2 additions & 0 deletions examples/templates/markup/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![deny(warnings)]
#![allow(clippy::unused_async)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::inherent_to_string_shadow_display)]

use std::{net::SocketAddr, sync::Arc};
use tokio::net::TcpListener;
2 changes: 1 addition & 1 deletion viz-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ thiserror.workspace = true

rfc7239 = "0.1" # realip
cookie = { version = "0.18", features = ["percent-encode"], optional = true }
form-data = { version = "0.5.0-rc.2", optional = true }
form-data = { version = "0.5.0", optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
serde_json = { workspace = true, optional = true }
serde_urlencoded = { workspace = true, optional = true }
47 changes: 24 additions & 23 deletions viz-core/src/middleware/otel/tracing.rs
Original file line number Diff line number Diff line change
@@ -8,10 +8,8 @@ use http::uri::Scheme;
use opentelemetry::{
global,
propagation::Extractor,
trace::{
FutureExt as OtelFutureExt, OrderMap, Span, SpanKind, Status, TraceContextExt, Tracer,
},
Context, Key, Value,
trace::{FutureExt as OtelFutureExt, Span, SpanKind, Status, TraceContextExt, Tracer},
Context, KeyValue,
};
use opentelemetry_semantic_conventions::trace::{
CLIENT_ADDRESS, CLIENT_SOCKET_ADDRESS, EXCEPTION_MESSAGE, HTTP_REQUEST_BODY_SIZE,
@@ -82,7 +80,7 @@ where
.tracer
.span_builder(format!("{} {}", req.method(), http_route))
.with_kind(SpanKind::Server)
.with_attributes_map(attributes)
.with_attributes(attributes)

Check warning on line 83 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L83

Added line #L83 was not covered by tests
.start_with_context(&*self.tracer, &parent_context);

span.add_event("request.started".to_string(), vec![]);
@@ -153,70 +151,73 @@ impl<'a> Extractor for RequestHeaderCarrier<'a> {
}
}

fn build_attributes(req: &Request, http_route: &str) -> OrderMap<Key, Value> {
let mut attributes = OrderMap::<Key, Value>::with_capacity(10);
fn build_attributes(req: &Request, http_route: &str) -> Vec<KeyValue> {
let mut attributes = Vec::with_capacity(10);

Check warning on line 155 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L154-L155

Added lines #L154 - L155 were not covered by tests
// <https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md#http-server>
attributes.insert(HTTP_ROUTE, http_route.to_string().into());
attributes.push(KeyValue::new(HTTP_ROUTE, http_route.to_string()));

Check warning on line 157 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L157

Added line #L157 was not covered by tests

// <https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md#common-attributes>
attributes.insert(HTTP_REQUEST_METHOD, req.method().to_string().into());
attributes.insert(
attributes.push(KeyValue::new(HTTP_REQUEST_METHOD, req.method().to_string()));
attributes.push(KeyValue::new(

Check warning on line 161 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L160-L161

Added lines #L160 - L161 were not covered by tests
NETWORK_PROTOCOL_VERSION,
format!("{:?}", req.version()).into(),
);
format!("{:?}", req.version()),
));

Check warning on line 164 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L163-L164

Added lines #L163 - L164 were not covered by tests

let remote_addr = req.remote_addr();
if let Some(remote_addr) = remote_addr {
attributes.insert(CLIENT_ADDRESS, remote_addr.to_string().into());
attributes.push(KeyValue::new(CLIENT_ADDRESS, remote_addr.to_string()));

Check warning on line 168 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L168

Added line #L168 was not covered by tests
}
if let Some(realip) = req.realip().map(|value| value.0).filter(|realip| {
remote_addr
.map(SocketAddr::ip)
.map_or(true, |remoteip| &remoteip != realip)
}) {
attributes.insert(CLIENT_SOCKET_ADDRESS, realip.to_string().into());
attributes.push(KeyValue::new(CLIENT_SOCKET_ADDRESS, realip.to_string()));

Check warning on line 175 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L175

Added line #L175 was not covered by tests
}

let uri = req.uri();
if let Some(host) = uri.host() {
attributes.insert(SERVER_ADDRESS, host.to_string().into());
attributes.push(KeyValue::new(SERVER_ADDRESS, host.to_string()));

Check warning on line 180 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L180

Added line #L180 was not covered by tests
}
if let Some(port) = uri
.port_u16()
.map(i64::from)
.filter(|port| *port != 80 && *port != 443)
{
attributes.insert(SERVER_PORT, port.into());
attributes.push(KeyValue::new(SERVER_PORT, port.to_string()));

Check warning on line 187 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L187

Added line #L187 was not covered by tests
}

if let Some(path_query) = uri.path_and_query() {
if path_query.path() != "/" {
attributes.insert(URL_PATH, path_query.path().to_string().into());
attributes.push(KeyValue::new(URL_PATH, path_query.path().to_string()));

Check warning on line 192 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L192

Added line #L192 was not covered by tests
}
if let Some(query) = path_query.query() {
attributes.insert(URL_QUERY, query.to_string().into());
attributes.push(KeyValue::new(URL_QUERY, query.to_string()));

Check warning on line 195 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L195

Added line #L195 was not covered by tests
}
}

attributes.insert(
attributes.push(KeyValue::new(

Check warning on line 199 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L199

Added line #L199 was not covered by tests
URL_SCHEME,
uri.scheme().unwrap_or(&Scheme::HTTP).to_string().into(),
);
uri.scheme().unwrap_or(&Scheme::HTTP).to_string(),
));

Check warning on line 202 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L201-L202

Added lines #L201 - L202 were not covered by tests

if let Some(content_length) = req
.content_length()
.and_then(|len| i64::try_from(len).ok())
.filter(|len| *len > 0)
{
attributes.insert(HTTP_REQUEST_BODY_SIZE, content_length.into());
attributes.push(KeyValue::new(
HTTP_REQUEST_BODY_SIZE,
content_length.to_string(),
));

Check warning on line 212 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L209-L212

Added lines #L209 - L212 were not covered by tests
}

if let Some(user_agent) = req
.header_typed::<UserAgent>()
.as_ref()
.map(UserAgent::as_str)
{
attributes.insert(USER_AGENT_ORIGINAL, user_agent.to_string().into());
attributes.push(KeyValue::new(USER_AGENT_ORIGINAL, user_agent.to_string()));

Check warning on line 220 in viz-core/src/middleware/otel/tracing.rs

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L220

Added line #L220 was not covered by tests
}

attributes
20 changes: 17 additions & 3 deletions viz-core/tests/request.rs
Original file line number Diff line number Diff line change
@@ -4,10 +4,19 @@ use headers::{authorization::Bearer, Authorization, ContentType, HeaderValue};
use http::uri::Scheme;
use serde::{Deserialize, Serialize};
use viz_core::{
header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, SET_COOKIE},
// TODO: reqwest and hyper haven't used the same version of `http`.
// header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, SET_COOKIE},
// StatusCode,
header::CONTENT_TYPE,
types::{self, PayloadError},
Error, IncomingBody, IntoResponse, Request, RequestExt, Response, ResponseExt, Result,
StatusCode,
Error,
IncomingBody,
IntoResponse,
Request,
RequestExt,
Response,
ResponseExt,
Result,
};

#[derive(Debug, Deserialize, Serialize, PartialEq)]
@@ -70,6 +79,10 @@ async fn request_body() -> Result<()> {
middleware::{cookie, limits},
Router,
};
use viz_test::http::{
header::{AUTHORIZATION, COOKIE},
StatusCode,
};
use viz_test::TestServer;

let router = Router::new()
@@ -300,6 +313,7 @@ async fn request_session() -> Result<()> {
middleware::{cookie, helper::CookieOptions, session},
Router,
};
use viz_test::http::header::{COOKIE, SET_COOKIE};
use viz_test::{nano_id, sessions, TestServer};

let router = Router::new()
3 changes: 2 additions & 1 deletion viz-core/tests/type_payload.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::HashMap;
use viz_core::{types, Error, Request, RequestExt, Response, ResponseExt, Result, StatusCode};
use viz_core::{types, Error, Request, RequestExt, Response, ResponseExt, Result};

#[tokio::test]
async fn payload() -> Result<()> {
use viz::{middleware::limits, Router};
use viz_test::http::StatusCode;
use viz_test::TestServer;

let router = Router::new()
1 change: 1 addition & 0 deletions viz-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -19,5 +19,6 @@ viz.workspace = true
sessions = { version = "0.5", features = ["memory"] }
nano-id = "0.3"

http = "0.2"
reqwest = { version = "0.11", features = ["cookies", "json", "multipart"]}
tokio = { workspace = true, features = ["full"] }
1 change: 1 addition & 0 deletions viz-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ use std::{net::SocketAddr, sync::Arc};
use tokio::net::TcpListener;
use viz::{serve, Error, Result, Router, Tree};

pub use http;
pub use nano_id;
pub use sessions;

2 changes: 1 addition & 1 deletion viz/src/serve.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ where
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
Builder::new(TokioExecutor::new())
.serve_connection_with_upgrades(Io::new(stream).into_inner(), Responder::new(tree, addr))
.serve_connection_with_upgrades(Io::new(stream), Responder::new(tree, addr))
.await
.map_err(Into::into)
}

0 comments on commit 71f3d18

Please sign in to comment.