XU XUv0.16.0

#github.com/xulang-org/packages/prometheus

import: prometheus #github.com/xulang-org/packages/prometheus

counts: 7 functions, 17 methods, 7 types, 0 constants, 0 method sets

Public API convenience helpers for Prometheus metrics exporter

github.com/xulang-org/packages · 1.17.2 · Install and use

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

40_standalone_prometheus/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@main

prometheus #github.com/xulang-org/packages/prometheus
#errors

fn verify_prometheus_metrics() {
    println("--- 1. Testing Prometheus Metrics & Exposition Formatting ---")
    $reg := prometheus.new_registry()

    // 1. Counter with Labels
    $http_requests := prometheus.new_counter("http_requests_total", "Total incoming HTTP requests")
    $lbl1 := []prometheus.Label{}
    lbl1.push(prometheus.Label{name: "method", value: "GET"})
    lbl1.push(prometheus.Label{name: "handler", value: "/api/orders"})
    http_requests.inc_labels(lbl1, 100)

    $lbl2 := []prometheus.Label{}
    lbl2.push(prometheus.Label{name: "method", value: "POST"})
    lbl2.push(prometheus.Label{name: "handler", value: "/api/orders"})
    http_requests.inc_labels(lbl2, 25)

    reg.register_counter(http_requests)
    println("   [PASS] Counter registered with 2 labeled series.")

    // 2. Gauge
    $mem_usage := prometheus.new_gauge("process_memory_bytes", "Resident memory bytes")
    mem_usage.set("134217728")
    reg.register_gauge(mem_usage)
    println("   [PASS] Gauge registered.")

    // 3. Gather text output
    output := reg.gather()
    println("   Exposition output preview:")
    println(output)

    prefix_help := "# HELP http_requests_total Total incoming HTTP requests"
    if output.len >= prefix_help.len && output[0:prefix_help.len] == prefix_help {
        println("   [PASS] Output conforms to standard Prometheus exposition format!")
    } else {
        println("   [FAIL] Output format mismatch!")
    }
}

fn verify_prometheus_pool() {
    println("\n--- 2. Testing Prometheus Pusher Connection Pool ---")
    $p := prometheus.open_pool("http://127.0.0.1:9091", 5)

    // 1. Connection Reuse Test
    fd1 := p.get_conn()
    println("   Borrow 1 fd: " + str(fd1))
    p.put_conn(fd1)

    fd2 := p.get_conn()
    println("   Borrow 2 (after return) fd: " + str(fd2))
    if fd1 == fd2 {
        println("   [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
    } else {
        println("   [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
    }

    // 2. Multi-connection concurrent borrow
    fd3 := p.get_conn()
    println("   Borrow 3 (concurrently) fd: " + str(fd3))
    if fd2 != fd3 {
        println("   [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
    } else {
        println("   [FAIL] Expected distinct fds for concurrent connections!")
    }
    p.put_conn(fd2)
    p.put_conn(fd3)

    // 3. High-level push delegation to Pushgateway
    $test_reg := prometheus.new_registry()
    $counter := prometheus.new_counter("xu_push_counter_total", "Test counter pushed to Pushgateway")
    counter.inc_by(42)
    test_reg.register_counter(counter)

    push_ok := p.push_metrics("xu_driver_test", test_reg.gather())
    if push_ok {
        println("   [PASS] Successfully pushed metrics to Pushgateway job 'xu_driver_test'!")
    } else {
        println("   [FAIL] Failed to push metrics to Pushgateway!")
    }

    p.close()
    println("   [PASS] Prometheus Pool closed cleanly.")
}

fn main() {
    println("=== Xu Prometheus Metrics Native Driver Verification ===\n")
    verify_prometheus_metrics()
    verify_prometheus_pool()
    println("\n[PROMETHEUS VERIFICATION 100% COMPLETED]")
}

API declarations

open_pusher

XuAPI
pub fn open_pusher(endpoint string, job string) Pusher

Purpose

Open pusher.

Parameters

endpoint string
endpoint (string)
job string
job (string)

Returns

Pusher — a value of this type

struct Label

XuAPI
pub struct Label {
    pub name string
    pub value string
}

Purpose

Structure encapsulating name, value.

Public fields

name string
name
value string
field (string)

See usage example

struct MetricSample

XuAPI
pub struct MetricSample {
    pub label_str string
    pub value string
}

Purpose

Structure encapsulating label_str, value.

Public fields

label_str string
field (string)
value string
field (string)

struct Counter

XuAPI
pub struct Counter {
    pub name string
    pub help string
    pub samples []MetricSample
}

Purpose

Structure encapsulating name, help, samples.

Public fields

name string
name
help string
field (string)
samples []MetricSample
field ([]MetricSample)

struct Gauge

XuAPI
pub struct Gauge {
    pub name string
    pub help string
    pub samples []MetricSample
}

Purpose

Structure encapsulating name, help, samples.

Public fields

name string
name
help string
field (string)
samples []MetricSample
field ([]MetricSample)

new_counter

XuAPI
pub fn new_counter(name string, help string) Counter

Purpose

Construct counter.

Parameters

name string
name (string)
help string
help (string)

Returns

Counter — a value of this type

See usage example

new_gauge

XuAPI
pub fn new_gauge(name string, help string) Gauge

Purpose

Construct gauge.

Parameters

name string
name (string)
help string
help (string)

Returns

Gauge — a value of this type

See usage example

($&Counter) inc

XuAPI
pub fn (c $&Counter) inc()

Purpose

Execute inc on $&Counter.

Parameters

c $&Counter
The Counter instance used by this call (exclusive borrow).

Returns

This call does not return a value.

($&Counter) inc_by

XuAPI
pub fn (c $&Counter) inc_by(delta int)

Purpose

Operation on $&Counter: inc by.

Parameters

c $&Counter
The Counter instance used by this call (exclusive borrow).
delta int
delta (int)

Returns

This call does not return a value.

($&Counter) inc_labels

XuAPI
pub fn (c $&Counter) inc_labels(labels []Label, delta int)

Purpose

Operation on $&Counter: inc labels.

Parameters

c $&Counter
The Counter instance used by this call (exclusive borrow).
labels []Label
labels ([]Label)
delta int
delta (int)

Returns

This call does not return a value.

($&Gauge) set

XuAPI
pub fn (g $&Gauge) set(val string)

Purpose

Execute set on $&Gauge.

Parameters

g $&Gauge
The Gauge instance used by this call (exclusive borrow).
val string
value (string)

Returns

This call does not return a value.

($&Gauge) set_labels

XuAPI
pub fn (g $&Gauge) set_labels(labels []Label, val string)

Purpose

Operation on $&Gauge: set labels.

Parameters

g $&Gauge
The Gauge instance used by this call (exclusive borrow).
labels []Label
labels ([]Label)
val string
value (string)

Returns

This call does not return a value.

(Counter) format

XuAPI
pub fn (c Counter) format() string

Purpose

Operation on Counter: format as a string.

Parameters

c Counter
The Counter instance used by this call (value receiver).

Returns

string — result string

(Gauge) format

XuAPI
pub fn (g Gauge) format() string

Purpose

Operation on Gauge: format as a string.

Parameters

g Gauge
The Gauge instance used by this call (value receiver).

Returns

string — result string

struct Pool

XuAPI
pub struct Pool {
    pub host string
    pub port int
    pub secure bool
    pub state chan pool.State
    pub max_idle int
}

Purpose

Connection Pool for Prometheus Pushgateway & Remote Exporters. Manages reusable TCP/HTTP socket connections.

Public fields

host string
host
port int
port
secure bool
field (bool)
state chan pool.State
field (channel; type chan pool.State)
max_idle int
field (int)

new_pool

XuAPI
pub fn new_pool(endpoint string, max_idle int) Pool

Purpose

Construct pool.

Parameters

endpoint string
endpoint (string)
max_idle int
max_idle (int)

Returns

Pool — a value of this type

open_pool

XuAPI
pub fn open_pool(endpoint string, max_idle int) Pool

Purpose

Open pool.

Parameters

endpoint string
endpoint (string)
max_idle int
max_idle (int)

Returns

Pool — a value of this type

See usage example

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

Purpose

Shares sockets and close state; clones configuration strings.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

Pool — a value of this type

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

Purpose

Operation on &Pool: read conn.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

int — a value of this type

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

Purpose

Operation on &Pool: put connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) discard_conn

XuAPI
pub fn (p &Pool) discard_conn(fd int)

Purpose

Operation on &Pool: discard connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) push_metrics

XuAPI
pub fn (p &Pool) push_metrics(job string, metrics_text string) bool

Purpose

Operation on &Pool: push metrics.

Parameters

p &Pool
The Pool instance used by this call (borrow).
job string
job (string)
metrics_text string
metrics_text (string)

Returns

bool — true or false

(&Pool) close

XuAPI
pub fn (p &Pool) close()

Purpose

Operation on &Pool: close the resource.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

This call does not return a value.

struct Pusher

XuAPI
pub struct Pusher {
    pub host string
    pub port int
    pub secure bool
    pub job string
}

Purpose

Structure encapsulating host, port, secure, job.

Public fields

host string
host
port int
port
secure bool
field (bool)
job string
field (string)

new_pusher

XuAPI
pub fn new_pusher(endpoint string, job string) Pusher

Purpose

Construct pusher.

Parameters

endpoint string
endpoint (string)
job string
job (string)

Returns

Pusher — a value of this type

(&Pusher) push

XuAPI
pub fn (p &Pusher) push(metrics_text string) bool

Purpose

Execute push on &Pusher.

Parameters

p &Pusher
The Pusher instance used by this call (borrow).
metrics_text string
metrics_text (string)

Returns

bool — true or false

struct Registry

XuAPI
pub struct Registry {
    pub counters []Counter
    pub gauges []Gauge
}

Purpose

Structure encapsulating counters, gauges.

Public fields

counters []Counter
field ([]Counter)
gauges []Gauge
field ([]Gauge)

new_registry

XuAPI
pub fn new_registry() Registry

Purpose

Construct registry.

Parameters

No arguments are required.

Returns

Registry — a value of this type

See usage example

($&Registry) register_counter

XuAPI
pub fn (r $&Registry) register_counter(c Counter)

Purpose

Operation on $&Registry: register counter.

Parameters

r $&Registry
The Registry instance used by this call (exclusive borrow).
c Counter
byte or character code (Counter)

Returns

This call does not return a value.

($&Registry) register_gauge

XuAPI
pub fn (r $&Registry) register_gauge(g Gauge)

Purpose

Operation on $&Registry: register gauge.

Parameters

r $&Registry
The Registry instance used by this call (exclusive borrow).
g Gauge
g (Gauge)

Returns

This call does not return a value.

(&Registry) gather

XuAPI
pub fn (r &Registry) gather() string

Purpose

Execute gather on &Registry.

Parameters

r &Registry
The Registry instance used by this call (borrow).

Returns

string — result string