           (swap! traces conj (assoc ~trace
                                :duration duration#
                                :end (interop/now)))
-          (run-tracing-callbacks!))))
+          (run-tracing-callbacks! end#))))
 
  (defmacro with-trace
      "Create a trace inside the scope of the with-trace macro

             #?(:cljs (catch :default e
                        (console :error "Error thrown from trace cb" k "while storing" @traces e)))))
      (reset! traces []))
    50))


(macros/deftime
  (defmacro finish-trace [trace]

   :child-of  (or child-of (:id *current-trace*))
   :start     (interop/now)})

;; On debouncing
;;
;; We debounce delivering traces to registered cbs so that
;; we can deliver them in batches. This aids us in efficiency
;; but also importantly lets us avoid slowing down the host
;; application by running any trace code in the critical path.
;;
;; We add a lightweight check on top of goog.functions/debounce
;; to avoid constant setting and cancelling of timeouts. This
;; means that we will deliver traces between 10-50 ms from the
;; last trace being created, which still achieves our goals.

(def debounce-time 50)

(defn debounce [f interval]
  #?(:cljs (goog.functions/debounce f interval)
     :clj  (f)))

(def schedule-debounce
  (debounce
    (fn tracing-cb-debounced []
      (doseq [[k cb] @trace-cbs]
