-
Notifications
You must be signed in to change notification settings - Fork 47
/
cloudwatch.clj
144 lines (112 loc) · 4.59 KB
/
cloudwatch.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(ns com.brunobonacci.mulog.publishers.cloudwatch
(:require [com.brunobonacci.mulog.publisher :as p]
[com.brunobonacci.mulog.buffer :as rb]
[com.brunobonacci.mulog.utils :as ut]
[com.brunobonacci.mulog.common.json :as json]
[cognitect.aws.client.api :as aws]))
;;
;; On CloudWatchLogs, the following constrain must be respected.
;; "Log events in a single PutLogEvents request must be in chronological order."
;;
;;
(defn- has-invalid-token?
[rs]
(-> rs
(:__type)
(= "InvalidSequenceTokenException")))
(defn- has-anomaly?
[rs]
(contains? rs :cognitect.anomalies/category))
(defn- create-cloudwatch-client
[params]
(aws/client params))
(defn- create-log-stream
[client group-name stream-name]
(aws/invoke client {:op :CreateLogStream
:request {:logGroupName group-name
:logStreamName stream-name}}))
(defn- publish!
[cloudwatch-client group-name stream-name records next-token]
(let [rq {:logGroupName group-name
:logStreamName stream-name
:logEvents records}
token @next-token
rs (aws/invoke cloudwatch-client {:op :PutLogEvents
:request (if (nil? token)
rq
(merge rq token))})]
(if (has-anomaly? rs)
(if (has-invalid-token? rs)
(swap! next-token assoc :sequenceToken (:expectedSequenceToken rs))
(throw
(ex-info
(format "μ/log cloudwatch publisher publish failure, group '%s' stream '%s' reason: %s "
(str group-name)
(str stream-name)
(str (:message rs)))
{:rs rs
:group-name group-name
:stream-name stream-name})))
(swap! next-token assoc :sequenceToken (:nextSequenceToken rs)))))
(defn- put-log-events
[cw-client stream-name {:keys [group-name] :as config} records next-token]
(let [request (->> records
(map (juxt #(get % :mulog/timestamp) json/to-json))
(map (fn [[k v]] {:timestamp k :message v}))
(sort-by :timestamp))]
(publish! cw-client group-name stream-name request next-token)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| C L O U D W A T C H L O G S |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftype CloudwatchPublisher [config buffer transform cw-client stream-name next-token]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_]
buffer)
(publish-delay [_]
(:publish-delay config))
(publish [_ buffer]
;; items are pairs [offset <item>]
(let [items (take (:max-items config) (rb/items buffer))
last-offset (-> items last first)]
(if-not (seq items)
buffer
;; else send to cloudwatch
(do
(put-log-events cw-client stream-name config (transform (map second items)) next-token)
(rb/dequeue buffer last-offset))))))
(def ^:const DEFAULT-CONFIG
{;; name of the cloudwatch log group where to put the data (REQUIRED)
;;:group-name "mulog"
:max-items 5000
:publish-delay 1000
;; function to transform records
:transform identity
:cloudwatch-client-config {:api :logs}})
(defn cloudwatch-publisher
[{:keys [group-name] :as config}]
{:pre [group-name]}
(let [cfg (->> config
(merge DEFAULT-CONFIG))
cloudwatch-client (create-cloudwatch-client (:cloudwatch-client-config cfg))
token (atom nil)
stream-name (ut/puid)
rs (create-log-stream cloudwatch-client group-name stream-name)]
(if (has-anomaly? rs)
(throw
(ex-info
(format "μ/log cloudwatch publisher initialization failure, group: '%s', stream: '%s', reason: %s"
(str group-name)
(str stream-name)
(:message rs))
{:rs rs
:group-name group-name
:stream-name stream-name}))
(CloudwatchPublisher.
cfg
(rb/agent-buffer 10000)
(or (:transform cfg) identity)
cloudwatch-client
stream-name
token))))