Commit ac7ff1f8 authored by Liz Fong-Jones's avatar Liz Fong-Jones
Browse files

Add reference-only snippets: Ch11 SLO prompts + Ch20 bug-fix diff

Not runnable, but worth having copy-pasteable:

- 2e/chapter-11-slos: the three LLM prompts from "Accelerating SLO Adoption with
  Generative AI" (generate/revise SLI, grade SLO), the two generated SLI
  expressions, and a bonus the book omits "for brevity" — an inferred GBNF
  constrained-decoding grammar for the SLI DSL plus a complete prompt that
  supplies it. The grammar is reconstructed from the book's examples and clearly
  labeled as not Honeycomb's production grammar.
- 2e/chapter-20-performance-engineering: Example 20-1, the five-line router-reuse
  bug-fix diff, as a read-only .patch.

(Per discussion, the Chapter 6 Honeycomb query snippets are intentionally not
included: they aren't directly pastable into the query builder.)

Co-Authored-By: default avatarClaude Opus 4.8 (1M context) <noreply@anthropic.com>
parent 04f05808
Loading
Loading
Loading
Loading
+23 −0
Changes for 2e/chapter-11-slos/README.md: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Chapter 11: Using Service Level Objectives for Reliability

Reference snippets from "Accelerating SLO Adoption with Generative AI." These
are **prompts and SLI expressions to copy**, not runnable programs — the prompts
go into your LLM tooling, and the SLI expressions into your observability tool's
SLI/derived-column definition.

| File | What it is |
|------|------------|
| [`generate-sli.prompt.md`](generate-sli.prompt.md) | Prompt to draft an SLI expression from an SLO title + description |
| [`revise-sli.prompt.md`](revise-sli.prompt.md) | Follow-up prompt to refine the SLI (exclude health checks, scope to POST /checkout) |
| [`grade-slo.prompt.md`](grade-slo.prompt.md) | System prompt to review an SLO and return an improved JSON definition |
| [`checkout-sli.txt`](checkout-sli.txt) | The SLI expression the first prompt produces |
| [`checkout-sli-refined.txt`](checkout-sli-refined.txt) | The refined SLI expression from the revise prompt |
| [`bonus-grammar/`](bonus-grammar/) | **Bonus:** the constrained-decoding grammar the book omits for brevity |

Treat the LLM as a draft generator, not an authority: validate the generated SLI
against real telemetry, and check your finalized prompts into version control so
they're consistent across teams.

The `$PROVIDER` placeholder in the prompts stands for your observability tool;
the example outputs use Honeycomb's field syntax (e.g. `$duration_ms`) and
standard OpenTelemetry semantic conventions.
+46 −0
Changes for 2e/chapter-11-slos/bonus-grammar/README.md: 46 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Bonus: supplying the SLI grammar (omitted from the book for brevity)

Chapter 11 generates SLI expressions by prompting an LLM, and notes that you
should "paste in the documentation or the underlying language grammar" and fill
in "the grammar and functions available in their query language." The book leaves
that grammar as an exercise. This directory fills it in.

> **This grammar is inferred, not authoritative.** It was reconstructed from the
> function vocabulary in the book's examples — it is *not* Honeycomb's real
> production grammar, which has more functions and different details. Use it as a
> template for writing the grammar of *your* query language, then substitute your
> tool's actual functions and field syntax.

There are two distinct ways to give a model the grammar, and they compose:

1. **In the prompt** (soft constraint). Describe the grammar in the prompt text
   so the model knows the shape of valid output. See
   [`generate-sli-with-grammar.prompt.md`](generate-sli-with-grammar.prompt.md).
   A capable hosted model often gets this right from the prompt alone.

2. **In the decoder** (hard constraint). Constrain token generation so invalid
   output is impossible, not just discouraged. Runtimes built on llama.cpp — and
   Ollama, which the book uses to run gpt-oss-20b — accept a
   [GBNF](https://github.com/ggml-org/llama.cpp/blob/master/grammars/README.md)
   grammar. [`sli.gbnf`](sli.gbnf) is that grammar for the SLI DSL.

Local open-weights models, which the book observes "begin to produce inferior
results," benefit most from doing both: the prompt teaches intent, the grammar
guarantees syntax.

## Enforcing the grammar with llama.cpp

```bash
llama-cli -m gpt-oss-20b.gguf \
  --grammar-file sli.gbnf \
  -f generate-sli-with-grammar.prompt.md
```

## Enforcing it with Ollama

Ollama exposes structured outputs via its API. You can pass the grammar through
to the underlying llama.cpp engine, or constrain with a JSON schema and a thin
wrapper. The simplest path is llama.cpp directly, as above.

> These commands are illustrative — they assume you've pulled a `.gguf` model
> locally. The grammar itself is the reusable artifact.
+55 −0
Changes for 2e/chapter-11-slos/bonus-grammar/generate-sli-with-grammar.prompt.md: 55 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Bonus prompt: generate an SLI, with the grammar supplied

The book's prompt says to "use `$PROVIDER`'s field syntax" and notes that you
fill in "the grammar and functions available in their query language" — but omits
the actual grammar for brevity. For a hosted model that can read documentation,
gesturing at the provider is often enough. For a local open-weights model
(gpt-oss-20b in Ollama), it isn't: you have to put the grammar **in the prompt**,
and ideally also **enforce** it during decoding (see [`README.md`](README.md) and
[`sli.gbnf`](sli.gbnf)).

This is what the prompt looks like once the grammar is filled in. It's
illustrative scaffolding (not printed in the book), and the grammar below is
*inferred* from the book's examples rather than Honeycomb's real production
grammar — swap in your own tool's functions and field syntax.

---

Now, you will be generating an expression in a domain specific language to define a service level indicator (SLI). The SLI measures the performance and reliability of a service per request based on certain criteria, to be used for computing a higher-level SLO.

The SLI expression is evaluated once per event and must return one of three values:
- TRUE  — the event is in scope and represents a good user experience
- FALSE — the event is in scope and represents a failed user experience
- NULL  — the event is out of scope and must be excluded from the SLO

The expression must conform to the following grammar (EBNF):

    expression  = if ;
    if          = "IF(" predicate "," predicate ")" ;   (* eligibility , success *)
    predicate   = and | or | not | comparison | exists | bool ;
    and         = "AND(" predicate { "," predicate } ")" ;
    or          = "OR(" predicate { "," predicate } ")" ;
    not         = "NOT(" predicate ")" ;
    comparison  = compfn "(" operand "," operand ")" ;
    compfn      = "EQUALS" | "LT" | "LTE" | "GT" | "GTE" | "STARTS_WITH" | "CONTAINS" ;
    exists      = "EXISTS(" field ")" ;
    operand     = field | string | number | bool | "NULL" ;
    field       = "$" name ;                              (* e.g. $http.route *)
    bool        = "TRUE" | "FALSE" ;

Use standard OpenTelemetry semantic convention field names (for example
`$http.route`, `$http.response.status_code`, `$http.request.method`,
`$user_agent.original`). Honeycomb exposes span duration as `$duration_ms`.

Here is the title describing the goal of the overall SLO:

<title>
Checkout succeeds quickly enough
</title>

And here is a more detailed description of the overall SLO:
<description>
Checkout should succeed within four seconds, 99.99% of the time.
</description>

separate <reasoning> and <expression> in your results.
+43 −0
Changes for 2e/chapter-11-slos/bonus-grammar/sli.gbnf: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Illustrative GBNF grammar for the Chapter 11 SLI domain-specific language.
#
# RECONSTRUCTED from the function and operator vocabulary shown in the book's
# examples (IF/AND/NOT/EQUALS/LT/LTE/STARTS_WITH/EXISTS/$field/...), with obvious
# siblings (OR/GT/GTE/CONTAINS) added by analogy. This is NOT Honeycomb's actual
# production grammar — the real one has more functions and different details.
# It's a stand-in for the grammar the book leaves as "fill in the blanks."
#
# GBNF (GGML BNF) is the grammar format used by llama.cpp — and therefore by
# runtimes built on it, such as Ollama — to constrain decoding. Passing a grammar
# forces an open-weights model (e.g. gpt-oss-20b) to emit only syntactically
# valid expressions, instead of merely being asked to.
#
# An SLI classifies each event as true (good), false (bad), or null (out of
# scope). The top-level IF() returns null for events that fail the eligibility
# predicate, so only in-scope events are graded by the success predicate.

root        ::= ws ifexpr ws

ifexpr      ::= "IF(" ws predicate ws "," ws predicate ws ")"

# A predicate evaluates to a boolean (or null) for a single event.
predicate   ::= andexpr | orexpr | notexpr | comparison | existsexpr | bool

andexpr     ::= "AND(" ws predicate (ws "," ws predicate)* ws ")"
orexpr      ::= "OR(" ws predicate (ws "," ws predicate)* ws ")"
notexpr     ::= "NOT(" ws predicate ws ")"

comparison  ::= compfn "(" ws operand ws "," ws operand ws ")"
compfn      ::= "EQUALS" | "LT" | "LTE" | "GT" | "GTE" | "STARTS_WITH" | "CONTAINS"

existsexpr  ::= "EXISTS(" ws field ws ")"

operand     ::= field | string | number | bool | "NULL"

field       ::= "$" name
name        ::= [a-zA-Z_] [a-zA-Z0-9_.]*

string      ::= "\"" ( [^"\\] | "\\" . )* "\""
number      ::= "-"? [0-9]+ ( "." [0-9]+ )?
bool        ::= "TRUE" | "FALSE"

ws          ::= [ \t\n]*
+11 −0
Changes for 2e/chapter-11-slos/checkout-sli-refined.txt: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
IF(
  AND(
     EQUALS($http.request.method, "POST"),
     EQUALS($http.route, "/checkout"),
     NOT(STARTS_WITH($user_agent.original, "kube-probe"))
  ),
  AND(
     LT($http.response.status_code, 400),
     LTE($duration_ms, 4000)
  )
)
Loading