...

Source file src/go.opencensus.io/plugin/ochttp/example_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  	"log"
    19  	"net/http"
    20  
    21  	"go.opencensus.io/plugin/ochttp"
    22  	"go.opencensus.io/plugin/ochttp/propagation/b3"
    23  	"go.opencensus.io/stats/view"
    24  	"go.opencensus.io/tag"
    25  )
    26  
    27  func ExampleTransport() {
    28  	// import (
    29  	// 		"go.opencensus.io/plugin/ochttp"
    30  	//		"go.opencensus.io/stats/view"
    31  	// )
    32  
    33  	if err := view.Register(
    34  		// Register a few default views.
    35  		ochttp.ClientSentBytesDistribution,
    36  		ochttp.ClientReceivedBytesDistribution,
    37  		ochttp.ClientRoundtripLatencyDistribution,
    38  		// Register a custom view.
    39  		&view.View{
    40  			Name:        "httpclient_latency_by_path",
    41  			TagKeys:     []tag.Key{ochttp.KeyClientPath},
    42  			Measure:     ochttp.ClientRoundtripLatency,
    43  			Aggregation: ochttp.DefaultLatencyDistribution,
    44  		},
    45  	); err != nil {
    46  		log.Fatal(err)
    47  	}
    48  
    49  	client := &http.Client{
    50  		Transport: &ochttp.Transport{},
    51  	}
    52  
    53  	// Use client to perform requests.
    54  	_ = client
    55  }
    56  
    57  var usersHandler http.Handler
    58  
    59  func ExampleHandler() {
    60  	// import "go.opencensus.io/plugin/ochttp"
    61  
    62  	http.Handle("/users", ochttp.WithRouteTag(usersHandler, "/users"))
    63  
    64  	// If no handler is specified, the default mux is used.
    65  	log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{}))
    66  }
    67  
    68  func ExampleHandler_mux() {
    69  	// import "go.opencensus.io/plugin/ochttp"
    70  
    71  	mux := http.NewServeMux()
    72  	mux.Handle("/users", ochttp.WithRouteTag(usersHandler, "/users"))
    73  	log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{
    74  		Handler:     mux,
    75  		Propagation: &b3.HTTPFormat{},
    76  	}))
    77  }
    78  

View as plain text