This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stats support for net/http servers (#534)
- Loading branch information
Showing
5 changed files
with
267 additions
and
32 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 |
---|---|---|
@@ -1,27 +1,117 @@ | ||
package ochttp | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"go.opencensus.io/stats" | ||
"go.opencensus.io/tag" | ||
"go.opencensus.io/stats/view" | ||
) | ||
|
||
func TestVarsInitialized(t *testing.T) { | ||
// Test that global initialization was successful | ||
for i, k := range []tag.Key{Host, StatusCode, Path, Method} { | ||
if k.Name() == "" { | ||
t.Errorf("key not initialized: %d", i) | ||
} | ||
func httpHandler(statusCode, respSize int) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(statusCode) | ||
body := make([]byte, respSize) | ||
w.Write(body) | ||
}) | ||
} | ||
|
||
func updateMean(mean float64, sample, count int) float64 { | ||
if count == 1 { | ||
return float64(sample) | ||
} | ||
for i, m := range []stats.Measure{ClientRequestCount, ClientResponseBytes, ClientRequestBytes, ClientLatency} { | ||
if m == nil { | ||
t.Errorf("measure not initialized: %d", i) | ||
} | ||
return mean + (float64(sample)-mean)/float64(count) | ||
} | ||
|
||
func TestHandlerStatsCollection(t *testing.T) { | ||
for _, v := range DefaultViews { | ||
v.Subscribe() | ||
} | ||
|
||
views := []string{ | ||
"opencensus.io/http/server/request_count", | ||
"opencensus.io/http/server/latency", | ||
"opencensus.io/http/server/request_bytes", | ||
"opencensus.io/http/server/response_bytes", | ||
} | ||
|
||
// TODO: test latency measurements? | ||
tests := []struct { | ||
name, method, target string | ||
count, statusCode, reqSize, respSize int | ||
}{ | ||
{"get 200", "GET", "http://opencensus.io/request/one", 10, 200, 512, 512}, | ||
{"post 503", "POST", "http://opencensus.io/request/two", 5, 503, 1024, 16384}, | ||
{"no body 302", "GET", "http://opencensus.io/request/three", 2, 302, 0, 0}, | ||
} | ||
totalCount, meanReqSize, meanRespSize := 0, 0.0, 0.0 | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
body := bytes.NewBuffer(make([]byte, test.reqSize)) | ||
r := httptest.NewRequest(test.method, test.target, body) | ||
w := httptest.NewRecorder() | ||
h := &Handler{ | ||
NoTrace: true, | ||
Handler: httpHandler(test.statusCode, test.respSize), | ||
} | ||
|
||
for i := 0; i < test.count; i++ { | ||
h.ServeHTTP(w, r) | ||
totalCount++ | ||
// Distributions do not track sum directly, we must | ||
// mimic their behaviour to avoid rounding failures. | ||
meanReqSize = updateMean(meanReqSize, test.reqSize, totalCount) | ||
meanRespSize = updateMean(meanRespSize, test.respSize, totalCount) | ||
} | ||
}) | ||
} | ||
for i, v := range DefaultViews { | ||
|
||
for _, viewName := range views { | ||
v := view.Find(viewName) | ||
if v == nil { | ||
t.Errorf("view not initialized: %d", i) | ||
t.Errorf("view not found %q", viewName) | ||
continue | ||
} | ||
rows, err := view.RetrieveData(viewName) | ||
if err != nil { | ||
t.Error(err) | ||
continue | ||
} | ||
if got, want := len(rows), 1; got != want { | ||
t.Errorf("len(%q) = %d; want %d", viewName, got, want) | ||
continue | ||
} | ||
data := rows[0].Data | ||
|
||
var count int | ||
var sum float64 | ||
switch data := data.(type) { | ||
case *view.CountData: | ||
count = int(*data) | ||
case *view.DistributionData: | ||
count = int(data.Count) | ||
sum = data.Sum() | ||
default: | ||
t.Errorf("Unkown data type: %v", data) | ||
continue | ||
} | ||
|
||
if got, want := count, totalCount; got != want { | ||
t.Fatalf("%s = %d; want %d", viewName, got, want) | ||
} | ||
|
||
// We can only check sum for distribution views. | ||
switch viewName { | ||
case "opencensus.io/http/server/request_bytes": | ||
if got, want := sum, meanReqSize*float64(totalCount); got != want { | ||
t.Fatalf("%s = %g; want %g", viewName, got, want) | ||
} | ||
case "opencensus.io/http/server/response_bytes": | ||
if got, want := sum, meanRespSize*float64(totalCount); got != want { | ||
t.Fatalf("%s = %g; want %g", viewName, got, want) | ||
} | ||
} | ||
} | ||
} |
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