Skip to content

Commit

Permalink
feat: qlog trace (#693)
Browse files Browse the repository at this point in the history
# Description

Support `YOMO_QLOG_TRACE=true` environment to enable `source/sfn` qlog
trace.

also, Close #688 , #687
  • Loading branch information
woorui authored Dec 22, 2023
1 parent 893a20a commit 60ee484
Show file tree
Hide file tree
Showing 17 changed files with 685 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ node_modules/
package-lock.json

*.log
*.qlog

# tls generated files
scripts/tls/
Expand Down
5 changes: 5 additions & 0 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func NewClient(appName, zipperAddr string, clientType ClientType, opts ...Client
for _, o := range opts {
o(option)
}

if qlogTraceEnabled() && option.quicConfig != nil {
option.quicConfig.Tracer = qlogTracer
}

clientID := id.New()

logger := option.logger
Expand Down
57 changes: 41 additions & 16 deletions core/client_options.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package core

import (
"context"
"crypto/tls"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/logging"
"github.com/quic-go/quic-go/qlog"
"github.com/yomorun/yomo/core/auth"
"github.com/yomorun/yomo/core/frame"
"github.com/yomorun/yomo/core/ylog"
Expand All @@ -28,27 +35,26 @@ type clientOptions struct {
tracerProvider trace.TracerProvider
}

func defaultClientOption() *clientOptions {
logger := ylog.Default()

defaultQuicConfig := &quic.Config{
Versions: []quic.VersionNumber{quic.Version1, quic.Version2},
MaxIdleTimeout: time.Second * 40,
KeepAlivePeriod: time.Second * 20,
MaxIncomingStreams: 1000,
MaxIncomingUniStreams: 1000,
HandshakeIdleTimeout: time.Second * 3,
InitialStreamReceiveWindow: 1024 * 1024 * 2,
InitialConnectionReceiveWindow: 1024 * 1024 * 2,
TokenStore: quic.NewLRUTokenStore(10, 5),
}
// DefaultClientQuicConfig be used when the `quicConfig` of client is nil.
var DefaultClientQuicConfig = &quic.Config{
Versions: []quic.VersionNumber{quic.Version1, quic.Version2},
MaxIdleTimeout: time.Second * 40,
KeepAlivePeriod: time.Second * 20,
MaxIncomingStreams: 1000,
MaxIncomingUniStreams: 1000,
HandshakeIdleTimeout: time.Second * 3,
InitialStreamReceiveWindow: 1024 * 1024 * 2,
InitialConnectionReceiveWindow: 1024 * 1024 * 2,
TokenStore: quic.NewLRUTokenStore(10, 5),
}

func defaultClientOption() *clientOptions {
opts := &clientOptions{
observeDataTags: make([]frame.Tag, 0),
quicConfig: defaultQuicConfig,
quicConfig: DefaultClientQuicConfig,
tlsConfig: pkgtls.MustCreateClientTLSConfig(),
credential: auth.NewCredential(""),
logger: logger,
logger: ylog.Default(),
}

return opts
Expand Down Expand Up @@ -104,3 +110,22 @@ func WithTracerProvider(tp trace.TracerProvider) ClientOption {
o.tracerProvider = tp
}
}

// qlog helps developers to debug quic protocol.
// See more: https://github.com/quic-go/quic-go?tab=readme-ov-file#quic-event-logging-using-qlog
func qlogTraceEnabled() bool {
return strings.ToLower(os.Getenv("YOMO_QLOG_TRACE")) == "true"
}

func qlogTracer(ctx context.Context, p logging.Perspective, connID quic.ConnectionID) *logging.ConnectionTracer {
role := "server"
if p == logging.PerspectiveClient {
role = "client"
}
filename := fmt.Sprintf("./log_%s_%s.qlog", connID, role)
f, err := os.Create(filename)
if err != nil {
log.Fatalf("qlog trace error: %s\n", err)
}
return qlog.NewConnectionTracer(f, p, connID)
}
4 changes: 2 additions & 2 deletions core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestFrameRoundTrip(t *testing.T) {

server := NewServer("zipper",
WithAuth("token", "auth-token"),
WithServerQuicConfig(DefalutQuicConfig),
WithServerQuicConfig(DefaultQuicConfig),
WithServerTLSConfig(nil),
WithServerLogger(discardingLogger),
WithConnMiddleware(ht.connMiddleware),
Expand All @@ -77,7 +77,7 @@ func TestFrameRoundTrip(t *testing.T) {
testaddr,
ClientTypeSource,
WithCredential("token:auth-token"),
WithClientQuicConfig(DefalutQuicConfig),
WithClientQuicConfig(DefaultClientQuicConfig),
WithClientTLSConfig(nil),
WithLogger(discardingLogger),
WithReConnect(),
Expand Down
6 changes: 3 additions & 3 deletions core/server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"golang.org/x/exp/slog"
)

// DefalutQuicConfig be used when `quicConfig` is nil.
var DefalutQuicConfig = &quic.Config{
// DefaultQuicConfig be used when `quicConfig` is nil.
var DefaultQuicConfig = &quic.Config{
Versions: []quic.VersionNumber{quic.Version1, quic.Version2},
MaxIdleTimeout: time.Second * 5,
KeepAlivePeriod: time.Second * 2,
Expand Down Expand Up @@ -42,7 +42,7 @@ func defaultServerOptions() *serverOptions {
logger := ylog.Default()

opts := &serverOptions{
quicConfig: DefalutQuicConfig,
quicConfig: DefaultQuicConfig,
tlsConfig: nil,
auths: map[string]auth.Authentication{},
logger: logger,
Expand Down
7 changes: 4 additions & 3 deletions example/1-pipeline/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require github.com/yomorun/yomo v0.0.0
require (
github.com/caarlos0/env/v6 v6.10.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
Expand All @@ -18,7 +19,7 @@ require (
github.com/matoous/go-nanoid/v2 v2.0.0 // indirect
github.com/onsi/ginkgo/v2 v2.13.1 // indirect
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
github.com/quic-go/quic-go v0.40.0 // indirect
github.com/quic-go/quic-go v0.40.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yomorun/y3 v1.0.5 // indirect
Expand All @@ -33,10 +34,10 @@ require (
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
Expand Down
Loading

1 comment on commit 60ee484

@vercel
Copy link

@vercel vercel bot commented on 60ee484 Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

yomo – ./

yomo.vercel.app
yomo-git-master-yomorun.vercel.app
yomo-yomorun.vercel.app
yomo.run
www.yomo.run

Please sign in to comment.