Skip to content

Commit e215c88

Browse files
committed
[otl] added open-telemetry publisher to publisher factory
1 parent 73406bd commit e215c88

File tree

5 files changed

+68
-79
lines changed

5 files changed

+68
-79
lines changed

doc/publishers/open-telemetry-publisher.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## OpenTelemetry (OTLP) publisher
2-
![since v0.10.0](https://img.shields.io/badge/since-v0.2.0-brightgreen)
2+
![since v0.10.0](https://img.shields.io/badge/since-v0.10.0-brightgreen)
33

44
This publisher doesn't require java/clojure instrumentation libraries.
55
It relies on existing ***μ/trace*** forms to capture and publish traces.
@@ -31,6 +31,8 @@ The available configuration options:
3131
;; OpenTelemetry Collector endpoint for OTLP HTTP/JSON (REQUIRED)
3232
:url "http://localhost:4318/"
3333

34+
;; Whether to send traces or logs (metrics in mulog are just logs)
35+
:send :traces
3436

3537
;; the maximum number of events which can be sent in a single
3638
;; batch request to Zipkin
@@ -55,12 +57,36 @@ The available configuration options:
5557

5658
How to use it:
5759

60+
In order to send **traces** and **logs** use:
61+
``` clojure
62+
(μ/start-publisher!
63+
{:type :multi
64+
:publishers
65+
[{:type :open-telemetry
66+
:send :traces
67+
:url "http://localhost:4318/"}
68+
{:type :open-telemetry
69+
:send :logs
70+
:url "http://localhost:4318/"}]})
71+
```
72+
73+
In order to send **traces** only use:
5874
``` clojure
5975
(μ/start-publisher!
6076
{:type :open-telemetry
77+
:send :traces
6178
:url "http://localhost:4318/"})
6279
```
6380

81+
In order to send **logs** only use:
82+
``` clojure
83+
(μ/start-publisher!
84+
{:type :open-telemetry
85+
:send :logs
86+
:url "http://localhost:4318/"})
87+
```
88+
89+
6490
Here is an example of how the traces look like:
6591

6692
![traces](../images/nested-traces.png)

mulog-core/dev/user.clj

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,15 @@
155155

156156
(def st2
157157
(u/start-publisher!
158-
{:type :zipkin :url "http://localhost:9411/"}))
159-
160-
(comment
161-
(require '[com.brunobonacci.mulog.publishers.open-telemetry :as ot])
158+
{:type :multi
159+
:publishers
160+
[{:type :open-telemetry
161+
:send :traces
162+
:url "http://localhost:4318/"}
163+
{:type :open-telemetry
164+
:send :logs
165+
:url "http://localhost:4318/"}]}))
162166

163-
(def otp (ot/open-telemetry-publisher
164-
{:type :open-telemetry
165-
:url "http://localhost:4318/"}))
166-
167-
(def st2
168-
(u/start-publisher! {:type :inline :publisher otp}))
169-
)
170167

171168
(u/set-global-context! {:app "demo" :version 1 :env "local"})
172169

mulog-core/src/com/brunobonacci/mulog/publisher.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,12 @@
340340
"com.brunobonacci.mulog.publishers.mbean-sampler/mbean-sampler-publisher"
341341
"com.brunobonacci/mulog-mbean-sampler"
342342
config))
343+
344+
345+
346+
(defmethod publisher-factory :open-telemetry
347+
[config]
348+
(load-dynamic-publisher
349+
"com.brunobonacci.mulog.publishers.open-telemetry/open-telemetry-publisher"
350+
"com.brunobonacci/mulog-opentelemetry"
351+
config))

mulog-opentelemetry/src/com/brunobonacci/mulog/publishers/open_telemetry.clj

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@
306306
;; function to transform records
307307
:transform identity
308308

309-
;; send only traces, only logs or both
310-
:send [:traces :logs]
309+
;; send traces or logs, REQUIRED
310+
;; :send :traces
311311

