github.com/sagernet/quic-go@v0.43.1-beta.1/qlog/trace.go (about)

     1  package qlog
     2  
     3  import (
     4  	"runtime/debug"
     5  	"time"
     6  
     7  	"github.com/francoispqt/gojay"
     8  	"github.com/sagernet/quic-go/logging"
     9  )
    10  
    11  // Setting of this only works when quic-go is used as a library.
    12  // When building a binary from this repository, the version can be set using the following go build flag:
    13  // -ldflags="-X github.com/sagernet/quic-go/qlog.quicGoVersion=foobar"
    14  var quicGoVersion = "(devel)"
    15  
    16  func init() {
    17  	if quicGoVersion != "(devel)" { // variable set by ldflags
    18  		return
    19  	}
    20  	info, ok := debug.ReadBuildInfo()
    21  	if !ok { // no build info available. This happens when quic-go is not used as a library.
    22  		return
    23  	}
    24  	for _, d := range info.Deps {
    25  		if d.Path == "github.com/sagernet/quic-go" {
    26  			quicGoVersion = d.Version
    27  			if d.Replace != nil {
    28  				if len(d.Replace.Version) > 0 {
    29  					quicGoVersion = d.Version
    30  				} else {
    31  					quicGoVersion += " (replaced)"
    32  				}
    33  			}
    34  			break
    35  		}
    36  	}
    37  }
    38  
    39  type topLevel struct {
    40  	trace trace
    41  }
    42  
    43  func (topLevel) IsNil() bool { return false }
    44  func (l topLevel) MarshalJSONObject(enc *gojay.Encoder) {
    45  	enc.StringKey("qlog_format", "NDJSON")
    46  	enc.StringKey("qlog_version", "draft-02")
    47  	enc.StringKeyOmitEmpty("title", "quic-go qlog")
    48  	enc.ObjectKey("configuration", configuration{Version: quicGoVersion})
    49  	enc.ObjectKey("trace", l.trace)
    50  }
    51  
    52  type configuration struct {
    53  	Version string
    54  }
    55  
    56  func (c configuration) IsNil() bool { return false }
    57  func (c configuration) MarshalJSONObject(enc *gojay.Encoder) {
    58  	enc.StringKey("code_version", c.Version)
    59  }
    60  
    61  type vantagePoint struct {
    62  	Name string
    63  	Type string
    64  }
    65  
    66  func (p vantagePoint) IsNil() bool { return false }
    67  func (p vantagePoint) MarshalJSONObject(enc *gojay.Encoder) {
    68  	enc.StringKeyOmitEmpty("name", p.Name)
    69  	enc.StringKeyOmitEmpty("type", p.Type)
    70  }
    71  
    72  type commonFields struct {
    73  	ODCID         *logging.ConnectionID
    74  	GroupID       *logging.ConnectionID
    75  	ProtocolType  string
    76  	ReferenceTime time.Time
    77  }
    78  
    79  func (f commonFields) MarshalJSONObject(enc *gojay.Encoder) {
    80  	if f.ODCID != nil {
    81  		enc.StringKey("ODCID", f.ODCID.String())
    82  		enc.StringKey("group_id", f.ODCID.String())
    83  	}
    84  	enc.StringKeyOmitEmpty("protocol_type", f.ProtocolType)
    85  	enc.Float64Key("reference_time", float64(f.ReferenceTime.UnixNano())/1e6)
    86  	enc.StringKey("time_format", "relative")
    87  }
    88  
    89  func (f commonFields) IsNil() bool { return false }
    90  
    91  type trace struct {
    92  	VantagePoint vantagePoint
    93  	CommonFields commonFields
    94  }
    95  
    96  func (trace) IsNil() bool { return false }
    97  func (t trace) MarshalJSONObject(enc *gojay.Encoder) {
    98  	enc.ObjectKey("vantage_point", t.VantagePoint)
    99  	enc.ObjectKey("common_fields", t.CommonFields)
   100  }