-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Send request metrics from queue proxy #3596
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
90345a5
checkpoint1
yanweiguo e436a3b
checkpoint2
yanweiguo 8c3d96f
add tests and update pkg
yanweiguo 76fe349
comment
yanweiguo e050611
Uri to URI
yanweiguo 9bace80
copyright
yanweiguo 7d55f83
remove the config key for test
yanweiguo 45f3782
revert testing code
yanweiguo c0b5d35
address comment
yanweiguo 7ae4bd7
merged master
yanweiguo 3838721
Update Gopkg.toml
mdemirhan 0bb3eda
Update main.go
mdemirhan 6611012
Merge branch 'master' into requestmetrics
yanweiguo 529a74f
Merge branch 'requestmetrics' of github.com:yanweiguo/serving into re…
yanweiguo 5b105ec
make service optional
yanweiguo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,65 @@ | ||
/* | ||
Copyright 2019 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package queue | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
pkghttp "github.com/knative/serving/pkg/http" | ||
"github.com/knative/serving/pkg/queue/stats" | ||
) | ||
|
||
type requestMetricHandler struct { | ||
handler http.Handler | ||
statsReporter stats.StatsReporter | ||
} | ||
|
||
// NewRequestMetricHandler creates an http.Handler that emits request metrics. | ||
func NewRequestMetricHandler(h http.Handler, r stats.StatsReporter) (http.Handler, error) { | ||
if r == nil { | ||
return nil, errors.New("StatsReporter must not be nil") | ||
} | ||
|
||
return &requestMetricHandler{ | ||
handler: h, | ||
statsReporter: r, | ||
}, nil | ||
} | ||
|
||
func (h *requestMetricHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
rr := pkghttp.NewResponseRecorder(w, http.StatusOK) | ||
startTime := time.Now() | ||
defer func() { | ||
// If ServeHTTP panics, recover, record the failure and panic again. | ||
err := recover() | ||
latency := time.Since(startTime) | ||
if err != nil { | ||
h.sendRequestMetrics(http.StatusInternalServerError, latency) | ||
panic(err) | ||
} else { | ||
h.sendRequestMetrics(rr.ResponseCode, latency) | ||
} | ||
}() | ||
h.handler.ServeHTTP(rr, r) | ||
} | ||
|
||
func (h *requestMetricHandler) sendRequestMetrics(respCode int, latency time.Duration) { | ||
h.statsReporter.ReportRequestCount(respCode, 1) | ||
h.statsReporter.ReportResponseTime(respCode, latency) | ||
} |
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,117 @@ | ||
/* | ||
Copyright 2019 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package queue | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/knative/serving/pkg/queue/stats" | ||
) | ||
|
||
func TestNewRequestMetricHandler_failure(t *testing.T) { | ||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
var r stats.StatsReporter | ||
_, err := NewRequestMetricHandler(baseHandler, r) | ||
if err == nil { | ||
t.Error("should get error when StatsReporter is emtpy") | ||
} | ||
} | ||
|
||
func TestRequestMetricHandler(t *testing.T) { | ||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
r := &fakeStatsReporter{} | ||
handler, err := NewRequestMetricHandler(baseHandler, r) | ||
if err != nil { | ||
t.Fatalf("failed to create handler: %v", err) | ||
} | ||
|
||
resp := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodPost, "http://example.com", bytes.NewBufferString("test")) | ||
handler.ServeHTTP(resp, req) | ||
|
||
// Serve one request, should get 1 request count and none zero latency | ||
if got, want := r.lastRespCode, http.StatusOK; got != want { | ||
t.Errorf("response code got %v, want %v", got, want) | ||
} | ||
if got, want := r.lastReqCount, 1; got != int64(want) { | ||
t.Errorf("request count got %v, want %v", got, want) | ||
} | ||
if r.lastReqLatency == 0 { | ||
t.Errorf("request latency got %v, want lager than 0", r.lastReqLatency) | ||
} | ||
} | ||
|
||
func TestRequestMetricHandler_PanickingHandler(t *testing.T) { | ||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
panic("no!") | ||
}) | ||
r := &fakeStatsReporter{} | ||
handler, err := NewRequestMetricHandler(baseHandler, r) | ||
if err != nil { | ||
t.Fatalf("failed to create handler: %v", err) | ||
} | ||
|
||
resp := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodPost, "http://example.com", bytes.NewBufferString("test")) | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("want ServeHTTP to panic, got nothing.") | ||
} | ||
|
||
// Serve one request, should get 1 request count and none zero latency | ||
if got, want := r.lastRespCode, http.StatusInternalServerError; got != want { | ||
t.Errorf("response code got %v, want %v", got, want) | ||
} | ||
if got, want := r.lastReqCount, 1; got != int64(want) { | ||
t.Errorf("request count got %v, want %v", got, want) | ||
} | ||
if r.lastReqLatency == 0 { | ||
t.Errorf("request latency got %v, want lager than 0", r.lastReqLatency) | ||
} | ||
}() | ||
handler.ServeHTTP(resp, req) | ||
|
||
} | ||
|
||
// fakeStatsReporter just record the last stat it received. | ||
type fakeStatsReporter struct { | ||
lastRespCode int | ||
lastReqCount int64 | ||
lastReqLatency time.Duration | ||
} | ||
|
||
func (r *fakeStatsReporter) ReportRequestCount(responseCode int, v int64) error { | ||
r.lastRespCode = responseCode | ||
r.lastReqCount = v | ||
return nil | ||
} | ||
|
||
func (r *fakeStatsReporter) ReportResponseTime(responseCode int, d time.Duration) error { | ||
r.lastRespCode = responseCode | ||
r.lastReqLatency = d | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to replace the log handler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Hmm, what I did is adding a handler layer to the log handler, isn't it?