-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmeasurement_subr.go
186 lines (153 loc) · 4.08 KB
/
measurement_subr.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
182
183
184
185
186
package atlas
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/pkg/errors"
)
// MeasurementResp contains all the results of the measurements
type MeasurementResp struct {
Measurements []int
}
// NewMeasurement create a new MeasurementRequest and fills some fields
func (c *Client) NewMeasurement() (req *MeasurementRequest) {
var defs []Definition
ps := NewProbeSet(c.config.PoolSize, c.config.AreaType, c.config.AreaValue, c.config.Tags)
req = &MeasurementRequest{
Definitions: defs,
IsOneoff: true,
Probes: []ProbeSet{ps},
}
c.verbose("probes: %#v", req.Probes)
return
}
func isPositive(tag string) (string, bool) {
bare := tag
if tag == "" {
return "", true
}
if tag[0] == '+' || tag[0] == '-' || tag[0] == '!' {
bare = tag[1:]
}
if tag[0] == '-' || tag[0] == '!' {
return bare, false
}
return bare, true
}
// splitTags analyse tags values:
// +tag / tag ==> tags_include
// -tag / !tag ==> tags_exclude
func splitTags(tags string) (in, out string) {
var (
aIn []string
aOut []string
)
all := strings.Split(tags, ",")
if len(all) == 0 {
return "", ""
}
for _, tag := range all {
if bare, yes := isPositive(tag); yes {
aIn = append(aIn, bare)
} else {
aOut = append(aOut, bare)
}
}
return strings.Join(aIn, ","), strings.Join(aOut, ",")
}
// NewProbeSet create a set of probes for later requests
func NewProbeSet(howmany int, settype, value string, tags string) (ps ProbeSet) {
var aIn, aOut string
if howmany == 0 {
howmany = 10
}
if settype == "" {
settype = "area"
}
if value == "" {
value = "WW"
}
// If tags were specified, analyze them
if tags != "" {
aIn, aOut = splitTags(tags)
}
ps = ProbeSet{
Requested: howmany,
Type: settype,
Value: value,
TagsInclude: aIn,
TagsExclude: aOut,
}
return
}
// AddDefinition create a new MeasurementRequest and fills some fields
func (m *MeasurementRequest) AddDefinition(fields map[string]string) *MeasurementRequest {
def := new(Definition)
err := FillDefinition(def, fields)
if err == nil {
m.Definitions = append(m.Definitions, *def)
}
return m
}
// createMeasurement creates a measurement for all types
func (c *Client) createMeasurement(t string, d *MeasurementRequest) (m *MeasurementResp, err error) {
opts := make(map[string]string)
opts = c.addAPIKey(opts)
req := c.prepareRequest("POST", fmt.Sprintf("measurements/%s", t), opts)
body, err := json.Marshal(d)
if err != nil {
return nil, errors.Wrapf(err, "encoding raw=%v", d)
}
buf := bytes.NewReader(body)
req.Body = ioutil.NopCloser(buf)
req.ContentLength = int64(buf.Len())
c.verbose("req: %#v", req)
c.verbose("body: %s", body)
resp, err := c.call(req)
c.verbose("resp: %v", resp)
if err != nil {
c.log.Printf("err(call): %v", err)
//return
}
m = &MeasurementResp{}
c.debug("handling errors")
body, err = c.handleAPIResponse(resp)
if err != nil {
c.debug("body=%s", string(body))
return m, errors.Wrap(err, "createMeasurement")
}
err = json.Unmarshal(body, m)
// Only display if debug/verbose
c.verbose("m: %v\nresp: %#v\nd: %v\n", m, string(body), d)
if err != nil {
err = fmt.Errorf("err: %v - m:%v", err, m)
return
}
return
}
// DNS creates a measurement
func (c *Client) DNS(d *MeasurementRequest) (m *MeasurementResp, err error) {
return c.createMeasurement("dns", d)
}
// HTTP creates a measurement
func (c *Client) HTTP(d *MeasurementRequest) (m *MeasurementResp, err error) {
return c.createMeasurement("http", d)
}
// NTP creates a measurement
func (c *Client) NTP(d *MeasurementRequest) (m *MeasurementResp, err error) {
return c.createMeasurement("ntp", d)
}
// Ping creates a measurement
func (c *Client) Ping(d *MeasurementRequest) (m *MeasurementResp, err error) {
return c.createMeasurement("ping", d)
}
// SSLCert creates a measurement
func (c *Client) SSLCert(d *MeasurementRequest) (m *MeasurementResp, err error) {
return c.createMeasurement("sslcert", d)
}
// Traceroute creates a measurement
func (c *Client) Traceroute(d *MeasurementRequest) (m *MeasurementResp, err error) {
return c.createMeasurement("traceroute", d)
}