Skip to content

Commit 2db3727

Browse files
am-kantoxoz-agent
andcommitted
Document @Telemetria behavior on multi-clause functions
Add warning admonition to moduledoc explaining that for multi-clause functions, @Telemetria must be placed on the bodyless head clause (the one declaring defaults). All clauses then inherit the annotation. Mixing head-level and per-clause annotations raises a compile-time error. Add matching sections to the cheatsheet: 'Multi-clause functions' (head annotation) and 'Per-clause annotations' (individual clause annotation). Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent fe5afaf commit 2db3727

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

lib/telemetria.ex

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,28 @@ defmodule Telemetria do
5252
- **`reshape: (map() -> map())`** — the function to be called on the resulting attributes
5353
to reshape them before sending to the actual telemetry handler; the default application-wide
5454
reshaper might be set in `:telemetria, :reshaper` config
55-
- **`messenger_channels: %{optional(atom()) => {module, keyword()}`** more handy messenger
55+
- **`messenger_channels: %{optional(atom()) => {module, keyword()}`** -- more handy messenger
5656
management, several channels config with channels names associated with their
5757
implementations and properties
5858
59+
> ### Multi-clause functions {: .warning}
60+
>
61+
> When a function has multiple clauses, the `@telemetria` attribute **must** be placed
62+
> on the bodyless head clause (the one declaring defaults), not on individual clauses.
63+
> This single annotation will wrap **all** clauses with telemetry.
64+
>
65+
> ```elixir
66+
> @telemetria level: :info, locals: [:i]
67+
> def classify(i \\ nil) # <-- attribute goes here, on the head
68+
> def classify(nil), do: :none
69+
> def classify(i) when i > 0, do: :positive
70+
> def classify(_i), do: :negative
71+
> ```
72+
>
73+
> Placing `@telemetria` on individual clauses instead will annotate only those
74+
> specific clauses. You may **not** mix head-level and clause-level annotations
75+
> for the same function -- it will raise a compile-time error.
76+
5977
### Simple example
6078
6179
The following code would emit the telemetry event for the function `weather`,

stuff/telemetria_cheatsheet.cheatmd

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ end
7575

7676
The event is emitted only when the `if` function returns `true` for the result.
7777

78+
### Multi-clause functions
79+
80+
```elixir
81+
@telemetria level: :info, locals: [:i]
82+
def classify(i \\ nil)
83+
def classify(nil), do: :none
84+
def classify(i) when i > 0, do: :positive
85+
def classify(_i), do: :negative
86+
```
87+
88+
For multi-clause functions, place `@telemetria` on the **bodyless head clause** (the one with defaults). All clauses will be wrapped. Do not mix head-level and per-clause annotations.
89+
90+
### Per-clause annotations
91+
92+
```elixir
93+
@telemetria level: :warning
94+
def handle(:error), do: alert()
95+
96+
@telemetria level: :debug
97+
def handle(:ok), do: :ok
98+
```
99+
100+
Alternatively, annotate individual clauses separately (each gets its own event with its own options). Cannot be combined with head-level annotation on the same function.
101+
78102
## Attribute Parameters
79103
{: .col-2}
80104

0 commit comments

Comments
 (0)