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.

244 lines
6.2 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 not equal to 0.
  42. #+BEGIN_EXAMPLE
  43. glc:neq(a, 0).
  44. #+END_EXAMPLE
  45. Select all events where 'a' exists and is less than 0.
  46. #+BEGIN_EXAMPLE
  47. glc:lt(a, 0).
  48. #+END_EXAMPLE
  49. Select all events where 'a' exists and is less than or equal to 0.
  50. #+BEGIN_EXAMPLE
  51. glc:lte(a, 0).
  52. #+END_EXAMPLE
  53. Select all events where 'a' exists.
  54. #+BEGIN_EXAMPLE
  55. glc:wc(a).
  56. #+END_EXAMPLE
  57. Select all events where 'a' does not exist.
  58. #+BEGIN_EXAMPLE
  59. glc:nf(a).
  60. #+END_EXAMPLE
  61. Select no input events. User as a black hole query.
  62. #+BEGIN_EXAMPLE
  63. glc:null(false).
  64. #+END_EXAMPLE
  65. Select all input events. Used as a passthrough query.
  66. #+BEGIN_EXAMPLE
  67. glc:null(true).
  68. #+END_EXAMPLE
  69. * Combined Logic
  70. - Combined logic is defined as logic matching multiple event filters
  71. Select all events where both 'a' AND 'b' exists and are greater than 0.
  72. #+BEGIN_EXAMPLE
  73. glc:all([glc:gt(a, 0), glc:gt(b, 0)]).
  74. #+END_EXAMPLE
  75. Select all events where 'a' OR 'b' exists and are greater than 0.
  76. #+BEGIN_EXAMPLE
  77. glc:any([glc:gt(a, 0), glc:gt(b, 0)]).
  78. #+END_EXAMPLE
  79. Select all events where 'a' AND 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  80. #+BEGIN_EXAMPLE
  81. glc:all([glc:gt(a, 1), glc:lt(b, 2)]).
  82. #+END_EXAMPLE
  83. Select all events where 'a' OR 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  84. #+BEGIN_EXAMPLE
  85. glc:any([glc:gt(a, 1), glc:lt(b, 2)]).
  86. #+END_EXAMPLE
  87. * Reduced Logic
  88. - Reduced logic is defined as logic which can be simplified to improve efficiency.
  89. 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.
  90. #+BEGIN_EXAMPLE
  91. glc_lib:reduce(
  92. glc:all([
  93. glc:any([glc:eq(a, 1), glc:eq(b, 2)]),
  94. glc:any([glc:eq(a, 1), glc:eq(c, 3)])])).
  95. #+END_EXAMPLE
  96. The previous example will produce and is equivalent to:
  97. #+BEGIN_EXAMPLE
  98. glc:all([glc:eq(a, 1), glc:eq(b, 2), glc:eq(c, 3)]).
  99. #+END_EXAMPLE
  100. # Composing Modules #
  101. To compose a module you will take your Query defined above and compile it.
  102. #+BEGIN_EXAMPLE
  103. glc:compile(Module, Query).
  104. #+END_EXAMPLE
  105. - At this point you will be able to handle an event using a compiled query.
  106. Begin by constructing an event list.
  107. #+BEGIN_EXAMPLE
  108. Event = gre:make([{'a', 2}], [list]).
  109. #+END_EXAMPLE
  110. Now pass it to your query module to be handled.
  111. #+BEGIN_EXAMPLE
  112. glc:handle(Module, Event).
  113. #+END_EXAMPLE
  114. * Handling output events
  115. - You can override the output action with an erlang function.
  116. Write all input events as info reports to the error logger.
  117. #+BEGIN_EXAMPLE
  118. glc:with(glc:null(true), fun(E) ->
  119. error_logger:info_report(gre:pairs(E)) end).
  120. #+END_EXAMPLE
  121. Write all input events where `error_level' exists and is less than 5 as info reports to the error logger.
  122. #+BEGIN_EXAMPLE
  123. glc:with(glc:lt(error_level, 5), fun(E) ->
  124. error_logger:info_report(gre:pairs(E)) end).
  125. #+END_EXAMPLE
  126. Write all input events where `error_level' exists and is 3 or 5 as info reports to the error logger.
  127. #+BEGIN_EXAMPLE
  128. glc:any([
  129. glc:with(glc:lt(error_level, 3), fun(E) ->
  130. error_logger:info_report(gre:pairs(E)) end),
  131. glc:with(glc:lt(error_level, 5), fun(E) ->
  132. error_logger:info_report(gre:pairs(E)) end)]).
  133. #+END_EXAMPLE
  134. # Composing Modules with stored state #
  135. To compose a module with state data you will add a third argument (orddict).
  136. #+BEGIN_EXAMPLE
  137. glc:compile(Module, Query, [{stored, value}]).
  138. #+END_EXAMPLE
  139. # Accessing stored state data #
  140. Return the stored value in this query module.
  141. #+BEGIN_EXAMPLE
  142. {ok, value} = glc:get(stored).
  143. #+END_EXAMPLE
  144. Return all stored values in this query module.
  145. #+BEGIN_EXAMPLE
  146. [...] = Module:get().
  147. #+END_EXAMPLE
  148. # Event Processing Statistics #
  149. Return the number of input events for this query module.
  150. #+BEGIN_EXAMPLE
  151. glc:input(Module).
  152. #+END_EXAMPLE
  153. Return the number of output events for this query module.
  154. #+BEGIN_EXAMPLE
  155. glc:output(Module).
  156. #+END_EXAMPLE
  157. Return the number of filtered events for this query module.
  158. #+BEGIN_EXAMPLE
  159. glc:filter(Module).
  160. #+END_EXAMPLE
  161. * Build
  162. #+BEGIN_EXAMPLE
  163. $ ./rebar compile
  164. #+END_EXAMPLE
  165. or
  166. #+BEGIN_EXAMPLE
  167. $ make
  168. #+END_EXAMPLE
  169. * CHANGELOG
  170. 0.1.8
  171. - Add support for not equal
  172. 0.1.7
  173. - Support multiple functions specified using `with/2`
  174. - Add support for greater than or less than operators
  175. - Add state storage option for output events or lookup
  176. 0.1.6
  177. - Add notfound event matching
  178. 0.1.5
  179. - Rewrite to make highly crash resilient
  180. - per module supervision
  181. - statistics data recovery
  182. - Add wildcard event matching
  183. - Add reset counters