From 38a1049e0c7b8365244e220c07bad35e10b04f90 Mon Sep 17 00:00:00 2001 From: wangxin688 <182467653@qq.com> Date: Fri, 7 Jun 2024 22:37:55 +0800 Subject: [PATCH] feat(example): add example --- _examples/main.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 _examples/main.go diff --git a/_examples/main.go b/_examples/main.go new file mode 100644 index 0000000..659679f --- /dev/null +++ b/_examples/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + + "github.com/wangxin688/gopromql" +) + +func main() { + + pb1 := example1() + + pb2 := example2() + + pb3 := example3() + + pb4 := example4() + + fmt.Println(pb1, pb2, pb3, pb4) +} + +func example1() string { + + pb, err := gopromql.NewPromqlBuilder("http_requests_total"). + WithFuncName("last_over_time"). + WithWindow("5m"). + Build() + if err != nil { + panic(err) + } + + return pb +} + +func example2() string { + + pb, err := gopromql.NewPromqlBuilder("http_requests_total"). + WithFuncName("sum_over_time"). + WithLabels(gopromql.Label{Name: "status_code", Matcher: "=", Value: "200"}). + WithLabels(gopromql.Label{Name: "method", Matcher: "=~", Value: "GET|POST|PUT"}). + WithLabels(gopromql.Label{Name: "path", Matcher: "!=", Value: "/health"}). + WithWindow("5m"). + Build() + if err != nil { + panic(err) + } + return pb +} + +func example3() string { + pb, err := gopromql.NewPromqlBuilder("http_requests_total"). + WithFuncName("sum_over_time"). + WithLabels(gopromql.Label{Name: "status_code", Matcher: "=", Value: "200"}). + WithLabels(gopromql.Label{Name: "method", Matcher: "=~", Value: "GET|POST|PUT"}). + WithLabels(gopromql.Label{Name: "path", Matcher: "!=", Value: "/health"}). + WithComp(gopromql.Compare{Op: gopromql.GreaterThan, Value: 0}). + WithWindow("5m"). + Build() + if err != nil { + panic(err) + } + + return pb +} + +func example4() string { + pb, err := gopromql.NewPromqlBuilder("http_requests_total"). + WithFuncName("sum_over_time"). + WithLabels(gopromql.Label{Name: "status_code", Matcher: "=", Value: "200"}). + WithAgg(gopromql.Aggregation{Op: gopromql.Max, AggWay: gopromql.GroupBy, By: []string{"method", "path"}}). + WithWindow("5m"). + Build() + if err != nil { + panic(err) + } + return pb +} diff --git a/go.mod b/go.mod index 5351ae8..846c999 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module gopromql +module github.com/wangxin688/gopromql go 1.22.1