25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
7.9 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. - Handle job execution and timing which can also get values stored
  23. - create input events that include runtime on successful function executions.
  24. * Handle fastest lookups of stored values.
  25. - provide state storage option to compile, caching the values in query module.
  26. * Usage
  27. To use goldrush in your application, you need to define it as a rebar dep or
  28. include it in erlang's path.
  29. Before composing modules, you'll need to define a query. The query syntax
  30. matches any number of `{erlang, terms}' and is composed as follows:
  31. * Simple Logic
  32. - Simple logic is defined as any logic matching a single event filter
  33. Select all events where 'a' exists and is greater than 0.
  34. #+BEGIN_EXAMPLE
  35. glc:gt(a, 0).
  36. #+END_EXAMPLE
  37. Select all events where 'a' exists and is greater than or equal to 0.
  38. #+BEGIN_EXAMPLE
  39. glc:gte(a, 0).
  40. #+END_EXAMPLE
  41. Select all events where 'a' exists and is equal to 0.
  42. #+BEGIN_EXAMPLE
  43. glc:eq(a, 0).
  44. #+END_EXAMPLE
  45. Select all events where 'a' exists and is not equal to 0.
  46. #+BEGIN_EXAMPLE
  47. glc:neq(a, 0).
  48. #+END_EXAMPLE
  49. Select all events where 'a' exists and is less than 0.
  50. #+BEGIN_EXAMPLE
  51. glc:lt(a, 0).
  52. #+END_EXAMPLE
  53. Select all events where 'a' exists and is less than or equal to 0.
  54. #+BEGIN_EXAMPLE
  55. glc:lte(a, 0).
  56. #+END_EXAMPLE
  57. Select all events where 'a' exists.
  58. #+BEGIN_EXAMPLE
  59. glc:wc(a).
  60. #+END_EXAMPLE
  61. Select all events where 'a' does not exist.
  62. #+BEGIN_EXAMPLE
  63. glc:nf(a).
  64. #+END_EXAMPLE
  65. Select no input events. User as a black hole query.
  66. #+BEGIN_EXAMPLE
  67. glc:null(false).
  68. #+END_EXAMPLE
  69. Select all input events. Used as a passthrough query.
  70. #+BEGIN_EXAMPLE
  71. glc:null(true).
  72. #+END_EXAMPLE
  73. * Combined Logic
  74. - Combined logic is defined as logic matching multiple event filters
  75. Select all events where both 'a' AND 'b' exists and are greater than 0.
  76. #+BEGIN_EXAMPLE
  77. glc:all([glc:gt(a, 0), glc:gt(b, 0)]).
  78. #+END_EXAMPLE
  79. Select all events where 'a' OR 'b' exists and are greater than 0.
  80. #+BEGIN_EXAMPLE
  81. glc:any([glc:gt(a, 0), glc:gt(b, 0)]).
  82. #+END_EXAMPLE
  83. Select all events where 'a' AND 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  84. #+BEGIN_EXAMPLE
  85. glc:all([glc:gt(a, 1), glc:lt(b, 2)]).
  86. #+END_EXAMPLE
  87. Select all events where 'a' OR 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  88. #+BEGIN_EXAMPLE
  89. glc:any([glc:gt(a, 1), glc:lt(b, 2)]).
  90. #+END_EXAMPLE
  91. * Reduced Logic
  92. - Reduced logic is defined as logic which can be simplified to improve efficiency.
  93. 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.
  94. #+BEGIN_EXAMPLE
  95. glc_lib:reduce(
  96. glc:all([
  97. glc:any([glc:eq(a, 1), glc:eq(b, 2)]),
  98. glc:any([glc:eq(a, 1), glc:eq(c, 3)])])).
  99. #+END_EXAMPLE
  100. The previous example will produce and is equivalent to:
  101. #+BEGIN_EXAMPLE
  102. glc:all([glc:eq(a, 1), glc:eq(b, 2), glc:eq(c, 3)]).
  103. #+END_EXAMPLE
  104. # Composing Modules #
  105. To compose a module you will take your Query defined above and compile it.
  106. #+BEGIN_EXAMPLE
  107. glc:compile(Module, Query).
  108. #+END_EXAMPLE
  109. - At this point you will be able to handle an event using a compiled query.
  110. Begin by constructing an event list.
  111. #+BEGIN_EXAMPLE
  112. Event = gre:make([{'a', 2}], [list]).
  113. #+END_EXAMPLE
  114. Now pass it to your query module to be handled.
  115. #+BEGIN_EXAMPLE
  116. glc:handle(Module, Event).
  117. #+END_EXAMPLE
  118. * Handling output events
  119. - You can override the output action with an erlang function.
  120. Write all input events as info reports to the error logger.
  121. #+BEGIN_EXAMPLE
  122. glc:with(glc:null(true), fun(E) ->
  123. error_logger:info_report(gre:pairs(E)) end).
  124. #+END_EXAMPLE
  125. Write all input events where `error_level' exists and is less than 5 as info reports to the error logger.
  126. #+BEGIN_EXAMPLE
  127. glc:with(glc:lt(error_level, 5), fun(E) ->
  128. error_logger:info_report(gre:pairs(E)) end).
  129. #+END_EXAMPLE
  130. Write all input events where `error_level' exists and is 3 or 5 as info reports to the error logger.
  131. #+BEGIN_EXAMPLE
  132. glc:any([
  133. glc:with(glc:lt(error_level, 3), fun(E) ->
  134. error_logger:info_report(gre:pairs(E)) end),
  135. glc:with(glc:lt(error_level, 5), fun(E) ->
  136. error_logger:info_report(gre:pairs(E)) end)]).
  137. #+END_EXAMPLE
  138. # Composing Modules with stored state #
  139. To compose a module with state data you will add a third argument (orddict).
  140. #+BEGIN_EXAMPLE
  141. glc:compile(Module, Query, [{stored, value}]).
  142. #+END_EXAMPLE
  143. # Accessing stored state data #
  144. Return the stored value in this query module.
  145. #+BEGIN_EXAMPLE
  146. {ok, value} = glc:get(stored).
  147. #+END_EXAMPLE
  148. Return all stored values in this query module.
  149. #+BEGIN_EXAMPLE
  150. [...] = Module:get().
  151. #+END_EXAMPLE
  152. # Composing Modules with stored state #
  153. To compose a module with state data you will add a third argument (orddict).
  154. #+BEGIN_EXAMPLE
  155. glc:compile(Module, Query, [{stored, value}]).
  156. #+END_EXAMPLE
  157. # Accessing stored state data #
  158. Return the stored value in this query module.
  159. #+BEGIN_EXAMPLE
  160. {ok, value} = glc:get(stored).
  161. #+END_EXAMPLE
  162. # Executing jobs #
  163. To execute a job through the query module, inputting an event on success.
  164. #+BEGIN_EXAMPLE
  165. Event = gre:make([{'a', 2}], [list]).
  166. Result = glc:run(Module, fun(Event, State) ->
  167. %% do not end with {error, _} or throw an exception
  168. end, Event).
  169. #+END_EXAMPLE
  170. # Event Processing Statistics #
  171. Return the number of input events for this query module.
  172. #+BEGIN_EXAMPLE
  173. glc:input(Module).
  174. #+END_EXAMPLE
  175. Return the number of output events for this query module.
  176. #+BEGIN_EXAMPLE
  177. glc:output(Module).
  178. #+END_EXAMPLE
  179. Return the number of filtered events for this query module.
  180. #+BEGIN_EXAMPLE
  181. glc:filter(Module).
  182. #+END_EXAMPLE
  183. # Job Processing Statistics #
  184. Return the number of job runs for this query module.
  185. #+BEGIN_EXAMPLE
  186. glc:job_run(Module).
  187. #+END_EXAMPLE
  188. Return the number of job errors for this query module.
  189. #+BEGIN_EXAMPLE
  190. glc:job_error(Module).
  191. #+END_EXAMPLE
  192. Return the number of job inputs for this query module.
  193. #+BEGIN_EXAMPLE
  194. glc:job_input(Module).
  195. #+END_EXAMPLE
  196. Return the amount of time jobs took for this query module.
  197. #+BEGIN_EXAMPLE
  198. glc:job_time(Module).
  199. #+END_EXAMPLE
  200. # Tips & Tricks #
  201. Return the average time jobs took for this query module.
  202. #+BEGIN_EXAMPLE
  203. glc:job_time(Module) / glc:job_input(Module) / 1000000.
  204. #+END_EXAMPLE
  205. Return the query combining the conditional logic of multiple modules
  206. #+BEGIN_EXAMPLE
  207. glc_lib:reduce(glc:all([Module1:info('query'), Module2:info('query')]).
  208. #+END_EXAMPLE
  209. * Build
  210. #+BEGIN_EXAMPLE
  211. $ ./rebar compile
  212. #+END_EXAMPLE
  213. or
  214. #+BEGIN_EXAMPLE
  215. $ make
  216. #+END_EXAMPLE
  217. * CHANGELOG
  218. 0.1.8
  219. - Add support for not equal
  220. 0.1.7
  221. - Support multiple functions specified using `with/2`
  222. - Add support for greater than or less than operators
  223. - Add state storage option for output events or lookup
  224. 0.1.7
  225. - Add job execution and timings
  226. - Add state storage option
  227. 0.1.6
  228. - Add notfound event matching
  229. 0.1.5
  230. - Rewrite to make highly crash resilient
  231. - per module supervision
  232. - statistics data recovery
  233. - Add wildcard event matching
  234. - Add reset counters