cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/prometheus.go
43lines · modecode
| 1 | package promapi |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "time" |
| 6 | |
| 7 | lru "github.com/hashicorp/golang-lru" |
| 8 | "github.com/prometheus/client_golang/api" |
| 9 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 10 | ) |
| 11 | |
| 12 | type Prometheus struct { |
| 13 | name string |
| 14 | uri string |
| 15 | api v1.API |
| 16 | timeout time.Duration |
| 17 | cache *lru.Cache |
| 18 | lock *partitionLocker |
| 19 | |
| 20 | slowQueryCache *lru.Cache |
| 21 | slowQueryLock sync.Mutex |
| 22 | } |
| 23 | |
| 24 | func NewPrometheus(name, uri string, timeout time.Duration) *Prometheus { |
| 25 | client, err := api.NewClient(api.Config{Address: uri}) |
| 26 | if err != nil { |
| 27 | // config validation should prevent this from ever happening |
| 28 | // panic so we don't need to return an error and it's easier to |
| 29 | // use this code in tests |
| 30 | panic(err) |
| 31 | } |
| 32 | cache, _ := lru.New(1000) |
| 33 | slowQueryCache, _ := lru.New(1000) |
| 34 | return &Prometheus{ |
| 35 | name: name, |
| 36 | uri: uri, |
| 37 | api: v1.NewAPI(client), |
| 38 | timeout: timeout, |
| 39 | cache: cache, |
| 40 | lock: newPartitionLocker((&sync.Mutex{})), |
| 41 | slowQueryCache: slowQueryCache, |
| 42 | } |
| 43 | } |
| 44 | |