Prometheus metrics for MoonBit.
moon add moonbitstack/moonp8s#p8s.counter("orders_total", help="Orders asked for.")
#p8s.counter("orders_refused_total", help="Orders refused.", on=raised)
#p8s.timed("order_seconds", help="Seconds to place an order.", buckets="0.5, 1, 5")
fn place(item : String) -> Int raise Sold {
...
}let registry = @moonp8s.Registry::new()
register_metrics(registry)
let receipt = place_metered("tea", now=clock)rule(name: "metrics", command: "moonx moonbitstack/moonp8s/cmd/metrics -C \"$mod_dir\" \"$input\" -o \"$output\"")dev_build(rule: "metrics", input: "orders.mbt", output: "orders.g.mbt")
formatter(ignore: [ "orders.g.mbt" ])import {
"moonbitstack/moonp8s",
"moonbitstack/moonp8s/metric",
}
try {
let registry = @moonp8s.Registry::new()
let requests = registry.counter("http_requests", "HTTP requests.")
requests.inc()
let latency = registry.histogram("request_latency_seconds", "Latency.")
latency.observe(0.25)
println(@moonp8s.text(registry.collect()))
} catch {
@metric.Refused(message) => println("refused: " + message)
}let body = @moonp8s.openmetrics(registry.collect())
let content_type = @moonp8s.Format::OpenMetrics100.content_type()| Type | Methods | Notes |
|---|---|---|
| Counter | inc(by~, exemplar?), reset(created?), get() | Only increases; a negative step is refused. |
| Gauge | set(v, timestamp?), inc(by~), dec(by~), set_function(f), get() | Pass the timestamp to date a sample; a function replaces the stored value. |
| Histogram | observe(v, exemplar?), snapshot() | Buckets from buckets~; linear and exponential build them. |
| GaugeHistogram | observe, snapshot() | A gauge's sum and count over histogram buckets. |
| Summary | observe(v), snapshot() | Count and sum by default; quantiles~ adds estimates. |
| Info | Info::new(labels) | A fixed label set whose value is 1. |
| StateSet | set, unset, set_many, get | StateSet::exclusive keeps exactly one state true. |
let shard = requests.shard()
shard.inc()
shard.flush()let h = @moonp8s.Histogram::new(buckets=@moonp8s.linear(1.0, 1.0, 5))let s = @moonp8s.Summary::new(quantiles=[(0.5, 0.01), (0.9, 0.01), (0.99, 0.001)])let requests = @moonp8s.Family::new(["method", "code"], fn() {
@moonp8s.Counter::new()
})
requests.labels(["GET", "200"]).inc()
requests.labels(["POST", "500"]).inc(by=2.0)struct Route {
method : String
path : String
} derive(Eq, Hash)
impl @moonp8s.LabelSet for Route with pairs(self) {
[("method", self.method), ("path", self.path)]
}let registry = @moonp8s.Registry::new(prefix="app", labels=[("region", "eu")])
let requests = registry.counter("http_requests", "HTTP requests.")
let sub = registry.sub(prefix="http", labels=[("server", "a")])| Function | Result |
|---|---|
| text(families) | Prometheus text format, version 0.0.4 |
| openmetrics(families) | OpenMetrics text format, version 1.0.0, ending in # EOF |
| negotiate(accept) | Format chosen from an Accept header |
| parse(text, format~) | the families an exposition describes |
let request = @moonp8s.push(
"pushgateway.local",
"batch",
registry.collect(),
grouping_key=[("instance", "worker-1")],
)
println(request.http_method + " " + request.path)
println(request.body)let handler = @moonp8s.app(registry)
let wrapped = @moonp8s.middleware(registry)| Package | Contents |
|---|---|
| moonbitstack/moonp8s | Re-exports the common types and functions. |
| moonbitstack/moonp8s/metric | Metric types, families, the registry, and the snapshot model. |
| moonbitstack/moonp8s/expo | Both text formats, their negotiation, and the parser. |
| moonbitstack/moonp8s/push | Push gateway request construction. |
| moonbitstack/moonp8s/asgi | The /metrics handler and the counting middleware. |
let exemplar_label_limit : Intfn format_float(d : Double) -> Stringfn is_valid_label_name(name : String) -> Boolfn is_valid_metric_name(name : String) -> Boolfn is_valid_unit(unit : String) -> Boolfn push(gateway : String, job : String, families : Array[MetricFamily], grouping_key? : Array[(String, String)], format? : Format) -> PushRequestfn push_add(gateway : String, job : String, families : Array[MetricFamily], grouping_key? : Array[(String, String)], format? : Format) -> PushRequestInstall
Download zipPrometheus metrics for MoonBit.