Skip to content

Commit 801fabc

Browse files
authored
Merge pull request #630 from csmarchbanks/api-examples
Provide godoc examples for using the api
2 parents 170205f + 48fdc30 commit 801fabc

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

api/prometheus/v1/example_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2019 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// Package v1_test provides examples making requests to Prometheus using the
15+
// Golang client.
16+
package v1_test
17+
18+
import (
19+
"context"
20+
"fmt"
21+
"os"
22+
"time"
23+
24+
"github.com/prometheus/client_golang/api"
25+
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
26+
)
27+
28+
func ExampleAPI_Query() {
29+
client, err := api.NewClient(api.Config{
30+
Address: "http://demo.robustperception.io:9090",
31+
})
32+
if err != nil {
33+
fmt.Printf("Error creating client: %v\n", err)
34+
os.Exit(1)
35+
}
36+
37+
api := v1.NewAPI(client)
38+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
39+
defer cancel()
40+
result, warnings, err := api.Query(ctx, "up", time.Now())
41+
if err != nil {
42+
fmt.Printf("Error querying Prometheus: %v\n", err)
43+
os.Exit(1)
44+
}
45+
if len(warnings) > 0 {
46+
fmt.Printf("Warnings: %v\n", warnings)
47+
}
48+
fmt.Printf("Result:\n%v\n", result)
49+
}
50+
51+
func ExampleAPI_QueryRange() {
52+
client, err := api.NewClient(api.Config{
53+
Address: "http://demo.robustperception.io:9090",
54+
})
55+
if err != nil {
56+
fmt.Printf("Error creating client: %v\n", err)
57+
os.Exit(1)
58+
}
59+
60+
api := v1.NewAPI(client)
61+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
62+
defer cancel()
63+
r := v1.Range{
64+
Start: time.Now().Add(-time.Hour),
65+
End: time.Now(),
66+
Step: time.Minute,
67+
}
68+
result, warnings, err := api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
69+
if err != nil {
70+
fmt.Printf("Error querying Prometheus: %v\n", err)
71+
os.Exit(1)
72+
}
73+
if len(warnings) > 0 {
74+
fmt.Printf("Warnings: %v\n", warnings)
75+
}
76+
fmt.Printf("Result:\n%v\n", result)
77+
}
78+
79+
func ExampleAPI_Series() {
80+
client, err := api.NewClient(api.Config{
81+
Address: "http://demo.robustperception.io:9090",
82+
})
83+
if err != nil {
84+
fmt.Printf("Error creating client: %v\n", err)
85+
os.Exit(1)
86+
}
87+
88+
api := v1.NewAPI(client)
89+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
90+
defer cancel()
91+
lbls, warnings, err := api.Series(ctx, []string{
92+
"{__name__=~\"scrape_.+\",job=\"node\"}",
93+
"{__name__=~\"scrape_.+\",job=\"prometheus\"}",
94+
}, time.Now().Add(-time.Hour), time.Now())
95+
if err != nil {
96+
fmt.Printf("Error querying Prometheus: %v\n", err)
97+
os.Exit(1)
98+
}
99+
if len(warnings) > 0 {
100+
fmt.Printf("Warnings: %v\n", warnings)
101+
}
102+
fmt.Println("Result:")
103+
for _, lbl := range lbls {
104+
fmt.Println(lbl)
105+
}
106+
}

0 commit comments

Comments
 (0)