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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*! Transmission handles the transmission of events to Honeycomb

*/
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use crossbeam_channel::{
    bounded, Receiver as ChannelReceiver, RecvTimeoutError, Sender as ChannelSender,
};

use log::{error, info, trace};
use parking_lot::Mutex;
use reqwest::{header, StatusCode};
use tokio::runtime::{Builder, Runtime};

use crate::errors::{Error, Result};
use crate::event::Event;
use crate::eventdata::EventData;
use crate::events::{Events, EventsResponse};
use crate::response::{HoneyResponse, Response};
use crate::sender::Sender;

const BATCH_ENDPOINT: &str = "/1/batch/";

const DEFAULT_NAME_PREFIX: &str = "libhoney-rust";
// DEFAULT_MAX_BATCH_SIZE how many events to collect in a batch
const DEFAULT_MAX_BATCH_SIZE: usize = 50;
// DEFAULT_MAX_CONCURRENT_BATCHES how many batches to maintain in parallel
const DEFAULT_MAX_CONCURRENT_BATCHES: usize = 10;
// DEFAULT_BATCH_TIMEOUT how frequently to send unfilled batches
const DEFAULT_BATCH_TIMEOUT: Duration = Duration::from_millis(100);
// DEFAULT_PENDING_WORK_CAPACITY how many events to queue up for busy batches
const DEFAULT_PENDING_WORK_CAPACITY: usize = 10_000;
// DEFAULT_SEND_TIMEOUT how much to wait to send an event
const DEFAULT_SEND_TIMEOUT: Duration = Duration::from_millis(1_000);

/// Options includes various options to tweak the behavious of the sender.
#[derive(Debug, Clone)]
pub struct Options {
    /// how many events to collect into a batch before sending. Overrides
    /// DEFAULT_MAX_BATCH_SIZE.
    pub max_batch_size: usize,

    /// how many batches can be inflight simultaneously. Overrides
    /// DEFAULT_MAX_CONCURRENT_BATCHES.
    pub max_concurrent_batches: usize,

    /// how often to send off batches. Overrides DEFAULT_BATCH_TIMEOUT.
    pub batch_timeout: Duration,

    /// how many events to allow to pile up. Overrides DEFAULT_PENDING_WORK_CAPACITY
    pub pending_work_capacity: usize,

    /// user_agent_addition is an option that allows you to augment the "User-Agent"
    /// header that libhoney sends along with each event.  The default User-Agent is
    /// "libhoney-go/<version>". If you set this variable, its contents will be appended
    /// to the User-Agent string, separated by a space. The expected format is
    /// product-name/version, eg "myapp/1.0"
    pub user_agent_addition: Option<String>,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            max_batch_size: DEFAULT_MAX_BATCH_SIZE,
            max_concurrent_batches: DEFAULT_MAX_CONCURRENT_BATCHES,
            batch_timeout: DEFAULT_BATCH_TIMEOUT,
            pending_work_capacity: DEFAULT_PENDING_WORK_CAPACITY,
            user_agent_addition: None,
        }
    }
}

/// `Transmission` handles collecting and sending individual events to Honeycomb
#[derive(Debug, Clone)]
pub struct Transmission {
    pub(crate) options: Options,
    user_agent: String,

    runtime: Arc<Mutex<Runtime>>,
    http_client: reqwest::Client,

    work_sender: ChannelSender<Event>,
    work_receiver: ChannelReceiver<Event>,
    response_sender: ChannelSender<Response>,
    response_receiver: ChannelReceiver<Response>,
}

impl Drop for Transmission {
    fn drop(&mut self) {
        self.stop().unwrap();
    }
}

impl Sender for Transmission {
    fn start(&mut self) {
        let work_receiver = self.work_receiver.clone();
        let response_sender = self.response_sender.clone();
        let options = self.options.clone();
        let user_agent = self.user_agent.clone();
        let http_client = self.http_client.clone();

        info!("transmission starting");
        // thread that processes all the work received
        let runtime = self.runtime.clone();
        runtime.lock().spawn(async {
            Self::process_work(
                work_receiver,
                response_sender,
                options,
                user_agent,
                http_client,
            )
            .await
        });
    }

    fn stop(&mut self) -> Result<()> {
        info!("transmission stopping");
        if self.work_sender.is_full() {
            error!("work sender is full");
            return Err(Error::sender_full("work"));
        }
        Ok(self.work_sender.send(Event::stop_event())?)
    }

