forked from wavefrontHQ/go-wavefront-management-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_example_test.go
51 lines (41 loc) · 1.2 KB
/
query_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package wavefront_test
import (
"fmt"
"io/ioutil"
"log"
"github.com/WavefrontHQ/go-wavefront-management-api"
)
func ExampleQuery() {
config := &wavefront.Config{
Address: "test.wavefront.com",
Token: "xxxx-xxxx-xxxx-xxxx-xxxx",
}
client, err := wavefront.NewClient(config)
if err != nil {
log.Fatal(err)
}
// enable debug - all requests get dumped to stdout before being executed
client.Debug(true)
// NewQueryParams generates a query using the given ts expression.
// By default the query period will be one hour since the current time.
query := client.NewQuery(wavefront.NewQueryParams(
`ts("cpu.load.1m.avg", dc=dc1)`,
))
// Set the query period to be one day instead of one hour
err = query.SetStartTime(24 * 60 * 60 * 1000)
if err != nil {
log.Fatal(err)
}
// Execute carries out the query
result, err := query.Execute()
if err != nil {
log.Fatal(err)
}
// The raw JSON response is available as RawResponse.
// This can be useful for debugging
b, _ := ioutil.ReadAll(result.RawResponse)
fmt.Println(string(b))
// The timeseries response can now be used to explore the results
fmt.Println(result.TimeSeries[0].Label)
fmt.Println(result.TimeSeries[0].DataPoints[0])
}