-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbulkquerycache_example_test.go
More file actions
55 lines (48 loc) · 1.47 KB
/
bulkquerycache_example_test.go
File metadata and controls
55 lines (48 loc) · 1.47 KB
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
52
53
54
55
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company
// SPDX-License-Identifier: Apache-2.0
package promquery_test
import (
"context"
"fmt"
"os"
"time"
"github.com/prometheus/common/model"
"github.com/sapcc/go-bits/must"
"github.com/sapcc/go-bits/promquery"
)
type HostName string
type HostFilesystemMetrics struct {
CapacityBytes int
UsedBytes int
}
var keyer = func(sample *model.Sample) HostName {
return HostName(sample.Metric["hostname"])
}
var queries = []promquery.BulkQuery[HostName, HostFilesystemMetrics]{
{
Query: "sum by (hostname) (filesystem_capacity_bytes)",
Description: "filesystem capacity data",
Keyer: keyer,
Filler: func(entry *HostFilesystemMetrics, sample *model.Sample) {
entry.CapacityBytes = int(sample.Value)
},
},
{
Query: "sum by (hostname) (filesystem_used_bytes)",
Description: "filesystem usage data",
Keyer: keyer,
Filler: func(entry *HostFilesystemMetrics, sample *model.Sample) {
entry.UsedBytes = int(sample.Value)
},
},
}
func ExampleBulkQueryCache() {
client := must.Return(promquery.ConfigFromEnv("PROMETHEUS").Connect())
cache := promquery.NewBulkQueryCache(queries, 5*time.Minute, client)
for _, arg := range os.Args[1:] {
hostName := HostName(arg)
entry := must.Return(cache.Get(context.Background(), hostName))
usagePercent := 100 * float64(entry.UsedBytes) / float64(entry.CapacityBytes)
fmt.Printf("disk usage on %s is %g%%\n", hostName, usagePercent)
}
}