$30 off During Our Annual Pro Sale. View Details »

Deno Fest 2023 @Tokyo - Chiezo

Chiezo
October 20, 2023

Deno Fest 2023 @Tokyo - Chiezo

Chiezo

October 20, 2023
Tweet

Other Decks in Programming

Transcript

  1. Persisting logs with Deno KV
    by Shun Ueda (Chiezo @hasundue)

    View Slide

  2. Topics
    Deno KV Hackathon
    Time-series data with Deno KV
    Logging with std/log
    2

    View Slide

  3. Who's speaking?
    Chiezo @hasundue
    Legal name: Shun Ueda
    A housemaker, PhD (Engineering)
    Field: high-temperature processing of materials
    e.g. Cooking
    Amateur programmer
    Vim/Neovim (2009-)
    Julia (2016-)
    Deno (2021-)
    3

    View Slide

  4. Deno KV Hackathon
    https://deno.com/blog/deno-kv-
    hackathon
    Jun 12 - Jun 16, 2023
    I wanted CUTE merch!
    4

    View Slide

  5. What's Deno KV?
    Deno KV is a key-value database built directly into the Deno runtime,
    ... available in the Deno CLI and on Deno Deploy.
    https://docs.deno.com/kv/manual
    const kv = await Deno.openKv();
    const prefs = {
    username: "ada",
    theme: "dark",
    language: "en-US",
    };
    const result = await kv.set(["preferences", "ada"], prefs);
    const entry = await kv.get(["preferences", "ada"]);
    5

    View Slide

  6. What to build?
    Logging on Deno Deploy:
    ... After closing the logs page,
    all streamed logs are
    discarded.
    https://docs.deno.com/deploy
    /manual/logs
    Can be persisted with Deno KV?
    6

    View Slide

  7. Branding
    Name ... Paleon (paleontology)
    Character ... A cute illustration
    of a child dinosaur who is
    conducting a paleontological
    study (by Bing Image Creator)
    7

    View Slide

  8. Time-series data in Deno KV?
    JavaScript arrays as keys
    const key = [...prefix, Date.now(), crypto.randomUUID()];
    return kv.set(key, record);
    std/ulid is a better choice right now
    Ordering
    ... within a given type, they are ordered by their value.
    https://docs.deno.com/kv/manual/key_space
    const iter = kv.list({ start, end }, { limit, reverse });
    8

    View Slide

  9. Designing APIs
    Abstraction of KV interface
    import { PaleonStorage } from "./mod.ts";
    type TestLogRecord = {
    datetime: Date;
    msg: string;
    };
    const paleon = await PaleonStorage.open("my-app");
    await paleon.write({ datetime: new Date(), msg: "hello" });
    for await (const record of paleon.read()) {
    console.log(record);
    }
    9

    View Slide

  10. Integration with std/log
    import * as log from "$std/log/mod.ts";
    import { BaseHandler } from "$std/log/handlers.ts";
    class LocalPaleonHandler extends BaseHandler {
    ...
    }
    setup({
    handlers: {
    console: new handlers.ConsoleHandler("DEBUG"),
    paleon: await LocalPaleonHandler.init("DEBUG"),
    },
    loggers: {
    default: { level: "DEBUG", handlers: ["console", "paleon"] },
    },
    });
    const logger = getLogger();
    10

    View Slide

  11. Demo SaaS with Fresh & Deno Deploy
    https://paleon.deno.dev
    11

    View Slide

  12. Client integrated with std/log
    import * as log from "https://deno.land/std/log/mod.ts";
    import { PaleonAppHandler } from "./client.ts";
    log.setup({
    handlers: {
    console: new log.handlers.ConsoleHandler("DEBUG"),
    paleon: new PaleonAppHandler("DEBUG", {
    project: "{PROJECT_NAME}",
    id: "{DEPLOYMENT_ID}",
    }),
    },
    loggers: {
    default: { level: "DEBUG", handlers: ["console", "paleon"] },
    },
    });
    const logger = getLogger();
    logger.info("Hello Paleon!");
    12

    View Slide

  13. Log Viewer with SSE streaming
    https://paleon.deno.dev/{PROJECT_NAME}/{DEPLOYMENT_ID}
    13

    View Slide

  14. Finally...
    14

    View Slide

  15. Conclusion
    Deno-kun is CUTE!!!
    Deno KV is well-designed
    Merch-driven development is productive
    15

    View Slide

  16. See also...
    Other works by me:
    denopendabot ... https://github.com/apps/denopendabot
    Dependabot-like GitHub App on Deno Deploy
    deno-molt ... https://deno.land/x/molt
    Update dependencies using deno_graph
    and more ...
    Find me on:
    GitHub ... https://github.com/hasundue
    Slack → deno-ja, vim-jp
    Discord → Deno official
    16

    View Slide