From c973dc05187e42c8791e7e1b8c234e1ccfe5f5c3 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Tue, 19 Nov 2024 14:41:04 -0700 Subject: [PATCH] Use a basic percentage base sampling: The sampling import isn't needed for now. The most simple case of using a random number below a percentage will suffice for now. Signed-off-by: Jacob Weinstock --- cmd/smee/main.go | 15 --------------- go.mod | 6 ------ go.sum | 12 ------------ internal/iso/iso.go | 30 ++++++++++++++++++++---------- internal/iso/iso_test.go | 19 +------------------ 5 files changed, 21 insertions(+), 61 deletions(-) diff --git a/cmd/smee/main.go b/cmd/smee/main.go index b0c428e7..8cefba0f 100644 --- a/cmd/smee/main.go +++ b/cmd/smee/main.go @@ -5,7 +5,6 @@ import ( "errors" "flag" "fmt" - "log/slog" "net" "net/netip" "net/url" @@ -20,8 +19,6 @@ import ( "github.com/go-logr/zapr" "github.com/insomniacslk/dhcp/dhcpv4" "github.com/insomniacslk/dhcp/dhcpv4/server4" - slogmulti "github.com/samber/slog-multi" - slogsampling "github.com/samber/slog-sampling" "github.com/tinkerbell/ipxedust" "github.com/tinkerbell/ipxedust/ihttp" "github.com/tinkerbell/smee/internal/dhcp/handler" @@ -258,17 +255,6 @@ func main() { if err != nil { panic(fmt.Errorf("failed to create backend: %w", err)) } - // Will print 10% of entries. - option := slogsampling.UniformSamplingOption{ - // The sample rate for sampling traces in the range [0.0, 1.0]. - Rate: 0.002, - } - - logger := slog.New( - slogmulti. - Pipe(option.NewMiddleware()). - Handler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})), - ) ih := iso.Handler{ Logger: log, Backend: br, @@ -283,7 +269,6 @@ func main() { } return cfg.iso.magicString }(), - SampleLogger: logger, } isoHandler, err := ih.Reverse() if err != nil { diff --git a/go.mod b/go.mod index e2b9c0c7..1919774b 100644 --- a/go.mod +++ b/go.mod @@ -16,8 +16,6 @@ require ( github.com/packethost/xff v0.0.0-20190305172552-d3e9190c41b3 github.com/peterbourgon/ff/v3 v3.4.0 github.com/prometheus/client_golang v1.20.5 - github.com/samber/slog-multi v1.2.4 - github.com/samber/slog-sampling v1.5.2 github.com/tinkerbell/ipxedust v0.0.0-20241108174245-aa0c0298057d github.com/tinkerbell/tink v0.12.1 github.com/vishvananda/netlink v1.3.0 @@ -39,10 +37,8 @@ require ( require ( dario.cat/mergo v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/bluele/gcache v0.0.2 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/cornelk/hashmap v1.0.8 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/emicklei/go-restful/v3 v3.12.1 // indirect github.com/evanphx/json-patch/v5 v5.9.0 // indirect @@ -84,8 +80,6 @@ require ( github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/rs/zerolog v1.33.0 // indirect - github.com/samber/lo v1.47.0 // indirect - github.com/samber/slog-common v0.17.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/u-root/uio v0.0.0-20230305220412-3e8cd9d6bf63 // indirect github.com/vishvananda/netns v0.0.4 // indirect diff --git a/go.sum b/go.sum index a6193065..14f282aa 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,6 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s= dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/bluele/gcache v0.0.2 h1:WcbfdXICg7G/DGBh1PFfcirkWOQV+v077yF1pSy3DGw= -github.com/bluele/gcache v0.0.2/go.mod h1:m15KV+ECjptwSPxKhOhQoAFQVtUFjTVkc3H8o0t/fp0= github.com/ccoveille/go-safecast v1.2.0 h1:H4X7aosepsU1Mfk+098CTdKpsDH0cfYJ2RmwXFjgvfc= github.com/ccoveille/go-safecast v1.2.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= @@ -11,8 +9,6 @@ github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/cornelk/hashmap v1.0.8 h1:nv0AWgw02n+iDcawr5It4CjQIAcdMMKRrs10HOJYlrc= -github.com/cornelk/hashmap v1.0.8/go.mod h1:RfZb7JO3RviW/rT6emczVuC/oxpdz4UsSB2LJSclR1k= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -154,14 +150,6 @@ github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWN github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc= -github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= -github.com/samber/slog-common v0.17.1 h1:jTqqLBgoJshpoxlPSGiypyOanjH6tY+i9bwyYmIbjhI= -github.com/samber/slog-common v0.17.1/go.mod h1:mZSJhinB4aqHziR0SKPqpVZjJ0JO35JfH+dDIWqaCBk= -github.com/samber/slog-multi v1.2.4 h1:k9x3JAWKJFPKffx+oXZ8TasaNuorIW4tG+TXxkt6Ry4= -github.com/samber/slog-multi v1.2.4/go.mod h1:ACuZ5B6heK57TfMVkVknN2UZHoFfjCwRxR0Q2OXKHlo= -github.com/samber/slog-sampling v1.5.2 h1:HaQmRGmLkVsXlVHEgIzgvxc6JYkkppL/7GN1B1g5LQM= -github.com/samber/slog-sampling v1.5.2/go.mod h1:n2PVbLAFRx8Im0KFt9srRrO/8wi9FAiTNBV7FmhbAHY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/internal/iso/iso.go b/internal/iso/iso.go index b695165e..58ece978 100644 --- a/internal/iso/iso.go +++ b/internal/iso/iso.go @@ -3,10 +3,11 @@ package iso import ( "bytes" "context" + "crypto/rand" "errors" "fmt" "io" - "log/slog" + "math/big" "net" "net/http" "net/http/httputil" @@ -14,7 +15,6 @@ import ( "path" "path/filepath" "strings" - "sync/atomic" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -23,7 +23,7 @@ import ( ) const ( - defaultConsoles = "console=ttyS0 console=ttyAMA0 console=tty1 console=tty0, console=ttyS1,115200" + defaultConsoles = "console=ttyS0 console=ttyAMA0 console=tty0, console=ttyS1 console=tty1" ) type Handler struct { @@ -43,16 +43,20 @@ type Handler struct { // in the source iso before patching. The field can be set // during build time by setting this field. // Ref: https://github.com/tinkerbell/hook/blob/main/linuxkit-templates/hook.template.yaml - MagicString string - SampleLogger *slog.Logger + MagicString string } -var total atomic.Int64 +func randomPercentage(precision int64) (float64, error) { + random, err := rand.Int(rand.Reader, big.NewInt(precision)) + if err != nil { + return 0, err + } + + return float64(random.Int64()) / float64(precision), nil +} func (h *Handler) RoundTrip(req *http.Request) (*http.Response, error) { - log := h.Logger.WithValues("method", req.Method, "urlPath", req.URL.Path, "xff", req.Header.Get("X-Forwarded-For"), "remoteAddr", req.RemoteAddr, "total", total.Load()) - splog := h.SampleLogger.With("method", req.Method, "urlPath", req.URL.Path, "xff", req.Header.Get("X-Forwarded-For"), "remoteAddr", req.RemoteAddr, "total", total.Load()) - total.Add(1) + log := h.Logger.WithValues("method", req.Method, "urlPath", req.URL.Path, "remoteAddr", req.RemoteAddr) log.V(1).Info("starting the ISO patching HTTP handler") if req.Method != http.MethodHead && req.Method != http.MethodGet { return &http.Response{ @@ -140,7 +144,13 @@ func (h *Handler) RoundTrip(req *http.Request) (*http.Response, error) { return resp, nil } - splog.Info("HTTP GET method received with a 206 status code") + // 0.002% of the time we log a 206 request message. + // In testing, it was observed that about 3000 HTTP 206 requests are made per ISO mount. + // 0.002% gives us about 5, +/- 3, log messages per ISO mount. + // We're optimizing for showing "enough" log messages so that progress can be observed. + if p, _ := randomPercentage(10000); p < 0.002 { + log.Info("HTTP GET method received with a 206 status code") + } // this roundtripper func should only return error when there is no response from the server. // for any other case we log the error and return a 500 response diff --git a/internal/iso/iso_test.go b/internal/iso/iso_test.go index f2325f37..63f878d4 100644 --- a/internal/iso/iso_test.go +++ b/internal/iso/iso_test.go @@ -1,14 +1,9 @@ package iso import ( - "log/slog" "net/http" "net/url" - "os" "testing" - - slogmulti "github.com/samber/slog-multi" - slogsampling "github.com/samber/slog-sampling" ) func TestReqPathInvalid(t *testing.T) { @@ -23,20 +18,8 @@ func TestReqPathInvalid(t *testing.T) { for name, tt := range tests { u, _ := url.Parse(tt.isoURL) t.Run(name, func(t *testing.T) { - // Will print 10% of entries. - option := slogsampling.UniformSamplingOption{ - // The sample rate for sampling traces in the range [0.0, 1.0]. - Rate: 0.002, - } - - logger := slog.New( - slogmulti. - Pipe(option.NewMiddleware()). - Handler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})), - ) h := &Handler{ - parsedURL: u, - SampleLogger: logger, + parsedURL: u, } req := http.Request{ Method: http.MethodGet,