1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::io;
use std::marker::PhantomData;
use hyper;
use hyper::{Request, Response, Error};
use super::{Push, XSpanIdString};
#[derive(Debug)]
pub struct AddContext<T, C>
where
C: Default + Push<XSpanIdString>,
{
inner: T,
marker: PhantomData<C>,
}
impl<T, C> AddContext<T, C>
where
C: Default + Push<XSpanIdString>,
{
pub fn new(inner: T) -> Self {
AddContext {
inner,
marker: PhantomData,
}
}
}
impl<T, C> hyper::server::NewService for AddContext<T, C>
where
C: Default + Push<XSpanIdString>,
T: hyper::server::NewService<Request=(Request, C::Result), Response=Response, Error=Error>,
{
type Request = Request;
type Response = Response;
type Error = Error;
type Instance = AddContext<T::Instance, C>;
fn new_service(&self) -> Result<Self::Instance, io::Error> {
self.inner.new_service().map(AddContext::new)
}
}
impl<T, C> hyper::server::Service for AddContext<T, C>
where
C: Default + Push<XSpanIdString>,
T: hyper::server::Service<
Request = (Request,
C::Result),
Response = Response,
Error = Error,
>,
{
type Request = Request;
type Response = Response;
type Error = Error;
type Future = T::Future;
fn call(&self, req: Self::Request) -> Self::Future {
let x_span_id = XSpanIdString::get_or_generate(&req);
let context = C::default().push(x_span_id);
self.inner.call((req, context))
}
}