Skip to content

Commit

Permalink
logs: add -posthog thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolajpp committed Nov 21, 2024
1 parent 94df8a0 commit 91e4763
Showing 1 changed file with 61 additions and 0 deletions.
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=~s30
::
=, strand=strand:spider
^- thread:spider
:: .arg: a pair of origin agent and log item
::
|= arg=vase
=/ m (strand ,vase)
^- form:m
=+ !<(arg=(unit (pair @tas log-item:l)) arg)
?> ?=(^ arg)
=* agent 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)
'agent'^s+agent
==
=/ event=json
%- pairs:enjs:format
:~ 'api_key'^s+posthog-key
timestamp/(id-event:enjs:l id.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))
==
|-
?: =(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)
==
?> ?=(%finished -.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))
(pure:m !>(`client-response))

0 comments on commit 91e4763

Please sign in to comment.