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.

237 lines
6.1 KiB

  1. # Goldrush #
  2. Goldrush is a small Erlang app that provides fast event stream processing
  3. # Features #
  4. * Event processing compiled to a query module
  5. - per module private event processing statistics
  6. - query module logic can be combined for any/all filters
  7. - query module logic can be reduced to efficiently match event processing
  8. * Complex event processing logic
  9. - match input events with greater than (gt) logic
  10. - match input events with less than (lt) logic
  11. - match input events with equal to (eq) logic
  12. - match input events with wildcard (wc) logic
  13. - match input events with notfound (nf) logic
  14. - match no input events (null blackhole) logic
  15. - match all input events (null passthrough) logic
  16. * Handle output events
  17. - Once a query has been composed the output action can be overriden
  18. with one or more erlang functions. The functions will be applied to each
  19. output event from the query.
  20. * Handle low latency retrieval of compile-time stored values.
  21. - Values stored are also provided to functions called on event output.
  22. * Usage
  23. To use goldrush in your application, you need to define it as a rebar dep or
  24. include it in erlang's path.
  25. Before composing modules, you'll need to define a query. The query syntax
  26. matches any number of `{erlang, terms}' and is composed as follows:
  27. * Simple Logic
  28. - Simple logic is defined as any logic matching a single event filter
  29. Select all events where 'a' exists and is greater than 0.
  30. #+BEGIN_EXAMPLE
  31. glc:gt(a, 0).
  32. #+END_EXAMPLE
  33. Select all events where 'a' exists and is greater than or equal to 0.
  34. #+BEGIN_EXAMPLE
  35. glc:gte(a, 0).
  36. #+END_EXAMPLE
  37. Select all events where 'a' exists and is equal to 0.
  38. #+BEGIN_EXAMPLE
  39. glc:eq(a, 0).
  40. #+END_EXAMPLE
  41. Select all events where 'a' exists and is less than 0.
  42. #+BEGIN_EXAMPLE
  43. glc:lt(a, 0).
  44. #+END_EXAMPLE
  45. Select all events where 'a' exists and is less than or equal to 0.
  46. #+BEGIN_EXAMPLE
  47. glc:lte(a, 0).
  48. #+END_EXAMPLE
  49. Select all events where 'a' exists.
  50. #+BEGIN_EXAMPLE
  51. glc:wc(a).
  52. #+END_EXAMPLE
  53. Select all events where 'a' does not exist.
  54. #+BEGIN_EXAMPLE
  55. glc:nf(a).
  56. #+END_EXAMPLE
  57. Select no input events. User as a black hole query.
  58. #+BEGIN_EXAMPLE
  59. glc:null(false).
  60. #+END_EXAMPLE
  61. Select all input events. Used as a passthrough query.
  62. #+BEGIN_EXAMPLE
  63. glc:null(true).
  64. #+END_EXAMPLE
  65. * Combined Logic
  66. - Combined logic is defined as logic matching multiple event filters
  67. Select all events where both 'a' AND 'b' exists and are greater than 0.
  68. #+BEGIN_EXAMPLE
  69. glc:all([glc:gt(a, 0), glc:gt(b, 0)]).
  70. #+END_EXAMPLE
  71. Select all events where 'a' OR 'b' exists and are greater than 0.
  72. #+BEGIN_EXAMPLE
  73. glc:any([glc:gt(a, 0), glc:gt(b, 0)]).
  74. #+END_EXAMPLE
  75. Select all events where 'a' AND 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  76. #+BEGIN_EXAMPLE
  77. glc:all([glc:gt(a, 1), glc:lt(b, 2)]).
  78. #+END_EXAMPLE
  79. Select all events where 'a' OR 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  80. #+BEGIN_EXAMPLE
  81. glc:any([glc:gt(a, 1), glc:lt(b, 2)]).
  82. #+END_EXAMPLE
  83. * Reduced Logic
  84. - Reduced logic is defined as logic which can be simplified to improve efficiency.
  85. Select all events where 'a' is equal to 1, 'b' is equal to 2 and 'c' is equal to 3 and collapse any duplicate logic.
  86. #+BEGIN_EXAMPLE
  87. glc_lib:reduce(
  88. glc:all([
  89. glc:any([glc:eq(a, 1), glc:eq(b, 2)]),
  90. glc:any([glc:eq(a, 1), glc:eq(c, 3)])])).
  91. #+END_EXAMPLE
  92. The previous example will produce and is equivalent to:
  93. #+BEGIN_EXAMPLE
  94. glc:all([glc:eq(a, 1), glc:eq(b, 2), glc:eq(c, 3)]).
  95. #+END_EXAMPLE
  96. # Composing Modules #
  97. To compose a module you will take your Query defined above and compile it.
  98. #+BEGIN_EXAMPLE
  99. glc:compile(Module, Query).
  100. #+END_EXAMPLE
  101. - At this point you will be able to handle an event using a compiled query.
  102. Begin by constructing an event list.
  103. #+BEGIN_EXAMPLE
  104. Event = gre:make([{'a', 2}], [list]).
  105. #+END_EXAMPLE
  106. Now pass it to your query module to be handled.
  107. #+BEGIN_EXAMPLE
  108. glc:handle(Module, Event).
  109. #+END_EXAMPLE
  110. * Handling output events
  111. - You can override the output action with an erlang function.
  112. Write all input events as info reports to the error logger.
  113. #+BEGIN_EXAMPLE
  114. glc:with(glc:null(true), fun(E) ->
  115. error_logger:info_report(gre:pairs(E)) end).
  116. #+END_EXAMPLE
  117. Write all input events where `error_level' exists and is less than 5 as info reports to the error logger.
  118. #+BEGIN_EXAMPLE
  119. glc:with(glc:lt(error_level, 5), fun(E) ->
  120. error_logger:info_report(gre:pairs(E)) end).
  121. #+END_EXAMPLE
  122. Write all input events where `error_level' exists and is 3 or 5 as info reports to the error logger.
  123. #+BEGIN_EXAMPLE
  124. glc:any([
  125. glc:with(glc:lt(error_level, 3), fun(E) ->
  126. error_logger:info_report(gre:pairs(E)) end),
  127. glc:with(glc:lt(error_level, 5), fun(E) ->
  128. error_logger:info_report(gre:pairs(E)) end)]).
  129. #+END_EXAMPLE
  130. # Composing Modules with stored state #
  131. To compose a module with state data you will add a third argument (orddict).
  132. #+BEGIN_EXAMPLE
  133. glc:compile(Module, Query, [{stored, value}]).
  134. #+END_EXAMPLE
  135. # Accessing stored state data #
  136. Return the stored value in this query module.
  137. #+BEGIN_EXAMPLE
  138. {ok, value} = glc:get(stored).
  139. #+END_EXAMPLE
  140. Return all stored values in this query module.
  141. #+BEGIN_EXAMPLE
  142. [...] = Module:get().
  143. #+END_EXAMPLE
  144. # Event Processing Statistics #
  145. Return the number of input events for this query module.
  146. #+BEGIN_EXAMPLE
  147. glc:input(Module).
  148. #+END_EXAMPLE
  149. Return the number of output events for this query module.
  150. #+BEGIN_EXAMPLE
  151. glc:output(Module).
  152. #+END_EXAMPLE
  153. Return the number of filtered events for this query module.
  154. #+BEGIN_EXAMPLE
  155. glc:filter(Module).
  156. #+END_EXAMPLE
  157. * Build
  158. #+BEGIN_EXAMPLE
  159. $ ./rebar compile
  160. #+END_EXAMPLE
  161. or
  162. #+BEGIN_EXAMPLE
  163. $ make
  164. #+END_EXAMPLE
  165. * CHANGELOG
  166. 0.1.7
  167. - Support multiple functions specified using `with/2`
  168. - Add support for greater than or less than operators
  169. - Add state storage option for output events or lookup
  170. 0.1.6
  171. - Add notfound event matching
  172. 0.1.5
  173. - Rewrite to make highly crash resilient
  174. - per module supervision
  175. - statistics data recovery
  176. - Add wildcard event matching
  177. - Add reset counters