$30 off During Our Annual Pro Sale. View Details »

Make an Event of It! (CodeMash 2014)

Make an Event of It! (CodeMash 2014)

The upsurge in asynchronous programming has brought event-driven patterns to the forefront. But even if you aren't re-writing your app in Node.js, evented patterns can improve your application. This talk demonstrates concrete examples of how events can benefit the various layers of your application. It shows how events can pitch in the fight against fat controllers (and fat models too!) It applies eventing to simplify testing and decouple our app from external dependencies. It even reveals how eventing can help shape data to provide flexibility and auditing.

Jason R Clark

January 09, 2014
Tweet

More Decks by Jason R Clark

Other Decks in Technology

Transcript

  1. Jason Clark
    @jasonrclark
    Ruby Agent Engineer
    Make an Event of It!
    Evented Patterns in Ruby
    Thursday, January 9, 14

    View Slide

  2. Where Are We Going?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Eventing
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    2
    Thursday, January 9, 14

    View Slide

  3. The Pattern
    http://flic.kr/p/6K9jC4
    Thursday, January 9, 14

    View Slide

  4. 4
    Thursday, January 9, 14

    View Slide

  5. 5
    Thursday, January 9, 14

    View Slide

  6. 5
    Thursday, January 9, 14

    View Slide

  7. 5
    Thursday, January 9, 14

    View Slide

  8. Events Aren't
    (Necessarily)
    6
    • Asynchronous
    • IO related
    • Distributed
    • Complicated
    Thursday, January 9, 14

    View Slide

  9. 7
    Thursday, January 9, 14

    View Slide

  10. 7
    Notifier
    Thursday, January 9, 14

    View Slide

  11. 7
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Thursday, January 9, 14

    View Slide

  12. 7
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  13. 7
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  14. 7
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  15. 8
    Why Not Methods?
    Thursday, January 9, 14

    View Slide

  16. 9
    Thursday, January 9, 14

    View Slide

  17. 9
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  18. 9
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  19. Why Not Callbacks?
    10
    Thursday, January 9, 14

    View Slide

  20. 11
    class Subscription < ActiveRecord::Base
    before_create :record_signup
    private
    def record_signup
    self.signed_up_on = Date.today
    end
    end
    Thursday, January 9, 14

    View Slide

  21. 11
    class Subscription < ActiveRecord::Base
    before_create :record_signup
    private
    def record_signup
    self.signed_up_on = Date.today
    end
    end
    Thursday, January 9, 14

    View Slide

  22. Where Have We Been?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Eventing
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    12
    Thursday, January 9, 14

    View Slide

  23. 13
    Internal Coupling
    http://flic.kr/p/KikA9
    Thursday, January 9, 14

    View Slide

  24. 14
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  25. 14
    app start
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  26. 14
    app start connect
    New Relic
    Server
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  27. {server config}
    14
    app start connect
    New Relic
    Server
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  28. {server config}
    14
    app start connect
    New Relic
    Server
    ?
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  29. 15
    def connect()
    config = config_from_server()
    finish_setup(config)
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  30. 16
    def finish_setup(config)
    add_naming_rule(config['rules'])
    @cross_app_tracing.set_key(config)
    @js_instrumentor.log_config(config)
    @beacon = Beacon.new
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  31. 17
    def test_finish_setup_naming_rules
    config = { 'rules' => [...] }
    @agent.finish_setup(config)
    assert_equal 2, @agent.transaction_rules.size
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  32. 18
    def test_finish_setup_naming_rules
    @agent.cross_app_tracing.stub(:set_key)
    @agent.js_instrumentor.stub(:log_config)
    config = { 'rules' => [...] }
    @agent.finish_setup(config)
    assert_equal 2, @agent.transaction_rules.size
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  33. 19
    def finish_setup(config)
    add_naming_rule(config['rules'])
    @cross_app_tracing.set_key(config)
    @js_instrumentor.log_config(config)
    @beacon = Beacon.new
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  34. 20
    class Beacon
    def initialize
    # Your code is bad and you should feel bad
    Net::HTTP.get("http://...")
    end
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  35. 21
    def test_finish_setup_naming_rules
    @agent.cross_app_tracing.stub(:set_key)
    @agent.js_instrumentor.stub(:log_config)
    Beacon.stub(:new).return(stub("fake beacon"))
    config = { 'rules' => [...] }
    @agent.finish_setup(config)
    assert_equal 2, @agent.transaction_rules.size
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  36. 22
    def finish_setup(config)
    add_naming_rule(config['rules'])
    @cross_app_tracing.set_key(config)
    @js_instrumentor.log_config(config)
    @beacon = Beacon.new
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  37. 23
    def finish_setup(config)
    events.notify(:configured)
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  38. 24
    class Beacon
    def initialize
    events.subscribe(:configured) do
    Net::HTTP.get("http://...")
    end
    end
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  39. 25
    def test_finish_setup_notifies
    called = false
    @events.subscribe(:configured) do
    called = true
    end
    @agent.finish_setup({})
    assert called
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  40. 26
    def test_add_naming_rules
    config = { 'rules' => [...] }
    @agent.add_naming_rules(config)
    assert_equal 2, @agent.transaction_rules.size
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  41. More Focused
    Tests
    27
    Thursday, January 9, 14

    View Slide

  42. More Independent
    Classes
    28
    Thursday, January 9, 14

    View Slide

  43. Implicit Ordering
    29
    Thursday, January 9, 14

    View Slide

  44. Debugging
    30
    Thursday, January 9, 14

    View Slide

  45. Performance
    31
    Thursday, January 9, 14

    View Slide

  46. Where Have We Been?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Eventing
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    32
    Thursday, January 9, 14

    View Slide

  47. http://flic.kr/p/euQpT
    External Coupling
    Thursday, January 9, 14

    View Slide

  48. 34
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  49. 34
    Javascript insertion
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  50. 34
    Javascript insertion
    Error collection
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  51. 34
    Javascript insertion
    Cross application tracing
    Error collection
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  52. 35
    No Setup Please!
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  53. 36
    module NewRelic::Rack
    class AgentHooks
    def call(env)
    notify(:before_call, env)
    result = @app.call(env)
    notify(:after_call, env, result)
    result
    end
    end
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  54. 37
    class CrossAppTracing
    def initialize
    events.subscribe(:before_call) do |e|
    process_request(e)
    end
    ...
    end
    end
    newrelic_rpm
    Thursday, January 9, 14

    View Slide

  55. Ever Heard of
    Composition?
    38
    Thursday, January 9, 14

    View Slide

  56. Looser Coupling
    39
    Thursday, January 9, 14

    View Slide

  57. Compatibilty
    Layer
    40
    Thursday, January 9, 14

    View Slide

  58. Where Have We Been?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Eventing
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    41
    Thursday, January 9, 14

    View Slide

  59. http://flic.kr/p/dYor95
    Adding Eventing
    Thursday, January 9, 14

    View Slide

  60. 43
    Thursday, January 9, 14

    View Slide

  61. 43
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  62. 43
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  63. 44
    gem install
    simple_events
    http://github.com/jasonrclark/simple_events
    Thursday, January 9, 14

    View Slide

  64. 45
    class SimpleEvents::Notifier
    def initialize
    @events = {}
    ...
    end
    end
    simple_events
    Thursday, January 9, 14

    View Slide

  65. 46
    class SimpleEvents::Notifier
    ...
    def subscribe(event, &handler)
    @events[event] ||= []
    @events[event] << handler
    check_for_runaway_subscriptions(event)
    end
    ...
    end
    simple_events
    Thursday, January 9, 14

    View Slide

  66. 47
    class SimpleEvents::Notifier
    ...
    def notify(event, *args)
    return unless @events.has_key?(event)
    @events[event].each do |handler|
    begin
    handler.call(*args)
    rescue => err
    logger.debug("Fail #{@event}", err)
    end
    end
    end
    end
    simple_events
    Thursday, January 9, 14

    View Slide

  67. 48
    class SimpleEvents::Notifier
    ...
    def notify(event, *args)
    return unless @events.has_key?(event)
    @events[event].each do |handler|
    begin
    handler.call(*args)
    rescue => err
    logger.debug("Fail #{@event}", err)
    end
    end
    end
    end
    simple_events
    Thursday, January 9, 14

    View Slide

  68. 49
    class SimpleEvents::Notifier
    ...
    def notify(event, *args)
    return unless @events.has_key?(event)
    @events[event].each do |handler|
    begin
    handler.call(*args)
    rescue => err
    logger.debug("Fail #{@event}", err)
    end
    end
    end
    end
    simple_events
    Thursday, January 9, 14

    View Slide

  69. 50
    class SimpleEvents::Notifier
    ...
    def notify(event, *args)
    return unless @events.has_key?(event)
    @events[event].each do |handler|
    begin
    handler.call(*args)
    rescue => err
    logger.debug("Fail #{@event}", err)
    end
    end
    end
    end
    simple_events
    Thursday, January 9, 14

    View Slide

  70. 51
    class SimpleEvents::Notifier
    ...
    def notify(event, *args)
    return unless @events.has_key?(event)
    @events[event].each do |handler|
    begin
    handler.call(*args)
    rescue => err
    logger.debug("Fail #{@event}", err)
    end
    end
    end
    end
    simple_events
    Thursday, January 9, 14

    View Slide

  71. Where Have We Been?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Eventing
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    52
    Thursday, January 9, 14

    View Slide

  72. ActiveSupport::
    Notifications
    http://flic.kr/p/aWNwU2
    Thursday, January 9, 14

    View Slide

  73. 54
    AS::Notifications
    =
    ActiveSupport::Notifications
    activesupport
    Thursday, January 9, 14

    View Slide

  74. Rails Request
    55
    •action_dispatch.request
    •start_processing.action_controller
    •process_action.action_controller
    •sql.active_record
    •render_template.action_view
    • ... and more!
    Thursday, January 9, 14

    View Slide

  75. 56
    events = []
    AS::Notifications.subscribe("event") do |*args|
    events << *args
    end
    activesupport
    Thursday, January 9, 14

    View Slide

  76. 57
    AS::Notifications.subscribe("event") do |*args|
    events << *args
    end
    ["render_template.action_view",
    "e2a1e92a3c613576c2b0"
    {:identifier=>"/file/path/here"}]
    activesupport
    Thursday, January 9, 14

    View Slide

  77. 58
    e = AS::Notifications::Event.new(*args)
    e.name # => "render_template..."
    e.duration # => 10 (in milliseconds)
    e.payload # => { :identifier => ... }
    activesupport
    Thursday, January 9, 14

    View Slide

  78. 59
    activesupport
    AS::Notifications.instrument("evt", :data => 1)
    Thursday, January 9, 14

    View Slide

  79. 60
    AS::Notifications.instrument("evt", :data => 1) do
    #... your timed code here
    end
    activesupport
    Thursday, January 9, 14

    View Slide

  80. • Regexp on Notifications.subscribe
    • Event#parent_of?
    • Temporary subscription
    • Unsubscribe
    • LogSubscriber
    61
    activesupport
    Thursday, January 9, 14

    View Slide

  81. Where Have We Been?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Events
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    62
    Thursday, January 9, 14

    View Slide

  82. http://flic.kr/p/a1dEec
    Notifiers
    Thursday, January 9, 14

    View Slide

  83. 64
    Thursday, January 9, 14

    View Slide

  84. 64
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  85. 64
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  86. Naming
    65
    Thursday, January 9, 14

    View Slide

  87. Flexible Payload
    66
    Thursday, January 9, 14

    View Slide

  88. Primitives at
    Boundaries
    67
    Thursday, January 9, 14

    View Slide

  89. Where Have We Been?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Events
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    68
    Thursday, January 9, 14

    View Slide

  90. 69
    http://flic.kr/p/8w9PyF
    Subscribers
    Thursday, January 9, 14

    View Slide

  91. 70
    Thursday, January 9, 14

    View Slide

  92. 70
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  93. 70
    Notifier
    Subscriber
    Subscriber
    Subscriber
    Eventing
    System
    Thursday, January 9, 14

    View Slide

  94. != self.only
    71
    Thursday, January 9, 14

    View Slide

  95. 72
    Thursday, January 9, 14

    View Slide

  96. 73
    class UserSignup < Mutations::Command
    def execute
    user = User.create!(inputs)
    NewsletterSubscriptions.create(user.id)
    UserMailer.async(:deliver_welcome, user.id)
    user
    end
    end
    Thursday, January 9, 14

    View Slide

  97. 74
    class SomeSubscriber
    def initialize
    events.subscribe(:configured) do
    log_config
    end
    end
    def log_config
    ...
    end
    end
    Thursday, January 9, 14

    View Slide

  98. Memory Leaks
    75
    Thursday, January 9, 14

    View Slide

  99. Where Have We Been?
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Events
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    76
    Thursday, January 9, 14

    View Slide

  100. http://flic.kr/p/ndraB
    Events as Data
    Thursday, January 9, 14

    View Slide

  101. 78
    Opened Account $100
    Check 0001 -$15
    Deposit $20
    Check 0002 -$21
    Total $84
    Bank Transactions
    Thursday, January 9, 14

    View Slide

  102. 79
    Customer Pricing
    Thursday, January 9, 14

    View Slide

  103. 79
    customer_id date plan
    1 1/1/2013 basic
    1 1/2/2013 premium
    1 2/1/2013 basic
    1 2/2/2013 premium
    Customer Pricing
    Thursday, January 9, 14

    View Slide

  104. 79
    customer_id date plan
    1 1/1/2013 basic
    1 1/2/2013 premium
    1 2/1/2013 basic
    1 2/2/2013 premium
    Customer Pricing
    Thursday, January 9, 14

    View Slide

  105. Event Sourcing
    80
    Thursday, January 9, 14

    View Slide

  106. 81 http://flic.kr/p/4LTNc9
    Thursday, January 9, 14

    View Slide

  107. 82
    Metrics
    10:00AM
    CPU/Utilization
    0.85
    Thursday, January 9, 14

    View Slide

  108. 83
    Metrics
    10:01AM
    CPU/Utilization
    0.24
    Thursday, January 9, 14

    View Slide

  109. 84
    Metrics
    Thursday, January 9, 14

    View Slide

  110. 85
    Metrics
    Thursday, January 9, 14

    View Slide

  111. 86
    Metrics
    Thursday, January 9, 14

    View Slide

  112. 87
    Metric Data
    Thursday, January 9, 14

    View Slide

  113. 87
    Metric Data
    Thursday, January 9, 14

    View Slide

  114. 87
    Metric Data
    Thursday, January 9, 14

    View Slide

  115. Other Examples
    88
    • Database replication
    • Log files
    • PubSub
    • Immutable data structures
    Thursday, January 9, 14

    View Slide

  116. Upsides
    89
    ★No Updates
    ★Automatic Audits
    ★Summarize/Transform/
    Cache
    Thursday, January 9, 14

    View Slide

  117. Downsides
    -So Much Data!
    -Hard to Query Efficiently
    -More Complex Code
    90
    Thursday, January 9, 14

    View Slide

  118. Jason Clark
    @jasonrclark
    Ruby Agent Engineer
    • The Pattern
    • Internal Coupling
    • External Coupling
    • Adding Eventing
    • ActiveSupport::Notifications
    • Notifiers
    • Subscribers
    • Events as Data
    ?
    Thursday, January 9, 14

    View Slide