Skip to content

Commit b70a849

Browse files
committed
Add general shape of timing APIs
1 parent 1cb7c79 commit b70a849

File tree

3 files changed

+92
-55
lines changed

3 files changed

+92
-55
lines changed

src/trace/mod.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
1-
//! Extract and inject [trace context](https://w3c.github.io/trace-context/) headers.
1+
//! HTTP timings and traces.
22
//!
3-
//! ## Examples
3+
//! This module implements parsers and serializers for timing-related headers.
4+
//! These headers enable tracing and timing requests, and help answer the
5+
//! question of: _"Where is my program spending its time?"_
46
//!
5-
//! ```
6-
//! use http_types::trace::TraceContext;
7+
//! # Specifications
78
//!
8-
//! let mut res = http_types::Response::new(200);
9-
//!
10-
//! res.insert_header(
11-
//! "traceparent",
12-
//! "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01"
13-
//! );
14-
//!
15-
//! let context = TraceContext::extract(&res).unwrap();
16-
//!
17-
//! let trace_id = u128::from_str_radix("0af7651916cd43dd8448eb211c80319c", 16);
18-
//! let parent_id = u64::from_str_radix("00f067aa0ba902b7", 16);
19-
//!
20-
//! assert_eq!(context.trace_id(), trace_id.unwrap());
21-
//! assert_eq!(context.parent_id(), parent_id.ok());
22-
//! assert_eq!(context.sampled(), true);
23-
//! ```
9+
//! - [W3C Trace-Context headers](https://w3c.github.io/trace-context/)
10+
//! - [W3C Server-Timing headers](https://w3c.github.io/server-timing/#the-server-timing-header-field)
2411
2512
mod server_timing;
2613
mod trace_context;
2714

28-
pub use server_timing::ServerTiming;
15+
pub use server_timing::{ServerTiming, TimingEntry};
2916
pub use trace_context::TraceContext;

src/trace/server_timing.rs

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,71 @@
1-
/// Communicate one or more metrics and descriptions for the given request-response cycle.
2-
///
3-
/// This is an implementation of the W3C [Server
4-
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
5-
/// header spec. Read more on
6-
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing)
7-
pub struct ServerTiming;
1+
use std::time::Duration;
82

3+
/// An individual [`ServerTiming`] entry.
4+
//
5+
// # Implementation notes
6+
//
97
// Four different cases are valid:
108
//
119
// 1. metric name only cache
12-
// 2. metric + metric cache;dur=2.4
13-
// 3. metric + description cache;desc="Cache Read"
10+
// 2. metric + value cache;dur=2.4
11+
// 3. metric + desc cache;desc="Cache Read"
1412
// 4. metric + value + desc cache;desc="Cache Read";dur=23.2
1513
//
1614
// Multiple different entries per line are supported; separated with a `,`.
15+
#[derive(Debug)]
16+
pub struct TimingEntry {
17+
name: String,
18+
dur: Option<Duration>,
19+
desc: Option<String>,
20+
}
21+
22+
impl TimingEntry {
23+
/// Create a new instance of `TimingEntry`.
24+
pub fn new(name: String, dur: Option<Duration>, desc: Option<String>) -> Self {
25+
Self { name, dur, desc }
26+
}
27+
28+
/// The timing name.
29+
pub fn name(&self) -> &String {
30+
&self.name
31+
}
32+
33+
/// Set the timing name.
34+
pub fn set_name(&mut self, name: String) {
35+
self.name = name;
36+
}
37+
38+
/// The timing duration.
39+
pub fn duration(&self) -> Option<Duration> {
40+
self.dur
41+
}
42+
43+
/// Set the timing name.
44+
pub fn set_duration(&mut self, dur: Option<Duration>) {
45+
self.dur = dur;
46+
}
47+
48+
/// The timing description.
49+
pub fn description(&self) -> Option<&String> {
50+
self.desc.as_ref()
51+
}
52+
53+
/// Set the timing description.
54+
pub fn set_description(&mut self, desc: Option<String>) {
55+
self.desc = desc;
56+
}
57+
}
58+
59+
/// Communicate one or more metrics and descriptions for the given request-response cycle.
60+
///
61+
/// This is an implementation of the W3C [Server
62+
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
63+
/// header spec. Read more on
64+
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing).
65+
#[derive(Debug)]
66+
pub struct ServerTiming {
67+
timings: Vec<TimingEntry>,
68+
}
1769

1870
mod test {
1971
const CASE1: &str =

src/trace/trace_context.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
//! Extract and inject [trace context](https://w3c.github.io/trace-context/) headers.
2-
//!
3-
//! ## Examples
4-
//!
5-
//! ```
6-
//! use http_types::trace::TraceContext;
7-
//!
8-
//! let mut res = http_types::Response::new(200);
9-
//!
10-
//! res.insert_header(
11-
//! "traceparent",
12-
//! "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01"
13-
//! );
14-
//!
15-
//! let context = TraceContext::extract(&res).unwrap();
16-
//!
17-
//! let trace_id = u128::from_str_radix("0af7651916cd43dd8448eb211c80319c", 16);
18-
//! let parent_id = u64::from_str_radix("00f067aa0ba902b7", 16);
19-
//!
20-
//! assert_eq!(context.trace_id(), trace_id.unwrap());
21-
//! assert_eq!(context.parent_id(), parent_id.ok());
22-
//! assert_eq!(context.sampled(), true);
23-
//! ```
24-
251
use rand::Rng;
262
use std::fmt;
273

284
use crate::Headers;
295

30-
/// A TraceContext object
6+
/// Extract and inject [Trace-Context](https://w3c.github.io/trace-context/) headers.
7+
///
8+
/// ## Examples
9+
///
10+
/// ```
11+
/// use http_types::trace::TraceContext;
12+
///
13+
/// let mut res = http_types::Response::new(200);
14+
///
15+
/// res.insert_header(
16+
/// "traceparent",
17+
/// "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01"
18+
/// );
19+
///
20+
/// let context = TraceContext::extract(&res).unwrap();
21+
///
22+
/// let trace_id = u128::from_str_radix("0af7651916cd43dd8448eb211c80319c", 16);
23+
/// let parent_id = u64::from_str_radix("00f067aa0ba902b7", 16);
24+
///
25+
/// assert_eq!(context.trace_id(), trace_id.unwrap());
26+
/// assert_eq!(context.parent_id(), parent_id.ok());
27+
/// assert_eq!(context.sampled(), true);
28+
/// ```
3129
#[derive(Debug)]
3230
pub struct TraceContext {
3331
id: u64,

0 commit comments

Comments
 (0)