    fn send(&mut self, event: Event) {
        let clock = Instant::now();
        if self.work_sender.is_full() {
            error!("work sender is full");
            self.response_sender
                .send(Response {
                    status_code: None,
                    body: None,
                    duration: clock.elapsed(),
                    metadata: event.metadata,
                    error: Some("queue overflow".to_string()),
                })
                .unwrap_or_else(|e| {
                    error!("response dropped, error: {}", e);
                });
        } else {
            let runtime = self.runtime.clone();
            let work_sender = self.work_sender.clone();
            let response_sender = self.response_sender.clone();
            runtime.lock().spawn(async move {
                work_sender
                    .clone()
                    .send_timeout(event.clone(), DEFAULT_SEND_TIMEOUT)
                    .map_err(|e| {
                        response_sender
                            .send(Response {
                                status_code: None,
                                body: None,
                                duration: clock.elapsed(),
                                metadata: event.metadata,
                                error: Some(e.to_string()),
                            })
                            .unwrap_or_else(|e| {
                                error!("response dropped, error: {}", e);
                            });
                    })
            });
        }
    }

    /// responses provides access to the receiver
    fn responses(&self) -> ChannelReceiver<Response> {
        self.response_receiver.clone()
    }
}

impl Transmission {
    fn new_runtime(options: Option<&Options>) -> Result<Runtime> {
        let mut builder = Builder::new();
        if let Some(opts) = options {
            builder.core_threads(opts.max_concurrent_batches);
        };
        Ok(builder
            .thread_name("libhoney-rust")
            .thread_stack_size(3 * 1024 * 1024)
            .threaded_scheduler()
            .enable_io()
            .enable_time()
            .build()?)
    }

    pub(crate) fn new(options: Options) -> Result<Self> {
        let runtime = Self::new_runtime(None)?;

        let (work_sender, work_receiver) = bounded(options.pending_work_capacity * 4);
        let (response_sender, response_receiver) = bounded(options.pending_work_capacity * 4);

        Ok(Self {
            runtime: Arc::new(Mutex::new(runtime)),
            options,
            work_sender,
            work_receiver,
            response_sender,
            response_receiver,
            user_agent: format!("{}/{}", DEFAULT_NAME_PREFIX, env!("CARGO_PKG_VERSION")),
            http_client: reqwest::Client::new(),
        })
    }

    async fn process_work(
        work_receiver: ChannelReceiver<Event>,
        response_sender: ChannelSender<Response>,
        options: Options,
        user_agent: String,
        http_client: reqwest::Client,
    ) {
        let runtime = Self::new_runtime(Some(&options)).expect("Could not start new runtime");
        let mut batches: HashMap<String, Events> = HashMap::new();
        let mut expired = false;

        loop {
            let options = options.clone();

            match work_receiver.recv_timeout(options.batch_timeout) {
                Ok(event) => {
                    if event.fields.contains_key("internal_stop_event") {
                        info!("got 'internal_stop_event' event");
                        break;
                    }
                    let key = format!(
                        "{}_{}_{}",
                        event.options.api_host, event.options.api_key, event.options.dataset
                    );
                    batches
                        .entry(key)
                        .and_modify(|v| v.push(event.clone()))
                        .or_insert({
                            let mut v = Vec::with_capacity(options.max_batch_size);
                            v.push(event);
                            v
                        });
                }
                Err(RecvTimeoutError::Timeout) => {
                    expired = true;
                }
                Err(RecvTimeoutError::Disconnected) => {
                    // TODO(nlopes): is this the right behaviour?
                    break;
                }
            };

            let mut batches_sent = Vec::new();
            for (batch_name, batch) in batches.iter_mut() {
                if batch.is_empty() {
                    break;
                }
                let options = options.clone();

                if batch.len() >= options.max_batch_size || expired {
                    trace!(
                        "Timer expired or batch size exceeded with {} event(s)",
                        batch.len()
                    );
                    let batch_copy = batch.clone();
                    let batch_response_sender = response_sender.clone();
                    let batch_user_agent = user_agent.to_string();
                    // This is a shallow clone that allows reusing HTTPS connections across batches.
                    // From the reqwest docs:
                    //   "You do not have to wrap the Client it in an Rc or Arc to reuse it, because
                    //    it already uses an Arc internally."
                    let client_copy = http_client.clone();

                    runtime.spawn(async move {
                        for response in Self::send_batch(
                            batch_copy,
                            options,
                            batch_user_agent,
                            Instant::now(),
                            client_copy,
                        )
                        .await
                        {
                            batch_response_sender
                                .send(response)
                                .expect("unable to enqueue batch response");
                        }
                    });
                    batches_sent.push(batch_name.to_string())
                }
            }
            // clear all sent batches
            batches_sent.iter_mut().for_each(|name| {
                batches.remove(name);
            });

            // If we get here and we were expired, then we've already triggered a send, so
            // we reset this to ensure it kicks off again
            if expired {
                expired = false;
            }
        }
        info!("Shutting down batch processing runtime");
        runtime.shutdown_background();
        info!("Batch processing runtime shut down");
    }

