xvm

package
v0.4.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MetricResultStatusSuccess MetricResultStatus = "success"
	MetricResultStatusError   MetricResultStatus = "error"

	// ResultTypeMatrix Range Vector (范围向量)
	// 示例查询
	//query := `node_cpu_seconds_total`
	//
	// 返回格式示例
	//{
	//  "resultType": "vector",
	//  "result": [
	//    {
	//      "metric": {"instance": "localhost:9100", "cpu": "0", "mode": "idle"},
	//      "value": [1634567890, "123.45"]
	//    },
	//    {
	//      "metric": {"instance": "localhost:9100", "cpu": "1", "mode": "idle"},
	//      "value": [1634567890, "234.56"]
	//    }
	//  ]
	//}
	ResultTypeMatrix ResultType = "matrix"

	// ResultTypeVector Instant Vector (瞬时向量)
	// 示例查询
	//query := `rate(node_cpu_seconds_total[5m])`
	//
	// 返回格式示例
	//{
	//  "resultType": "matrix",
	//  "result": [
	//    {
	//      "metric": {"instance": "localhost:9100", "cpu": "0"},
	//      "values": [
	//        [1634567890, "123.45"],
	//        [1634567900, "123.46"],
	//        [1634567910, "123.47"]
	//      ]
	//    }
	//  ]
	//}
	ResultTypeVector ResultType = "vector"

	// ResultTypeScalar Scalar (标量)
	// 示例查询
	//query := `count(up)`
	//
	// 返回格式示例
	//{
	//  "resultType": "scalar",
	//  "result": [1634567890, "42"]
	//}
	ResultTypeScalar ResultType = "scalar"

	// ResultTypeString String (字符串)
	// 示例查询
	//query := `prometheus_build_info`
	//
	// 返回格式示例
	//{
	//  "resultType": "string",
	//  "result": [1634567890, "v2.30.0"]
	//}
	ResultTypeString ResultType = "string"
)

Variables

View Source
var (
	ErrInvalidAuth = fmt.Errorf("认证失败: 无效的用户名或密码")
	ErrQuery       = fmt.Errorf("查询失败")
)

Functions

This section is empty.

Types

type ClientOptionFunc

type ClientOptionFunc func(*clientOptions)

ClientOptionFunc 客户端配置选项函数类型

func WithClientOptionBasicAuth

func WithClientOptionBasicAuth(username, password string) ClientOptionFunc

WithClientOptionBasicAuth 设置 Basic Auth 认证

func (ClientOptionFunc) Apply

func (x ClientOptionFunc) Apply(options *clientOptions)

type IClientOption

type IClientOption interface {
	Apply(options *clientOptions)
}

type IMetricsClient

type IMetricsClient interface {
	QueryRange(ctx context.Context, query string, opts ...IQueryRangeOption) (*QueryResult, error)
	Query(ctx context.Context, query string, queryOptions ...IQueryOption) (*QueryResult, error)
}

func NewMetricsClient

func NewMetricsClient(baseURL string, opts ...IClientOption) (IMetricsClient, error)

NewMetricsClient 创建新的 VictoriaMetrics 客户端

type IQueryOption

type IQueryOption interface {
	Apply(options *queryOptions)
}

type IQueryRangeOption

type IQueryRangeOption interface {
	Apply(options *queryRangeOptions)
}

type MatrixMetric

type MatrixMetric struct {
	// 指标的标签集合
	Labels map[string]string `json:"metric"`
	// matrix 类型的时间序列数据
	Values []Point `json:"values"`
}

type Metric

type Metric struct {
	// 指标的标签集合
	Labels map[string]string `json:"metric"`
	// matrix 类型的时间序列数据
	Values []Point `json:"values"`
	// vector/scalar 类型的单个值
	Value *Point `json:"value"`
}

Metric 表示指标数据

type MetricData

type MetricData struct {
	ResultType ResultType `json:"resultType"`
	Result     []Metric   `json:"result"`
}

MetricData 表示指标数据

func (*MetricData) IsMatrix

func (x *MetricData) IsMatrix() bool

func (*MetricData) IsScalar

func (x *MetricData) IsScalar() bool

func (*MetricData) IsString

func (x *MetricData) IsString() bool

func (*MetricData) IsVector

func (x *MetricData) IsVector() bool

func (*MetricData) MatrixValue

func (x *MetricData) MatrixValue() []MatrixMetric

func (*MetricData) ScalarValue

func (x *MetricData) ScalarValue() []ScalarMetric

func (*MetricData) StringValue

func (x *MetricData) StringValue() []StringMetric

