Skip to content

Commit 05afccf

Browse files
committed
Fix db exception when using metrics
1 parent c18129f commit 05afccf

File tree

6 files changed

+39
-26
lines changed

6 files changed

+39
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix db exception.
6+
57
## 0.56.1
68

79
- Fix usage reporting.

src/eca/handlers.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
:client-info (:client-info params)
2424
:workspace-folders (:workspace-folders params)
2525
:client-capabilities (:capabilities params))
26+
(metrics/set-extra-metrics! db*)
2627
(when-not (:pureConfig config)
2728
(db/load-db-from-cache! db* config metrics))
2829

src/eca/metrics.clj

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
(ns eca.metrics
22
(:require
33
[clojure.string :as string]
4+
[eca.config :as config]
45
[eca.logger :as logger]
5-
[eca.shared :as shared]
6-
[eca.config :as config]))
6+
[eca.shared :as shared]))
7+
8+
(set! *warn-on-reflection* true)
9+
10+
(def ^:dynamic extra-base-metrics {})
11+
12+
(defn set-extra-metrics! [db*]
13+
(alter-var-root #'extra-base-metrics
14+
(fn [_]
15+
{:client-name (:name (:client-info @db*))
16+
:client-version (:version (:client-info @db*))
17+
:workspace-roots (string/join ", " (map (comp shared/uri->filename :uri)
18+
(:workspace-folders @db*)))})))
719

820
(defprotocol IMetrics
921
(start! [this])
1022
(count! [this name value attrs]))
1123

12-
(defrecord NoopMetrics [db*]
24+
(defrecord NoopMetrics []
1325
IMetrics
1426
(start! [_])
1527
(count! [_ _ _ _]))
1628

17-
(defn default-attrs [db*]
18-
{:hostname (shared/hostname)
19-
:client-name (:name (:client-info @db*))
20-
:client-version (:version (:client-info @db*))
21-
:server-version (config/eca-version)
22-
:os-name (System/getProperty "os.name")
23-
:os-version (System/getProperty "os.version")
24-
:os-arch (System/getProperty "os.arch")
25-
:workspace-roots (string/join ", " (map (comp shared/uri->filename :uri)
26-
(:workspace-folders @db*)))})
29+
(defn default-attrs []
30+
(merge extra-base-metrics
31+
{:hostname (shared/hostname)
32+
:server-version (config/eca-version)
33+
:os-name (System/getProperty "os.name")
34+
:os-version (System/getProperty "os.version")
35+
:os-arch (System/getProperty "os.arch")}))
2736

2837
(defn format-time-delta-ms [start-time end-time]
2938
(format "%.0fms" (float (/ (- end-time start-time) 1000000))))
@@ -33,7 +42,10 @@
3342

3443
(defn metrify-task [{:keys [task-id metrics time]}]
3544
(logger/info (str task-id " " time))
36-
(count! metrics (str "task-" (name task-id)) 1 (default-attrs (:db* metrics))))
45+
(try
46+
(count! metrics (str "task-" (name task-id)) 1 (default-attrs))
47+
(catch Exception e
48+
(logger/error e))))
3749

3850
(defmacro task*
3951
"Executes `body` logging `message` formatted with the time spent
@@ -54,5 +66,5 @@
5466
(meta &form)))
5567

5668
(defn count-up! [name extra-attrs metrics]
57-
(count! metrics name 1 (merge (default-attrs (:db* metrics))
69+
(count! metrics name 1 (merge (default-attrs)
5870
extra-attrs)))

src/eca/opentelemetry.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
[io.opentelemetry.sdk.autoconfigure AutoConfiguredOpenTelemetrySdk]
88
[java.util.function Function]))
99

10-
(defrecord OtelMetrics [db* config]
10+
(defrecord OtelMetrics [otlp-config]
1111
metrics/IMetrics
1212

1313
(start! [_this]
1414
(otel-api/set-global-otel!
1515
(-> (AutoConfiguredOpenTelemetrySdk/builder)
16-
(.addPropertiesCustomizer ^Function (constantly (:otlp config)))
16+
(.addPropertiesCustomizer ^Function (constantly otlp-config))
1717
(.build)
1818
.getOpenTelemetrySdk)))
1919

src/eca/server.clj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,9 @@
117117
(jsonrpc.server/send-request server "editor/getDiagnostics" (assoc-some {} :uri uri)))))
118118

119119
(defn ^:private ->Metrics [db*]
120-
(let [config (config/all @db*)]
121-
(if (:otlp config)
122-
(opentelemetry/->OtelMetrics db* (config/all @db*))
123-
(metrics/->NoopMetrics db*))))
120+
(if-let [otlp-config (:otlp (config/all @db*))]
121+
(opentelemetry/->OtelMetrics otlp-config)
122+
(metrics/->NoopMetrics)))
124123

125124
(defn start-server! [server]
126125
(let [db* (atom db/initial-db)

test/eca/test_helper.clj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@
3131
(editor-diagnostics [_ _uri] (future {:diagnostics @diagnostics*})))
3232

3333
(defn ^:private make-components []
34-
(let [db* (atom db/initial-db)]
35-
{:db* (atom db/initial-db)
36-
:messenger (->TestMessenger (atom {}) (atom []))
37-
:metrics (metrics/->NoopMetrics db*)
38-
:config config/initial-config}))
34+
{:db* (atom db/initial-db)
35+
:messenger (->TestMessenger (atom {}) (atom []))
36+
:metrics (metrics/->NoopMetrics)
37+
:config config/initial-config})
3938

4039
(def components* (atom (make-components)))
4140
(defn components [] @components*)

0 commit comments

Comments
 (0)