    async fn send_batch(
        events: Events,
        options: Options,
        user_agent: String,
        clock: Instant,
        client: reqwest::Client,
    ) -> Vec<Response> {
        let mut opts: crate::client::Options = crate::client::Options::default();
        let mut payload: Vec<EventData> = Vec::new();

        for event in &events {
            opts = event.options.clone();
            payload.push(EventData {
                data: event.fields.clone(),
                time: event.timestamp,
                samplerate: event.options.sample_rate,
            })
        }

        let endpoint = format!("{}{}{}", opts.api_host, BATCH_ENDPOINT, &opts.dataset);

        let user_agent = if let Some(ua_addition) = options.user_agent_addition {
            format!("{}{}", user_agent, ua_addition)
        } else {
            user_agent
        };

        trace!("Sending payload: {:#?}", payload);
        let response = client
            .post(&endpoint)
            .header(header::USER_AGENT, user_agent)
            .header(header::CONTENT_TYPE, "application/json")
            .header("X-Honeycomb-Team", opts.api_key)
            .json(&payload)
            .send()
            .await;

        trace!("Received response: {:#?}", response);
        match response {
            Ok(res) => match res.status() {
                StatusCode::OK => {
                    let responses: Vec<HoneyResponse>;
                    match res.json().await {
                        Ok(r) => responses = r,
                        Err(e) => {
                            return events.to_response(None, None, clock, Some(e.to_string()));
                        }
                    }
                    let total_responses = if responses.is_empty() {
                        1
                    } else {
                        responses.len() as u64
                    };

                    let spent = Duration::from_secs(clock.elapsed().as_secs() / total_responses);

                    responses
                        .iter()
                        .zip(events.iter())
                        .map(|(hr, e)| Response {
                            status_code: StatusCode::from_u16(hr.status).ok(),
                            body: None,
                            duration: spent,
                            metadata: e.metadata.clone(),
                            error: hr.error.clone(),
                        })
                        .collect()
                }
                status => {
                    let body = match res.text().await {
                        Ok(t) => t,
                        Err(e) => format!("HTTP Error but could not read response body: {}", e),
                    };
                    events.to_response(Some(status), Some(body), clock, None)
                }
            },
            Err(err) => events.to_response(None, None, clock, Some(err.to_string())),
        }
    }
}

#[cfg(test)]
mod tests {
    use reqwest::StatusCode;

    use super::*;
    use crate::client;

    #[test]
    fn test_defaults() {
        let transmission = Transmission::new(Options::default()).unwrap();
        assert_eq!(
            transmission.user_agent,
            format!("{}/{}", DEFAULT_NAME_PREFIX, env!("CARGO_PKG_VERSION"))
        );

        assert_eq!(transmission.options.max_batch_size, DEFAULT_MAX_BATCH_SIZE);
        assert_eq!(transmission.options.batch_timeout, DEFAULT_BATCH_TIMEOUT);
        assert_eq!(
            transmission.options.max_concurrent_batches,
            DEFAULT_MAX_CONCURRENT_BATCHES
        );
        assert_eq!(
            transmission.options.pending_work_capacity,
            DEFAULT_PENDING_WORK_CAPACITY
        );
    }

    #[test]
    fn test_modifiable_defaults() {
        let transmission = Transmission::new(Options {
            user_agent_addition: Some(" something/0.3".to_string()),
            ..Options::default()
        })
        .unwrap();
        assert_eq!(
            transmission.options.user_agent_addition,
            Some(" something/0.3".to_string())
        );
    }

    #[test]
    fn test_responses() {
        use crate::fields::FieldHolder;

        let mut transmission = Transmission::new(Options {
            max_batch_size: 5,
            ..Options::default()
        })
        .unwrap();
        transmission.start();

        let api_host = &mockito::server_url();
        let _m = mockito::mock(
            "POST",
            mockito::Matcher::Regex(r"/1/batch/(.*)$".to_string()),
        )
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"
[
  { "status":202 },
  { "status":202 },
  { "status":202 },
  { "status":202 },
  { "status":202 }
]
"#,
        )
        .create();

