XU XUv0.16.0

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

导入方式: prometheus #github.com/xulang-org/packages/prometheus

公开 API: 7 函数, 17 方法, 7 类型, 0 常量, 0 方法集

扩展包 `prometheus #github.com/xulang-org/packages/prometheus`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

40_standalone_prometheus/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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 声明

open_pusher

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

用途

执行 open pusher 操作。

参数

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

返回值

Pusher — 该类型的值

struct Label

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

用途

承载 name、value 等属性的结构体。

公开字段

name string
名字
value string
字段(string)

查看用法示例

struct MetricSample

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

用途

承载 label_str、value 等属性的结构体。

公开字段

label_str string
字段(string)
value string
字段(string)

struct Counter

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

用途

承载 name、help、samples 等属性的结构体。

公开字段

name string
名字
help string
字段(string)
samples []MetricSample
字段([]MetricSample)

struct Gauge

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

用途

承载 name、help、samples 等属性的结构体。

公开字段

name string
名字
help string
字段(string)
samples []MetricSample
字段([]MetricSample)

new_counter

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

用途

构造 counter。

参数

name string
名字(string)
help string
help(string)

返回值

Counter — 该类型的值

查看用法示例

new_gauge

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

用途

构造 gauge。

参数

name string
名字(string)
help string
help(string)

返回值

Gauge — 该类型的值

查看用法示例

($&Counter) inc

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

用途

在 $&Counter 上执行 inc 操作。

参数

c $&Counter
本次调用使用的 Counter 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

不返回值。

($&Counter) inc_by

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

用途

$&Counter 上的操作:执行 inc by 操作。

参数

c $&Counter
本次调用使用的 Counter 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
delta int
增量(int)

返回值

不返回值。

($&Counter) inc_labels

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

用途

$&Counter 上的操作:执行 inc labels 操作。

参数

c $&Counter
本次调用使用的 Counter 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
labels []Label
labels([]Label)
delta int
增量(int)

返回值

不返回值。

($&Gauge) set

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

用途

在 $&Gauge 上执行 set 操作。

参数

g $&Gauge
本次调用使用的 Gauge 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
val string
值(string)

返回值

不返回值。

($&Gauge) set_labels

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

用途

$&Gauge 上的操作:设置 labels。

参数

g $&Gauge
本次调用使用的 Gauge 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
labels []Label
labels([]Label)
val string
值(string)

返回值

不返回值。

(Counter) format

XuAPI
pub fn (c Counter) format() string

用途

Counter 上的操作:格式化为字符串。

参数

c Counter
本次调用使用的 Counter 实例;按值接收。

返回值

string — 结果字符串

(Gauge) format

XuAPI
pub fn (g Gauge) format() string

用途

Gauge 上的操作:格式化为字符串。

参数

g Gauge
本次调用使用的 Gauge 实例;按值接收。

返回值

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
}

用途

Prometheus 指标导出与推送连接池管理器。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

new_pool

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

用途

构造 pool。

参数

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

返回值

Pool — 该类型的值

open_pool

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

用途

执行 open pool 操作。

参数

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

返回值

Pool — 该类型的值

查看用法示例

(&Pool) dup

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

用途

创建并返回持有独立状态或底层的副本对象。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Pool — 该类型的值

(&Pool) get_conn

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

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

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

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) discard_conn

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

用途

&Pool 上的操作:丢弃连接。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) push_metrics

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

用途

&Pool 上的操作:执行 push metrics 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
job string
job(string)
metrics_text string
metrics_text(string)

返回值

bool — 是否成立

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

struct Pusher

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

用途

承载 host、port、secure、job 等属性的结构体。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
job string
字段(string)

new_pusher

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

用途

构造 pusher。

参数

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

返回值

Pusher — 该类型的值

(&Pusher) push

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

用途

&Pusher 上的操作:压入。

参数

p &Pusher
本次调用使用的 Pusher 实例;以只读借用传入,调用方仍持有实例。
metrics_text string
metrics_text(string)

返回值

bool — 是否成立

struct Registry

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

用途

承载 counters、gauges 等属性的结构体。

公开字段

counters []Counter
字段([]Counter)
gauges []Gauge
字段([]Gauge)

new_registry

XuAPI
pub fn new_registry() Registry

用途

构造 registry。

参数

无需传入参数。

返回值

Registry — 该类型的值

查看用法示例

($&Registry) register_counter

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

用途

$&Registry 上的操作:执行 register counter 操作。

参数

r $&Registry
本次调用使用的 Registry 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
c Counter
字节或字符码(Counter)

返回值

不返回值。

($&Registry) register_gauge

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

用途

$&Registry 上的操作:执行 register gauge 操作。

参数

r $&Registry
本次调用使用的 Registry 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
g Gauge
g(Gauge)

返回值

不返回值。

(&Registry) gather

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

用途

在 &Registry 上执行 gather 操作。

参数

r &Registry
本次调用使用的 Registry 实例;以只读借用传入,调用方仍持有实例。

返回值

string — 结果字符串