github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/example_httpserver_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package instana_test
     5  
     6  import (
     7  	"log"
     8  	"net/http"
     9  
    10  	instana "github.com/mier85/go-sensor"
    11  	"github.com/opentracing/opentracing-go"
    12  )
    13  
    14  // This example shows how to instrument an HTTP server with Instana tracing
    15  func Example_tracingNamedHandlerFunc() {
    16  	sensor := instana.NewSensor("my-http-server")
    17  
    18  	// To instrument a handler function, pass it as an argument to instana.TracingHandlerFunc()
    19  	http.HandleFunc("/", instana.TracingHandlerFunc(sensor, "/", func(w http.ResponseWriter, req *http.Request) {
    20  		// Extract the parent span and use its tracer to initialize any child spans to trace the calls
    21  		// inside the handler, e.g. database queries, 3rd-party API requests, etc.
    22  		if parent, ok := instana.SpanFromContext(req.Context()); ok {
    23  			sp := parent.Tracer().StartSpan("index", opentracing.ChildOf(parent.Context()))
    24  			defer sp.Finish()
    25  		}
    26  
    27  		// ...
    28  
    29  		w.Write([]byte("OK"))
    30  	}))
    31  
    32  	// In case your handler is implemented as an http.Handler, pass its ServeHTTP method instead.
    33  	// You can also use instana.TracingNamedHandlerFunc() to provide a unique route identifier to
    34  	// group the calls to this route later in Instana UI.
    35  	http.HandleFunc("/files", instana.TracingNamedHandlerFunc(sensor, "index", "/:path", http.FileServer(http.Dir("./")).ServeHTTP))
    36  
    37  	if err := http.ListenAndServe(":0", nil); err != nil {
    38  		log.Fatalf("failed to start server: %s", err)
    39  	}
    40  }
    41  
    42  // This example demonstrates how to instrument a 3rd-party HTTP router that uses pattern matching to make sure
    43  // the original path template is forwarded to Instana
    44  func Example_httpRoutePatternMatching() {
    45  	sensor := instana.NewSensor("my-http-server")
    46  
    47  	// Initialize your router. Here for simplicity we use stdlib http.ServeMux, however you
    48  	// can use any router that supports http.Handler/http.HandlerFunc, such as github.com/gorilla/mux
    49  	r := http.NewServeMux()
    50  
    51  	// Wrap the handler using instana.TracingHandlerFunc() and pass the path template
    52  	// used to route requests to it. This value will be attached to the span and displayed
    53  	// in UI as `http.path_tpl` if the request path is different (we assume that this request
    54  	// has been routed to the handler via a matched pattern)
    55  	r.HandleFunc("/articles/{category}/{id:[0-9]+}", instana.TracingHandlerFunc(
    56  		sensor,
    57  		"/articles/{category}/{id:[0-9]+}",
    58  		func(w http.ResponseWriter, req *http.Request) {
    59  			// ...
    60  		},
    61  	))
    62  
    63  	if err := http.ListenAndServe(":0", nil); err != nil {
    64  		log.Fatalf("failed to start server: %s", err)
    65  	}
    66  }