        for i in 0..5 {
            let mut event = Event::new(&client::Options {
                api_key: "some_api_key".to_string(),
                api_host: api_host.to_string(),
                ..client::Options::default()
            });
            event.add_field("id", serde_json::from_str(&i.to_string()).unwrap());
            transmission.send(event);
        }
        for (i, response) in transmission.responses().iter().enumerate() {
            if i == 4 {
                break;
            }
            assert_eq!(response.status_code, Some(StatusCode::ACCEPTED));
            assert_eq!(response.body, None);
        }
        transmission.stop().unwrap();
    }

    #[test]
    fn test_metadata() {
        use serde_json::json;

        let mut transmission = Transmission::new(Options {
            max_batch_size: 1,
            ..Options::default()
        })
        .unwrap();
        transmission.start();

        let api_host = &mockito::server_url();
        let _m = mockito::mock(
            "POST",
            mockito::Matcher::Regex(r"/1/batch/(.*)$".to_string()),
        )
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"
[
  { "status":202 }
]
"#,
        )
        .create();

        let metadata = Some(json!("some metadata in a string"));
        let mut event = Event::new(&client::Options {
            api_key: "some_api_key".to_string(),
            api_host: api_host.to_string(),
            ..client::Options::default()
        });
        event.metadata = metadata.clone();
        transmission.send(event);

        if let Some(response) = transmission.responses().iter().next() {
            assert_eq!(response.status_code, Some(StatusCode::ACCEPTED));
            assert_eq!(response.metadata, metadata);
        } else {
            panic!("did not receive an expected response");
        }
        transmission.stop().unwrap();
    }

    #[test]
    fn test_multiple_batches() {
        // What we try to test here is if events are sent in separate batches, depending
        // on their combination of api_host, api_key, dataset.
        //
        // For that, we set max_batch_size to 2, then we send 3 events, 2 with one
        // combination and 1 with another.  Only the two should be sent, and we should get
        // back two responses.
        use serde_json::json;
        let mut transmission = Transmission::new(Options {
            max_batch_size: 2,
            batch_timeout: Duration::from_secs(5),
            ..Options::default()
        })
        .unwrap();
        transmission.start();

        let api_host = &mockito::server_url();
        let _m = mockito::mock(
            "POST",
            mockito::Matcher::Regex(r"/1/batch/(.*)$".to_string()),
        )
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"
[
  { "status":202 },
  { "status":202 }
]"#,
        )
        .create();

        let mut event1 = Event::new(&client::Options {
            api_key: "some_api_key".to_string(),
            api_host: api_host.to_string(),
            dataset: "same".to_string(),
            ..client::Options::default()
        });
        event1.metadata = Some(json!("event1"));
        let mut event2 = event1.clone();
        event2.metadata = Some(json!("event2"));
        let mut event3 = event1.clone();
        event3.options.dataset = "other".to_string();
        event3.metadata = Some(json!("event3"));

        transmission.send(event3);
        transmission.send(event2);
        transmission.send(event1);

        let response1 = transmission.responses().iter().next().unwrap();
        let response2 = transmission.responses().iter().next().unwrap();
        let _ = transmission
            .responses()
            .recv_timeout(Duration::from_millis(250))
            .err();

        assert_eq!(response1.status_code, Some(StatusCode::ACCEPTED));
        assert_eq!(response2.status_code, Some(StatusCode::ACCEPTED));

        // Responses can come out of order so we check against any of the metadata
        assert!(
            response1.metadata == Some(json!("event1"))
                || response1.metadata == Some(json!("event2"))
        );
        assert!(
            response2.metadata == Some(json!("event1"))
                || response2.metadata == Some(json!("event2"))
        );
        transmission.stop().unwrap();
    }

    #[test]
    fn test_bad_response() {
        use serde_json::json;

        let mut transmission = Transmission::new(Options::default()).unwrap();
        transmission.start();

        let api_host = &mockito::server_url();
        let _m = mockito::mock(
            "POST",
            mockito::Matcher::Regex(r"/1/batch/(.*)$".to_string()),
        )
        .with_status(400)
        .with_header("content-type", "application/json")
        .with_body("request body is malformed and cannot be read as JSON")
        .create();

        let mut event = Event::new(&client::Options {
            api_key: "some_api_key".to_string(),
            api_host: api_host.to_string(),
            ..client::Options::default()
        });

        event.metadata = Some(json!("some metadata in a string"));
        transmission.send(event);

        if let Some(response) = transmission.responses().iter().next() {
            assert_eq!(response.status_code, Some(StatusCode::BAD_REQUEST));
            assert_eq!(
                response.body,
                Some("request body is malformed and cannot be read as JSON".to_string())
            );
        } else {
            panic!("did not receive an expected response");
        }
        transmission.stop().unwrap();
    }
}