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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::time::Duration;
use std::time::Instant;
use reqwest::blocking::Client;
use reqwest::blocking::Response;
use reqwest::header::CONTENT_TYPE;
use reqwest::header::HeaderMap;
use thrift::protocol::TBinaryOutputProtocol;
use thrift::protocol::TListIdentifier;
use thrift::protocol::TOutputProtocol;
use thrift::protocol::TType;
use thrift::transport::TBufferedWriteTransport;
use opentracingrust::FinishedSpan;
use super::super::thrift_gen::zipkin_core::Endpoint;
use super::super::thrift_gen::zipkin_core::Span;
use super::super::Result;
use super::thrift_encode;
pub struct HttpCollector {
endpoint: Endpoint,
flush_count: usize,
flush_last: Instant,
flush_timeout: Duration,
headers: HeaderMap,
spans: Vec<Span>,
target_url: String,
}
impl HttpCollector {
pub fn new(opts: HttpCollectorOpts) -> HttpCollector {
let target_url = opts.target_url();
HttpCollector {
endpoint: opts.endpoint,
flush_count: opts.flush_count,
flush_last: Instant::now(),
flush_timeout: opts.flush_timeout,
headers: opts.headers,
spans: Vec::with_capacity(opts.flush_count),
target_url,
}
}
pub fn collect(&mut self, span: FinishedSpan) {
let span = thrift_encode(&span, &self.endpoint);
self.spans.push(span);
}
pub fn flush(&mut self) -> Result<Option<Response>> {
self.flush_last = Instant::now();
if self.spans.is_empty() {
return Ok(None);
}
let mut spans = Vec::new();
spans.append(&mut self.spans);
let payload = self.encode(spans)?;
let response = Client::new()
.post(&self.target_url)
.headers(self.headers.clone())
.header(CONTENT_TYPE, "application/x-thrift")
.body(payload)
.send()?;
Ok(Some(response))
}
pub fn lazy_flush(&mut self) -> Result<Option<Response>> {
let flush = self.spans.len() > self.flush_count;
let flush = flush || self.flush_last.elapsed() > self.flush_timeout;
if flush {
return self.flush();
}
Ok(None)
}
fn encode(&mut self, spans: Vec<Span>) -> Result<Vec<u8>> {
let mut buffer: Vec<u8> = Vec::new();
let len: i32 = spans.len() as i32;
{
let transport = TBufferedWriteTransport::new(&mut buffer);
let mut protocol = TBinaryOutputProtocol::new(transport, true);
protocol.write_list_begin(&TListIdentifier::new(TType::Struct, len))?;
for span in spans {
span.write_to_out_protocol(&mut protocol)?;
}
protocol.write_list_end()?;
protocol.flush()?;
}
Ok(buffer)
}
}
impl Drop for HttpCollector {
fn drop(&mut self) {
if let Err(error) = self.flush() {
if !::std::thread::panicking() {
panic!("Error flushing spans: {:?}", error);
}
}
}
}
pub struct HttpCollectorOpts<'a> {
endpoint: Endpoint,
flush_count: usize,
flush_timeout: Duration,
headers: HeaderMap,
target: &'a str,
}
impl<'a> HttpCollectorOpts<'a> {
pub fn new<S>(url: S, endpoint: Endpoint) -> HttpCollectorOpts<'a>
where
S: Into<&'a str>,
{
HttpCollectorOpts {
endpoint,
flush_count: 1000,
flush_timeout: Duration::from_secs(1),
headers: HeaderMap::new(),
target: url.into(),
}
}
pub fn flush_count(mut self, count: usize) -> HttpCollectorOpts<'a> {
self.flush_count = count;
self
}
pub fn flush_timeout(mut self, timeout: Duration) -> HttpCollectorOpts<'a> {
self.flush_timeout = timeout;
self
}
pub fn headers(mut self, headers: HeaderMap) -> HttpCollectorOpts<'a> {
self.headers = headers;
self
}
fn target_url(&self) -> String {
format!("{}/api/v1/spans", self.target)
}
}