OpenMetrics 1.0 text parser, canonical encoder, semantic validator, and document merge toolkit for MoonBit.
moon add JYPeng487/moonopenmetricsimport {
"JYPeng487/moonopenmetrics",
}///|
test {
let input =
#|# TYPE requests counter
#|# HELP requests processed requests
#|requests_total{method="GET"} 3
#|# EOF
#|
let document = match @moonopenmetrics.parse(input) {
Ok(document) => document
Err(error) => fail(error.to_string())
}
let report = @moonopenmetrics.validate(document)
inspect(report.error_count, content="0")
inspect(document.families.length(), content="1")
inspect(@moonopenmetrics.encode(document).contains("# EOF"), content="true")
}///|
test {
let registry = @moonopenmetrics.Registry::new()
match registry.register("requests", Counter, help="processed requests") {
Ok(_) => ()
Err(error) => fail(error)
}
match
registry.increment_counter("requests", delta=2.0, labels=[
@moonopenmetrics.Label::new("method", "GET"),
]) {
Ok(_) => ()
Err(error) => fail(error)
}
let text = registry.to_text()
inspect(text.contains("requests_total{method=\"GET\"} 2"), content="true")
inspect(
@moonopenmetrics.validate(registry.snapshot()).error_count,
content="0",
)
}///|
test {
let registry = @moonopenmetrics.Registry::new()
match
registry.register(
"latency_seconds",
Histogram,
help="request latency",
unit="seconds",
) {
Ok(_) => ()
Err(error) => fail(error)
}
match registry.observe_histogram("latency_seconds", 0.2, [0.1, 0.5, 1.0]) {
Ok(_) => ()
Err(error) => fail(error)
}
inspect(
registry.to_text().contains("latency_seconds_bucket{le=\"+Inf\"} 1"),
content="true",
)
}moon run cmd/mainmoon run cmd/main -- validate
moon run cmd/main -- normalize
moon run cmd/main -- helpmoon run cmd/main -- validate "# EOF"| Type | Purpose |
|---|---|
| Document | Ordered families plus presence of the EOF marker |
| MetricFamily | HELP, TYPE, UNIT, and the family's samples |
| Sample | Name, labels, value, optional fractional Unix-second timestamp and exemplar |
| Exemplar | Observation labels, value, and optional fractional Unix-second timestamp |
| ValidationReport | Structured issues plus error/warning counts |
| Registry | Explicit, deterministic in-memory aggregation |
moon fmt --check
moon info --target all
moon check --target all --deny-warn
moon test --target all --deny-warn
moon build --target all
moon run cmd/main
moon packagemodel.mbt public domain model
text.mbt / number.mbt lexical rules and escaping
labels.mbt label parser and encoder
sample_parser.mbt sample, timestamp, and exemplar grammar
parser.mbt / encoder.mbt complete document conversion
validation.mbt cross-line semantic checks
registry.mbt deterministic aggregation
merge.mbt snapshot merge and selection
report.mbt structural and diagnostic reports
cmd/main runnable validator/normalizer
examples/complete.om reproducible fixturepub(all) struct MetricFamily {
name : String
help : String?
metric_type : MetricType
unit : String?
samples : Array[Sample]
} derive(Eq)fn MetricFamily::new(name : String, metric_type? : MetricType, help? : String, unit? : String) -> MetricFamilypub(all) enum MetricType {
Unknown
Counter
Gauge
Histogram
GaugeHistogram
Summary
Info
StateSet
} derive(Eq)pub(all) struct MetricsSummary {
family_count : Int
sample_count : Int
label_count : Int
exemplar_count : Int
counter_count : Int
gauge_count : Int
histogram_count : Int
gauge_histogram_count : Int
summary_count : Int
info_count : Int
state_set_count : Int
unknown_count : Int
} derive(Eq)pub struct Registry {
// private fields
}fn Registry::register(self : Registry, name : String, metric_type : MetricType, help? : String, unit? : String) -> Result[Unit, String]fn ValidationIssue::error(code : String, family : String, message : String, sample? : String) -> ValidationIssuefn ValidationIssue::warning(code : String, family : String, message : String, sample? : String) -> ValidationIssuepub(all) struct ValidationReport {
issues : Array[ValidationIssue]
error_count : Int
warning_count : Int
} derive(Eq)fn escape_help(value : StringView) -> Stringfn escape_label_value(value : StringView) -> Stringfn format_number(value : Double) -> Stringfn is_valid_label_name(name : StringView) -> Boolfn is_valid_metric_name(name : StringView) -> Boolfn merge(left : Document, right : Document, policy? : ConflictPolicy) -> Result[Document, MergeError]fn parse_number(value : StringView) -> Result[Double, String]fn parse_timestamp(value : StringView) -> Result[Double, String]fn unescape_help(value : StringView) -> Result[String, String]fn unescape_label_value(value : StringView) -> Result[String, String]OpenMetrics 1.0 text parser, canonical encoder, semantic validator, and document merge toolkit for MoonBit.