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.

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