moonlog-loki

    Grafana Loki sink for moonbitstack/moonlog.

    moonlog
    log
    loki
    grafana-loki
    moonbit
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    35 minutes ago
    Downloads
    4

    #moonlog-loki

    Grafana Loki sink for moonbitstack/moonlog.

    Limits

    pub(all) struct Limits {
    capacity : Int
    batch : Int
    timeout : Int
    }

    How much a Loki sink holds while it waits for its next push, and how much goes in one request.

    capacity is a ceiling on entries waiting to be sent; when the queue is full the oldest goes, because a shipper that stops the program it logs for is worse than one that loses the start of an outage. timeout is in seconds and applies to one request.

    Limits::batch

    fn Limits::batch(self : Limits, batch : Int) -> Limits

    The same limits, pushng a different number of entries at a time.

    Limits::capacity

    fn Limits::capacity(self : Limits, capacity : Int) -> Limits

    The same limits, holding a different number of entries.

    Limits::new

    fn Limits::new(capacity? : Int, batch? : Int, timeout? : Int) -> Limits

    Eight thousand entries, which is a few seconds of a busy service, and a thousand of them — a second at a busy service — in one request. The numbers the shared specification gives a Loki queue.

    Limits::timeout

    fn Limits::timeout(self : Limits, timeout : Int) -> Limits

    The same limits, waiting a different number of seconds for an answer.

    Loki

    pub struct Loki {
    url : String
    labels : Array[(String, String)]
    tenant : String?
    auth : (String, String)?
    limits : Limits
    clock : () -> Int64
    held : Array[Pending]
    sent : Int64
    rejected : Int64
    dropped : Int64
    failures : Int64
    }

    A sink that ships entries to Loki.

    write only queues: a log call must not wait for a network. push does the sending, from an async context the application already runs, so the retry that a busy Loki asks for lives in the caller's loop — and the slot's own queue, retries and health machine still govern how the sink is treated when a push keeps failing.

    Each level gets a stream of its own, so a query can select on it without parsing a line, and the entry's fields travel as structured metadata.
    impl Sink for Loki

    Loki::body

    fn Loki::body(self : Loki) -> String

    The push body the pending entries would make, without taking them.

    What a check before a deployment prints, and what a test reads instead of standing up a server.

    Loki::dropped

    fn Loki::dropped(self : Loki) -> Int64

    How many were dropped because the queue was full.

    Loki::encode

    fn Loki::encode(self : Loki, batch : Array[Pending]) -> String

    The push body: one stream per level, each with its own labels.

    Loki::failures

    fn Loki::failures(self : Loki) -> Int64

    How many pushes came back with something to retry.

    Loki::new

    fn Loki::new(url~ : String, labels? : Array[(String, String)], tenant? : String, auth? : (String, String), limits? : Limits, clock? : () -> Int64) -> Loki

    A sink pushing to url, which is the Loki base address: /loki/api/v1/push is appended unless the address already ends with it.

    labels are the stream's own, such as the job and the host; the level is added per stream. auth is a user and password for basic authentication, and tenant is sent as X-Scope-OrgID for a multi-tenant Loki.

    Loki::pending

    fn Loki::pending(self : Loki) -> Int

    How many entries are waiting.

    Loki::push

    async fn Loki::push(self : Loki) -> Report

    Pushes one batch, and answers what became of it.

    Call it from a loop the application already runs; retry in the answer is that loop's cue to wait and come back, and everything it did not send stays queued, in order.

    Loki::rejected

    fn Loki::rejected(self : Loki) -> Int64

    How many Loki refused for good.

    Loki::sent

    fn Loki::sent(self : Loki) -> Int64

    How many entries Loki has taken.

    Loki::slot

    fn Loki::slot(self : Loki, name? : String) ->
    Slot

    A slot around this sink.

    Loki::url

    fn Loki::url(self : Loki) -> String

    The address entries are pushed to.

    Loki::write

    fn Loki::write(self : Loki, entry :
    Entry
    ) -> Result[Unit, String]

    Keeps the entry, with its fields as metadata, for the next push.

    Pending

    type Pending

    One entry waiting to be shipped: what it says, when it happened, and the fields that go with it.

    Report

    pub(all) struct Report {
    sent : Int
    rejected : Int
    retry : String?
    }

    What one push did.

    A batch either lands, is refused for good, or is worth trying again: Loki answers 400 for an entry it will never take — a timestamp outside the tenant's window is the usual one — and 429 or 5xx when it is merely busy. Sending the first kind again would spin for ever, so those entries are dropped and counted, and only the second kind comes back to the queue.

    Report::idle

    fn Report::idle() -> Report

    Nothing was pending, so nothing was tried.

    Report::pushed

    fn Report::pushed(self : Report) -> Bool

    Whether the push asked to be repeated.

    Verdict

    pub(all) enum Verdict {
    Accepted
    Refused
    Retry
    } derive(Eq,
    Debug
    )

    What to do with a batch that came back as status.

    Verdict::equal

    fn Verdict::equal(Verdict, Verdict) -> Bool
    automatically derived

    Verdict::not_equal

    fn Verdict::not_equal(x : Verdict, y : Verdict) -> Bool
    automatically derived

    Verdict::of

    fn Verdict::of(status : Int) -> Verdict

    How a status is read: 2xx landed, 400 and 404 will never land, everything else — 408, 429, 5xx — is worth another try.

    Verdict::to_repr

    automatically derived