-
Notifications
You must be signed in to change notification settings - Fork 2
/
fielder.go
603 lines (556 loc) · 18.6 KB
/
fielder.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package main
import (
"fmt"
"math"
"math/rand"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/dgryski/go-wyhash"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// spices is a list of common spices
var spices = []string{
"allspice", "anise", "basil", "bay", "black pepper", "cardamom", "cayenne",
"cinnamon", "cloves", "coriander", "cumin", "curry", "dill", "fennel", "fenugreek",
"garlic", "ginger", "marjoram", "mustard", "nutmeg", "oregano", "paprika", "parsley",
"pepper", "rosemary", "saffron", "sage", "salt", "tarragon", "thyme", "turmeric", "vanilla",
"caraway", "chili", "masala", "lemongrass", "mint", "poppy", "sesame", "sumac", "mace",
"nigella", "peppercorn", "wasabi",
}
// adjectives is a list of common adjectives
var adjectives = []string{
"able", "bad", "best", "better", "big", "black", "certain", "clear", "different", "early",
"easy", "economic", "federal", "free", "full", "good", "great", "hard", "high", "human",
"important", "international", "large", "late", "little", "local", "long", "low", "major",
"military", "national", "new", "old", "only", "other", "political", "possible", "public",
"real", "recent", "right", "small", "social", "special", "strong", "sure", "true", "white",
"whole", "young",
}
// nouns is a list of common nouns
var nouns = []string{
"angle", "ant", "apple", "arch", "arm", "army", "baby", "bag", "ball", "band", "basin", "basket", "bath", "bed", "bee", "bell",
"berry", "bird", "blade", "board", "boat", "bone", "book", "boot", "bottle", "box", "boy", "brain", "brake", "branch", "brick", "bridge",
"brush", "bucket", "bulb", "button", "cake", "camera", "card", "carriage", "cart", "cat", "chain", "cheese", "chess", "chin", "church", "circle",
"clock", "cloud", "coat", "collar", "comb", "cord", "cow", "cup", "curtain", "cushion", "dog", "door", "drain", "drawer", "dress", "drop",
"ear", "egg", "engine", "eye", "face", "farm", "feather", "finger", "fish", "flag", "floor", "fly", "foot", "fork", "fowl", "frame",
"garden", "girl", "glove", "goat", "gun", "hair", "hammer", "hand", "hat", "head", "heart", "hook", "horn", "horse", "hospital", "house",
"island", "jewel", "kettle", "key", "knee", "knife", "knot", "leaf", "leg", "library", "line", "lip", "lock", "map", "match", "monkey",
"moon", "mouth", "muscle", "nail", "neck", "needle", "nerve", "net", "nose", "nut", "office", "orange", "oven", "parcel", "pen", "pencil",
"picture", "pig", "pin", "pipe", "plane", "plate", "plough", "pocket", "pot", "potato", "prison", "pump", "rail", "rat", "receipt", "ring",
"rod", "roof", "root", "sail", "school", "scissors", "screw", "seed", "sheep", "shelf", "ship", "shirt", "shoe", "skin", "skirt", "snake",
"sock", "spade", "sponge", "spoon", "spring", "square", "stamp", "star", "station", "stem", "stick", "stocking", "stomach", "store", "street", "sun",
"table", "tail", "thread", "throat", "thumb", "ticket", "toe", "tongue", "tooth", "town", "train", "tray", "tree", "trousers", "umbrella", "wall",
"watch", "wheel", "whip", "whistle", "window", "wing", "wire", "worm",
}
// constfield is a field that *doesn't* start with slash
var constfield = regexp.MustCompile(`^([^/].*)$`)
// genfield is used to parse generator fields by matching valid commands and numeric arguments
// the second parameter, if it exists, includes the comma
var genfield = regexp.MustCompile(`^/([ibfsuk][awxrgqt]?)([0-9.-]+)?(,[0-9.-]+)?$`)
// keysplitter separates fields that look like number.name (ex: 1.myfield)
var keysplitter = regexp.MustCompile(`^([0-9]+)\.(.*$)`)
type Rng struct {
rng *rand.Rand
}
func NewRng(seed string) Rng {
return Rng{rand.New(rand.NewSource(int64(wyhash.Hash([]byte(seed), 2467825690))))}
}
func (r Rng) Intn(n int) int64 {
return int64(r.rng.Intn(n))
}
// Chooses a random element from a slice of strings.
func (r Rng) Choice(a []string) string {
if len(a) == 0 {
return ""
}
return a[r.Intn(len(a))]
}
// Chooses a random element from a slice of strings, with a quadratic bias
// towards the first elements.
func (r Rng) QuadraticChoice(a []string) string {
sq := float64(len(a) * len(a))
rn := r.Float(0, sq)
choice := len(a) - int(math.Floor(math.Sqrt(rn))) - 1
return a[choice]
}
func (r Rng) Bool() bool {
return r.Intn(2) == 0
}
func (r Rng) Int(min, max int) int64 {
return int64(r.rng.Intn(max-min) + min)
}
func (r Rng) Float(min, max float64) float64 {
return r.rng.Float64()*(max-min) + min
}
func (r Rng) Gaussian(mean, stddev float64) float64 {
return r.rng.NormFloat64()*stddev + mean
}
func (r Rng) GaussianInt(mean, stddev float64) int64 {
return int64(r.rng.NormFloat64()*stddev + mean)
}
func (r Rng) String(len int) string {
var b strings.Builder
for i := 0; i < len; i++ {
b.WriteByte(byte("abcdefghijklmnopqrstuvwxyz"[r.Int(0, 26)]))
}
return b.String()
}
func (r Rng) HexString(len int) string {
var b strings.Builder
for i := 0; i < len; i++ {
b.WriteByte(byte("0123456789abcdef"[r.Int(0, 16)]))
}
return b.String()
}
func (r Rng) WordPair() string {
return r.Choice(adjectives) + "-" + r.Choice(nouns)
}
func (r Rng) BoolWithProb(p float64) bool {
return r.Float(0, 100) < p
}
// getProcessID returns the process ID
func getProcessID() int64 {
return int64(os.Getpid())
}
func (r Rng) getValueGenerators() []func() any {
return []func() any{
func() any { return r.Intn(100) },
func() any { return r.BoolWithProb(99) },
func() any { return r.BoolWithProb(50) },
func() any { return r.BoolWithProb(1) },
func() any { return r.Int(-100, 100) },
func() any { return r.Float(0, 1000) },
func() any { return r.Float(0, 1) },
func() any { return r.GaussianInt(50, 30) },
func() any { return r.Gaussian(10000, 1000) },
func() any { return r.Gaussian(500, 300) },
func() any { return r.String(2) },
func() any { return r.String(5) },
func() any { return r.String(10) },
func() any { return r.String(4) + "-" + r.HexString(8) + "-" + r.String(4) },
func() any { return r.HexString(16) },
}
}
// getWordList returns a list of words with the specified cardinality;
// if a source word list is specified and cardinality fits within it, it uses it.
func getWordList(rng Rng, cardinality int, source []string) []string {
generator := rng.WordPair
if source != nil && len(source) >= cardinality {
generator = func() string { return rng.Choice(source) }
}
words := make([]string, cardinality)
for i := 0; i < cardinality; i++ {
words[i] = generator()
}
return words
}
type EligibilityPeriod struct {
word string
start time.Duration
end time.Duration
}
type PeriodicEligibility struct {
rng Rng
periods []EligibilityPeriod
period time.Duration
}
// generates a list of eligibility periods for a set of words
// each word is eligible for some period of time that is proportional to its position in the list
// this is so that all the words are not available at the same time, but eventually all of them are
func newPeriodicEligibility(rng Rng, words []string, period time.Duration) *PeriodicEligibility {
cardinality := len(words)
periods := make([]EligibilityPeriod, cardinality)
for i := 0; i < cardinality; i++ {
// calculate a period length that is proportional to the number of remaining words
periodLength := time.Duration(float64(period) * float64(cardinality-i) / float64(cardinality))
// startTime is a random value that ensures it will end before the next period starts
startTime := (period - periodLength) * time.Duration(rng.Float(0, 1))
periods[i] = EligibilityPeriod{
word: words[i],
start: startTime,
end: startTime + periodLength,
}
}
return &PeriodicEligibility{
rng: rng,
periods: periods,
period: period,
}
}
// gets one word from the list of eligible words based on the time since the start of the period
// This is, on average, slower than the random selection, but the random one can sometimes
// be very slow, so we use this as a fallback if we try randomly a few times and fail.
func (pe *PeriodicEligibility) getEligibleWordFallback(durationSinceStart time.Duration) string {
tInPeriod := durationSinceStart % pe.period
eligibleIndexes := make([]int, 0, 20)
for i, period := range pe.periods {
if period.start <= tInPeriod && tInPeriod < period.end {
eligibleIndexes = append(eligibleIndexes, i)
}
}
if len(eligibleIndexes) == 0 {
// shouldn't happen, but if it does, just pick the first word
return pe.periods[0].word
}
ix := eligibleIndexes[pe.rng.Intn(len(eligibleIndexes))]
return pe.periods[ix].word
}
func (pe *PeriodicEligibility) getEligibleWord(durationSinceStart time.Duration) string {
tInPeriod := durationSinceStart % pe.period
// try 10 times to find an eligible word
for i := 0; i < 5; i++ {
ix := pe.rng.Intn(len(pe.periods))
period := pe.periods[ix]
if period.start <= tInPeriod && tInPeriod < period.end {
return period.word
}
}
// use the fallback
return pe.getEligibleWordFallback(durationSinceStart)
}
// parseUserFields expects a list of fields in the form of name=constant or name=/gen.
// See README.md for more information.
func parseUserFields(rng Rng, userfields map[string]string) (map[string]func() any, error) {
// groups 1 2 3 4
fields := make(map[string]func() any)
for name, value := range userfields {
// see if it's a constant
if constfield.MatchString(value) {
fields[name] = getConst(value)
continue
}
// see if it's a generator
matches := genfield.FindStringSubmatch(value)
if matches == nil {
return nil, fmt.Errorf("unparseable user field %s=%s", name, value)
}
var err error
gentype := matches[1]
p1 := matches[2]
p2 := matches[3]
switch gentype {
case "i", "ir", "ig":
fields[name], err = getIntGen(rng, gentype, p1, p2)
if err != nil {
return nil, fmt.Errorf("invalid int in user field %s=%s: %w", name, value, err)
}
case "f", "fr", "fg":
fields[name], err = getFloatGen(rng, gentype, p1, p2)
if err != nil {
return nil, fmt.Errorf("invalid float in user field %s=%s: %w", name, value, err)
}
case "b":
n := 50.0
var err error
if p1 != "" {
n, err = strconv.ParseFloat(p1, 64)
if err != nil || n < 0 || n > 100 {
return nil, fmt.Errorf("invalid bool option in %s=%s", name, value)
}
}
fields[name] = func() any { return rng.BoolWithProb(n) }
case "s", "sw", "sx", "sa", "sq":
n := 16
if p1 != "" {
n, err = strconv.Atoi(p1)
if err != nil {
return nil, fmt.Errorf("invalid string option in %s=%s", name, value)
}
}
switch gentype {
case "sw":
// words with specified cardinality in a rectangular distribution
words := getWordList(rng, n, nil)
fields[name] = func() any { return rng.Choice(words) }
case "sq":
// words with specified cardinality in a quadratic distribution
words := getWordList(rng, n, nil)
fields[name] = func() any { return rng.QuadraticChoice(words) }
case "sx":
fields[name] = func() any { return rng.HexString(n) }
default:
fields[name] = func() any { return rng.String(n) }
}
case "k":
fields[name], err = getKeyGen(rng, p1, p2)
if err != nil {
return nil, fmt.Errorf("invalid key in key field %s=%s: %w", name, value, err)
}
case "u", "uq":
// Generate a URL-like string with a random path and possibly a query string
fields[name], err = getURLGen(rng, gentype, p1, p2)
if err != nil {
return nil, fmt.Errorf("invalid float in user field %s=%s: %w", name, value, err)
}
case "st":
// Generate a semi-plausible mix of status codes; percentage of 400s and 500s can be controlled by the extra args
twos := 95.0
fours := 4.0
fives := 1.0
if p1 != "" {
fours, err = strconv.ParseFloat(p1, 64)
if err != nil {
return nil, fmt.Errorf("invalid float in user field %s=%s: %w", name, value, err)
}
}
if p2 != "" {
fives, err = strconv.ParseFloat(p2[1:], 64)
if err != nil {
return nil, fmt.Errorf("invalid float in user field %s=%s: %w", name, value, err)
}
}
twos = 100 - fours - fives
fields[name] = func() any {
r := rng.Float(0, 100)
if r < twos {
return rng.QuadraticChoice([]string{"200", "200", "200", "201", "202"})
} else if r < twos+fours {
return rng.QuadraticChoice([]string{"404", "400", "400", "400", "402", "429", "403"})
} else {
return "500"
}
}
default:
return nil, fmt.Errorf("invalid generator type %s in field %s=%s", gentype, name, value)
}
}
return fields, nil
}
func getConst(value string) func() any {
var gen func() any
if value == "true" {
gen = func() any { return true }
} else if value == "false" {
gen = func() any { return false }
} else {
if i, err := strconv.ParseInt(value, 10, 64); err == nil {
gen = func() any { return i }
} else if f, err := strconv.ParseFloat(value, 64); err == nil {
gen = func() any { return f }
} else {
gen = func() any { return value }
}
}
return gen
}
func gaussianDefaults(v1, v2 float64) (float64, float64) {
if v1 == 0 && v2 == 0 {
v1 = 100
v2 = 10
} else if v2 == 0 {
v2 = v1 / 10
}
return v1, v2
}
func getIntGen(rng Rng, gentype, p1, p2 string) (func() any, error) {
var v1, v2 int
var err error
if p1 == "" {
v1 = 0
} else {
v1, err = strconv.Atoi(p1)
if err != nil {
return nil, fmt.Errorf("%s is not an int", p1)
}
}
if p2 == "" || p2 == "," {
v2 = v1
v1 = 0
} else {
v2, err = strconv.Atoi(p2[1:])
if err != nil {
return nil, fmt.Errorf("%s is not an int", p2[:1])
}
}
if gentype == "ig" {
g1, g2 := gaussianDefaults(float64(v1), float64(v2))
return func() any { return rng.GaussianInt(g1, g2) }, nil
} else {
if v1 == 0 && v2 == 0 {
v2 = 100
}
return func() any { return rng.Int(v1, v2) }, nil
}
}
func getFloatGen(rng Rng, gentype, p1, p2 string) (func() any, error) {
var v1, v2 float64
var err error
if p1 == "" {
v1 = 0
} else {
v1, err = strconv.ParseFloat(p1, 64)
if err != nil {
return nil, fmt.Errorf("%s is not a number", p1)
}
}
if p2 == "" || p2 == "," {
v2 = v1
v1 = 0
} else {
v2, err = strconv.ParseFloat(p2[1:], 64)
if err != nil {
return nil, fmt.Errorf("%s is not a number", p2[:1])
}
}
if gentype == "fg" {
g1, g2 := gaussianDefaults(v1, v2)
return func() any { return rng.GaussianInt(g1, g2) }, nil
} else {
if v1 == 0 && v2 == 0 {
v2 = 100
}
return func() any { return rng.Float(v1, v2) }, nil
}
}
func getURLGen(rng Rng, gentype, p1, p2 string) (func() any, error) {
var c1 int = 3
var c2 int = 10
var err error
if p1 != "" {
c1, err = strconv.Atoi(p1)
if err != nil {
return nil, fmt.Errorf("%s is not a number", p1)
}
}
if p2 != "" && p2 != "," {
c2, err = strconv.Atoi(p2[1:])
if err != nil {
return nil, fmt.Errorf("%s is not a number", p2[:1])
}
}
path1words := getWordList(rng, c1, nouns)
path1 := func() string { return rng.Choice(path1words) }
path2 := func() string { return "" }
if c2 != 0 {
path2words := getWordList(rng, c2, adjectives)
path2 = func() string { return rng.Choice(path2words) }
}
if gentype == "uq" {
return func() any {
return "https://example.com/" + path1() + "/" + path2() + "?extra=" + rng.String(10)
}, nil
} else {
return func() any {
return "https://example.com/" + path1() + "/" + path2()
}, nil
}
}
func getKeyGen(rng Rng, p1, p2 string) (func() any, error) {
var cardinality, period int
var err error
if p1 == "" {
cardinality = 50
} else {
cardinality, err = strconv.Atoi(p1)
if err != nil {
return nil, fmt.Errorf("%s is not an int", p1)
}
if cardinality > len(nouns) {
return nil, fmt.Errorf("cardinality %d cannot be more than %d", cardinality, len(nouns))
}
}
if p2 == "" || p2 == "," {
period = 60
} else {
period, err = strconv.Atoi(p2[1:])
if err != nil {
return nil, fmt.Errorf("%s is not an int", p2[:1])
}
}
ep := newPeriodicEligibility(rng, nouns[:cardinality], time.Duration(period)*time.Second)
startTime := time.Now()
return func() any { return ep.getEligibleWord(time.Since(startTime)) }, nil
}
type Fielder struct {
fields map[string]func() any
names []string
}
// Fielder is an object that takes a name and generates a map of
// fields based on using the name as a random seed.
// It takes a set of field specifications that are used to generate the fields.
// It also takes two counts: the number of fields to generate and the number of
// service names to generate. The field names are randomly generated by
// combining an adjective and a noun and are consistent for a given fielder.
// The field values are randomly generated.
// Fielder also includes the process_id.
func NewFielder(seed string, userFields map[string]string, nextras, nservices int) (*Fielder, error) {
rng := NewRng(seed)
gens := rng.getValueGenerators()
fields, err := parseUserFields(rng, userFields)
if err != nil {
return nil, err
}
for i := 0; i < nextras; i++ {
fieldname := rng.WordPair()
fields[fieldname] = gens[rng.Intn(len(gens))]
}
fields["process_id"] = func() any { return getProcessID() }
names := make([]string, nservices)
for i := 0; i < nservices; i++ {
names[i] = rng.Choice(spices)
}
return &Fielder{fields: fields, names: names}, nil
}
func (f *Fielder) GetServiceName(n int) string {
return f.names[n%len(f.names)]
}
// Searches for a field name that includes a level marker.
// These markers look like "1.fieldname" and are used to
// indicate that the field should be included at a specific
// level in the trace, where 0 is the root.
func (f *Fielder) atLevel(name string, level int) (string, bool) {
matches := keysplitter.FindStringSubmatch(name)
if len(matches) == 0 {
return name, true
}
keylevel, _ := strconv.Atoi(matches[1])
if keylevel == level {
return matches[2], true
}
return matches[2], false
}
func (f *Fielder) GetFields(count int64, level int) map[string]any {
fields := make(map[string]any)
if count != 0 {
fields["count"] = count
}
for k, v := range f.fields {
k, ok := f.atLevel(k, level)
if !ok {
continue
}
fields[k] = v()
}
return fields
}
func (f *Fielder) AddFields(span trace.Span, count int64, level int) {
attrs := make([]attribute.KeyValue, 0, 1+len(f.fields))
if count != 0 {
attrs = append(attrs, attribute.Int64("count", count))
}
for key, val := range f.fields {
key, ok := f.atLevel(key, level)
if !ok {
continue
}
switch v := val().(type) {
case int64:
attrs = append(attrs, attribute.Int64(key, v))
case uint64:
attrs = append(attrs, attribute.Int64(key, int64(v)))
case float64:
attrs = append(attrs, attribute.Float64(key, v))
case string:
attrs = append(attrs, attribute.String(key, v))
case bool:
attrs = append(attrs, attribute.Bool(key, v))
default:
panic(fmt.Sprintf("unknown type %T for %s -- implementation error in fielder.go", v, key))
}
}
span.SetAttributes(attrs...)
}