route_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ochttp_test
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. "github.com/google/go-cmp/cmp"
  20. "go.opencensus.io/plugin/ochttp"
  21. "go.opencensus.io/stats/view"
  22. "go.opencensus.io/tag"
  23. )
  24. func TestWithRouteTag(t *testing.T) {
  25. v := &view.View{
  26. Name: "request_total",
  27. Measure: ochttp.ServerLatency,
  28. Aggregation: view.Count(),
  29. TagKeys: []tag.Key{ochttp.KeyServerRoute},
  30. }
  31. view.Register(v)
  32. var e testStatsExporter
  33. view.RegisterExporter(&e)
  34. defer view.UnregisterExporter(&e)
  35. mux := http.NewServeMux()
  36. handler := ochttp.WithRouteTag(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  37. w.WriteHeader(204)
  38. }), "/a/")
  39. mux.Handle("/a/", handler)
  40. plugin := ochttp.Handler{Handler: mux}
  41. req, _ := http.NewRequest("GET", "/a/b/c", nil)
  42. rr := httptest.NewRecorder()
  43. plugin.ServeHTTP(rr, req)
  44. if got, want := rr.Code, 204; got != want {
  45. t.Fatalf("Unexpected response, got %d; want %d", got, want)
  46. }
  47. view.Unregister(v) // trigger exporting
  48. got := e.rowsForView("request_total")
  49. want := []*view.Row{
  50. {Data: &view.CountData{Value: 1}, Tags: []tag.Tag{{Key: ochttp.KeyServerRoute, Value: "/a/"}}},
  51. }
  52. if diff := cmp.Diff(got, want); diff != "" {
  53. t.Errorf("Unexpected view data exported, -got, +want: %s", diff)
  54. }
  55. }
  56. func TestSetRoute(t *testing.T) {
  57. v := &view.View{
  58. Name: "request_total",
  59. Measure: ochttp.ServerLatency,
  60. Aggregation: view.Count(),
  61. TagKeys: []tag.Key{ochttp.KeyServerRoute},
  62. }
  63. view.Register(v)
  64. var e testStatsExporter
  65. view.RegisterExporter(&e)
  66. defer view.UnregisterExporter(&e)
  67. mux := http.NewServeMux()
  68. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  69. ochttp.SetRoute(r.Context(), "/a/")
  70. w.WriteHeader(204)
  71. })
  72. mux.Handle("/a/", handler)
  73. plugin := ochttp.Handler{Handler: mux}
  74. req, _ := http.NewRequest("GET", "/a/b/c", nil)
  75. rr := httptest.NewRecorder()
  76. plugin.ServeHTTP(rr, req)
  77. if got, want := rr.Code, 204; got != want {
  78. t.Fatalf("Unexpected response, got %d; want %d", got, want)
  79. }
  80. view.Unregister(v) // trigger exporting
  81. got := e.rowsForView("request_total")
  82. want := []*view.Row{
  83. {Data: &view.CountData{Value: 1}, Tags: []tag.Tag{{Key: ochttp.KeyServerRoute, Value: "/a/"}}},
  84. }
  85. if diff := cmp.Diff(got, want); diff != "" {
  86. t.Errorf("Unexpected view data exported, -got, +want: %s", diff)
  87. }
  88. }
  89. type testStatsExporter struct {
  90. vd []*view.Data
  91. }
  92. func (t *testStatsExporter) ExportView(d *view.Data) {
  93. t.vd = append(t.vd, d)
  94. }
  95. func (t *testStatsExporter) rowsForView(name string) []*view.Row {
  96. var rows []*view.Row
  97. for _, d := range t.vd {
  98. if d.View.Name == name {
  99. rows = append(rows, d.Rows...)
  100. }
  101. }
  102. return rows
  103. }