-
Notifications
You must be signed in to change notification settings - Fork 3
/
prom_file_handler.go
50 lines (40 loc) · 1.02 KB
/
prom_file_handler.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"syscall"
)
type promHandler struct {
config envConfig
}
func newPromFileHandler(config envConfig) (*promHandler, error) {
if !strings.HasSuffix(config.PrometheusConfigFilePath, ".json") {
return nil, fmt.Errorf("file does not have .json extension")
}
err := syscall.Access(config.PrometheusConfigFilePath, syscall.O_RDWR)
if err != nil {
return nil, err
}
handler := &promHandler{
config: config,
}
return handler, nil
}
func (h *promHandler) write(containersScrapeConfig map[string]containerScrapeConfig) error {
targetGroups := make([]containerScrapeConfig, 0, len(containersScrapeConfig))
for _, v := range containersScrapeConfig {
targetGroups = append(targetGroups, v)
}
data, err := json.Marshal(targetGroups)
if err != nil {
return err
}
log.Debugf("Write %s to %s", string(data), h.config.PrometheusConfigFilePath)
err = ioutil.WriteFile(h.config.PrometheusConfigFilePath, data, 0644)
if err != nil {
return err
}
return err
}