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.

331 lines
8.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. - All query modules must be compiled before use
  106. To compose a module you will take your Query defined above and compile it.
  107. #+BEGIN_EXAMPLE
  108. glc:compile(Module, Query).
  109. glc:compile(Module, Query, State).
  110. glc:compile(Module, Query, State, ResetStatistics).
  111. #+END_EXAMPLE
  112. - At this point you will be able to handle an event using a compiled query.
  113. Begin by constructing an event list.
  114. #+BEGIN_EXAMPLE
  115. Event = gre:make([{'a', 2}], [list]).
  116. #+END_EXAMPLE
  117. Now pass it to your query module to be handled.
  118. #+BEGIN_EXAMPLE
  119. glc:handle(Module, Event).
  120. #+END_EXAMPLE
  121. * Handling output events
  122. - You can override the output action with an erlang function.
  123. Write all input events as info reports to the error logger.
  124. #+BEGIN_EXAMPLE
  125. glc:with(glc:null(true), fun(E) ->
  126. error_logger:info_report(gre:pairs(E)) end).
  127. #+END_EXAMPLE
  128. Write all input events where `error_level' exists and is less than 5 as info reports to the error logger.
  129. #+BEGIN_EXAMPLE
  130. glc:with(glc:lt(error_level, 5), fun(E) ->
  131. error_logger:info_report(gre:pairs(E)) end).
  132. #+END_EXAMPLE
  133. Write all input events where `error_level' exists and is 3 or 5 as info reports to the error logger.
  134. #+BEGIN_EXAMPLE
  135. glc:any([
  136. glc:with(glc:lt(error_level, 3), fun(E) ->
  137. error_logger:info_report(gre:pairs(E)) end),
  138. glc:with(glc:lt(error_level, 5), fun(E) ->
  139. error_logger:info_report(gre:pairs(E)) end)]).
  140. #+END_EXAMPLE
  141. # Composing Modules with stored state #
  142. To compose a module with state data you will add a third argument (orddict).
  143. #+BEGIN_EXAMPLE
  144. glc:compile(Module, Query, [{stored, value}]).
  145. #+END_EXAMPLE
  146. # Accessing stored state data #
  147. Return the stored value in this query module.
  148. #+BEGIN_EXAMPLE
  149. {ok, value} = glc:get(stored).
  150. #+END_EXAMPLE
  151. Return all stored values in this query module.
  152. #+BEGIN_EXAMPLE
  153. [...] = Module:get().
  154. #+END_EXAMPLE
  155. * Composing Modules with stored data
  156. - You can create query modules with local state to compare to event data in `with' and `run'
  157. To compose a module with state data you will add a third argument (orddict).
  158. #+BEGIN_EXAMPLE
  159. glc:compile(Module, Query, [{stored, value}]).
  160. #+END_EXAMPLE
  161. * Accessing stored data in constant time
  162. - You can use query modules in a way similar to mochiglobal
  163. Return the stored value in this query module.
  164. #+BEGIN_EXAMPLE
  165. {ok, value} = glc:get(stored).
  166. #+END_EXAMPLE
  167. * Job processing with composed modules
  168. - You can use query modules to execute jobs, if the job doesn't error, process an event.
  169. - `with' is similar to `run', the main difference is additional statistics and execution order
  170. To execute a job through the query module, inputting an event on success.
  171. #+BEGIN_EXAMPLE
  172. Event = gre:make([{'a', 2}], [list]).
  173. Result = glc:run(Module, fun(Event, State) ->
  174. %% do not end with {error, _} or throw an exception
  175. end, Event).
  176. #+END_EXAMPLE
  177. * Event Processing Statistics
  178. Return the number of input events for this query module.
  179. #+BEGIN_EXAMPLE
  180. glc:input(Module).
  181. #+END_EXAMPLE
  182. Return the number of output events for this query module.
  183. #+BEGIN_EXAMPLE
  184. glc:output(Module).
  185. #+END_EXAMPLE
  186. Return the number of filtered events for this query module.
  187. #+BEGIN_EXAMPLE
  188. glc:filter(Module).
  189. #+END_EXAMPLE
  190. * Job Processing Statistics
  191. Return the number of job runs for this query module.
  192. #+BEGIN_EXAMPLE
  193. glc:job_run(Module).
  194. #+END_EXAMPLE
  195. Return the number of job errors for this query module.
  196. #+BEGIN_EXAMPLE
  197. glc:job_error(Module).
  198. #+END_EXAMPLE
  199. Return the number of job inputs for this query module.
  200. #+BEGIN_EXAMPLE
  201. glc:job_input(Module).
  202. #+END_EXAMPLE
  203. Return the amount of time jobs took for this query module.
  204. #+BEGIN_EXAMPLE
  205. glc:job_time(Module).
  206. #+END_EXAMPLE
  207. * Some Tips & Tricks
  208. - This is really just a drop in the bucket.
  209. Return the average time jobs took for this query module.
  210. #+BEGIN_EXAMPLE
  211. glc:job_time(Module) / glc:job_input(Module) / 1000000.
  212. #+END_EXAMPLE
  213. Return the query combining the conditional logic of multiple modules
  214. #+BEGIN_EXAMPLE
  215. glc_lib:reduce(glc:all([Module1:info('query'), Module2:info('query')]).
  216. #+END_EXAMPLE
  217. * Build
  218. #+BEGIN_EXAMPLE
  219. $ ./rebar compile
  220. #+END_EXAMPLE
  221. or
  222. #+BEGIN_EXAMPLE
  223. $ make
  224. #+END_EXAMPLE
  225. * CHANGELOG
  226. 0.1.9
  227. - Add support for running jobs
  228. 0.1.8
  229. - Add support for not equal
  230. 0.1.7
  231. - Support multiple functions specified using `with/2`
  232. - Add support for greater than or less than operators
  233. - Add state storage option for output events or lookup
  234. 0.1.7
  235. - Add job execution and timings
  236. - Add state storage option
  237. 0.1.7
  238. - Add job execution and timings
  239. - Add state storage option
  240. 0.1.6
  241. - Add notfound event matching
  242. 0.1.5
  243. - Rewrite to make highly crash resilient
  244. - per module supervision
  245. - statistics data recovery
  246. - Add wildcard event matching
  247. - Add reset counters