...

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

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

     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  	"fmt"
    19  	"log"
    20  	"net/http"
    21  	"time"
    22  
    23  	"go.opencensus.io/zpages"
    24  
    25  	"go.opencensus.io/examples/exporter"
    26  	"go.opencensus.io/plugin/ochttp"
    27  	"go.opencensus.io/stats/view"
    28  	"go.opencensus.io/trace"
    29  )
    30  
    31  const (
    32  	metricsLogFile = "/tmp/metrics.log"
    33  	tracesLogFile  = "/tmp/trace.log"
    34  )
    35  
    36  func main() {
    37  	// Start z-Pages server.
    38  	go func() {
    39  		mux := http.NewServeMux()
    40  		zpages.Handle(mux, "/debug")
    41  		log.Fatal(http.ListenAndServe("127.0.0.1:8081", mux))
    42  	}()
    43  
    44  	// Using log exporter to export metrics but you can choose any supported exporter.
    45  	exporter, err := exporter.NewLogExporter(exporter.Options{
    46  		ReportingInterval: 10 * time.Second,
    47  		MetricsLogFile:    metricsLogFile,
    48  		TracesLogFile:     tracesLogFile,
    49  	})
    50  	if err != nil {
    51  		log.Fatalf("Error creating log exporter: %v", err)
    52  	}
    53  	exporter.Start()
    54  	defer exporter.Stop()
    55  	defer exporter.Close()
    56  
    57  	// Always trace for this demo. In a production application, you should
    58  	// configure this to a trace.ProbabilitySampler set at the desired
    59  	// probability.
    60  	trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
    61  
    62  	// Report stats at every second.
    63  	view.SetReportingPeriod(1 * time.Second)
    64  
    65  	client := &http.Client{Transport: &ochttp.Transport{}}
    66  
    67  	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
    68  		fmt.Fprintf(w, "hello world")
    69  
    70  		// Provide an example of how spans can be annotated with metadata
    71  		_, span := trace.StartSpan(req.Context(), "child")
    72  		defer span.End()
    73  		span.Annotate([]trace.Attribute{trace.StringAttribute("key", "value")}, "something happened")
    74  		span.AddAttributes(trace.StringAttribute("hello", "world"))
    75  		time.Sleep(time.Millisecond * 125)
    76  
    77  		r, _ := http.NewRequest("GET", "https://example.com", nil)
    78  
    79  		// Propagate the trace header info in the outgoing requests.
    80  		r = r.WithContext(req.Context())
    81  		resp, err := client.Do(r)
    82  		if err != nil {
    83  			log.Println(err)
    84  		} else {
    85  			// TODO: handle response
    86  			resp.Body.Close()
    87  		}
    88  	})
    89  	log.Fatal(http.ListenAndServe(":50030", &ochttp.Handler{}))
    90  }
    91  

View as plain text