-
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.
feature: allow configuration for Go x/trace.FlightRecorder
Signed-off-by: Sandor Szücs <[email protected]>
- Loading branch information
Showing
9 changed files
with
253 additions
and
37 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
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
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
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
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
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,76 @@ | ||
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, | ||
FlightRecorderTargetURL: service.URL, | ||
}, rr...) | ||
defer pr.Close() | ||
|
||
rsp, err := pr.Client().Get(pr.URL) | ||
if err != nil { | ||
t.Fatalf("Failed to GET %q: %v", pr.URL, err) | ||
} | ||
defer rsp.Body.Close() | ||
_, err = io.ReadAll(rsp.Body) | ||
if err != nil { | ||
t.Fatalf("Failed to read body: %v", err) | ||
} | ||
|
||
switch rsp.StatusCode { | ||
case 200, 201, 204: | ||
// ok | ||
default: | ||
t.Fatalf("Failed to get status OK: %d", rsp.StatusCode) | ||
} | ||
} |
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
Oops, something went wrong.