forked from cockroachdb/cockroach
/
format.go
99 lines (87 loc) · 2.32 KB
/
format.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
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package tracing
import (
"bytes"
"fmt"
"sort"
"strings"
"time"
basictracer "github.com/opentracing/basictracer-go"
opentracing "github.com/opentracing/opentracing-go"
)
type traceLogData struct {
opentracing.LogRecord
depth int
}
type traceLogs []traceLogData
func (l traceLogs) Len() int {
return len(l)
}
func (l traceLogs) Less(i, j int) bool {
return l[i].Timestamp.Before(l[j].Timestamp)
}
func (l traceLogs) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// FormatRawSpans formats the given spans for human consumption, showing the
// relationship using nesting and times as both relative to the previous event
// and cumulative.
func FormatRawSpans(spans []basictracer.RawSpan) string {
m := make(map[uint64]*basictracer.RawSpan)
for i, sp := range spans {
m[sp.Context.SpanID] = &spans[i]
}
var depth func(uint64) int
depth = func(parentID uint64) int {
p := m[parentID]
if p == nil {
return 0
}
return depth(p.ParentSpanID) + 1
}
var logs traceLogs
var start time.Time
for _, sp := range spans {
if sp.ParentSpanID == 0 {
start = sp.Start
}
d := depth(sp.ParentSpanID)
for _, e := range sp.Logs {
logs = append(logs, traceLogData{LogRecord: e, depth: d})
}
}
sort.Sort(logs)
var buf bytes.Buffer
var last time.Time
if len(logs) > 0 {
last = logs[0].Timestamp
}
for _, entry := range logs {
fmt.Fprintf(&buf, "% 10.3fms % 10.3fms%s",
1000*entry.Timestamp.Sub(start).Seconds(),
1000*entry.Timestamp.Sub(last).Seconds(),
strings.Repeat(" ", entry.depth+1))
for i, f := range entry.Fields {
if i != 0 {
buf.WriteByte(' ')
}
fmt.Fprintf(&buf, "%s:%v", f.Key(), f.Value())
}
buf.WriteByte('\n')
}
return buf.String()
}