-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4214 from tloncorp/mp/logs
logs: new agent to gather and submit app logs
- Loading branch information
Showing
10 changed files
with
278 additions
and
9 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
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,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 | ||
== | ||
-- |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
:~ %groups | ||
:~ %logs | ||
%groups | ||
%chat | ||
%heap | ||
%diary | ||
|
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,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) | ||
== | ||
== | ||
-- | ||
-- |
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,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] | ||
== | ||
-- |
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,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)) |
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,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)) | ||
!>(&) | ||
-- |