Skip to content

Commit 38a1049

Browse files
committed
feat(example): add example
1 parent 27eaf68 commit 38a1049

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

_examples/main.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/wangxin688/gopromql"
7+
)
8+
9+
func main() {
10+
11+
pb1 := example1()
12+
13+
pb2 := example2()
14+
15+
pb3 := example3()
16+
17+
pb4 := example4()
18+
19+
fmt.Println(pb1, pb2, pb3, pb4)
20+
}
21+
22+
func example1() string {
23+
24+
pb, err := gopromql.NewPromqlBuilder("http_requests_total").
25+
WithFuncName("last_over_time").
26+
WithWindow("5m").
27+
Build()
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
return pb
33+
}
34+
35+
func example2() string {
36+
37+
pb, err := gopromql.NewPromqlBuilder("http_requests_total").
38+
WithFuncName("sum_over_time").
39+
WithLabels(gopromql.Label{Name: "status_code", Matcher: "=", Value: "200"}).
40+
WithLabels(gopromql.Label{Name: "method", Matcher: "=~", Value: "GET|POST|PUT"}).
41+
WithLabels(gopromql.Label{Name: "path", Matcher: "!=", Value: "/health"}).
42+
WithWindow("5m").
43+
Build()
44+
if err != nil {
45+
panic(err)
46+
}
47+
return pb
48+
}
49+
50+
func example3() string {
51+
pb, err := gopromql.NewPromqlBuilder("http_requests_total").
52+
WithFuncName("sum_over_time").
53+
WithLabels(gopromql.Label{Name: "status_code", Matcher: "=", Value: "200"}).
54+
WithLabels(gopromql.Label{Name: "method", Matcher: "=~", Value: "GET|POST|PUT"}).
55+
WithLabels(gopromql.Label{Name: "path", Matcher: "!=", Value: "/health"}).
56+
WithComp(gopromql.Compare{Op: gopromql.GreaterThan, Value: 0}).
57+
WithWindow("5m").
58+
Build()
59+
if err != nil {
60+
panic(err)
61+
}
62+
63+
return pb
64+
}
65+
66+
func example4() string {
67+
pb, err := gopromql.NewPromqlBuilder("http_requests_total").
68+
WithFuncName("sum_over_time").
69+
WithLabels(gopromql.Label{Name: "status_code", Matcher: "=", Value: "200"}).
70+
WithAgg(gopromql.Aggregation{Op: gopromql.Max, AggWay: gopromql.GroupBy, By: []string{"method", "path"}}).
71+
WithWindow("5m").
72+
Build()
73+
if err != nil {
74+
panic(err)
75+
}
76+
return pb
77+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module gopromql
1+
module github.com/wangxin688/gopromql
22

33
go 1.22.1

0 commit comments

Comments
 (0)