Skip to content

Commit

Permalink
Merge pull request #4214 from tloncorp/mp/logs
Browse files Browse the repository at this point in the history
logs: new agent to gather and submit app logs
  • Loading branch information
mikolajpp authored Nov 25, 2024
2 parents 0a9709d + 22d3300 commit 6e794ec
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 9 deletions.
8 changes: 6 additions & 2 deletions desk/app/activity.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/- a=activity, c=channels, ch=chat, g=groups
/+ *activity, ch-utils=channel-utils, v=volume, aj=activity-json,
imp=import-aid
/+ default-agent, verb, dbug
/+ default-agent, verb, dbug, logs
::
=/ verbose |
=>
Expand Down Expand Up @@ -108,7 +108,11 @@
++ on-agent on-agent:def
++ on-peek peek:cor
++ on-leave on-leave:def
++ on-fail on-fail:def
++ on-fail
|= [=term =tang]
^- (quip card _this)
:_ this
[(log-fail:logs /logs our.bowl (fail-event:logs term tang))]~
--
|_ [=bowl:gall cards=(list card)]
++ abet [(flop cards) state]
Expand Down
10 changes: 8 additions & 2 deletions desk/app/channels-server.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
::
/- c=channels, g=groups
/+ utils=channel-utils, imp=import-aid
/+ default-agent, verb, dbug, neg=negotiate
/+ default-agent, verb, dbug, neg=negotiate, logs
::
%- %- agent:neg
[| [~.channels^%1 ~ ~] ~]
Expand Down Expand Up @@ -58,7 +58,12 @@
::
++ on-peek on-peek:def
++ on-leave on-leave:def
++ on-fail on-fail:def
++ on-fail
|= [=term =tang]
^- (quip card _this)
:_ this
[(log-fail:logs /logs our.bowl (fail-event:logs term tang))]~
::
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
Expand Down Expand Up @@ -412,6 +417,7 @@
|= [=(pole knot) =sign:agent:gall]
^+ cor
?+ pole ~|(bad-agent-wire+pole !!)
[%logs ~] cor
[%pimp ~] cor
[%wake ~] cor
::
Expand Down
11 changes: 9 additions & 2 deletions desk/app/channels.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
::
/- c=channels, g=groups, ha=hark, activity
/- meta
/+ default-agent, verb, dbug, sparse, neg=negotiate, imp=import-aid
/+ default-agent, verb, dbug, sparse, neg=negotiate, imp=import-aid, logs
/+ utils=channel-utils, volume, s=subscriber
:: performance, keep warm
/+ channel-json
Expand Down Expand Up @@ -78,7 +78,12 @@
::
++ on-peek peek:cor
++ on-leave on-leave:def
++ on-fail on-fail:def
++ on-fail
|= [=term =tang]
^- (quip card _this)
:_ this
[(log-fail:logs /logs our.bowl (fail-event:logs term tang))]~
::
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
Expand Down Expand Up @@ -641,6 +646,8 @@
?+ pole ~|(bad-agent-wire+pole !!)
~ cor
[%pimp ~] cor
[%logs ~] cor
::
[%hark ~]
?> ?=(%poke-ack -.sign)
?~ p.sign cor
Expand Down
9 changes: 7 additions & 2 deletions desk/app/groups.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/- meta
/- e=epic
/+ default-agent, verb, dbug
/+ v=volume, s=subscriber, imp=import-aid
/+ v=volume, s=subscriber, imp=import-aid, logs
/+ of
/+ epos-lib=saga
:: performance, keep warm
Expand Down Expand Up @@ -69,7 +69,11 @@
++ on-peek peek:cor
::
++ on-leave on-leave:def
++ on-fail on-fail:def
++ on-fail
|= [=term =tang]
^- (quip card _this)
:_ this
[(log-fail:logs /logs our.bowl (fail-event:logs term tang))]~
::
++ on-agent
|= [=wire =sign:agent:gall]
Expand Down Expand Up @@ -636,6 +640,7 @@
?+ pole ~|(bad-agent-take/pole !!)
~ cor
[%epic ~] (take-epic sign)
[%logs ~] cor
[%helm *] cor
[%activity %submit *] cor
[%groups %role ~] cor
Expand Down
91 changes: 91 additions & 0 deletions desk/app/logs.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
:: logs: gather log events and submit reports
::
/- l=logs
/+ default-agent
=>
|%
+$ card card:agent:gall
+$ current-state [%0 ~]
--
=| current-state
=* state -
^- agent:gall
=<
|_ =bowl:gall
+* this .
def ~(. (default-agent this %|) bowl)
cor ~(. +> [bowl ~])
::
++ on-init on-init:def
++ on-save on-save:def
++ on-load on-load:def
++ on-poke
|= [=mark =vase]
^- (quip card _this)
=^ cards state
abet:(poke:cor mark vase)
[cards this]
++ on-watch on-watch:def
++ on-peek on-peek:def
++ on-leave on-leave:def
++ on-agent on-agent:def
++ on-arvo
|= [=wire =sign-arvo]
=^ cards state
abet:(arvo:cor wire sign-arvo)
[cards this]
++ on-fail on-fail:def
--
::
|_ [=bowl:gall cards=(list card)]
++ abet [(flop cards) state]
++ cor .
++ emit |=(=card cor(cards [card cards]))
++ emil |=(caz=(list card) cor(cards (welp (flop caz) cards)))
++ give |=(=gift:agent:gall (emit %give gift))
::
++ send-posthog-event
|= [origin=path =time =log-event:l]
^+ cor
=/ fard=(fyrd:khan cage)
[q.byk.bowl %posthog noun+!>(`[origin time log-event])]
::
(emit %pass /posthog [%arvo %k %fard fard])
::
++ poke
|= [=mark =vase]
^+ cor
?> =(our src):bowl
?+ mark ~|(bad-mark+mark !!)
%log-action
=+ !<(=a-log:l vase)
?- -.a-log
%log
(send-posthog-event sap.bowl now.bowl +.a-log)
==
==
::
++ arvo
|= [=(pole knot) =sign-arvo]
?+ pole ~|(bad-arvo-wire+pole !!)
:: /posthog
::
[%posthog ~]
?> ?=([%khan %arow *] sign-arvo)
=/ =(avow:khan cage) p.sign-arvo
?: ?=(%| -.avow)
%- (slog tang.p.avow)
cor
=/ =cage p.avow
=+ !<(response=(unit client-response:iris) q.cage)
?~ response
((slog leaf+"logs: failed to submit event" ~) cor)
?. ?=(%finished -.u.response)
~|(bad-client-response+u.response !!)
=* status-code status-code.response-header.u.response
?: !=(200 status-code)
%- (slog leaf+"logs: failed to submit event" ~)
cor
cor
==
--
3 changes: 2 additions & 1 deletion desk/desk.bill
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
:~ %groups
:~ %logs
%groups
%chat
%heap
%diary
Expand Down
41 changes: 41 additions & 0 deletions desk/lib/logs.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/- *logs
|%
::
++ fail-event
|= [=term =tang]
^- $>(%fail log-event)
[%fail term tang]
::
++ log-fail
|= [=wire our=ship event=$>(%fail log-event)]
^- card:agent:gall
[%pass wire %agent [our %logs] %poke log-action+!>([%log event])]
::
++ enjs
=, format
|%
++ tang
|= t=^tang
^- $>(%a json)
?~ t a+~
=/ tame=(list tape)
%- zing
%+ turn t
(cury wash [0 80])
a+(turn tame tape:enjs)
::
++ log-event
|= e=^log-event
^- $>(%o json)
=* event-type -.e
?- -.e
%fail
=- ?>(?=(%o -.-) -)
%- pairs:enjs
:~ type/s+event-type
description/s+desc.e
stacktrace/(tang crash.e)
==
==
--
--
18 changes: 18 additions & 0 deletions desk/sur/logs.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/+ mp=mop-extensions
::
|%
:: $log-event
::
:: %fail: agent failure
::
+$ log-event
$% [%fail desc=term crash=tang]
==
::
:: $log-item: event with timestamp
+$ log-item [=time event=log-event]
::
+$ a-log
$% [%log log-event]
==
--
61 changes: 61 additions & 0 deletions desk/ted/posthog.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
:: posthog: submit an log event to PostHog
::
/- spider
/+ io=strandio, l=logs
::
=+ posthog-key='phc_GyI5iD7kM6RRbb1hIU0fiGmTCh4ha44hthJYJ7a89td'
=+ posthog-url='https://eu.i.posthog.com/capture/'
=+ posthog-retry=3
=+ posthog-retry-delay=~s5
::
=, strand=strand:spider
^- thread:spider
:: .arg: a pair of origin and log item
::
|= arg=vase
=/ m (strand ,vase)
^- form:m
=+ !<(arg=(unit (pair path log-item:l)) arg)
?> ?=(^ arg)
=* origin p.u.arg
=* log-item q.u.arg
;< =bowl:strand bind:m get-bowl:io
=/ log-event-json=$>(%o json) (log-event:enjs:l event.log-item)
=/ props=json
:- %o
%- ~(uni by p.log-event-json)
^- (map @t json)
%- my
:~ 'distinct_id'^s+(scot %p our.bowl)
'origin'^s+(spat origin)
==
=/ event=json
%- pairs:enjs:format
:~ 'api_key'^s+posthog-key
timestamp/(time:enjs:format time.log-item)
event/s+'Backend Log'
properties+props
==
::
=/ =request:http
:* %'POST'
posthog-url
~['content-type'^'application/json']
`(as-octs:mimes:html (en:json:html event))
==
:: retry loop
::
|-
?: =(0 posthog-retry)
(pure:m !>(~))
;< ~ bind:m (send-request:io request)
;< =client-response:iris bind:m take-client-response:io
::NOTE this logic must be adjusted when %iris supports
:: partial responses
?> ?=(%finished -.client-response)
?: =(200 status-code.response-header.client-response)
(pure:m !>(`client-response))
=* status-code status-code.response-header.client-response
%- (slog leaf+"posthog request failed: status {<status-code>}" ~)
;< ~ bind:m (sleep:io posthog-retry-delay)
$(posthog-retry (dec posthog-retry))
35 changes: 35 additions & 0 deletions desk/tests/app/logs.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/- l=logs
/+ *test-agent
/= agent /app/logs
|%
++ dap %logs-test
++ test-log-fail
%- eval-mare
=/ m (mare ,~)
;< * bind:m (do-init dap agent)
:: ;< * bind:m (set-scry-gate ,~)
;< * bind:m (jab-bowl |=(b=bowl b(sap /gall/test)))
;< =bowl:gall bind:m get-bowl
=/ ev-fail=log-event:l
[%fail %test-fail leaf+"test stacktrace" ~]
;< caz=(list card) bind:m
(do-poke log-action+!>([%log ev-fail]))
=/ fard=(fyrd:khan cage)
[q.byk.bowl %posthog noun+!>(`[`path`/gall/test [now.bowl ev-fail]])]
:: expect log submission -posthog
::
?> ?=([[%pass *] ~] caz)
=/ card=card:agent:gall i.caz
?> ?=([%pass [%posthog ~] %arvo %k %fard *] card)
:: compare fard args value
::
;< ~ bind:m
%+ ex-equal
!>(q.q.args.fard)
!>(q.q.args.p.q.card)
:: compare fard type
::
%+ ex-equal
!>((~(nest ut p.q.args.fard) | p.q.args.p.q.card))
!>(&)
--

0 comments on commit 6e794ec

Please sign in to comment.