-
Notifications
You must be signed in to change notification settings - Fork 64
/
mapper.clj
61 lines (58 loc) · 3.36 KB
/
mapper.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns ziggurat.mapper
(:require [sentry-clj.async :as sentry]
[ziggurat.metrics :as metrics]
[ziggurat.messaging.producer :as producer]
[ziggurat.new-relic :as nr]
[ziggurat.sentry :refer [sentry-reporter]])
(:import (java.time Instant)))
(defn- send-msg-to-channel [channels message topic-entity return-code]
(when-not (contains? (set channels) return-code)
(throw (ex-info "Invalid mapper return code" {:code return-code})))
(producer/publish-to-channel-instant-queue topic-entity return-code message))
(defn mapper-func [mapper-fn topic-entity channels]
(fn [message]
(let [topic-entity-name (name topic-entity)
new-relic-transaction-name (str topic-entity-name ".handler-fn")
metric-namespace (str topic-entity-name ".message-processing")]
(nr/with-tracing "job" new-relic-transaction-name
(try
(let [start-time (.toEpochMilli (Instant/now))
return-code (mapper-fn message)
end-time (.toEpochMilli (Instant/now))]
(metrics/report-time (str topic-entity-name ".handler-fn-execution-time") (- end-time start-time))
(case return-code
:success (metrics/increment-count metric-namespace "success")
:retry (do (metrics/increment-count metric-namespace "retry")
(producer/retry message topic-entity))
:skip (metrics/increment-count metric-namespace "skip")
:block 'TODO
(do
(send-msg-to-channel channels message topic-entity return-code)
(metrics/increment-count metric-namespace "success"))))
(catch Throwable e
(producer/retry message topic-entity)
(sentry/report-error sentry-reporter e (str "Actor execution failed for " topic-entity-name))
(metrics/increment-count metric-namespace "failure")))))))
(defn channel-mapper-func [mapper-fn topic-entity channel]
(fn [message]
(let [topic-entity-name (name topic-entity)
channel-name (name channel)
metric-namespace (str topic-entity-name "." channel-name)
message-processing-namespace (str metric-namespace ".message-processing")]
(nr/with-tracing "job" metric-namespace
(try
(let [start-time (.toEpochMilli (Instant/now))
return-code (mapper-fn message)
end-time (.toEpochMilli (Instant/now))]
(metrics/report-time (str metric-namespace ".execution-time") (- end-time start-time))
(case return-code
:success (metrics/increment-count message-processing-namespace "success")
:retry (do (metrics/increment-count message-processing-namespace "retry")
(producer/retry-for-channel message topic-entity channel))
:skip (metrics/increment-count message-processing-namespace "skip")
:block 'TODO
(throw (ex-info "Invalid mapper return code" {:code return-code}))))
(catch Throwable e
(producer/retry-for-channel message topic-entity channel)
(sentry/report-error sentry-reporter e (str "Channel execution failed for " topic-entity-name " and for channel " channel-name))
(metrics/increment-count message-processing-namespace "failure")))))))