-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
702 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
(ns ethlance.server.tracing.api | ||
(:require | ||
["@opentelemetry/api" :refer [trace context SpanStatusCode ROOT_CONTEXT]])) | ||
|
||
(def SPAN_STATUS_OK (.-OK SpanStatusCode)) | ||
(def SPAN_STATUS_ERROR (.-ERROR SpanStatusCode)) | ||
|
||
(defn set-span-context! [span] | ||
(.setSpan trace (.active context) span)) | ||
|
||
(defn active-context [] | ||
(.active context)) | ||
|
||
(defn set-span-attributes! | ||
[span attributes] | ||
(doseq [[k v] attributes] (.setAttribute span k v)) | ||
span) | ||
|
||
(defonce tracer (.getTracer trace "ethl-dev" "0.0.1")) | ||
|
||
(defn start-span | ||
([span-name] | ||
(start-span span-name nil (active-context))) | ||
([span-name attributes] | ||
(start-span span-name attributes (active-context))) | ||
([span-name attributes context] | ||
(.startSpan tracer span-name (clj->js {"attributes" attributes}) context))) | ||
|
||
(defn start-nested-span [parent span-name & [attributes]] | ||
(let [ctx (set-span-context! parent) | ||
span (start-span span-name attributes ctx)] | ||
(set-span-attributes! span attributes))) | ||
|
||
(defn end-span! [span] | ||
(.end span)) | ||
|
||
(defn add-event! [span event-name k-v-map] | ||
(.addEvent span event-name (clj->js k-v-map))) | ||
|
||
(defn start-active-span [span-name callback] | ||
(.startActiveSpan tracer span-name callback)) | ||
|
||
(defn with-active-span [span-name callback] | ||
(-> (start-active-span span-name callback) | ||
(end-span! ,,,))) | ||
|
||
(defn get-active-span | ||
([] | ||
(.getActiveSpan trace))) | ||
|
||
(defn set-span-error! | ||
[span error & [message]] | ||
(doto span | ||
(.setStatus ,,, (clj->js {"code" SPAN_STATUS_ERROR "message" message})) | ||
(.recordException ,,, error))) | ||
|
||
(defn set-span-ok! | ||
[span] | ||
(.setStatus span (clj->js {"code" SPAN_STATUS_OK}))) | ||
|
||
(defn with-context | ||
[provided-context fn-to-call] | ||
(.with context provided-context fn-to-call js/undefined)) | ||
|
||
(defn with-span-context [span fn-to-call] | ||
(with-context (set-span-context! span) fn-to-call)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns ethlance.server.tracing.macros | ||
(:require | ||
[taoensso.timbre] | ||
[cljs.core.async.impl.ioc-macros :as ioc] | ||
[district.shared.error-handling])) | ||
|
||
(defmacro go! | ||
"just like go, just executes immediately until the first put/take" | ||
[& body] | ||
`(let [c# (cljs.core.async/chan 1) | ||
f# ~(ioc/state-machine body 1 &env ioc/async-custom-terminators) | ||
state# (-> (f#) | ||
(ioc/aset-all! cljs.core.async.impl.ioc-helpers/USER-START-IDX c#))] | ||
(cljs.core.async.impl.ioc-helpers/run-state-machine state#) | ||
c#)) | ||
|
||
(defmacro safe-go! [& body] | ||
`(go! | ||
(try | ||
~@body | ||
(catch :default e# | ||
(when-let [span# (ethlance.server.tracing.api/get-active-span)] | ||
(ethlance.server.tracing.api/set-span-error! span# e#)) | ||
(taoensso.timbre/error "Go block exception" | ||
(merge {:error e#} | ||
(ex-data e#) | ||
~(district.shared.error-handling/compiletime-info &env &form *ns*))) | ||
e#)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
(ns ethlance.server.tracing.macros | ||
(:require-macros [ethlance.server.tracing.macros])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(ns ethlance.server.tracing.setup | ||
(:require | ||
["@opentelemetry/sdk-node" :refer [NodeSDK]] | ||
["@opentelemetry/sdk-trace-node" :refer [ConsoleSpanExporter]] | ||
["@opentelemetry/exporter-trace-otlp-http" :refer [OTLPTraceExporter]] | ||
|
||
["@opentelemetry/sdk-metrics" :refer [PeriodicExportingMetricReader | ||
ConsoleMetricExporter]] | ||
["@opentelemetry/resources" :refer [Resource]] | ||
["@opentelemetry/semantic-conventions" :refer [SEMRESATTRS_SERVICE_NAME | ||
SEMRESATTRS_SERVICE_VERSION]] | ||
["@opentelemetry/api" :refer [trace]])) | ||
|
||
|
||
(def exporters | ||
{:OTLPTraceExporter (fn [config] (new OTLPTraceExporter (clj->js config))) | ||
:ConsoleSpanExporter (fn [& [_config]] (new ConsoleSpanExporter))}) | ||
|
||
(defn get-exporter [{:keys [name config]}] | ||
((get exporters name) config)) | ||
|
||
(defn init-sdk [config] | ||
(let [service-name (get-in config [:sdk :name]) | ||
service-version (get-in config [:sdk :version]) | ||
resource (new Resource (clj->js {SEMRESATTRS_SERVICE_NAME service-name | ||
SEMRESATTRS_SERVICE_VERSION service-version})) | ||
trace-exporter (get-exporter (get-in config [:trace-exporter])) | ||
metric-exporter (new ConsoleMetricExporter) | ||
params {:resource resource | ||
:traceExporter trace-exporter | ||
:metricReader (new PeriodicExportingMetricReader (clj->js {:exporter metric-exporter}))}] | ||
(new NodeSDK (clj->js params)))) | ||
|
||
(def sdk (atom nil)) | ||
|
||
(defn start [config] | ||
(let [instance (init-sdk config)] | ||
(.start instance) | ||
(reset! sdk instance))) | ||
|
||
(defn stop [] | ||
(.shutdown @sdk)) | ||
|
||
;; It’s generally recommended to call getTracer in your app when you need it | ||
;; rather than exporting the tracer instance to the rest of your app. | ||
;; This helps avoid trickier application load issues when other required dependencies are involved. | ||
(defn get-tracer [scope-name scope-version] | ||
(.getTracer trace scope-name scope-version)) | ||
|
||
(defonce tracer (get-tracer "syncer" "0.0.1")) |
Oops, something went wrong.