本文主要是介绍【博客707】模版化拆解并获取victoriametrics的metricsql各个元素,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
golang解析victoriametrics的metricsql
场景:
- 需要拆解metricsql中的部分元素,比如:rollup function,label filter等
- 需要对语法合法性进行判断,同时拒绝某些查询函数
- 我们需要拆解metricsql并进行改造
使用victoriametrics的metricsql项目来解析metricsql
example:
package mainimport ("fmt""log""github.com/VictoriaMetrics/metricsql"
)func ExampleParse() {expr, err := metricsql.Parse(`sum(rate(foo{bar="baz"}[5m])) by (x,y)`)if err != nil {log.Fatalf("parse error: %s", err)}fmt.Printf("parsed expr: %s\n", expr.AppendString(nil))ae := expr.(*metricsql.AggrFuncExpr)fmt.Printf("aggr func: name=%s, arg=%s, modifier=%s\n", ae.Name, ae.Args[0].AppendString(nil), ae.Modifier.AppendString(nil))fe := ae.Args[0].(*metricsql.FuncExpr)fmt.Printf("func: name=%s, arg=%s\n", fe.Name, fe.Args[0].AppendString(nil))re := fe.Args[0].(*metricsql.RollupExpr)fmt.Printf("rollup: expr=%s, window=%s\n", re.Expr.AppendString(nil), re.Window.AppendString(nil))me := re.Expr.(*metricsql.MetricExpr)fmt.Printf("metric: labelFilter1=%s, labelFilter2=%s", me.LabelFilterss[0][0].AppendString(nil), me.LabelFilterss[0][1].AppendString(nil))// Output:// parsed expr: sum(rate(foo{bar="baz"}[5m])) by(x,y)// aggr func: name=sum, arg=rate(foo{bar="baz"}[5m]), modifier=by(x,y)// func: name=rate, arg=foo{bar="baz"}[5m]// rollup: expr=foo{bar="baz"}, window=5m// metric: labelFilter1=__name__="foo", labelFilter2=bar="baz"
}func ExampleExpandWithExprs() {// mql can contain arbitrary MetricsQL extensions - see https://github.com/VictoriaMetrics/VictoriaMetrics/wiki/MetricsQLmql := `WITH (commonFilters = {job="$job", instance="$instance"},f(a, b) = 100*(a/b),)f(disk_free_bytes{commonFilters}, disk_total_bytes{commonFilters})`// Convert mql to PromQLpql, err := metricsql.ExpandWithExprs(mql)if err != nil {log.Fatalf("cannot expand with expressions: %s", err)}fmt.Printf("%s\n", pql)// Output:// 100 * (disk_free_bytes{job="$job",instance="$instance"} / disk_total_bytes{job="$job",instance="$instance"})
}func main() {ExampleParse()ExampleExpandWithExprs()
}
result:
go run main.go
parsed expr: sum(rate(foo{bar="baz"}[5m])) by(x,y)
aggr func: name=sum, arg=rate(foo{bar="baz"}[5m]), modifier=by(x,y)
func: name=rate, arg=foo{bar="baz"}[5m]
rollup: expr=foo{bar="baz"}, window=5m
metric: labelFilter1=__name__="foo", labelFilter2=bar="baz"100 * (disk_free_bytes{job="$job",instance="$instance"} / disk_total_bytes{job="$job",instance="$instance"})
这篇关于【博客707】模版化拆解并获取victoriametrics的metricsql各个元素的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!