This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
176 lines (157 loc) · 4.54 KB
/
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mackerel
import (
"errors"
"fmt"
"net/http"
"reflect"
"sync"
"text/template"
"github.com/mackerelio/mackerel-client-go"
)
type handlerClient struct {
services map[string]*mackerel.Service
roles map[string]map[string]*mackerel.Role
hosts map[string]*mackerel.Host
mu sync.RWMutex
snapshot []*mackerel.HostMetricValue
}
var _ http.Handler = &handlerClient{}
func (c *handlerClient) FindServices() ([]*mackerel.Service, error) {
if len(c.services) == 0 {
return nil, nil
}
a := make([]*mackerel.Service, 0, len(c.services))
for _, s := range c.services {
a = append(a, s)
}
return a, nil
}
func (c *handlerClient) CreateService(param *mackerel.CreateServiceParam) (*mackerel.Service, error) {
if _, ok := c.services[param.Name]; ok {
return nil, errors.New("the service already exists")
}
s := &mackerel.Service{
Name: param.Name,
Memo: param.Memo,
}
if c.services == nil {
c.services = make(map[string]*mackerel.Service)
}
c.services[param.Name] = s
return s, nil
}
func (c *handlerClient) FindRoles(serviceName string) ([]*mackerel.Role, error) {
m := c.roles[serviceName]
a := make([]*mackerel.Role, 0, len(m))
for _, r := range m {
a = append(a, r)
}
return a, nil
}
func (c *handlerClient) CreateRole(serviceName string, param *mackerel.CreateRoleParam) (*mackerel.Role, error) {
m, ok := c.roles[serviceName]
if !ok {
m = make(map[string]*mackerel.Role)
if c.roles == nil {
c.roles = make(map[string]map[string]*mackerel.Role)
}
c.roles[serviceName] = m
}
if _, ok := m[param.Name]; ok {
return nil, errors.New("the role already exists")
}
r := &mackerel.Role{
Name: param.Name,
Memo: param.Memo,
}
m[r.Name] = r
return r, nil
}
func (c *handlerClient) FindHosts(param *mackerel.FindHostsParam) ([]*mackerel.Host, error) {
// BUG(lufia): currently, FindHosts supports seraching by CustomIdentifier only.
for _, h := range c.hosts {
if h.CustomIdentifier == param.CustomIdentifier {
return []*mackerel.Host{h}, nil
}
}
return nil, nil
}
func (c *handlerClient) CreateHost(param *mackerel.CreateHostParam) (string, error) {
id := fmt.Sprintf("%d", len(c.hosts)+1)
h := &mackerel.Host{
ID: id,
Name: param.Name,
DisplayName: param.DisplayName,
CustomIdentifier: param.CustomIdentifier,
Meta: param.Meta,
Interfaces: param.Interfaces,
// Roles: meta.RoleFullnames
}
if c.hosts == nil {
c.hosts = make(map[string]*mackerel.Host)
}
c.hosts[id] = h
return id, nil
}
func (c *handlerClient) UpdateHost(hostID string, param *mackerel.UpdateHostParam) (string, error) {
h, ok := c.hosts[hostID]
if !ok {
return "", errors.New("the host is not exist")
}
h.Name = param.Name
h.DisplayName = param.DisplayName
h.CustomIdentifier = param.CustomIdentifier
h.Meta = param.Meta
h.Interfaces = param.Interfaces
// h.Roles = param.RoleFullnames
return h.ID, nil
}
func (c *handlerClient) CreateGraphDefs(defs []*mackerel.GraphDefsParam) error {
return nil
}
func (c *handlerClient) PostHostMetricValues(metrics []*mackerel.HostMetricValue) error {
c.mu.Lock()
defer c.mu.Unlock()
c.snapshot = metrics
return nil
}
func (c *handlerClient) PostServiceMetricValues(name string, metrics []*mackerel.MetricValue) error {
// BUG(lufia): The pull mode don't support to post the service metrics.
return nil
}
// {{.Name}} is starting with "custom.", this is designed for the push mode.
// But container-agent or go-mackerel-plugin will add "custom." prefix.
// Therefore we should drop "custom." prefix if the pull mode.
var metricsTemplate = template.Must(template.New("metrics").Funcs(template.FuncMap{"formatValue": formatValue}).Parse(`
{{- range . -}}
{{slice .Name 7}} {{.Value | formatValue}} {{.Time}}
{{end -}}
`))
func formatValue(v reflect.Value) (reflect.Value, error) {
var s string
switch k := v.Kind(); k {
default:
return reflect.ValueOf(nil), fmt.Errorf("invalid type: %v", k)
case reflect.Bool:
if v.Bool() {
s = "1"
} else {
s = "0"
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s = fmt.Sprintf("%d", v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s = fmt.Sprintf("%d", v.Uint())
case reflect.Float32, reflect.Float64:
s = fmt.Sprintf("%f", v.Float())
}
return reflect.ValueOf(s), nil
}
func (c *handlerClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.mu.RLock()
a := c.snapshot
c.mu.RUnlock()
if err := metricsTemplate.Execute(w, a); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}