|
15 | 15 | package exporter // import "go.opencensus.io/examples/exporter"
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "log" |
| 18 | + "encoding/hex" |
| 19 | + "fmt" |
| 20 | + "regexp" |
| 21 | + "time" |
19 | 22 |
|
20 | 23 | "go.opencensus.io/stats/view"
|
21 | 24 | "go.opencensus.io/trace"
|
22 | 25 | )
|
23 | 26 |
|
| 27 | +// indent these many spaces |
| 28 | +const indent = " " |
| 29 | + |
| 30 | +// reZero provides a simple way to detect an empty ID |
| 31 | +var reZero = regexp.MustCompile(`^0+$`) |
| 32 | + |
24 | 33 | // PrintExporter is a stats and trace exporter that logs
|
25 | 34 | // the exported data to the console.
|
| 35 | +// |
| 36 | +// The intent is help new users familiarize themselves with the |
| 37 | +// capabilities of opencensus. |
| 38 | +// |
| 39 | +// This should NOT be used for production workloads. |
26 | 40 | type PrintExporter struct{}
|
27 | 41 |
|
28 | 42 | // ExportView logs the view data.
|
29 | 43 | func (e *PrintExporter) ExportView(vd *view.Data) {
|
30 |
| - log.Println(vd) |
| 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 | + } |
31 | 63 | }
|
32 | 64 |
|
33 | 65 | // ExportSpan logs the trace span.
|
34 | 66 | func (e *PrintExporter) ExportSpan(vd *trace.SpanData) {
|
35 |
| - log.Println(vd) |
| 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 | + } |
36 | 105 | }
|
0 commit comments