Open-Source Wikis

/

Prometheus

/

Subsystems

/

Model

prometheus/prometheus

Model

Purpose

model/ holds the cross-cutting domain types that almost every other package consumes: labels, native histograms, exemplars, relabel rules, and the metric parsers.

Sub-packages

Path Contents
model/labels The Labels type (3 implementations selected by build tag), Builder, Matcher, Regexp.
model/histogram Native histogram types (Histogram, FloatHistogram), conversion, validation.
model/exemplar The Exemplar type and label-set length constants.
model/metadata MetricType, unit, help — the "metadata" attached to a series.
model/relabel Relabel config (Action, Config) and the Process() engine.
model/timestamp Helpers for converting time.Time <-> milliseconds.
model/value Stale marker (StaleNaN) and exemplar marker bit-patterns.
model/textparse Parsers for the various scrape protocols.
model/rulefmt Rule-file YAML schema (used by both server and promtool).

model/labels

Three storage representations selected at build time:

  • slicelabels — sorted []Label{Name, Value}. Simple, allocation-friendly, default for tests.
  • stringlabels — packed string with length-prefixed name+value pairs. Better cache locality. CI runs both.
  • dedupelabels — packed string with a per-symbol-table dedupe table. Used in agent and remote write hot paths to deduplicate label values across series.

The build-tag selection means a single binary cannot mix representations; the same code paths compile against all three.

Notable types:

  • Labels — the immutable label set. Methods: Get(name), Has(name), Hash(), String().
  • Builder — mutable builder; Set, Del, Labels().
  • ScratchBuilder — pool-friendly builder for parsers.
  • SymbolTable — string interning across many label sets.
  • Matcher — a single equality / regexp match.
  • MatchTypeMatchEqual, MatchNotEqual, MatchRegexp, MatchNotRegexp.
  • FastRegexMatcher — pre-compiled, cached regex matcher used by selectors.

labels/regexp.go is one of the largest files in the repo (~36 KB) — it contains many specialised regex shortcuts (literal-prefix, alternation, character class, etc.) used to avoid full regex evaluation when a faster check exists.

model/histogram

Native histograms are sparse, exponential-bucket histograms with optional float bucket counts. The package provides:

  • Histogram — integer bucket counts.
  • FloatHistogram — float bucket counts (after rate/binop).
  • BucketSpan — sparse bucket layout.
  • Convert* helpers — between integer and float forms.
  • CompactBuckets and Compactify — schema-down algorithms used during binary operations.

Custom-bucket histograms (NHCB) reuse the same types with explicit CustomValues boundaries instead of an exponential schema.

model/relabel

Relabel rules are first-class: they are used by both scrape config (relabel_configs, metric_relabel_configs) and alert config (alert_relabel_configs).

lset := relabel.Process(in, configs...)

Supported actions: replace, keep, drop, keepequal, dropequal, hashmod, labelmap, labeldrop, labelkeep, lowercase, uppercase. The implementation is in model/relabel/relabel.go.

Performance: hot path; benchmarked routinely. Heavy regex configs are the primary tuning concern.

model/textparse

Parsers for ingestion. The dispatcher picks a parser based on the negotiated Content-Type. Implementations:

  • PromParser — Prometheus 0.0.4 text format (line-by-line, tolerant).
  • OpenMetricsParser — OpenMetrics 0.0.1 / 1.0.0 (more strict, with exemplars and _created).
  • ProtobufParser — Prometheus protobuf (the recommended path for native histograms and start timestamps).
  • NHCBParser — wraps a classical histogram parser to emit NHCB output.

Each parser's Next() returns the next token; the loop in scrape/scrape.go handles dispatch. The parsers must be allocation-friendly — they run on every scrape.

model/exemplar and model/metadata

Small, self-contained packages. Exemplar enforces a maximum label-set string length (ExemplarMaxLabelSetLength, 128 bytes). metadata.MetricType (Counter, Gauge, Histogram, Summary, GaugeHistogram, Info, Stateset, Unknown) is what scrape and OTLP write into the WAL/TSDB metadata records.

model/rulefmt

The on-disk rule-file YAML structure:

type RuleGroups struct {
    Groups []RuleGroup `yaml:"groups"`
}

type RuleGroup struct {
    Name        string         `yaml:"name"`
    Interval    time.Duration  `yaml:"interval,omitempty"`
    Limit       int            `yaml:"limit,omitempty"`
    Rules       []RuleNode     `yaml:"rules"`
    QueryOffset *time.Duration `yaml:"query_offset,omitempty"`
}

Used by both the server (rules.Manager) and promtool test rules. Validation is exposed via RuleGroups.Validate().

Integration points

  • Every package that touches series, samples, or scrape input depends on model/.
  • Build tags slicelabels|stringlabels|dedupelabels flow through every dependent package automatically — they are at the module level, not per-package.

Entry points for modification

  • New label representation: copy one of the existing label files (labels_*.go), wire it through the _dedupelabels.go/_slicelabels.go/_stringlabels.go build tag pattern, and add it to CI.
  • New parser: add a sub-package under model/textparse/, register a content-type, and add a fixture under scrape/testdata/.
  • New histogram operation: add to histogram/generic.go first (it is type-parameterised) and let it specialise to both integer and float histograms.

See Storage and Scrape for primary consumers.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Model – Prometheus wiki | Factory