Skip to content

Commit

Permalink
Identify the stack frames leading up to the deprecated function
Browse files Browse the repository at this point in the history
  • Loading branch information
hlship committed Jun 5, 2024
1 parent ad9b121 commit 231dc42
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
38 changes: 33 additions & 5 deletions common/src/io/pedestal/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as string]
[clj-commons.ansi :refer [perr]]))
[clj-commons.ansi :refer [perr]]
[clj-commons.format.exceptions :as e]))

(defn vec-conj
[v value]
Expand All @@ -34,9 +35,9 @@
(nil? val) nil

(string? val)
(let [slashx (string/index-of val "/")
ns-str (when slashx
(subs val 0 slashx))
(let [slashx (string/index-of val "/")
ns-str (when slashx
(subs val 0 slashx))
symbol-str (if slashx
(subs val (inc slashx))
val)]
Expand Down Expand Up @@ -89,6 +90,27 @@
(when default-var-name
(resolver nil nil default-var-name)))))

(defn- truncate-to
[limit coll]
(let [n (count coll)]
(if (<= n limit)
coll
(drop (- n limit) coll))))

(defn- call-stack
[t limit]
(let [elements (->> (e/expand-stack-trace t)
(drop-while #(string/starts-with? (:name %) "io.pedestal.internal/")))
names (->> (#'e/transform-stack-trace-elements
elements nil)
(remove :omitted)
(map #'e/format-stack-frame)
(map :name)
distinct
reverse
(truncate-to limit))]
(interpose " -> " names)))

(def *deprecations (atom #{}))

(defn deprecation-warning
Expand All @@ -99,7 +121,10 @@
[:yellow
[:bold "WARNING: "]
label
" is deprecated and may be removed in a future release"])))
" is deprecated and may be removed in a future release"]

"\nCall stack: ... "
(call-stack (RuntimeException.) 4))))

(defmacro deprecated
[label & body]
Expand All @@ -111,3 +136,6 @@
[]
(swap! *deprecations empty))

(comment

(reset-deprecations))
9 changes: 9 additions & 0 deletions tests/dev/playground.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[io.pedestal.http :as http]
[io.pedestal.http.route :as route]
[io.pedestal.tracing :as tel]
[io.pedestal.internal :as i]
[net.lewisship.trace :refer [trace]]
[clojure.core.async :refer [go <! timeout]]
[io.pedestal.tracing :as t])
Expand Down Expand Up @@ -94,10 +95,18 @@
:not-running))


(defn deprecated-fn
[]
(i/deprecated (random-uuid))
:done)

(comment
(start)
(stop)

(deprecated-fn)
(i/reset-deprecations)


(m/increment-counter ::hit-rate nil)
(m/advance-counter ::request-time {:path "/api"} 47)
Expand Down

0 comments on commit 231dc42

Please sign in to comment.