forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
trace.go
101 lines (85 loc) · 2.51 KB
/
trace.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
package qlog
import (
"runtime/debug"
"time"
"github.com/quic-go/quic-go/logging"
"github.com/francoispqt/gojay"
)
// Setting of this only works when quic-go is used as a library.
// When building a binary from this repository, the version can be set using the following go build flag:
// -ldflags="-X github.com/quic-go/quic-go/qlog.quicGoVersion=foobar"
var quicGoVersion = "(devel)"
func init() {
if quicGoVersion != "(devel)" { // variable set by ldflags
return
}
info, ok := debug.ReadBuildInfo()
if !ok { // no build info available. This happens when quic-go is not used as a library.
return
}
for _, d := range info.Deps {
if d.Path == "github.com/quic-go/quic-go" {
quicGoVersion = d.Version
if d.Replace != nil {
if len(d.Replace.Version) > 0 {
quicGoVersion = d.Version
} else {
quicGoVersion += " (replaced)"
}
}
break
}
}
}
type topLevel struct {
trace trace
}
func (topLevel) IsNil() bool { return false }
func (l topLevel) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("qlog_format", "NDJSON")
enc.StringKey("qlog_version", "draft-02")
enc.StringKeyOmitEmpty("title", "quic-go qlog")
enc.ObjectKey("configuration", configuration{Version: quicGoVersion})
enc.ObjectKey("trace", l.trace)
}
type configuration struct {
Version string
}
func (c configuration) IsNil() bool { return false }
func (c configuration) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("code_version", c.Version)
}
type vantagePoint struct {
Name string
Type string
}
func (p vantagePoint) IsNil() bool { return false }
func (p vantagePoint) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKeyOmitEmpty("name", p.Name)
enc.StringKeyOmitEmpty("type", p.Type)
}
type commonFields struct {
ODCID *logging.ConnectionID
GroupID *logging.ConnectionID
ProtocolType string
ReferenceTime time.Time
}
func (f commonFields) MarshalJSONObject(enc *gojay.Encoder) {
if f.ODCID != nil {
enc.StringKey("ODCID", f.ODCID.String())
enc.StringKey("group_id", f.ODCID.String())
}
enc.StringKeyOmitEmpty("protocol_type", f.ProtocolType)
enc.Float64Key("reference_time", float64(f.ReferenceTime.UnixNano())/1e6)
enc.StringKey("time_format", "relative")
}
func (f commonFields) IsNil() bool { return false }
type trace struct {
VantagePoint vantagePoint
CommonFields commonFields
}
func (trace) IsNil() bool { return false }
func (t trace) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("vantage_point", t.VantagePoint)
enc.ObjectKey("common_fields", t.CommonFields)
}