open_pusher
pub fn open_pusher(endpoint string, job string) PusherPurpose
Open pusher.
Parameters
endpointstring- endpoint (string)
jobstring- job (string)
Returns
Pusher — a value of this type
#github.com/xulang-org/packages/prometheusPublic API convenience helpers for Prometheus metrics exporter
github.com/xulang-org/packages · 1.17.2 · Install and use
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@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]")
}open_pusherpub fn open_pusher(endpoint string, job string) PusherOpen pusher.
endpoint stringjob stringPusher — a value of this type
struct Labelpub struct Label {
pub name string
pub value string
}Structure encapsulating name, value.
name stringvalue stringstruct MetricSamplepub struct MetricSample {
pub label_str string
pub value string
}Structure encapsulating label_str, value.
label_str stringvalue stringstruct Counterpub struct Counter {
pub name string
pub help string
pub samples []MetricSample
}Structure encapsulating name, help, samples.
name stringhelp stringsamples []MetricSamplestruct Gaugepub struct Gauge {
pub name string
pub help string
pub samples []MetricSample
}Structure encapsulating name, help, samples.
name stringhelp stringsamples []MetricSamplenew_counterpub fn new_counter(name string, help string) CounterConstruct counter.
name stringhelp stringCounter — a value of this type
new_gaugepub fn new_gauge(name string, help string) GaugeConstruct gauge.
name stringhelp stringGauge — a value of this type
($&Counter) incpub fn (c $&Counter) inc()Execute inc on $&Counter.
c $&CounterCounter instance used by this call (exclusive borrow).This call does not return a value.
($&Counter) inc_bypub fn (c $&Counter) inc_by(delta int)Operation on $&Counter: inc by.
c $&CounterCounter instance used by this call (exclusive borrow).delta intThis call does not return a value.
($&Counter) inc_labelspub fn (c $&Counter) inc_labels(labels []Label, delta int)Operation on $&Counter: inc labels.
c $&CounterCounter instance used by this call (exclusive borrow).labels []Labeldelta intThis call does not return a value.
($&Gauge) setpub fn (g $&Gauge) set(val string)Execute set on $&Gauge.
g $&GaugeGauge instance used by this call (exclusive borrow).val stringThis call does not return a value.
($&Gauge) set_labelspub fn (g $&Gauge) set_labels(labels []Label, val string)Operation on $&Gauge: set labels.
g $&GaugeGauge instance used by this call (exclusive borrow).labels []Labelval stringThis call does not return a value.
(Counter) formatpub fn (c Counter) format() stringOperation on Counter: format as a string.
c CounterCounter instance used by this call (value receiver).string — result string
(Gauge) formatpub fn (g Gauge) format() stringOperation on Gauge: format as a string.
g GaugeGauge instance used by this call (value receiver).string — result string
struct Poolpub struct Pool {
pub host string
pub port int
pub secure bool
pub state chan pool.State
pub max_idle int
}Connection Pool for Prometheus Pushgateway & Remote Exporters. Manages reusable TCP/HTTP socket connections.
host stringport intsecure boolstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, max_idle int) PoolConstruct pool.
endpoint stringmax_idle intPool — a value of this type
open_poolpub fn open_pool(endpoint string, max_idle int) PoolOpen pool.
endpoint stringmax_idle intPool — a value of this type
(&Pool) duppub fn (p &Pool) dup() PoolShares sockets and close state; clones configuration strings.
p &PoolPool instance used by this call (borrow).Pool — a value of this type
(&Pool) get_connpub fn (p &Pool) get_conn() intOperation on &Pool: read conn.
p &PoolPool instance used by this call (borrow).int — a value of this type
(&Pool) put_connpub fn (p &Pool) put_conn(fd int)Operation on &Pool: put connection.
p &PoolPool instance used by this call (borrow).fd intThis call does not return a value.
(&Pool) discard_connpub fn (p &Pool) discard_conn(fd int)Operation on &Pool: discard connection.
p &PoolPool instance used by this call (borrow).fd intThis call does not return a value.
(&Pool) push_metricspub fn (p &Pool) push_metrics(job string, metrics_text string) boolOperation on &Pool: push metrics.
p &PoolPool instance used by this call (borrow).job stringmetrics_text stringbool — true or false
(&Pool) closepub fn (p &Pool) close()Operation on &Pool: close the resource.
p &PoolPool instance used by this call (borrow).This call does not return a value.
struct Pusherpub struct Pusher {
pub host string
pub port int
pub secure bool
pub job string
}Structure encapsulating host, port, secure, job.
host stringport intsecure booljob stringnew_pusherpub fn new_pusher(endpoint string, job string) PusherConstruct pusher.
endpoint stringjob stringPusher — a value of this type
(&Pusher) pushpub fn (p &Pusher) push(metrics_text string) boolExecute push on &Pusher.
p &PusherPusher instance used by this call (borrow).metrics_text stringbool — true or false
struct Registrypub struct Registry {
pub counters []Counter
pub gauges []Gauge
}Structure encapsulating counters, gauges.
counters []Countergauges []Gaugenew_registrypub fn new_registry() RegistryConstruct registry.
No arguments are required.
Registry — a value of this type
($&Registry) register_counterpub fn (r $&Registry) register_counter(c Counter)Operation on $&Registry: register counter.
r $&RegistryRegistry instance used by this call (exclusive borrow).c CounterThis call does not return a value.
($&Registry) register_gaugepub fn (r $&Registry) register_gauge(g Gauge)Operation on $&Registry: register gauge.
r $&RegistryRegistry instance used by this call (exclusive borrow).g GaugeThis call does not return a value.
(&Registry) gatherpub fn (r &Registry) gather() stringExecute gather on &Registry.
r &RegistryRegistry instance used by this call (borrow).string — result string