You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

549 lines
17KB

  1. //! Utilities to efficiently send data to influx
  2. //!
  3. use std::iter::FromIterator;
  4. use std::io::{Write, Read};
  5. use std::sync::mpsc::{Sender, Receiver, channel};
  6. use std::thread;
  7. use std::collections::HashMap;
  8. use std::fs::{self, OpenOptions};
  9. use std::time::Duration;
  10. use hyper::status::StatusCode;
  11. use hyper::client::response::Response;
  12. use hyper::Url;
  13. use hyper::client::Client;
  14. use influent::measurement::{Measurement, Value};
  15. use zmq;
  16. use chrono::{DateTime, Utc, TimeZone};
  17. use sloggers::types::Severity;
  18. use super::{nanos, file_logger};
  19. use warnings::Warning;
  20. const WRITER_ADDR: &'static str = "ipc:///tmp/mm/influx";
  21. //const WRITER_ADDR: &'static str = "tcp://127.0.0.1:17853";
  22. const DB_NAME: &'static str = "mm";
  23. const DB_HOST: &'static str = "http://washington.0ptimus.internal:8086/write";
  24. //const DB_HOST: &'static str = "http://harrison.0ptimus.internal:8086/write";
  25. const ZMQ_RCV_HWM: i32 = 0;
  26. const ZMQ_SND_HWM: i32 = 0;
  27. pub fn pull(ctx: &zmq::Context) -> Result<zmq::Socket, zmq::Error> {
  28. let socket = ctx.socket(zmq::PULL)?;
  29. socket.bind(WRITER_ADDR)?;
  30. socket.set_rcvhwm(ZMQ_RCV_HWM)?;
  31. Ok(socket)
  32. }
  33. pub fn push(ctx: &zmq::Context) -> Result<zmq::Socket, zmq::Error> {
  34. let socket = ctx.socket(zmq::PUSH)?;
  35. socket.connect(WRITER_ADDR)?;
  36. socket.set_sndhwm(ZMQ_SND_HWM)?;
  37. Ok(socket)
  38. }
  39. fn escape(s: &str) -> String {
  40. s.replace(" ", "\\ ")
  41. .replace(",", "\\,")
  42. }
  43. fn as_string(s: &str) -> String {
  44. // the second replace removes double escapes
  45. //
  46. format!("\"{}\"", s.replace("\"", "\\\"")
  47. .replace(r#"\\""#, r#"\""#))
  48. }
  49. #[test]
  50. fn it_checks_as_string_does_not_double_escape() {
  51. let raw = "this is \\\"an escaped string\\\" so it's problematic";
  52. let escaped = as_string(&raw);
  53. assert_eq!(escaped, format!("\"{}\"", raw).as_ref());
  54. }
  55. fn as_integer(i: &i64) -> String {
  56. format!("{}i", i)
  57. }
  58. fn as_float(f: &f64) -> String {
  59. f.to_string()
  60. }
  61. fn as_boolean(b: &bool) -> &str {
  62. if *b { "t" } else { "f" }
  63. }
  64. pub fn now() -> i64 {
  65. nanos(Utc::now()) as i64
  66. }
  67. /// Serialize the measurement into influx line protocol
  68. /// and append to the buffer.
  69. ///
  70. /// # Examples
  71. ///
  72. /// ```
  73. /// extern crate influent;
  74. /// extern crate logging;
  75. ///
  76. /// use influent::measurement::{Measurement, Value};
  77. /// use std::string::String;
  78. /// use logging::influx::serialize;
  79. ///
  80. /// fn main() {
  81. /// let mut buf = String::new();
  82. /// let mut m = Measurement::new("test");
  83. /// m.add_field("x", Value::Integer(1));
  84. /// serialize(&m, &mut buf);
  85. /// }
  86. ///
  87. /// ```
  88. ///
  89. pub fn serialize(measurement: &Measurement, line: &mut String) {
  90. line.push_str(&escape(measurement.key));
  91. for (tag, value) in measurement.tags.iter() {
  92. line.push_str(",");
  93. line.push_str(&escape(tag));
  94. line.push_str("=");
  95. line.push_str(&escape(value));
  96. }
  97. let mut was_spaced = false;
  98. for (field, value) in measurement.fields.iter() {
  99. line.push_str({if !was_spaced { was_spaced = true; " " } else { "," }});
  100. line.push_str(&escape(field));
  101. line.push_str("=");
  102. match value {
  103. &Value::String(ref s) => line.push_str(&as_string(s)),
  104. &Value::Integer(ref i) => line.push_str(&as_integer(i)),
  105. &Value::Float(ref f) => line.push_str(&as_float(f)),
  106. &Value::Boolean(ref b) => line.push_str(as_boolean(b))
  107. };
  108. }
  109. match measurement.timestamp {
  110. Some(t) => {
  111. line.push_str(" ");
  112. line.push_str(&t.to_string());
  113. }
  114. _ => {}
  115. }
  116. }
  117. pub fn serialize_owned(measurement: &OwnedMeasurement, line: &mut String) {
  118. line.push_str(&escape(measurement.key));
  119. let add_tag = |line: &mut String, key: &str, value: &str| {
  120. line.push_str(",");
  121. line.push_str(&escape(key));
  122. line.push_str("=");
  123. line.push_str(&escape(value));
  124. };
  125. for (key, value) in measurement.tags.iter() {
  126. add_tag(line, key, value);
  127. }
  128. for (key, value) in measurement.string_tags.iter() {
  129. add_tag(line, key, value);
  130. }
  131. let mut was_spaced = false;
  132. for (field, value) in measurement.fields.iter() {
  133. line.push_str({if !was_spaced { was_spaced = true; " " } else { "," }});
  134. line.push_str(&escape(field));
  135. line.push_str("=");
  136. match value {
  137. &OwnedValue::String(ref s) => line.push_str(&as_string(s)),
  138. &OwnedValue::Integer(ref i) => line.push_str(&as_integer(i)),
  139. &OwnedValue::Float(ref f) => line.push_str(&as_float(f)),
  140. &OwnedValue::Boolean(ref b) => line.push_str(as_boolean(b))
  141. };
  142. }
  143. match measurement.timestamp {
  144. Some(t) => {
  145. line.push_str(" ");
  146. line.push_str(&t.to_string());
  147. }
  148. _ => {}
  149. }
  150. }
  151. pub fn writer(warnings: Sender<Warning>) -> thread::JoinHandle<()> {
  152. thread::spawn(move || {
  153. let _ = fs::create_dir("/tmp/mm");
  154. let ctx = zmq::Context::new();
  155. let socket = pull(&ctx).expect("influx::writer failed to create pull socket");
  156. let url = Url::parse_with_params(DB_HOST, &[("db", DB_NAME), ("precision", "ns")]).expect("influx writer url should parse");
  157. let client = Client::new();
  158. let mut buf = String::with_capacity(4096);
  159. let mut server_resp = String::with_capacity(4096);
  160. let mut count = 0;
  161. loop {
  162. if let Ok(bytes) = socket.recv_bytes(0) {
  163. if let Ok(msg) = String::from_utf8(bytes) {
  164. count = match count {
  165. 0 => {
  166. buf.push_str(&msg);
  167. 1
  168. }
  169. n @ 1...40 => {
  170. buf.push_str("\n");
  171. buf.push_str(&msg);
  172. n + 1
  173. }
  174. _ => {
  175. buf.push_str("\n");
  176. buf.push_str(&msg);
  177. match client.post(url.clone())
  178. .body(&buf)
  179. .send() {
  180. Ok(Response { status, .. }) if status == StatusCode::NoContent => {}
  181. Ok(mut resp) => {
  182. resp.read_to_string(&mut server_resp); //.unwrap_or(0);
  183. warnings.send(
  184. Warning::Error(
  185. format!("Influx server: {}", server_resp)));
  186. server_resp.clear();
  187. }
  188. Err(why) => {
  189. warnings.send(
  190. Warning::Error(
  191. format!("Influx write error: {}", why)));
  192. }
  193. }
  194. buf.clear();
  195. 0
  196. }
  197. }
  198. }
  199. }
  200. }
  201. })
  202. }
  203. #[derive(Debug, Clone, PartialEq)]
  204. pub enum OwnedValue {
  205. String(String),
  206. Float(f64),
  207. Integer(i64),
  208. Boolean(bool)
  209. }
  210. #[derive(Clone)]
  211. pub struct OwnedMeasurement {
  212. pub key: &'static str,
  213. pub timestamp: Option<i64>,
  214. pub fields: HashMap<&'static str, OwnedValue>,
  215. pub tags: HashMap<&'static str, &'static str>,
  216. pub string_tags: HashMap<&'static str, String>
  217. }
  218. impl OwnedMeasurement {
  219. pub fn new(key: &'static str) -> Self {
  220. OwnedMeasurement {
  221. key,
  222. timestamp: None,
  223. fields: HashMap::new(),
  224. tags: HashMap::new(),
  225. string_tags: HashMap::new()
  226. }
  227. }
  228. pub fn add_tag(mut self, key: &'static str, value: &'static str) -> Self {
  229. self.tags.insert(key, value);
  230. self
  231. }
  232. pub fn add_string_tag(mut self, key: &'static str, value: String) -> Self {
  233. self.string_tags.insert(key, value);
  234. self
  235. }
  236. pub fn add_field(mut self, key: &'static str, value: OwnedValue) -> Self {
  237. self.fields.insert(key, value);
  238. self
  239. }
  240. pub fn set_timestamp(mut self, timestamp: i64) -> Self {
  241. self.timestamp = Some(timestamp);
  242. self
  243. }
  244. pub fn set_tag(mut self, key: &'static str, value: &'static str) -> Self {
  245. *self.tags.entry(key).or_insert(value) = value;
  246. self
  247. }
  248. }
  249. pub fn dur_nanos(d: ::std::time::Duration) -> i64 {
  250. (d.as_secs() * 1_000_000_000_u64 + (d.subsec_nanos() as u64)) as i64
  251. }
  252. //pub fn now() -> i64 { ::latency::dt_nanos(Utc::now()) }
  253. /// exactly like `writer`, but also returns a `Sender<Measurement>` and accepts
  254. /// incoming `Measurement`s that way *in addition* to the old socket/`String`
  255. /// method
  256. ///
  257. pub struct InfluxWriter {
  258. kill_switch: Sender<()>,
  259. thread: Option<thread::JoinHandle<()>>,
  260. }
  261. impl InfluxWriter {
  262. pub fn new(log_path: &str, warnings: Sender<Warning>) -> (Self, Sender<OwnedMeasurement>) {
  263. let (kill_switch, terminate) = channel();
  264. let (tx, rx) = channel();
  265. let logger = file_logger(log_path, Severity::Info);
  266. let thread = thread::spawn(move || {
  267. info!(logger, "initializing zmq");
  268. let _ = fs::create_dir("/tmp/mm");
  269. let ctx = zmq::Context::new();
  270. let socket = pull(&ctx).expect("influx::writer failed to create pull socket");
  271. info!(logger, "initializing url";
  272. "DB_HOST" => DB_HOST,
  273. "DB_NAME" => DB_NAME);
  274. let url = Url::parse_with_params(DB_HOST, &[("db", DB_NAME), ("precision", "ns")]).expect("influx writer url should parse");
  275. let client = Client::new();
  276. info!(logger, "initializing buffers");
  277. let mut meas_buf = String::with_capacity(4096);
  278. let mut buf = String::with_capacity(4096);
  279. let mut server_resp = String::with_capacity(4096);
  280. let mut count = 0;
  281. let next = |prev: u8, s: &str, buf: &mut String| -> u8 {
  282. debug!(logger, "appending serialized measurement to buffer";
  283. "prev" => prev,
  284. "buf.len()" => buf.len());
  285. match prev {
  286. 0 => {
  287. buf.push_str(s);
  288. 1
  289. }
  290. n @ 1...80 => {
  291. buf.push_str("\n");
  292. buf.push_str(s);
  293. n + 1
  294. }
  295. _ => {
  296. buf.push_str("\n");
  297. if s.len() > 0 {
  298. buf.push_str(s);
  299. }
  300. debug!(logger, "sending buffer to influx";
  301. "buf.len()" => buf.len());
  302. let resp = client.post(url.clone())
  303. .body(buf.as_str())
  304. .send();
  305. match resp {
  306. Ok(Response { status, .. }) if status == StatusCode::NoContent => {
  307. debug!(logger, "server responded ok: 204 NoContent");
  308. }
  309. Ok(mut resp) => {
  310. let mut server_resp = String::with_capacity(1024);
  311. //server_resp.push_str(&format!("sent at {}:\n", Utc::now()));
  312. //server_resp.push_str(&buf);
  313. //server_resp.push_str("\nreceived:\n");
  314. resp.read_to_string(&mut server_resp); //.unwrap_or(0);
  315. error!(logger, "influx server error";
  316. "status" => resp.status.to_string(),
  317. "body" => server_resp);
  318. }
  319. Err(why) => {
  320. error!(logger, "http request failed: {:?}", why);
  321. // warnings.send(
  322. // Warning::Error(
  323. // format!("Influx write error: {}", why)));
  324. }
  325. }
  326. buf.clear();
  327. 0
  328. }
  329. }
  330. };
  331. let mut rcvd_msg = false;
  332. loop {
  333. rcvd_msg = false;
  334. rx.try_recv()
  335. .map(|meas| {
  336. debug!(logger, "rcvd new OwnedMeasurement";
  337. "count" => count);
  338. serialize_owned(&meas, &mut meas_buf);
  339. count = next(count, &meas_buf, &mut buf);
  340. meas_buf.clear();
  341. rcvd_msg = true;
  342. });
  343. socket.recv_bytes(zmq::DONTWAIT).ok()
  344. .and_then(|bytes| {
  345. String::from_utf8(bytes).ok()
  346. }).map(|s| {
  347. debug!(logger, "rcvd new serialized";
  348. "count" => count);
  349. count = next(count, &s, &mut buf);
  350. rcvd_msg = true;
  351. });
  352. let end = terminate.try_recv()
  353. .map(|_| {
  354. let _ = next(::std::u8::MAX, "", &mut buf);
  355. true
  356. }).unwrap_or(false);
  357. if end { break }
  358. if !rcvd_msg {
  359. #[cfg(feature = "no-thrash")]
  360. thread::sleep(Duration::from_millis(1) / 10);
  361. }
  362. }
  363. crit!(logger, "goodbye");
  364. });
  365. let writer = InfluxWriter {
  366. kill_switch,
  367. thread: Some(thread)
  368. };
  369. (writer, tx)
  370. }
  371. }
  372. impl Drop for InfluxWriter {
  373. fn drop(&mut self) {
  374. self.kill_switch.send(());
  375. if let Some(thread) = self.thread.take() {
  376. let _ = thread.join();
  377. }
  378. }
  379. }
  380. mod tests {
  381. use super::*;
  382. #[test]
  383. fn it_spawns_a_writer_thread_and_sends_dummy_measurement_to_influxdb() {
  384. let ctx = zmq::Context::new();
  385. let socket = push(&ctx).unwrap();
  386. let (tx, rx) = channel();
  387. let w = writer(tx.clone());
  388. let mut buf = String::with_capacity(4096);
  389. let mut meas = Measurement::new("rust_test");
  390. meas.add_tag("a", "t");
  391. meas.add_field("c", Value::Float(1.23456));
  392. let now = now();
  393. meas.set_timestamp(now);
  394. serialize(&meas, &mut buf);
  395. socket.send_str(&buf, 0);
  396. drop(w);
  397. }
  398. #[test]
  399. fn it_serializes_a_measurement_in_place() {
  400. let mut buf = String::with_capacity(4096);
  401. let mut meas = Measurement::new("rust_test");
  402. meas.add_tag("a", "b");
  403. meas.add_field("c", Value::Float(1.0));
  404. let now = now();
  405. meas.set_timestamp(now);
  406. serialize(&meas, &mut buf);
  407. let ans = format!("rust_test,a=b c=1 {}", now);
  408. assert_eq!(buf, ans);
  409. }
  410. #[test]
  411. fn it_serializes_a_hard_to_serialize_message() {
  412. let raw = r#"error encountered trying to send krkn order: Other("Failed to send http request: Other("Resource temporarily unavailable (os error 11)")")"#;
  413. let mut buf = String::new();
  414. let mut server_resp = String::new();
  415. let mut m = Measurement::new("rust_test");
  416. m.add_field("s", Value::String(&raw));
  417. let now = now();
  418. m.set_timestamp(now);
  419. serialize(&m, &mut buf);
  420. println!("{}", buf);
  421. buf.push_str("\n");
  422. let buf_copy = buf.clone();
  423. buf.push_str(&buf_copy);
  424. println!("{}", buf);
  425. let url = Url::parse_with_params(DB_HOST, &[("db", DB_NAME), ("precision", "ns")]).expect("influx writer url should parse");
  426. let client = Client::new();
  427. match client.post(url.clone())
  428. .body(&buf)
  429. .send() {
  430. Ok(Response { status, .. }) if status == StatusCode::NoContent => {}
  431. Ok(mut resp) => {
  432. resp.read_to_string(&mut server_resp); //.unwrap_or(0);
  433. panic!("{}", server_resp);
  434. }
  435. Err(why) => {
  436. panic!(why)
  437. }
  438. }
  439. }
  440. #[test]
  441. fn it_serializes_a_hard_to_serialize_message_from_owned() {
  442. let raw = r#"error encountered trying to send krkn order: Other("Failed to send http request: Other("Resource temporarily unavailable (os error 11)")")"#;
  443. let mut buf = String::new();
  444. let mut server_resp = String::new();
  445. let mut m = OwnedMeasurement::new("rust_test")
  446. .add_field("s", OwnedValue::String(raw.to_string()))
  447. .set_timestamp(now());
  448. serialize_owned(&m, &mut buf);
  449. println!("{}", buf);
  450. buf.push_str("\n");
  451. let buf_copy = buf.clone();
  452. buf.push_str(&buf_copy);
  453. println!("{}", buf);
  454. let url = Url::parse_with_params(DB_HOST, &[("db", DB_NAME), ("precision", "ns")]).expect("influx writer url should parse");
  455. let client = Client::new();
  456. match client.post(url.clone())
  457. .body(&buf)
  458. .send() {
  459. Ok(Response { status, .. }) if status == StatusCode::NoContent => {}
  460. Ok(mut resp) => {
  461. resp.read_to_string(&mut server_resp); //.unwrap_or(0);
  462. panic!("{}", server_resp);
  463. }
  464. Err(why) => {
  465. panic!(why)
  466. }
  467. }
  468. }
  469. }