This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
/
prometheus.go
92 lines (79 loc) · 1.85 KB
/
prometheus.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package service
import (
"bytes"
"fmt"
"text/template"
)
// docker-compose service
type PrometheusService struct {
Config PrometheusConfig
Name string
IP string
}
func (prom PrometheusService) String() string {
tmpl, err := template.New("PrometheusService").Parse(prometheusServieTemplate)
if err != nil {
fmt.Printf("Failed to parse template, %v", err)
return ""
}
result := new(bytes.Buffer)
err = tmpl.Execute(result, prom)
if err != nil {
fmt.Printf("Failed to render template, %v", err)
return ""
}
return result.String()
}
var prometheusServieTemplate = `{{ .Name }}:
hostname: {{ .Name }}
image: prom/prometheus:v2.3.2
ports:
- 9090:9090
volumes:
- "./prometheus.yml:/etc/prometheus/prometheus.yml"
networks:
app_net:
ipv4_address: {{ .IP }}
restart: "no"
`
func NewPrometheusService(ip string, targetHostNames []string) *PrometheusService {
return &PrometheusService{
Name: "prometheus",
IP: ip,
Config: NewPrometheusConfig(targetHostNames),
}
}
// prometheus server config
type PrometheusConfig struct {
Targets []string
}
func NewPrometheusConfig(targetHostNames []string) PrometheusConfig {
return PrometheusConfig{
Targets: targetHostNames,
}
}
func (promConfig PrometheusConfig) String() string {
tmpl, err := template.New("PrometheusConfig").Parse(prometheusConfigTemplate)
if err != nil {
fmt.Printf("Failed to parse template, %v", err)
return ""
}
result := new(bytes.Buffer)
err = tmpl.Execute(result, promConfig)
if err != nil {
fmt.Printf("Failed to render template, %v", err)
return ""
}
return result.String()
}
var prometheusConfigTemplate = `global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'klaytn'
static_configs:
- targets:
{{- range .Targets }}
- "{{ . }}:61001"
{{- end }}
`