312312
;; extra http options
313313
:http-opts {}
@@ -366,33 +366,15 @@
366366
;; one for each.
367367
;;
368368
(defn open-telemetry-publisher
369-
[{:keys [url max-items] :as config}]
370-
{:pre [url]}
371-
(let [config (merge DEFAULT-CONFIG config)
372-
send (:send config)]
373-
374-
(when-not (coll? send)
375-
(throw
376-
(ex-info "Invalid config. `:send` must be a collection."
377-
{:config config})))
369+
[{:keys [url send max-items] :as config}]
370+
{:pre [url send]}
371+
(let [config (merge DEFAULT-CONFIG config)]
378372

379-
(when-not (#{#{:traces} #{:logs} #{:traces :logs}}
380-
(set send))
373+
(when-not (#{:traces :logs} send)
381374
(throw
382-
(ex-info "Invalid config. `:send` must be either `[:traces]` or `[:logs]` or both: `[:traces :logs]`"
375+
(ex-info "Invalid config. `:send` must be either `:traces` or `:logs`"
383376
{:config config})))
384377

385-
(let [send (set send)
386-
387-
traces (when (:traces send)
388-
(open-telemetry-trace-publisher config))
389-
390-
logs (when (:logs send)
391-
(open-telemetry-log-publisher config))]
392-
393-
(if (and traces logs)
394-
{:type :multi
395-
:publishers
396-
[{:type :inline :publisher traces}
397-
{:type :inline :publisher logs}]}
398-
(or traces logs)))))
378+
(case send
379+
:traces (open-telemetry-trace-publisher config)
380+
:logs (open-telemetry-log-publisher config))))

mulog-opentelemetry/test/com/brunobonacci/mulog/publishers/open_telemetry_test.clj

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@
3939

4040

4141

42-
(defn system-temp-dir []
43-
(->
44-
(java.lang.System/getProperty "java.io.tmpdir")
45-
(str/replace #"/$" "")))
46-
47-
48-
4942
(defn service-ready?
5043
[host port client-settings]
5144
(fn []
@@ -82,19 +75,12 @@
8275

8376
(wait-for-condition "opentelemetry service ready" (service-ready? host port2 {}))
8477

85-
#_(def publisher (u/start-publisher!
86-
{:type :open-telemetry
87-
:url (str "http://" host ":" port "/")
88-
"publish-delay" 500}))
8978

9079
(def publisher (u/start-publisher!
91-
{:type :inline
92-
:publisher
93-
(ot/open-telemetry-publisher
94-
{:type :open-telemetry
95-
:send [:traces]
96-
:url (str "http://" host ":" port "/")
97-
:publish-delay 500})}))
80+
{:type :open-telemetry
81+
:send :traces
82+
:url (str "http://" host ":" port "/")
83+
:publish-delay 500}))
9884

9985
(def global (u/global-context))
10086
(u/set-global-context!
@@ -250,7 +236,7 @@
250236
(repl-test {:labels [:container]} "test OpenTelemetry publisher to OTLP collector [traces]"
251237

252238
(def test-id (f/flake))
253-
(def test-dir (str "./tmp/optel-" test-id))
239+
(def test-dir (str "/tmp/optel-" test-id))
254240
(io/make-parents (io/file (str test-dir "/test")))
255241
(io/copy (io/file "./test-config.yml") (io/file (str test-dir "/config.yml")))
256242

@@ -281,13 +267,10 @@
281267
"publish-delay" 500}))
282268

283269
(def publisher (u/start-publisher!
284-
{:type :inline
285-
:publisher
286-
(ot/open-telemetry-publisher
287-
{:type :open-telemetry
288-
:send [:traces]
289-
:url (str "http://" host ":" port "/")
290-
:publish-delay 500})}))
270+
{:type :open-telemetry
271+
:send :traces
272+
:url (str "http://" host ":" port "/")
273+
:publish-delay 500}))
291274

292275
(def global (u/global-context))
293276
(u/set-global-context!
@@ -406,7 +389,7 @@
406389
(repl-test {:labels [:container]} "test OpenTelemetry publisher to OTLP collector [logs]"
407390

408391
(def test-id (f/flake))
409-
(def test-dir (str "./tmp/optel-" test-id))
392+
(def test-dir (str "/tmp/optel-" test-id))
410393
(io/make-parents (io/file (str test-dir "/test")))
411394
(io/copy (io/file "./test-config.yml") (io/file (str test-dir "/config.yml")))
412395

@@ -431,19 +414,11 @@
431414
(try (.exists (io/file (str test-dir "/optel.json")))
432415
(catch Exception _ false))))
433416

434-
#_(def publisher (u/start-publisher!
435-
{:type :open-telemetry
436-
:url (str "http://" host ":" port "/")
437-
"publish-delay" 500}))
438-
439417
(def publisher (u/start-publisher!
440-
{:type :inline
441-
:publisher
442-
(ot/open-telemetry-publisher
443-
{:type :open-telemetry
444-
:send [:logs]
445-
:url (str "http://" host ":" port "/")
446-
:publish-delay 500})}))
418+
{:type :open-telemetry
419+
:send :logs
420+
:url (str "http://" host ":" port "/")
421+
:publish-delay 500}))
447422

448423
(def global (u/global-context))
449424
(u/set-global-context!

0 commit comments

Comments
 (0)