github.com/quic-go/quic-go@v0.44.0/qlog/trace.go (about)

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