Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. // Alerting with context:
  2. //
  3. // This example shows how to annotate an alert event with recent lines from
  4. // a logging stream, to give context for the alert. This is accomplished with
  5. // a custom reducer that tails the log stream, and a join of the current tail
  6. // against each alert event as it occurs.
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // simulated alert and logging streams
  11. //
  12. sub alerts() {
  13. emit -every :5s: -limit 3
  14. | put event="alert"
  15. }
  16. sub logs() {
  17. emit -every :250ms: -limit 50
  18. | put message="at "+Date.toString(time)+" the count was "+Number.toString(count())
  19. }
  20. //
  21. // tail a field in a stream of points
  22. //
  23. reducer tail_field(field, N) {
  24. var context = [];
  25. var c_i = 0, c_n = 0;
  26.  
  27. function update() {
  28. context[c_i] = *field;
  29. c_n = Math.min(c_n + 1, N);
  30. c_i = (c_i + 1) % N;
  31. }
  32.  
  33. function get_tail(tail, j, c_j) {
  34. if (tail == null) {
  35. tail = []; j = 0; c_j = (c_i - c_n + N) % N;
  36. }
  37. if (j < c_n) {
  38. tail[j] = context[c_j];
  39. return get_tail(tail, j + 1, (c_j + 1) % N);
  40. } else {
  41. return tail;
  42. }
  43. }
  44.  
  45. function result() {
  46. return get_tail(null, null, null);
  47. }
  48. }
  49. //
  50. // Live stream demo: tap the alert and logging streams, and join them
  51. // whenever an alert fires.
  52. //
  53. (
  54. alerts;
  55. logs | put context = tail_field('message', 5);
  56. )| join -onchange 1
  57. | @table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement