Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server-timing endpoint sequences #751

Merged
merged 3 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Unreleased changes are available as `avenga/couper:edge` container.
* **Fixed**
* Erroneously sending an empty [`Server-Timing` header](https://docs.couper.io/configuration/command-line#oberservation-options) ([#700](https://github.com/avenga/couper/pull/700))
* `WWW-Authenticate` header `realm` param value for [`basic_auth`](https://docs.couper.io/configuration/block/basic_auth) ([#715](https://github.com/avenga/couper/pull/715))
* [`Server-Timing` header](https://docs.couper.io/configuration/block/settings) only reporting last requests/proxies of [endpoint sequences](https://docs.couper.io/configuration/block/endpoint#endpoint-sequence) ([#751](https://github.com/avenga/couper/pull/751))

* **Dependencies**
* build with go 1.20 ([#745](https://github.com/avenga/couper/pull/745))
Expand Down
32 changes: 17 additions & 15 deletions handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,27 @@ func (e *Endpoint) newRedirect() *http.Response {
}
}

func newChannels(l sequence.List) (inputChannels, outputChannels map[string][]chan *producer.Result) {
func newChannels(l sequence.List) (inputChannels, outputChannels map[string][]chan *producer.Result, resultChannels map[string]chan *producer.Result) {
inputChannels = make(map[string][]chan *producer.Result)
outputChannels = make(map[string][]chan *producer.Result)
resultChannels = make(map[string]chan *producer.Result)
for _, item := range l {
fillChannels(item, inputChannels, outputChannels)
ch := make(chan *producer.Result, 1)
outputChannels[item.Name] = []chan *producer.Result{ch}
fillChannels(item, inputChannels, outputChannels, resultChannels)
}
return inputChannels, outputChannels
return inputChannels, outputChannels, resultChannels
}

func fillChannels(item *sequence.Item, inputChannels, outputChannels map[string][]chan *producer.Result) {
func fillChannels(item *sequence.Item, inputChannels, outputChannels map[string][]chan *producer.Result, resultChannels map[string]chan *producer.Result) {
for _, dep := range item.Deps() {
ch := make(chan *producer.Result, 1)
inputChannels[item.Name] = append(inputChannels[item.Name], ch)
outputChannels[dep.Name] = append(outputChannels[dep.Name], ch)
fillChannels(dep, inputChannels, outputChannels)
fillChannels(dep, inputChannels, outputChannels, resultChannels)
}
if _, ok := resultChannels[item.Name]; !ok {
ch := make(chan *producer.Result, 1)
outputChannels[item.Name] = append(outputChannels[item.Name], ch)
resultChannels[item.Name] = ch
}
}

Expand Down Expand Up @@ -314,7 +318,7 @@ func (e *Endpoint) produce(req *http.Request) (producer.ResultMap, error) {

outreq := req.WithContext(context.WithValue(req.Context(), request.ResponseBlock, e.opts.Response != nil))

inputChannels, outputChannels := newChannels(e.opts.Items)
inputChannels, outputChannels, resultChannels := newChannels(e.opts.Items)
for name, prod := range e.opts.Producers {
go func(n string, rt producer.Roundtrip, intChs, outChs []chan *producer.Result) {
defer func() {
Expand All @@ -337,7 +341,7 @@ func (e *Endpoint) produce(req *http.Request) (producer.ResultMap, error) {
passToOutputChannels(res, outChs)
}(name, prod, inputChannels[name], outputChannels[name])
}
readResults(e.opts.Items, outputChannels, results)
readResults(resultChannels, results)

var err error // TODO: prefer default resp err
// TODO: additionally log all panic error types
Expand All @@ -351,12 +355,10 @@ func (e *Endpoint) produce(req *http.Request) (producer.ResultMap, error) {
return results, err
}

func readResults(items sequence.List, outputChannels map[string][]chan *producer.Result, beresps producer.ResultMap) {
for _, item := range items {
for _, outCh := range outputChannels[item.Name] {
res := <-outCh
beresps[item.Name] = res
}
func readResults(resultChannels map[string]chan *producer.Result, beresps producer.ResultMap) {
for name, resultCh := range resultChannels {
res := <-resultCh
beresps[name] = res
}
}

Expand Down
31 changes: 31 additions & 0 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,37 @@ func TestHTTPServer_ServerTiming(t *testing.T) {
t.Errorf("Unexpected header from 'second' Couper: %s", s)
}

req, err = http.NewRequest(http.MethodGet, "http://anyserver:9090/seq", nil)
helper.Must(err)

res, err = client.Do(req)
helper.Must(err)

headers = res.Header.Values("Server-Timing")
if l := len(headers); l != 2 {
t.Fatalf("Unexpected number of headers: %d", 2)
}

dataCouper1 = strings.Split(headers[0], ", ")
dataCouper2 = strings.Split(headers[1], ", ")

sort.Strings(dataCouper1)
sort.Strings(dataCouper2)

if len(dataCouper1) != 2 || len(dataCouper2) != 5 {
t.Fatal("Unexpected number of metrics")
}

exp1 = regexp.MustCompile(`b1_total_[0-9a-f]{6};dur=\d+(.\d)* b1_ttfb_[0-9a-f]{6};dur=\d+(.\d)*`)
if s := strings.Join(dataCouper1, " "); !exp1.MatchString(s) {
t.Errorf("Unexpected header from 'first' Couper: %s", s)
}

exp2 = regexp.MustCompile(`b1_total;dur=\d+(.\d)* b1_ttfb;dur=\d+(.\d)* b2_REQ_tcp;dur=\d+(.\d)* b2_REQ_total;dur=\d+(.\d)* b2_REQ_ttfb;dur=\d+(.\d)*`)
if s := strings.Join(dataCouper2, " "); !exp2.MatchString(s) {
t.Errorf("Unexpected header from 'second' Couper: %s", s)
}

req, err = http.NewRequest(http.MethodGet, "http://anyserver:9090/empty", nil)
helper.Must(err)

Expand Down
6 changes: 6 additions & 0 deletions server/testdata/integration/http/01_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ server "first" {
}
}

endpoint "/seq" {
proxy {
backend = "b1"
}
}

endpoint "/empty" {
response {
status = 204
Expand Down
12 changes: 12 additions & 0 deletions server/testdata/integration/http/02_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ server "second" {
backend = "b1"
}

request "REQ" {
backend = "b2"
}
}
endpoint "/seq" {
proxy {
backend = "b1"
set_request_headers = {
x-req = backend_responses.REQ.status
}
}

request "REQ" {
backend = "b2"
}
Expand Down