...

Source file src/go.opencensus.io/plugin/ochttp/route_test.go

Documentation: go.opencensus.io/plugin/ochttp

     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  
    15  package ochttp_test
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"go.opencensus.io/plugin/ochttp"
    24  	"go.opencensus.io/stats/view"
    25  	"go.opencensus.io/tag"
    26  )
    27  
    28  func TestWithRouteTag(t *testing.T) {
    29  	v := &view.View{
    30  		Name:        "request_total",
    31  		Measure:     ochttp.ServerLatency,
    32  		Aggregation: view.Count(),
    33  		TagKeys:     []tag.Key{ochttp.KeyServerRoute},
    34  	}
    35  	view.Register(v)
    36  	var e testStatsExporter
    37  	view.RegisterExporter(&e)
    38  	defer view.UnregisterExporter(&e)
    39  
    40  	mux := http.NewServeMux()
    41  	handler := ochttp.WithRouteTag(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    42  		w.WriteHeader(204)
    43  	}), "/a/")
    44  	mux.Handle("/a/", handler)
    45  	plugin := ochttp.Handler{Handler: mux}
    46  	req, _ := http.NewRequest("GET", "/a/b/c", nil)
    47  	rr := httptest.NewRecorder()
    48  	plugin.ServeHTTP(rr, req)
    49  	if got, want := rr.Code, 204; got != want {
    50  		t.Fatalf("Unexpected response, got %d; want %d", got, want)
    51  	}
    52  
    53  	view.Unregister(v) // trigger exporting
    54  
    55  	got := e.rowsForView("request_total")
    56  	for i := range got {
    57  		view.ClearStart(got[i].Data)
    58  	}
    59  	want := []*view.Row{
    60  		{Data: &view.CountData{Value: 1}, Tags: []tag.Tag{{Key: ochttp.KeyServerRoute, Value: "/a/"}}},
    61  	}
    62  	if diff := cmp.Diff(got, want); diff != "" {
    63  		t.Errorf("Unexpected view data exported, -got, +want: %s", diff)
    64  	}
    65  }
    66  
    67  func TestSetRoute(t *testing.T) {
    68  	v := &view.View{
    69  		Name:        "request_total",
    70  		Measure:     ochttp.ServerLatency,
    71  		Aggregation: view.Count(),
    72  		TagKeys:     []tag.Key{ochttp.KeyServerRoute},
    73  	}
    74  	view.Register(v)
    75  	var e testStatsExporter
    76  	view.RegisterExporter(&e)
    77  	defer view.UnregisterExporter(&e)
    78  
    79  	mux := http.NewServeMux()
    80  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    81  		ochttp.SetRoute(r.Context(), "/a/")
    82  		w.WriteHeader(204)
    83  	})
    84  	mux.Handle("/a/", handler)
    85  	plugin := ochttp.Handler{Handler: mux}
    86  	req, _ := http.NewRequest("GET", "/a/b/c", nil)
    87  	rr := httptest.NewRecorder()
    88  	plugin.ServeHTTP(rr, req)
    89  	if got, want := rr.Code, 204; got != want {
    90  		t.Fatalf("Unexpected response, got %d; want %d", got, want)
    91  	}
    92  
    93  	view.Unregister(v) // trigger exporting
    94  
    95  	got := e.rowsForView("request_total")
    96  	for i := range got {
    97  		view.ClearStart(got[i].Data)
    98  	}
    99  	want := []*view.Row{
   100  		{Data: &view.CountData{Value: 1}, Tags: []tag.Tag{{Key: ochttp.KeyServerRoute, Value: "/a/"}}},
   101  	}
   102  	if diff := cmp.Diff(got, want); diff != "" {
   103  		t.Errorf("Unexpected view data exported, -got, +want: %s", diff)
   104  	}
   105  }
   106  
   107  type testStatsExporter struct {
   108  	vd []*view.Data
   109  }
   110  
   111  func (t *testStatsExporter) ExportView(d *view.Data) {
   112  	t.vd = append(t.vd, d)
   113  }
   114  
   115  func (t *testStatsExporter) rowsForView(name string) []*view.Row {
   116  	var rows []*view.Row
   117  	for _, d := range t.vd {
   118  		if d.View.Name == name {
   119  			rows = append(rows, d.Rows...)
   120  		}
   121  	}
   122  	return rows
   123  }
   124  

View as plain text