Day 3 – Web events and debugging

Getting a look into what is going on when you run your application

ruleset a60x22 {
  meta {
    name "Web events"
    description <<
      demonstrating web events and the urls that go with them
    >>
    author "Mike Grace"
    logging on
  }

  rule should_not_run {
    select when pageview "shouldnotselectbecauseofthiscrazyregex"
    {
      notify("addedNumber",addedNumber) with sticky = true;
    }
  }

  rule should_run {
    select when pageview ".*"
    {
      emit <|
        console.log("wohoo!");
      |>;
    }
  }
}

Inspecting web event after running app on example.com with bookmarklet using Charles. The same can be done using Firebug‘s net tab or Chrome’s developer tools.

App run on example.com with bookmarklet:

  • Line 8 logging is turned on so server logs will be returned with event directives.
  • Line 11-16 rule should not run when app is run on example.com because selection expression won’t match.
  • Line 18-25 rule should run when app is run on example.com because of selection expression matching all URLs.
  • Line 21-23 emit action in action block will run contained JavaScript as is in a closure.

What happens when a Kynetx app is run from a bookmarklet in a browser and how do I see what is going on?

  • Clicking the bookmarklet inserts a script tag onto the page

Bookmarklet:

<a href="javascript:(function(){var d=document;var s=d.createElement('script');s.text=&quot;KOBJ_config={'a60x22:kynetx_app_version':'dev','rids':['a60x22']};&quot;;d.body.appendChild(s);var l=d.createElement('script');l.src='http://init.kobj.net/js/shared/kobj-static.js';d.body.appendChild(l);})()">dev_Web events</a>

Bookmarklet JavaScript cleaned up:

(function() {
    var d = document;
    var s = d.createElement('script');
    s.text = "KOBJ_config={'a60x22:kynetx_app_version':'dev','rids':['a60x22']};";
    d.body.appendChild(s);
    var l = d.createElement('script');
    l.src = 'http://init.kobj.net/js/shared/kobj-static.js';
    d.body.appendChild(l);
})()
  • Clicking the bookmarklet sets the KOBJ_config on the page for when the Kynetx Runtime loads.
  • Clicking the bookmarklet creates script tag on page that requests the Kynetx Runtime.
  • Once the runtime loads on the page, it looks at the KOBJ_config and raises a pageview event to the Kynetx server by creating a script tag on the page that requests a specific url telling the server about the event
http://cs.kobj.net/blue/event/web/pageview/a60x22/129214254756746356898872181773?kvars=%7B%7D&caller=http%3A%2F%2Fexample.com%2F&referer=&title=Example+Web+Page&endpoint=[object%20Object]&a60x22:kynetx_app_version=dev
  • cs.kobj.net is the domain where events are raised to the Kynetx server
  • blue is the api version
  • event… an event is being raised
  • web is the domain of the event
  • pageview is the type of event
  • a60x22 is the ruleset id of the app that this event should be sent to
  • long cache busting number
  • kvars contains contextual data about the event for the rule to process like what url the event is being raised from, the referrer  if any, the title of the page, etc.
  • a60x22:kynetx_app_version=dev is what version of the ruleset the event should be sent to

Based on the contextual data sent by the raised event, this is what the Kynetx server sends back to the page in the script tag reformatted for readability:

// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 [global] Found 2 rules for RID a60x22
// 2010/12/12 01:29:09 DEBUG Events.pm a60x22 should_run Adding to schedule: a60x22 & should_run
// 2010/12/12 01:29:09 DEBUG Scheduler.pm a60x22 should_run Schedule iterator returning should_run with current RID count 0 and current rule count 1
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run Getting ruleset a60x22 for http://example.com/
// 2010/12/12 01:29:09 DEBUG Repository.pm a60x22 should_run Using cached ruleset for a60x22 (dev) with key ruleset:dev:a60x22 & optimization version 7
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run Found 2 rules for RID a60x22
// 2010/12/12 01:29:09 INFO Rules.pm a60x22 should_run Processing rules for site a60x22
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run Global vars:
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run Rule should_run is active
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run [selected] should_run
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run
//------------------- begin rule execution: should_run ------------------------
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run Rule not pre optimized...
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run [rules::optimize_pre] foreach vars:
// 2010/12/12 01:29:09 INFO Rules.pm a60x22 should_run fired
// 2010/12/12 01:29:09 DEBUG Actions.pm a60x22 should_run blocktype is every
// 2010/12/12 01:29:09 DEBUG Actions.pm a60x22 should_run actions list contains 1 actions
// 2010/12/12 01:29:09 DEBUG Scheduler.pm a60x22 should_run Moving to next RID
// 2010/12/12 01:29:09 DEBUG Scheduler.pm a60x22 should_run Resetting schedule
// 2010/12/12 01:29:09 DEBUG Rules.pm a60x22 should_run Finished processing rules for a60x22
// 2010/12/12 01:29:09 DEBUG AST.pm a60x22 should_run Generating JS for a60x22
// 2010/12/12 01:29:09 DEBUG AST.pm a60x22 should_run Generating resource statement for a60x22
// 2010/12/12 01:29:09 DEBUG Log.pm a60x22 should_run [logging] Storing logging data for a60x22
// 2010/12/12 01:29:09 DEBUG Log.pm a60x22 should_run results for should_run
// 2010/12/12 01:29:09 DEBUG Log.pm a60x22 should_run TXN_ID: E352BF86-05C9-11E0-8A2A-ECC731FEA127
// 2010/12/12 01:29:09 DEBUG Session.pm a60x22 should_run Cleaning up session
// 2010/12/12 01:29:09 INFO Response.pm a60x22 should_run Event processing finished
// 2010/12/12 01:29:09 DEBUG Response.pm a60x22 should_run __FLUSH__
// 2010/12/12 01:29:09 DEBUG Response.pm a60x22 should_run Called with GET /blue/event/web/pageview/a60x22/129214254756746356898872181773?kvars=%7B%7D&caller=http%3A%2F%2Fexample.com%2F&referer=&title=Example+Web+Page&endpoint=[object%20Object]&a60x22:kynetx_app_version=dev HTTP/1.0
// 2010/12/12 01:29:09 DEBUG Response.pm a60x22 should_run Returning javascript from evaluation
// KNS Sun Dec 12 08:29:09 2010
KOBJ.registerClosure('a60x22',
  function($K) {
    (function() {
      (function() {
        function callBacks() {};
        console.log("wohoo!");
        ;
        callBacks();
      } ());
    } ());
    KOBJ.logVerify = KOBJ.logVerify || function(t, a, c) {};
    KOBJ.logVerify('E352BF86-05C9-11E0-8A2A-ECC731FEA127', 'a60x22', 'cs.kobj.net');
  },
  '129214254756746356898872181773'
);
  • 1: 2 rules were found for processing in ruleset a60x22
  • 10: should_run rule selected because selection expression matched current url.
  • 12: execution/evaluation of should_run rule begins
  • 15: action block fired. This is important when there is a condition on the action block
  • 37: the JavaScript in the emit block in the action block of the should_run rule. This JavaScript will get executed once this JavaScript loads on the page.

Notes:

  • logging is super helpful when developing apps!
  • logging is a pig! Turn it off when you deploy a version to production. Having logging turned on, usually more than doubles the size of the JavaScript that the browser has to download from the Kynetx servers AND it takes the Kynetx servers longer to respond with the event directives when it has to generate the logging output.
  • Event directives are the ‘stuff’ that gets returned in response to a raised event. For web events, this ‘stuff’ happens to be JavaScript.
  • What gets logged changes all the time as the language and system rapidly improve. Learning to read the logging really helps catch bugs in your apps.
  • If you have suggestions on what you would like to see in the logs then let myself or the Kynetx team *know.

* Although some of us would like the power of telepathy, we don’t have it… yet. So please find us on twitter, send us an email, or visit us at our office and we will listen. : )

Get the bookmarklet to try it out yourself!

This entry was posted in Kynetx and tagged . Bookmark the permalink.

Leave a comment