-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sandor Szücs <[email protected]>
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |