open_pusher
pub fn open_pusher(endpoint string, job string) Pusher用途
执行 open pusher 操作。
参数
endpointstring- endpoint(string)
jobstring- job(string)
返回值
Pusher — 该类型的值
#github.com/xulang-org/packages/prometheus扩展包 `prometheus #github.com/xulang-org/packages/prometheus`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
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) Pusher执行 open pusher 操作。
endpoint stringjob stringPusher — 该类型的值
struct Labelpub struct Label {
pub name string
pub value string
}承载 name、value 等属性的结构体。
name stringvalue stringstruct MetricSamplepub struct MetricSample {
pub label_str string
pub value string
}承载 label_str、value 等属性的结构体。
label_str stringvalue stringstruct Counterpub struct Counter {
pub name string
pub help string
pub samples []MetricSample
}承载 name、help、samples 等属性的结构体。
name stringhelp stringsamples []MetricSamplestruct Gaugepub struct Gauge {
pub name string
pub help string
pub samples []MetricSample
}承载 name、help、samples 等属性的结构体。
name stringhelp stringsamples []MetricSamplenew_counterpub fn new_counter(name string, help string) Counter构造 counter。
name stringhelp stringCounter — 该类型的值
new_gaugepub fn new_gauge(name string, help string) Gauge构造 gauge。
name stringhelp stringGauge — 该类型的值
($&Counter) incpub fn (c $&Counter) inc()在 $&Counter 上执行 inc 操作。
c $&CounterCounter 实例;以独占借用传入,方法可修改它,调用方仍持有实例。不返回值。
($&Counter) inc_bypub fn (c $&Counter) inc_by(delta int)$&Counter 上的操作:执行 inc by 操作。
c $&CounterCounter 实例;以独占借用传入,方法可修改它,调用方仍持有实例。delta int不返回值。
($&Counter) inc_labelspub fn (c $&Counter) inc_labels(labels []Label, delta int)$&Counter 上的操作:执行 inc labels 操作。
c $&CounterCounter 实例;以独占借用传入,方法可修改它,调用方仍持有实例。labels []Labeldelta int不返回值。
($&Gauge) setpub fn (g $&Gauge) set(val string)在 $&Gauge 上执行 set 操作。
g $&GaugeGauge 实例;以独占借用传入,方法可修改它,调用方仍持有实例。val string不返回值。
($&Gauge) set_labelspub fn (g $&Gauge) set_labels(labels []Label, val string)$&Gauge 上的操作:设置 labels。
g $&GaugeGauge 实例;以独占借用传入,方法可修改它,调用方仍持有实例。labels []Labelval string不返回值。
(Counter) formatpub fn (c Counter) format() stringCounter 上的操作:格式化为字符串。
c CounterCounter 实例;按值接收。string — 结果字符串
(Gauge) formatpub fn (g Gauge) format() stringGauge 上的操作:格式化为字符串。
g GaugeGauge 实例;按值接收。string — 结果字符串
struct Poolpub struct Pool {
pub host string
pub port int
pub secure bool
pub state chan pool.State
pub max_idle int
}Prometheus 指标导出与推送连接池管理器。
host stringport intsecure boolstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, max_idle int) Pool构造 pool。
endpoint stringmax_idle intPool — 该类型的值
open_poolpub fn open_pool(endpoint string, max_idle int) Pool执行 open pool 操作。
endpoint stringmax_idle intPool — 该类型的值
(&Pool) duppub fn (p &Pool) dup() Pool创建并返回持有独立状态或底层的副本对象。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Pool — 该类型的值
(&Pool) get_connpub fn (p &Pool) get_conn() int&Pool 上的操作:读取 conn。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Pool) put_connpub fn (p &Pool) put_conn(fd int)&Pool 上的操作:执行 put conn 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) discard_connpub fn (p &Pool) discard_conn(fd int)&Pool 上的操作:丢弃连接。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) push_metricspub fn (p &Pool) push_metrics(job string, metrics_text string) bool&Pool 上的操作:执行 push metrics 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。job stringmetrics_text stringbool — 是否成立
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。
struct Pusherpub struct Pusher {
pub host string
pub port int
pub secure bool
pub job string
}承载 host、port、secure、job 等属性的结构体。
host stringport intsecure booljob stringnew_pusherpub fn new_pusher(endpoint string, job string) Pusher构造 pusher。
endpoint stringjob stringPusher — 该类型的值
(&Pusher) pushpub fn (p &Pusher) push(metrics_text string) bool&Pusher 上的操作:压入。
p &PusherPusher 实例;以只读借用传入,调用方仍持有实例。metrics_text stringbool — 是否成立
struct Registrypub struct Registry {
pub counters []Counter
pub gauges []Gauge
}承载 counters、gauges 等属性的结构体。
counters []Countergauges []Gaugenew_registrypub fn new_registry() Registry构造 registry。
无需传入参数。
Registry — 该类型的值
($&Registry) register_counterpub fn (r $&Registry) register_counter(c Counter)$&Registry 上的操作:执行 register counter 操作。
r $&RegistryRegistry 实例;以独占借用传入,方法可修改它,调用方仍持有实例。c Counter不返回值。
($&Registry) register_gaugepub fn (r $&Registry) register_gauge(g Gauge)$&Registry 上的操作:执行 register gauge 操作。
r $&RegistryRegistry 实例;以独占借用传入,方法可修改它,调用方仍持有实例。g Gauge不返回值。
(&Registry) gatherpub fn (r &Registry) gather() string在 &Registry 上执行 gather 操作。
r &RegistryRegistry 实例;以只读借用传入,调用方仍持有实例。string — 结果字符串