Skip to content

Commit

Permalink
fixed flamegraph sorting and added exception tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Domagala committed Nov 20, 2021
1 parent a3c41a6 commit a55bcc2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# omni-trace
Omnipotent/omniscient tracing core for debugging clojure(script)

very early alpha, api is still unstable
very early alpha, api is still unstable but its only for dev time so there shouldn't be any problems


[![Clojars Project](https://img.shields.io/clojars/v/org.clojars.cyrik/omni-trace.svg)](https://clojars.org/org.clojars.cyrik/omni-trace)
Expand Down Expand Up @@ -34,7 +34,8 @@ or just through github source:
(e/insert-coin :dime)
(e/insert-coin :nickel)
(e/insert-coin :penny)
(e/press-button :a1))
(e/press-button :a1)
(e/retrieve-change-returned))
;look at traces for every function that was traced
@o/workspace
;connect to portal
Expand All @@ -48,6 +49,13 @@ or just through github source:

![Screenshot](docs/flamegraph.png)

## Features
- Works in clojure and clojurescript
- Instrument whole namespaces from the repl
- show the trace as a Flamegraph in Portal or anything else that understands Vega.js
- remembers the call that caused the exception and shows the arguments


## In the works
- better trace output to the REPL
- callbacks from Portal so you can rerun an updated function with the old params by clicking on it in the Flamegraph
Expand Down
3 changes: 3 additions & 0 deletions dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

o/instrumented-vars

(tap> ^{:portal.viewer/default :portal.viewer/hiccup}
[:div "custom thing here" [:portal.viewer/inspector {:complex :data-structure}]])

(o/uninstrument-ns 'omni-trace.testing-ns)
(macroexpand '(o/instrument-ns 'omni-trace.testing-ns))

Expand Down
7 changes: 5 additions & 2 deletions dev/user.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
(e/insert-coin :dime)
(e/insert-coin :nickel)
(e/insert-coin :penny)
(e/press-button :a1))
(e/press-button :a1)
(e/retrieve-change-returned))
;look at traces for every function that was traced
@o/workspace
;connect to portal
(def portal (p/open))
(add-tap #'p/submit)
;send the trace to portal as a vegajs flamegraph
(tap> (flame/flamegraph (flame/flamedata @o/workspace)))
(tap> ^{:portal.viewer/default :portal.viewer/hiccup} [:button {:onclick (str "alert('123')")} (str "alert('123')")])
[:button {:onclick "alert('123')"} "Test it!"]
;remove tracing from a namesapce
(o/uninstrument-ns 'omni-trace.testing-ns)

(o/reset-workspace!)
o/instrumented-vars
(macroexpand '(o/uninstrument-ns 'omni-trace.testing-ns))
(alter-meta! (var omni-trace.testing-ns/insert-coin) assoc-in [:stuffs] "yeah123")
Expand Down
3 changes: 2 additions & 1 deletion src/omni_trace/flamegraph.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
{:signal
"{n: datum.name,
a:datum.args,
t:datum.thrown,
r: datum.return}"}}
:update
{:x {:field "a0"}
Expand All @@ -47,7 +48,7 @@
:parentKey "parent"}
{:type "partition"
;:field "size"
:sort {:field "value"}
:sort {:field "id"}
:padding 2
:size
[{:signal "width"}
Expand Down
21 changes: 17 additions & 4 deletions src/omni_trace/omni_trace.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,29 @@
#?(:clj (System/currentTimeMillis)
:cljs (.now js/Date)))

(defn reset-workspace! []
(reset! workspace {}))

(defn log [workspace id trace]
(swap! workspace assoc id trace))

(defn trace-fn-call [name f args opts]
(let [parent (or *trace-log-parent*
{:workspace (::workspace opts) :parent :root})
call-id (keyword (gensym))
call-id (keyword (gensym ""))
before-time (now)
this (assoc parent :parent call-id)
res (binding [*trace-log-parent* this]
(apply f args))]
(swap! (:workspace parent)
#(assoc % call-id {:id call-id :name name :args args :start before-time :end (now) :parent (:parent parent) :return res}))
(try
(apply f args)
(catch #?(:clj Throwable :cljs :default) t
(log (:workspace parent)
call-id
{:id call-id :name name :args args :start before-time :end (now) :parent (:parent parent) :thrown (#?(:clj Throwable->map :cljs identity) t)})
(throw t))))]
(log (:workspace parent)
call-id
{:id call-id :name name :args args :start before-time :end (now) :parent (:parent parent) :return res})
res))

(defn instrumented [sym v opts]
Expand Down
1 change: 1 addition & 0 deletions src/omni_trace/testing_ns.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@
(defn retrieve-change-returned
[machine]
[(:change-returned machine)
(throw #?(:cljs (js/Error. "Oops") :clj (Exception. "Oops")))
(dissoc machine :change-returned)])

0 comments on commit a55bcc2

Please sign in to comment.