func (*MetricData) VectorValue

func (x *MetricData) VectorValue() []VectorMetric

type MetricResult

type MetricResult struct {
	Status    MetricResultStatus `json:"status"`
	IsPartial bool               `json:"isPartial"`
	Data      MetricData         `json:"data"`
	Error     string             `json:"error,omitempty"`
	ErrorType string             `json:"errorType,omitempty"`
}

MetricResult 表示查询结果

func (*MetricResult) GetMetricSeries

func (r *MetricResult) GetMetricSeries() []struct {
	Labels map[string]string
	Values []MetricValue
}

GetMetricSeries 获取完整的时间序列数据

func (*MetricResult) GetValuesForMetric

func (r *MetricResult) GetValuesForMetric(metricLabels map[string]string) []Point

GetValuesForMetric 获取指定指标的所有值

func (*MetricResult) IsError

func (r *MetricResult) IsError() bool

IsError 检查查询是否出错

func (*MetricResult) IsSuccess

func (r *MetricResult) IsSuccess() bool

IsSuccess 检查查询是否成功

type MetricResultStatus

type MetricResultStatus string

MetricResultStatus 定义查询结果状态类型

type MetricValue

type MetricValue struct {
	Timestamp time.Time
	Value     float64
	RawValue  string
}

MetricValue 用于格式化输出的辅助结构体

func FormatMetricValues

func FormatMetricValues(points []Point) []MetricValue

FormatMetricValues 将 Points 转换为更易使用的格式

type MetricsClient

type MetricsClient struct {
	// contains filtered or unexported fields
}

MetricsClient VictoriaMetrics 客户端结构体

func (*MetricsClient) Query

func (c *MetricsClient) Query(ctx context.Context, query string, queryOptions ...IQueryOption) (*QueryResult, error)

Query 执行即时查询

func (*MetricsClient) QueryRange

func (c *MetricsClient) QueryRange(ctx context.Context, query string, opts ...IQueryRangeOption) (*QueryResult, error)

QueryRange 查询时间范围内的指标数据 ${query} [${start}, ${end}] interval(${step}) query - PromQL start - 开始时间 end - 结束时间 step - 步长 数据点的时间间隔

type Point

type Point [2]interface{}

Point 表示时间序列中的一个数据点

func (Point) StringValue

func (p Point) StringValue() string

StringValue 获取数据点的原始字符串值

func (Point) Timestamp

func (p Point) Timestamp() time.Time

Timestamp 获取数据点的时间戳

func (Point) Value

func (p Point) Value() float64

Value 获取数据点的值

type QueryOptionFunc

type QueryOptionFunc func(*queryOptions)

QueryOptionFunc 查询选项函数类型

func WithQueryOptionLimit

func WithQueryOptionLimit(limit uint32) QueryOptionFunc

WithQueryOptionLimit 设置每个时间序列最大返回的数据数量

func WithQueryOptionTimestamp

func WithQueryOptionTimestamp(timestamp time.Time) QueryOptionFunc

WithQueryOptionTimestamp 设置查询时间戳

func (QueryOptionFunc) Apply

func (x QueryOptionFunc) Apply(options *queryOptions)

type QueryRangeOptionFunc

type QueryRangeOptionFunc func(*queryRangeOptions)

QueryRangeOptionFunc 查询范围选项函数类型

func WithQueryRangeOptionEnd

func WithQueryRangeOptionEnd(end time.Time) QueryRangeOptionFunc

WithQueryRangeOptionEnd 设置查询结束时间

func WithQueryRangeOptionLimit

func WithQueryRangeOptionLimit(limit uint32) QueryRangeOptionFunc

WithQueryRangeOptionLimit 设置每个时间序列最大返回的数据数量

func WithQueryRangeOptionStart

func WithQueryRangeOptionStart(start time.Time) QueryRangeOptionFunc

WithQueryRangeOptionStart 设置查询开始时间

func (QueryRangeOptionFunc) Apply

func (x QueryRangeOptionFunc) Apply(options *queryRangeOptions)

type QueryResult

type QueryResult struct {
	IsPartial bool       `json:"isPartial"`
	Data      MetricData `json:"data"`
}

type ResultType

type ResultType string

ResultType 定义结果类型

type ScalarMetric

type ScalarMetric struct {
	Value Point `json:"values"`
}

type StringMetric

type StringMetric struct {
	Value Point `json:"values"`
}

type VectorMetric

type VectorMetric struct {
	// 指标的标签集合
	Labels map[string]string `json:"metric"`
	// vector 类型的时间序列数据
	Value Point `json:"values"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL