...

Source file src/go.opencensus.io/plugin/ochttp/span_annotating_client_trace_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  	"errors"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"strings"
    22  	"sync"
    23  	"testing"
    24  
    25  	"go.opencensus.io/plugin/ochttp"
    26  	"go.opencensus.io/trace"
    27  )
    28  
    29  func TestSpanAnnotatingClientTrace(t *testing.T) {
    30  	server := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
    31  		resp.Write([]byte("Hello, world!"))
    32  	}))
    33  	defer server.Close()
    34  
    35  	recorder := &testExporter{}
    36  
    37  	trace.RegisterExporter(recorder)
    38  
    39  	tr := ochttp.Transport{
    40  		NewClientTrace: ochttp.NewSpanAnnotatingClientTrace,
    41  		StartOptions: trace.StartOptions{
    42  			Sampler: trace.AlwaysSample(),
    43  		},
    44  	}
    45  
    46  	req, err := http.NewRequest("POST", server.URL, strings.NewReader("req-body"))
    47  	if err != nil {
    48  		t.Errorf("error creating request: %v", err)
    49  	}
    50  
    51  	resp, err := tr.RoundTrip(req)
    52  	if err != nil {
    53  		t.Errorf("response error: %v", err)
    54  	}
    55  	if err := resp.Body.Close(); err != nil {
    56  		t.Errorf("error closing response body: %v", err)
    57  	}
    58  	if got, want := resp.StatusCode, 200; got != want {
    59  		t.Errorf("resp.StatusCode=%d; want=%d", got, want)
    60  	}
    61  
    62  	if got, want := len(recorder.spans), 1; got != want {
    63  		t.Fatalf("span count=%d; want=%d", got, want)
    64  	}
    65  
    66  	var annotations []string
    67  	for _, annotation := range recorder.spans[0].Annotations {
    68  		annotations = append(annotations, annotation.Message)
    69  	}
    70  
    71  	required := []string{
    72  		"GetConn", "GotConn", "GotFirstResponseByte", "ConnectStart",
    73  		"ConnectDone", "WroteHeaders", "WroteRequest",
    74  	}
    75  
    76  	if errs := requiredAnnotations(required, annotations); len(errs) > 0 {
    77  		for _, err := range errs {
    78  			t.Error(err)
    79  		}
    80  	}
    81  
    82  }
    83  
    84  type testExporter struct {
    85  	mu    sync.Mutex
    86  	spans []*trace.SpanData
    87  }
    88  
    89  func (t *testExporter) ExportSpan(s *trace.SpanData) {
    90  	t.mu.Lock()
    91  	t.spans = append(t.spans, s)
    92  	t.mu.Unlock()
    93  }
    94  
    95  func requiredAnnotations(required []string, list []string) []error {
    96  	var errs []error
    97  	for _, item := range required {
    98  		var found bool
    99  		for _, v := range list {
   100  			if v == item {
   101  				found = true
   102  			}
   103  		}
   104  		if !found {
   105  			errs = append(errs, errors.New("missing expected annotation: "+item))
   106  		}
   107  	}
   108  	return errs
   109  }
   110  

View as plain text