...

Source file src/go.opencensus.io/examples/http/helloworld_client/main.go

Documentation: go.opencensus.io/examples/http/helloworld_client

     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 main
    16  
    17  import (
    18  	"log"
    19  	"net/http"
    20  	"time"
    21  
    22  	"go.opencensus.io/plugin/ochttp"
    23  	"go.opencensus.io/trace"
    24  
    25  	"go.opencensus.io/examples/exporter"
    26  	"go.opencensus.io/stats/view"
    27  )
    28  
    29  const server = "http://localhost:50030"
    30  
    31  func main() {
    32  	// Register stats and trace exporters to export the collected data.
    33  	exporter := &exporter.PrintExporter{}
    34  	view.RegisterExporter(exporter)
    35  	trace.RegisterExporter(exporter)
    36  
    37  	// Always trace for this demo. In a production application, you should
    38  	// configure this to a trace.ProbabilitySampler set at the desired
    39  	// probability.
    40  	trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
    41  
    42  	// Report stats at every second.
    43  	view.SetReportingPeriod(1 * time.Second)
    44  
    45  	client := &http.Client{Transport: &ochttp.Transport{}}
    46  
    47  	resp, err := client.Get(server)
    48  	if err != nil {
    49  		log.Printf("Failed to get response: %v", err)
    50  	} else {
    51  		resp.Body.Close()
    52  	}
    53  
    54  	time.Sleep(2 * time.Second) // Wait until stats are reported.
    55  }
    56  

View as plain text