-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay.zzzgo
887 lines (857 loc) · 24.2 KB
/
replay.zzzgo
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
// TEMPLATE-FILE
// TEMPLATE-FILE
package xopconsole
import (
"bufio"
"context"
"io"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/xoplog/xop-go/xopat"
"github.com/xoplog/xop-go/xopbase"
"github.com/xoplog/xop-go/xopnum"
"github.com/xoplog/xop-go/xopproto"
"github.com/xoplog/xop-go/xoptrace"
"github.com/xoplog/xop-go/xoputil"
"github.com/xoplog/xop-go/xoputil/replayutil"
"github.com/xoplog/xop-go/xoputil/xopversion"
"github.com/pkg/errors"
)
type replayData struct {
lineCount int
currentLine string
errors []error
spans map[xoptrace.HexBytes8]*replaySpan
requests map[xoptrace.HexBytes8]*replayRequest
dest xopbase.Logger
attributeDefinitions *replayutil.GlobalAttributeDefinitions
}
type replaySpan struct {
replayData
request *replayRequest
span xopbase.Span
version int
}
type replayRequest struct {
replaySpan
ts time.Time
trace xoptrace.Trace
version int64
name string
sourceAndVersion string
namespaceAndVersion string
baseRequest xopbase.Request
requestAttributes *replayutil.RequestAttributeDefinitons
bundle xoptrace.Bundle
attributeRegistry *xopat.Registry
}
type replayLine struct {
replayData
ts time.Time
spanID xoptrace.HexBytes8
level xopnum.Level
message string
stack []runtime.Frame
line xopbase.Line
attributes []func(xopbase.Line)
}
// xop alert 2023-05-31T22:20:09.200456-07:00 72b09846e8ed0099 "like a rock\"\\<'\n\r\t\b\x00" frightening=stuff STACK: /Users/sharnoff/src/github.com/muir/xop-go/xoptest/xoptestutil/cases.go:39 /Users/sharnoff/src/github.com/muir/xop-go/xopconsole/replay_test.go:43 /usr/local/Cellar/go/1.20.1/libexec/src/testing/testing.go:1576
type lineType int
const (
lineTypeLine lineType = iota
lineTypeTemplate
lineTypeModel
lineTypeLink
)
func (x replayLine) replayLine(ctx context.Context, t string) error {
var err error
x.ts, t, err = oneTime(t)
if err != nil {
return err
}
spanIDString, _, t := oneWordTerminal(t, " ")
if spanIDString == "" {
return errors.Errorf("missing idString")
}
spanID := xoptrace.NewHexBytes8FromString(spanIDString)
spanData, ok := x.spans[spanID]
if !ok {
return errors.Errorf("missing span %s", spanIDString)
}
var lineLink xoptrace.Trace
var lineModel xopbase.ModelArg
var lineType lineType
message, t := oneString(t)
if t != "" {
switch t[0] {
case ' ':
t = t[1:]
case ':':
switch message {
case "TEMPLATE":
lineType = lineTypeTemplate
message, t = oneStringAndSpace(t[1:])
case "MODEL":
lineType = lineTypeModel
message, t = oneStringAndSpace(t[1:])
var sep byte
lineModel, sep, t, err = readAttributeAny(t)
if err != nil {
return err
}
switch sep {
case ' ', '\000':
// okay
default:
return errors.Errorf("invalid data after model, sep is '%s'", string(sep))
}
case "LINK":
lineType = lineTypeLink
message, t = oneStringAndSpace(t[1:])
var linkString string
linkString, t = oneStringAndSpace(t)
var ok bool
lineLink, ok = xoptrace.TraceFromString(linkString)
if !ok {
return errors.Errorf("invalid link line")
}
default:
return errors.Errorf("invalid line, invalid prefix (%s)", message)
}
default:
return errors.Errorf("invalid line, message is word (%s) that isn't space-terminated (%c)", message, t[0])
}
}
for {
if t == "" {
break
}
var key string
var sep byte
key, sep, t = oneWord(t, "=:")
switch sep {
case ':':
if key != "STACK" {
return errors.Errorf("invalid stack indicator (key=%s, remaining=%s)", key, t)
}
if len(t) == 0 {
return errors.Errorf("invalid stack: empty")
}
if t[0] != ' ' {
return errors.Errorf("invalid stack: missing leading space")
}
t = t[1:]
for {
var fileAndNumber string
fileAndNumber, sep, t = oneWordTerminal(t, " ")
i := strings.LastIndexByte(fileAndNumber, ':')
if i == -1 {
return errors.Errorf("invalid stack frame (%s), no colon", fileAndNumber)
}
file := fileAndNumber[:i]
lineNum := fileAndNumber[i+1:]
if lineNum == "" {
return errors.Errorf("invalid stack frame, line #")
}
num, err := strconv.ParseInt(lineNum, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid stack frame, line num")
}
x.stack = append(x.stack, runtime.Frame{
File: file,
Line: int(num),
})
if sep == '\000' {
break
}
}
break
case '=':
key = unquote(key)
if len(t) == 0 {
return errors.Errorf("empty value")
}
var value string
var sep byte
switch t[0] {
case '(': // )
var ma xopbase.ModelArg
ma, sep, t, err = readAttributeAny(t)
if err != nil {
return errors.Wrap(err, "parse any attribute")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Any(xopat.K(key), ma) })
if sep == '\000' {
break
}
continue
case '"':
value, t = oneString(t)
if len(t) == 0 {
// valid for a terminal string value
x.attributes = append(x.attributes, func(line xopbase.Line) { line.String(xopat.K(key), value, xopbase.StringDataType) })
break
}
sep, t = t[0], t[1:]
default:
value, sep, t = oneWordTerminal(t, " (/") // )
}
switch sep {
case '(':
i := strings.IndexByte(t, ')')
if i == -1 {
return errors.Errorf("invalid type specifier")
}
if len(t) < i+1 {
// (
return errors.Errorf("no data after ): %s", t)
}
typ := t[:i]
t = t[i+1:]
if t != "" && t[0] == ' ' {
t = t[1:]
}
switch typ {
case "bool":
b, err := strconv.ParseBool(value)
if err != nil {
return errors.Wrap(err, "invalid bool")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Bool(xopat.K(key), b) })
case "dur":
dur, err := time.ParseDuration(value)
if err != nil {
return errors.Wrap(err, "invalid duration")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Duration(xopat.K(key), dur) })
case "f32":
f, err := strconv.ParseFloat(value, 32)
if err != nil {
return errors.Wrap(err, "invalid float")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Float64(xopat.K(key), f, xopbase.Float32DataType) })
case "f64":
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return errors.Wrap(err, "invalid float")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Float64(xopat.K(key), f, xopbase.Float64DataType) })
case "string":
x.attributes = append(x.attributes, func(line xopbase.Line) { line.String(xopat.K(key), value, xopbase.StringDataType) })
case "stringer":
x.attributes = append(x.attributes, func(line xopbase.Line) { line.String(xopat.K(key), value, xopbase.StringerDataType) })
case "error":
x.attributes = append(x.attributes, func(line xopbase.Line) { line.String(xopat.K(key), value, xopbase.ErrorDataType) })
case "i8", "i16", "i32", "i64", "int":
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid int")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Int64(xopat.K(key), i, xopbase.StringToDataType[typ]) })
case "u", "u8", "u16", "u32", "u64", "uintptr":
i, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid uint")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Uint64(xopat.K(key), i, xopbase.StringToDataType[typ]) })
case "time":
ts, err := time.Parse(time.RFC3339Nano, value)
if err != nil {
return errors.Wrap(err, "invalid time")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Time(xopat.K(key), ts) })
default:
return errors.Errorf("invalid type: %s", typ)
}
case ' ', '\000':
if value == "" {
return errors.Errorf("invalid value")
}
switch value[0] {
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid int")
}
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Int64(xopat.K(key), i, xopbase.IntDataType) })
default:
switch value {
case "t":
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Bool(xopat.K(key), true) })
case "f":
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Bool(xopat.K(key), false) })
default:
x.attributes = append(x.attributes, func(line xopbase.Line) { line.String(xopat.K(key), value, xopbase.StringDataType) })
}
}
if sep == '\000' {
break
}
continue
case '/':
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid enum int")
}
var s string
s, sep, t = oneWordTerminal(t, " ")
m := xopat.Make{
Key: key,
}
ea, err := spanData.request.attributeRegistry.ConstructEnumAttribute(m, xopat.AttributeTypeEnum)
if err != nil {
return errors.Wrap(err, "build enum attribute")
}
enum := ea.Add64(i, s)
x.attributes = append(x.attributes, func(line xopbase.Line) { line.Enum(&ea.EnumAttribute, enum) })
if sep == '\000' {
break
}
default:
// error
}
default:
return errors.Errorf("invalid input (%s %c %s)", key, sep, t)
}
}
line := spanData.span.NoPrefill().Line(x.level, x.ts, x.stack)
for _, af := range x.attributes {
af(line)
}
switch lineType {
case lineTypeLine:
line.Msg(message)
case lineTypeTemplate:
line.Template(message)
case lineTypeModel:
line.Model(message, lineModel)
case lineTypeLink:
line.Link(message, lineLink)
default:
return errors.Errorf("invalid line type %d", lineType)
}
return nil
}
var shortCodeRE = regexp.MustCompile(`^T\d+\.\d+(\.\S+)?`)
// Example:
//
// xop Span 2023-06-03T12:22:47.699766-07:00 Start c253fd02cd66f874 5f23a4838a2c7205 "a fork one span" T1.1.A
// xop Span 2023-09-19T21:08:15.545908-07:00 v1 ed738448be5cde53 http.route="/some/thing"
func (x replayData) replaySpan(ctx context.Context, t string) error {
var err error
var ts time.Time
ts, t, err = oneTime(t)
if err != nil {
return err
}
var n string
n, _, t = oneWord(t, " ")
if n == "" {
return errors.Errorf("invalid span start")
}
var spanIDString string
var sep byte
spanIDString, sep, t = oneWordTerminal(t, " ")
if spanIDString == "" {
return errors.Errorf("invalid span spanID (empty)")
}
spanID := xoptrace.NewHexBytes8FromString(spanIDString)
if n == "Start" {
var parentIDString string
parentIDString, _, t = oneWord(t, " ")
if parentIDString == "" {
return errors.Errorf("invalid span parentID")
}
parentID := xoptrace.NewHexBytes8FromString(parentIDString)
parentSpan, ok := x.spans[parentID]
if !ok {
return errors.Errorf("%s span %s is missing parent %s", n, spanIDString, parentIDString)
}
bundle := parentSpan.request.bundle.Copy()
bundle.Parent.Set(bundle.Trace)
bundle.Parent.SpanID().Set(parentID)
bundle.Trace.SpanID().Set(spanID)
var name string
name, t = oneString(t)
if len(t) > 0 && t[0] == ' ' {
t = t[1:]
}
var shortCode string
shortCode, _, t = oneWordTerminal(t, " ")
if shortCode == "" {
return errors.Errorf("invalid span sequence code")
}
m := shortCodeRE.FindStringSubmatch(shortCode)
if m == nil {
return errors.Errorf("invalid span sequence code")
}
var spanSequenceCode string
if len(m) > 1 {
spanSequenceCode = m[1]
}
span := parentSpan.span.Span(ctx, ts, bundle, name, spanSequenceCode)
x.spans[spanID] = &replaySpan{
replayData: x,
span: span,
request: parentSpan.request,
}
return nil
}
if n[0] != 'v' {
return errors.Errorf("invalid span numbering")
}
v, err := strconv.ParseUint(n[1:], 10, 64)
if err != nil {
return errors.Wrapf(err, "invalid span numbering")
}
spanData, ok := x.spans[spanID]
if !ok {
return errors.Errorf("span id %s not found", spanIDString)
}
spanData.version = int(v)
err = spanData.collectMetadata(sep, t)
if err != nil {
return err
}
spanData.span.Done(ts, false)
return nil
}
// xop Def 2023-09-10T09:15:27.76661-07:00 {"type":"defineKey","key":"http.route","desc":"HTTP handler route used to handle the request. If there are path parameters in the route their generic names should be used, eg \u0027/invoice/{number}\u0027 or \u0027/invoice/:number\u0027 depending on the router used","ns":"xop 0.0.0","indexed":true,"prom":10,"vtype":"String"}
func (x replayData) replayDef(ctx context.Context, t string) error {
// skip timestamp
_, _, t = oneWord(t, " ")
if t == "" {
return errors.Errorf("unexpected missing key definition")
}
if t[0] != '{' { // }
return errors.Errorf("unexpected invalid key definition")
}
err := x.attributeDefinitions.Decode(t)
if err != nil {
return err
}
return nil
}
// so far: xop Request
// this func: timestamp "Start1" or "vNNN"
func (x replayData) replayRequest(ctx context.Context, t string) error {
ts, t, err := oneTime(t)
if err != nil {
return err
}
var n string
n, _, t = oneWord(t, " ")
switch n {
case "":
return errors.Errorf("invalid request")
case "Start1":
return replayRequest{
replaySpan: replaySpan{
replayData: x,
},
ts: ts,
}.replayRequestStart(ctx, t)
default:
if !strings.HasPrefix(n, "v") {
return errors.Errorf("invalid request with %s", t)
}
v, err := strconv.ParseInt(n[1:], 10, 64)
if err != nil {
return errors.Wrap(err, "invalid request, invalid version number")
}
var requestIDString string
var sep byte
requestIDString, sep, t = oneWordTerminal(t, " ")
requestID := xoptrace.NewHexBytes8FromString(requestIDString)
y, ok := x.requests[requestID]
if !ok {
return errors.Errorf("update to request %s that doesn't exist", requestIDString)
}
y.version = v
err = y.collectMetadata(sep, t)
if err != nil {
return err
}
y.span.Done(ts, false)
}
return nil
}
// so far: xop Request timestamp Start1
// this func: trace-headder request-name source+version namespace+version
// xop Request 2023-06-02T22:35:26.81344-07:00 Start1 00-d456604ffc88ac5f4f971afbfce39cda-8fd4b01b0c7684a5-01 TestReplayConsole/one-span xopconsole.test-0.0.0 xopconsole.test-0.0.0
func (x replayRequest) replayRequestStart(ctx context.Context, t string) error {
th, _, t := oneWord(t, " ")
if th == "" {
return errors.Errorf("missing trace header")
}
var ok bool
x.trace, ok = xoptrace.TraceFromString(th)
if !ok {
return errors.Errorf("invalid trace header")
}
x.name, t = oneStringAndSpace(t)
if x.name == "" {
return errors.Errorf("missing request name")
}
x.sourceAndVersion, t = oneStringAndSpace(t)
if x.sourceAndVersion == "" {
return errors.Errorf("missing source+version, trace is %s/%s, name is %s, remaining is %s", th, x.trace, x.name, t)
}
x.namespaceAndVersion, t = oneStringAndSpace(t)
if x.namespaceAndVersion == "" {
return errors.Errorf("missing namespace+version, remaining is %s", t)
}
x.bundle = xoptrace.Bundle{
Trace: x.trace,
Parent: xoptrace.NewTrace(),
}
for t != "" {
var sep byte
var word string
word, sep, t = oneWordMaybeQuoted(t, " :=")
switch sep {
case '\000':
if word != "" {
return errors.Errorf("parse error, got '%s'", word)
}
break
case ':':
var value string
value, sep, t = oneStringAndSep(t)
switch sep {
case '\000', ' ':
// okay
default:
return errors.Errorf("unexpected value in request start: %s: '%s'", word, value)
}
switch word {
case "parent":
if len(value) == 16 {
x.bundle.Parent.TraceID().Set(x.trace.GetTraceID())
x.bundle.Parent.SpanID().SetString(value)
} else {
if !x.bundle.Parent.SetString(value) {
return errors.Errorf("invalid parent in request start: '%s'", value)
}
}
case "state":
x.bundle.State.SetString(value)
case "baggage":
x.bundle.Baggage.SetString(value)
default:
return errors.Errorf("unexpected keyword in request start: '%s'", word)
}
default:
return errors.Errorf("unexpected stuff in request start: '%s'", word)
}
}
ns, nsVers := xopversion.SplitVersion(x.namespaceAndVersion)
so, soVers := xopversion.SplitVersion(x.sourceAndVersion)
sourceInfo := xopbase.SourceInfo{
Source: so,
SourceVersion: soVers,
Namespace: ns,
NamespaceVersion: nsVers,
}
request := x.dest.Request(ctx, x.ts, x.bundle, x.name, sourceInfo)
x.baseRequest = request
x.span = request
x.request = &x
x.attributeRegistry = xopat.NewRegistry(false)
x.requestAttributes = x.attributeDefinitions.NewRequestAttributeDefinitions(x.bundle.Trace.SpanID().String())
x.spans[x.bundle.Trace.GetSpanID()] = &x.replaySpan
x.requests[x.bundle.Trace.GetSpanID()] = &x
return nil
}
func oneStringAndSep(t string) (string, byte, string) {
s, t := oneString(t)
if t == "" {
return s, '\000', t
}
return s, t[0], t[1:]
}
// oneStringAndSpace grabs a word/quoted string and if
// a space follows, it eats that space too
func oneStringAndSpace(t string) (string, string) {
a, b := oneString(t)
if a == "" {
return a, b
}
if len(b) > 0 && b[0] == ' ' {
return a, b[1:]
}
return a, b
}
func unquote(t string) string {
if t == "" {
return ""
}
if t[0] == '"' {
u, err := strconv.Unquote(t)
if err == nil {
return u
}
}
return t
}
// oneString reads a possibly-quoted string. A quoted
// string ends with a quote. An un-quoted string ends
// with something that breaks the simple-word pattern.
func oneString(t string) (string, string) {
if len(t) == 0 {
return "", ""
}
if t[0] == '"' {
for i := 1; i < len(t); i++ {
switch t[i] {
case '\\':
if i < len(t) {
i++
}
case '"':
one, err := strconv.Unquote(t[0 : i+1])
if err != nil {
return "", t
}
return one, t[i+1:]
}
}
}
one := xoputil.UnquotedConsoleStringRE.FindString(t)
if one != "" {
return one, t[len(one):]
}
return "", t
}
func oneTime(t string) (time.Time, string, error) {
w, _, t := oneWord(t, " ")
ts, err := time.Parse(time.RFC3339, w)
return ts, t, err
}
func oneWordMaybeQuoted(t string, boundary string) (found string, sep byte, newT string) {
if t == "" {
return "", '\000', ""
}
if t[0] == '"' {
for i := 1; i < len(t); i++ {
switch t[i] {
case '\\':
if i < len(t) {
i++
}
case '"':
one, err := strconv.Unquote(t[0 : i+1])
if err != nil {
return "", '\000', t
}
t = t[i+1:]
if t == "" {
return one, '\000', ""
}
return one, t[0], t[1:]
}
}
}
return oneWord(t, boundary)
}
// oneWordTerminal is low-level and simply looks for the provided
// boundary character(s). It returns t if the boundary character is missing. Use
// this when the word may be the last thing on a line.
func oneWordTerminal(t string, boundary string) (found string, sep byte, newT string) {
i := strings.IndexAny(t, boundary)
switch i {
case -1:
return t, '\000', ""
case 0:
return "", t[0], t[1:]
default:
return t[:i], t[i], t[i+1:]
}
}
// oneWord is low-level and simply looks for the provided
// boundary character(s). It returns an empty string if the boundary character
// is missing. Use this when the word is never the last thing on a line.
func oneWord(t string, boundary string) (found string, sep byte, newT string) {
i := strings.IndexAny(t, boundary)
switch i {
case -1:
return "", '\000', t
case 0:
return "", t[0], t[1:]
default:
return t[:i], t[i], t[i+1:]
}
}
func readAttributeAny(t string) (xopbase.ModelArg, byte, string, error) {
var ma xopbase.ModelArg
if t == "" {
return ma, ' ', "", errors.Errorf("expected non-empty value")
}
if t[0] != '(' /*)*/ {
return ma, ' ', "", errors.Errorf("expected open-paren")
}
t = t[1:]
var sep byte
sizeString, sep, t := oneWord(t /*(*/, ")")
size, err := strconv.ParseUint(sizeString, 10, 64)
if err != nil {
return ma, ' ', "", errors.Wrap(err, "parse size")
}
if len(t) <= int(size) {
return ma, ' ', "", errors.Errorf("invalid model size, not enough left")
}
ma.Encoded = []byte(t[:size])
t = t[size:]
encodingString, sep, t := oneWord(t, "/")
if en, ok := xopproto.Encoding_value[encodingString]; ok {
ma.Encoding = xopproto.Encoding(en)
} else {
return ma, ' ', "", errors.Errorf("invalid encoding (%s) when decoding attribute", encodingString)
}
ma.ModelType, sep, t = oneWordMaybeQuoted(t, " ,")
return ma, sep, t, nil
}
func (x *replaySpan) collectMetadata(sep byte, t string) error {
var key string
for {
key, sep, t = oneWord(t, "=")
if sep == '\000' {
return nil
}
var err error
aDef := x.request.requestAttributes.Lookup(key)
if aDef == nil {
return errors.Errorf("missing definition for '%s'", key)
}
for {
t, sep, err = x.oneMetadataValue(sep, t, aDef)
if err != nil {
return errors.Wrapf(err, "for metadata key %s", key)
}
if sep == ',' {
continue
}
break
}
}
}
func (x *replaySpan) oneMetadataValue(sep byte, t string, aDef *replayutil.DecodeAttributeDefinition) (string, byte, error) {
switch aDef.AttributeType {
// MACRO ZZZAttribute
case xopproto.AttributeType_ZZZ:
registeredAttribute, err := x.request.attributeRegistry.ConstructZZZAttribute(aDef.Make, xopat.AttributeType(aDef.AttributeType))
if err != nil {
return "", '\000', err
}
// //////// {
// CONDITIONAL ONLY:Any
ra := registeredAttribute
var v xopbase.ModelArg
v, sep, t, err = readAttributeAny(t)
// CONDITIONAL SKIP:Any,Enum
ra := registeredAttribute
var value string
value, sep, t = oneStringAndSep(t)
// CONDITIONAL ONLY:Enum
ra := ®isteredAttribute.EnumAttribute
var value string
value, sep, t = oneWord(t, "/")
// END CONDITIONAL
// //////// }
// //////// {
// CONDITIONAL ONLY:Int,Int8,Int16,Int32,Int64
v, err := strconv.ParseInt(value, 10, 64)
// CONDITIONAL ONLY:Enum
if sep != '/' {
return "", '\000', errors.Errorf("invalid enum")
}
var v xoputil.DecodeEnum
v.I, err = strconv.ParseInt(value, 10, 64)
if err != nil {
return "", '\000', errors.Wrap(err, "parse int for enum")
}
v.S, sep, t = oneWordTerminal(t, " ,")
if v.S == "" {
return "", '\000', errors.Errorf("invalid enum")
}
// CONDITIONAL ONLY:Bool
v := value == "t"
// CONDITIONAL ONLY:String
v := value
// CONDITIONAL ONLY:Duration
v, err := time.ParseDuration(value)
// CONDITIONAL ONLY:Time
v, err := time.Parse(time.RFC3339Nano, value)
// CONDITIONAL ONLY:Float64
v, err := strconv.ParseFloat(value, 64)
// CONDITIONAL ONLY:Link
v, ok := xoptrace.TraceFromString(value)
if !ok {
return "", '\000', errors.Errorf("invalid trace")
}
// END CONDITIONAL
// //////// }
// CONDITIONAL ONLY:Int,Int8,Int16,Int32,Int64,Time,Duration,Float64,Any
if err != nil {
return "", '\000', errors.Wrap(err, "invalid ZZZ")
}
// END CONDITIONAL
// CONDITIONAL ONLY:Int,Int8,Int16,Int32,Duration
x.span.MetadataInt64(&ra.Int64Attribute, int64(v))
// ELSE CONDITIONAL
x.span.MetadataZZZ(ra, v)
// END CONDITIONAL
// preceding blank line required
default:
return "", '\000', errors.Errorf("unexpected attribute type (%s)", aDef.AttributeType)
}
return t, sep, nil
}
func Replay(ctx context.Context, inputStream io.Reader, dest xopbase.Logger) error {
scanner := bufio.NewScanner(inputStream)
x := replayData{
dest: dest,
spans: make(map[xoptrace.HexBytes8]*replaySpan),
requests: make(map[xoptrace.HexBytes8]*replayRequest),
attributeDefinitions: replayutil.NewGlobalAttributeDefinitions(),
}
for scanner.Scan() {
x.lineCount++
t := scanner.Text()
if !strings.HasPrefix(t, "xop ") {
continue
}
x.currentLine = t
t = t[len("xop "):]
kind, _, t := oneWord(t, " ")
var err error
switch kind {
case "Request":
err = x.replayRequest(ctx, t)
case "Span":
err = x.replaySpan(ctx, t)
case "Def":
err = x.replayDef(ctx, t)
//MACRO LogLevel
case "zZZ":
err = replayLine{
replayData: x,
level: xopnum.ZZZLevel,
}.replayLine(ctx, t)
// prior line must be blank
default:
err = errors.Errorf("invalid kind designator '%s'", kind)
}
if err != nil {
x.errors = append(x.errors, errors.Wrapf(err, "line %d: %s", x.lineCount, x.currentLine))
}
}
if len(x.errors) != 0 {
// TODO: use a multi-error
return x.errors[0]
}
return nil
}