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.

510 lines
16KB

  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://localhost: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. pub struct OwnedMeasurement {
  211. pub key: &'static str,
  212. pub timestamp: Option<i64>,
  213. pub fields: HashMap<&'static str, OwnedValue>,
  214. pub tags: HashMap<&'static str, &'static str>,
  215. pub string_tags: HashMap<&'static str, String>
  216. }
  217. impl OwnedMeasurement {
  218. pub fn new(key: &'static str) -> Self {
  219. OwnedMeasurement {
  220. key,
  221. timestamp: None,
  222. fields: HashMap::new(),
  223. tags: HashMap::new(),
  224. string_tags: HashMap::new()
  225. }
  226. }
  227. pub fn add_tag(mut self, key: &'static str, value: &'static str) -> Self {
  228. self.tags.insert(key, value);
  229. self
  230. }
  231. pub fn add_string_tag(mut self, key: &'static str, value: String) -> Self {
  232. self.string_tags.insert(key, value);
  233. self
  234. }
  235. pub fn add_field(mut self, key: &'static str, value: OwnedValue) -> Self {
  236. self.fields.insert(key, value);
  237. self
  238. }
  239. pub fn set_timestamp(mut self, timestamp: i64) -> Self {
  240. self.timestamp = Some(timestamp);
  241. self
  242. }
  243. }
  244. pub fn dur_nanos(d: ::std::time::Duration) -> i64 {
  245. (d.as_secs() * 1_000_000_000_u64 + (d.subsec_nanos() as u64)) as i64
  246. }
  247. //pub fn now() -> i64 { ::latency::dt_nanos(Utc::now()) }
  248. /// exactly like `writer`, but also returns a `Sender<Measurement>` and accepts
  249. /// incoming `Measurement`s that way *in addition* to the old socket/`String`
  250. /// method
  251. ///
  252. pub fn writer_str_or_meas(log_path: &str, warnings: Sender<Warning>) -> (thread::JoinHandle<()>, Sender<OwnedMeasurement>) {
  253. let (tx, rx) = channel();
  254. let logger = file_logger(log_path, Severity::Info);
  255. let thread = thread::spawn(move || {
  256. info!(logger, "initializing zmq");
  257. let _ = fs::create_dir("/tmp/mm");
  258. let ctx = zmq::Context::new();
  259. let socket = pull(&ctx).expect("influx::writer failed to create pull socket");
  260. info!(logger, "initializing url";
  261. "DB_HOST" => DB_HOST,
  262. "DB_NAME" => DB_NAME);
  263. let url = Url::parse_with_params(DB_HOST, &[("db", DB_NAME), ("precision", "ns")]).expect("influx writer url should parse");
  264. let client = Client::new();
  265. info!(logger, "initializing buffers");
  266. let mut meas_buf = String::with_capacity(4096);
  267. let mut buf = String::with_capacity(4096);
  268. let mut server_resp = String::with_capacity(4096);
  269. let mut count = 0;
  270. let next = |prev: u8, s: &str, buf: &mut String| -> u8 {
  271. debug!(logger, "appending serialized measurement to buffer";
  272. "prev" => prev,
  273. "buf.len()" => buf.len());
  274. match prev {
  275. 0 => {
  276. buf.push_str(s);
  277. 1
  278. }
  279. n @ 1...80 => {
  280. buf.push_str("\n");
  281. buf.push_str(s);
  282. n + 1
  283. }
  284. _ => {
  285. buf.push_str("\n");
  286. buf.push_str(s);
  287. debug!(logger, "sending buffer to influx";
  288. "buf.len()" => buf.len());
  289. let resp = client.post(url.clone())
  290. .body(buf.as_str())
  291. .send();
  292. match resp {
  293. Ok(Response { status, .. }) if status == StatusCode::NoContent => {
  294. debug!(logger, "server responded ok: 204 NoContent");
  295. }
  296. Ok(mut resp) => {
  297. let mut server_resp = String::with_capacity(1024);
  298. //server_resp.push_str(&format!("sent at {}:\n", Utc::now()));
  299. //server_resp.push_str(&buf);
  300. //server_resp.push_str("\nreceived:\n");
  301. resp.read_to_string(&mut server_resp); //.unwrap_or(0);
  302. error!(logger, "influx server error";
  303. "status" => resp.status.to_string(),
  304. "body" => server_resp);
  305. }
  306. Err(why) => {
  307. error!(logger, "http request failed: {:?}", why);
  308. // warnings.send(
  309. // Warning::Error(
  310. // format!("Influx write error: {}", why)));
  311. }
  312. }
  313. buf.clear();
  314. 0
  315. }
  316. }
  317. };
  318. let mut rcvd_msg = false;
  319. loop {
  320. rcvd_msg = false;
  321. rx.try_recv()
  322. .map(|meas| {
  323. debug!(logger, "rcvd new OwnedMeasurement";
  324. "count" => count);
  325. serialize_owned(&meas, &mut meas_buf);
  326. count = next(count, &meas_buf, &mut buf);
  327. meas_buf.clear();
  328. rcvd_msg = true;
  329. });
  330. socket.recv_bytes(zmq::DONTWAIT).ok()
  331. .and_then(|bytes| {
  332. String::from_utf8(bytes).ok()
  333. }).map(|s| {
  334. debug!(logger, "rcvd new serialized";
  335. "count" => count);
  336. count = next(count, &s, &mut buf);
  337. rcvd_msg = true;
  338. });
  339. if !rcvd_msg {
  340. #[cfg(feature = "no-thrash")]
  341. thread::sleep(Duration::from_millis(1) / 10);
  342. }
  343. }
  344. crit!(logger, "goodbye");
  345. });
  346. (thread, tx)
  347. }
  348. mod tests {
  349. use super::*;
  350. #[test]
  351. fn it_spawns_a_writer_thread_and_sends_dummy_measurement_to_influxdb() {
  352. let ctx = zmq::Context::new();
  353. let socket = push(&ctx).unwrap();
  354. let (tx, rx) = channel();
  355. let w = writer(tx.clone());
  356. let mut buf = String::with_capacity(4096);
  357. let mut meas = Measurement::new("rust_test");
  358. meas.add_tag("a", "t");
  359. meas.add_field("c", Value::Float(1.23456));
  360. let now = now();
  361. meas.set_timestamp(now);
  362. serialize(&meas, &mut buf);
  363. socket.send_str(&buf, 0);
  364. drop(w);
  365. }
  366. #[test]
  367. fn it_serializes_a_measurement_in_place() {
  368. let mut buf = String::with_capacity(4096);
  369. let mut meas = Measurement::new("rust_test");
  370. meas.add_tag("a", "b");
  371. meas.add_field("c", Value::Float(1.0));
  372. let now = now();
  373. meas.set_timestamp(now);
  374. serialize(&meas, &mut buf);
  375. let ans = format!("rust_test,a=b c=1 {}", now);
  376. assert_eq!(buf, ans);
  377. }
  378. #[test]
  379. fn it_serializes_a_hard_to_serialize_message() {
  380. let raw = r#"error encountered trying to send krkn order: Other("Failed to send http request: Other("Resource temporarily unavailable (os error 11)")")"#;
  381. let mut buf = String::new();
  382. let mut server_resp = String::new();
  383. let mut m = Measurement::new("rust_test");
  384. m.add_field("s", Value::String(&raw));
  385. let now = now();
  386. m.set_timestamp(now);
  387. serialize(&m, &mut buf);
  388. println!("{}", buf);
  389. buf.push_str("\n");
  390. let buf_copy = buf.clone();
  391. buf.push_str(&buf_copy);
  392. println!("{}", buf);
  393. let url = Url::parse_with_params(DB_HOST, &[("db", DB_NAME), ("precision", "ns")]).expect("influx writer url should parse");
  394. let client = Client::new();
  395. match client.post(url.clone())
  396. .body(&buf)
  397. .send() {
  398. Ok(Response { status, .. }) if status == StatusCode::NoContent => {}
  399. Ok(mut resp) => {
  400. resp.read_to_string(&mut server_resp); //.unwrap_or(0);
  401. panic!("{}", server_resp);
  402. }
  403. Err(why) => {
  404. panic!(why)
  405. }
  406. }
  407. }
  408. #[test]
  409. fn it_serializes_a_hard_to_serialize_message_from_owned() {
  410. let raw = r#"error encountered trying to send krkn order: Other("Failed to send http request: Other("Resource temporarily unavailable (os error 11)")")"#;
  411. let mut buf = String::new();
  412. let mut server_resp = String::new();
  413. let mut m = OwnedMeasurement::new("rust_test")
  414. .add_field("s", OwnedValue::String(raw.to_string()))
  415. .set_timestamp(now());
  416. serialize_owned(&m, &mut buf);
  417. println!("{}", buf);
  418. buf.push_str("\n");
  419. let buf_copy = buf.clone();
  420. buf.push_str(&buf_copy);
  421. println!("{}", buf);
  422. let url = Url::parse_with_params(DB_HOST, &[("db", DB_NAME), ("precision", "ns")]).expect("influx writer url should parse");
  423. let client = Client::new();
  424. match client.post(url.clone())
  425. .body(&buf)
  426. .send() {
  427. Ok(Response { status, .. }) if status == StatusCode::NoContent => {}
  428. Ok(mut resp) => {
  429. resp.read_to_string(&mut server_resp); //.unwrap_or(0);
  430. panic!("{}", server_resp);
  431. }
  432. Err(why) => {
  433. panic!(why)
  434. }
  435. }
  436. }
  437. }