Skip to content

Commit

Permalink
missing files
Browse files Browse the repository at this point in the history
Signed-off-by: Sandor Szücs <[email protected]>
  • Loading branch information
szuecs committed Mar 20, 2024
1 parent 083bc5e commit c42035a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
52 changes: 52 additions & 0 deletions filters/diag/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package diag

import (
"time"

log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters"
)

func getDurationArg(a interface{}) (time.Duration, error) {
if s, ok := a.(string); ok {
return time.ParseDuration(s)
}
return 0, filters.ErrInvalidFilterParameters
}

type traceSpec struct{}

type trace struct {
d time.Duration
}

// NewTrace creates a filter specification for the trace() filter
func NewTrace() filters.Spec {
return &traceSpec{}
}

func (*traceSpec) Name() string {
return filters.TraceName
}

func (ts *traceSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) != 1 {
return nil, filters.ErrInvalidFilterParameters
}

d, err := getDurationArg(args[0])
if err != nil {
log.Warnf("d failed on creation of trace(): %v", err)
return nil, filters.ErrInvalidFilterParameters
}

return &trace{
d: d,
}, nil
}

func (tr *trace) Request(ctx filters.FilterContext) {
ctx.StateBag()[filters.TraceName] = tr.d
}

func (*trace) Response(filters.FilterContext) {}
59 changes: 59 additions & 0 deletions proxy/flightrecorder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package proxy_test

import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/filters/diag"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/proxy/proxytest"
xtrace "golang.org/x/exp/trace"
)

func TestFlightRecorder(t *testing.T) {
service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed)))
return
}

var buf bytes.Buffer
n, err := io.Copy(&buf, r.Body)
if err != nil {
t.Fatalf("Failed to copy data: %v", err)
}
if n < 100 {
t.Fatalf("Failed to write enough data: %d bytes", n)
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte(http.StatusText(http.StatusCreated)))

}))
defer service.Close()

backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer backend.Close()

flightRecorder := xtrace.NewFlightRecorder()
flightRecorder.Start()

spec := diag.NewTrace()
fr := make(filters.Registry)
fr.Register(spec)

doc := fmt.Sprintf(`r: * -> trace("20µs") -> "%s"`, backend.URL)
rr := eskip.MustParse(doc)

pr := proxytest.WithParams(fr, proxy.Params{
FlightRecorder: flightRecorder,
}, rr...)
_ = pr
pr.Client().Get(pr.URL)
}

0 comments on commit c42035a

Please sign in to comment.