Publish metrics¶
Source · io.systemd.Metrics · systemd v260 interface · Unprivileged
systemd v260 services publish metrics by serving io.systemd.Metrics on a
socket under /run/systemd/report/. This example does the same from Go. It
serves the systemd interface with the generated types: the handlers take
metrics.ListOut and metrics.DescribeOut, and the IDL is
metrics.InterfaceDescription. It is the server-side counterpart of
Read metrics.
Run it¶
Output (captured)
$ go run ./examples/metrics /tmp/demo.metrics
# demo.metrics
io.example.MetricsDemo.goroutines 6
io.example.MetricsDemo.uptimeSeconds 0
io.example.MetricsDemo.listCalls 1
io.example.MetricsDemo.build{go} "go1.24.7"
$ varlinkctl call --more /tmp/demo.metrics io.systemd.Metrics.Describe '{}'
{"name":"io.example.MetricsDemo.goroutines","description":"Goroutines in the provider process","type":"gauge"}
{"name":"io.example.MetricsDemo.uptimeSeconds","description":"Seconds since the provider started","type":"counter"}
{"name":"io.example.MetricsDemo.listCalls","description":"List calls served","type":"counter"}
{"name":"io.example.MetricsDemo.build","description":"Build information","type":"string"}
The code¶
| examples/metrics-provider/main.go | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
How it works¶
- Generated types on the server side. Registering
metrics.InterfaceDescriptionand handling withmetrics.ListOutmeans the types are checked against systemd's own IDL at startup. LastIsFinal().ListandDescribereturn finite lists, so the last metric is the final reply and clients don't see a trailing empty item. This is the same thing systemd's own providers do.- Requires
more. The IDL marks both methods# [Requires 'more' flag], so a plain call getsorg.varlink.service.ExpectedMore. - Socket placement. To be found by tools that scan
/run/systemd/report/, install a.socketunit withListenStream=/run/systemd/report/<name>. The provider picks it up throughListenActivated.