notices.bend

bend-kit-camber@0.12.0.0 · 34 lines · 0 definitions

1# Application-only or actual transport completion, with a closed safe access projection.
2import Base
3import bend-kit-time@0.2.0.0/time.bend as Time
4import bend-kit-int@0.2.0.0/int.bend as Int
5import bend-kit-http@0.36.0.0/completion.bend as Transport
6import bend-kit-notch@0.2.0.0/notch.bend as Notch
7
8type Outcome is Data:
9  ApplicationOnly{}
10  Transport{outcome: Transport.Outcome}
11
12type Notice is Data:
13  Notice{status: U32, duration: Time.Duration, mapped: Bool, stopped: Bool, outcome: Outcome}
14
15# No caller-supplied logger context or request fields can enter an access line.
16type Access is Data:
17  Disabled{}
18  Enabled{format: Notch.Format}
19
20def outcome.name(outcome: Outcome) -> String:
21  match outcome:
22    case ApplicationOnly{}: "application_only"
23    case Transport{Transport.HostAccepted{}}: "host_accepted"
24    case Transport{Transport.WriteFailed{code, message}}: "write_failed"
25    case Transport{Transport.IncompleteResponse{}}: "incomplete_response"
26
27def access(config: Access, notice: Notice) -> IO(Unit):
28  match config:
29    case Disabled{}: IO.pure(Unit, Unit{})
30    case Enabled{format}:
31      Notice{status, duration, mapped, stopped, outcome} = notice
32      Notch.info(Notch.Logger{Notch.Info{}, format, Nil{}}, "access",
33        [Notch.num("status", status), Notch.str("duration_ms", Int.I64.show(Time.Duration.to_ms(duration))),
34         Notch.flag("mapped", mapped), Notch.flag("stopped", stopped), Notch.str("outcome", outcome.name(outcome))])