Skip to content

Commit

Permalink
Use a basic percentage base sampling:
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jacobweinstock committed Nov 19, 2024
1 parent 809353d commit c973dc0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 61 deletions.
15 changes: 0 additions & 15 deletions cmd/smee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"flag"
"fmt"
"log/slog"
"net"
"net/netip"
"net/url"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -283,7 +269,6 @@ func main() {
}
return cfg.iso.magicString
}(),
SampleLogger: logger,
}
isoHandler, err := ih.Reverse()
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ 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=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
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=
Expand Down Expand Up @@ -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=
Expand Down
30 changes: 20 additions & 10 deletions internal/iso/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package iso
import (
"bytes"
"context"
"crypto/rand"
"errors"
"fmt"
"io"
"log/slog"
"math/big"
"net"
"net/http"
"net/http/httputil"
"net/url"
"path"
"path/filepath"
"strings"
"sync/atomic"

apierrors "k8s.io/apimachinery/pkg/api/errors"

Expand All @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand Down
19 changes: 1 addition & 18 deletions internal/iso/iso_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand Down

0 comments on commit c973dc0

Please sign in to comment.