...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package exporter
16
17 import (
18 "encoding/hex"
19 "fmt"
20 "regexp"
21 "time"
22
23 "go.opencensus.io/stats/view"
24 "go.opencensus.io/trace"
25 )
26
27
28 const indent = " "
29
30
31 var reZero = regexp.MustCompile(`^0+$`)
32
33
34
35
36
37
38
39
40 type PrintExporter struct{}
41
42
43 func (e *PrintExporter) ExportView(vd *view.Data) {
44 for _, row := range vd.Rows {
45 fmt.Printf("%v %-45s", vd.End.Format("15:04:05"), vd.View.Name)
46
47 switch v := row.Data.(type) {
48 case *view.DistributionData:
49 fmt.Printf("distribution: min=%.1f max=%.1f mean=%.1f", v.Min, v.Max, v.Mean)
50 case *view.CountData:
51 fmt.Printf("count: value=%v", v.Value)
52 case *view.SumData:
53 fmt.Printf("sum: value=%v", v.Value)
54 case *view.LastValueData:
55 fmt.Printf("last: value=%v", v.Value)
56 }
57 fmt.Println()
58
59 for _, tag := range row.Tags {
60 fmt.Printf("%v- %v=%v\n", indent, tag.Key.Name(), tag.Value)
61 }
62 }
63 }
64
65
66 func (e *PrintExporter) ExportSpan(vd *trace.SpanData) {
67 var (
68 traceID = hex.EncodeToString(vd.SpanContext.TraceID[:])
69 spanID = hex.EncodeToString(vd.SpanContext.SpanID[:])
70 parentSpanID = hex.EncodeToString(vd.ParentSpanID[:])
71 )
72 fmt.Println()
73 fmt.Println("#----------------------------------------------")
74 fmt.Println()
75 fmt.Println("TraceID: ", traceID)
76 fmt.Println("SpanID: ", spanID)
77 if !reZero.MatchString(parentSpanID) {
78 fmt.Println("ParentSpanID:", parentSpanID)
79 }
80
81 fmt.Println()
82 fmt.Printf("Span: %v\n", vd.Name)
83 fmt.Printf("Status: %v [%v]\n", vd.Status.Message, vd.Status.Code)
84 fmt.Printf("Elapsed: %v\n", vd.EndTime.Sub(vd.StartTime).Round(time.Millisecond))
85
86 if len(vd.Annotations) > 0 {
87 fmt.Println()
88 fmt.Println("Annotations:")
89 for _, item := range vd.Annotations {
90 fmt.Print(indent, item.Message)
91 for k, v := range item.Attributes {
92 fmt.Printf(" %v=%v", k, v)
93 }
94 fmt.Println()
95 }
96 }
97
98 if len(vd.Attributes) > 0 {
99 fmt.Println()
100 fmt.Println("Attributes:")
101 for k, v := range vd.Attributes {
102 fmt.Printf("%v- %v=%v\n", indent, k, v)
103 }
104 }
105 }
106
View as plain text