forked from elastic/go-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
181 lines (151 loc) · 3.57 KB
/
metrics.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
177
178
179
180
181
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
package estransport
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
)
// Measurable defines the interface for transports supporting metrics.
//
type Measurable interface {
Metrics() (Metrics, error)
}
// connectionable defines the interface for transports returning a list of connections.
//
type connectionable interface {
connections() []*Connection
}
// Metrics represents the transport metrics.
//
type Metrics struct {
Requests int `json:"requests"`
Failures int `json:"failures"`
Responses map[int]int `json:"responses"`
Connections []fmt.Stringer `json:"connections"`
}
// ConnectionMetric represents metric information for a connection.
//
type ConnectionMetric struct {
URL string `json:"url"`
Failures int `json:"failures,omitempty"`
IsDead bool `json:"dead,omitempty"`
DeadSince *time.Time `json:"dead_since,omitempty"`
Meta struct {
ID string `json:"id"`
Name string `json:"name"`
Roles []string `json:"roles"`
} `json:"meta"`
}
// metrics represents the inner state of metrics.
//
type metrics struct {
sync.RWMutex
requests int
failures int
responses map[int]int
connections []*Connection
}
// Metrics returns the transport metrics.
//
func (c *Client) Metrics() (Metrics, error) {
if c.metrics == nil {
return Metrics{}, errors.New("transport metrics not enabled")
}
c.metrics.RLock()
defer c.metrics.RUnlock()
if lockable, ok := c.pool.(sync.Locker); ok {
lockable.Lock()
defer lockable.Unlock()
}
m := Metrics{
Requests: c.metrics.requests,
Failures: c.metrics.failures,
Responses: c.metrics.responses,
}
if pool, ok := c.pool.(connectionable); ok {
for _, c := range pool.connections() {
c.Lock()
cm := ConnectionMetric{
URL: c.URL.String(),
IsDead: c.IsDead,
Failures: c.Failures,
}
if !c.DeadSince.IsZero() {
cm.DeadSince = &c.DeadSince
}
if c.ID != "" {
cm.Meta.ID = c.ID
}
if c.Name != "" {
cm.Meta.Name = c.Name
}
if len(c.Roles) > 0 {
cm.Meta.Roles = c.Roles
}
m.Connections = append(m.Connections, cm)
c.Unlock()
}
}
return m, nil
}
// String returns the metrics as a string.
//
func (m Metrics) String() string {
var (
i int
b strings.Builder
)
b.WriteString("{")
b.WriteString("Requests:")
b.WriteString(strconv.Itoa(m.Requests))
b.WriteString(" Failures:")
b.WriteString(strconv.Itoa(m.Failures))
if len(m.Responses) > 0 {
b.WriteString(" Responses: ")
b.WriteString("[")
for code, num := range m.Responses {
b.WriteString(strconv.Itoa(code))
b.WriteString(":")
b.WriteString(strconv.Itoa(num))
if i+1 < len(m.Responses) {
b.WriteString(", ")
}
i++
}
b.WriteString("]")
}
b.WriteString(" Connections: [")
for i, c := range m.Connections {
b.WriteString(c.String())
if i+1 < len(m.Connections) {
b.WriteString(", ")
}
i++
}
b.WriteString("]")
b.WriteString("}")
return b.String()
}
// String returns the connection information as a string.
//
func (cm ConnectionMetric) String() string {
var b strings.Builder
b.WriteString("{")
b.WriteString(cm.URL)
if cm.IsDead {
fmt.Fprintf(&b, " dead=%v", cm.IsDead)
}
if cm.Failures > 0 {
fmt.Fprintf(&b, " failures=%d", cm.Failures)
}
if cm.DeadSince != nil {
fmt.Fprintf(&b, " dead_since=%s", cm.DeadSince.Local().Format(time.Stamp))
}
b.WriteString("}")
return b.String()
}