github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/example_instrumentation_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package instana_test
     5  
     6  import (
     7  	"context"
     8  	"net/http"
     9  
    10  	instana "github.com/instana/go-sensor"
    11  )
    12  
    13  // This example demonstrates how to instrument an HTTP handler with Instana and register it
    14  // in http.DefaultServeMux
    15  func ExampleTracingHandlerFunc() {
    16  	// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended
    17  	// to use a single instance throughout your application
    18  	sensor := instana.NewSensor("my-http-server")
    19  
    20  	http.HandleFunc("/", instana.TracingNamedHandlerFunc(sensor, "root", "/", func(w http.ResponseWriter, req *http.Request) {
    21  		// handler code
    22  	}))
    23  }
    24  
    25  // This example demonstrates how to instrument an HTTP client with Instana
    26  func ExampleRoundTripper() {
    27  	// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended
    28  	// to use a single instance throughout your application
    29  	sensor := instana.NewSensor("my-http-client")
    30  	span := sensor.Tracer().StartSpan("entry")
    31  
    32  	// Make sure to finish the span so it can be properly recorded in the backend
    33  	defer span.Finish()
    34  
    35  	// http.DefaultTransport is used as a default RoundTripper, however you can provide
    36  	// your own implementation
    37  	client := &http.Client{
    38  		Transport: instana.RoundTripper(sensor, nil),
    39  	}
    40  
    41  	// Inject parent span into the request context
    42  	ctx := instana.ContextWithSpan(context.Background(), span)
    43  	req, _ := http.NewRequest("GET", "https://www.instana.com", nil)
    44  
    45  	// Execute request as usual
    46  	client.Do(req.WithContext(ctx))
    47  }
    48  
    49  // This example demonstrates how to instrument an *sql.DB instance created with sql.Open()
    50  func ExampleSQLOpen() {
    51  	// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended
    52  	// to use a single instance throughout your application
    53  	sensor := instana.NewSensor("my-http-client")
    54  
    55  	// Instrument the driver. Normally this would be a type provided by the driver library, e.g.
    56  	// pq.Driver{} or mysql.Driver{}, but here we use a test mock to avoid bringing external dependencies
    57  	instana.InstrumentSQLDriver(sensor, "your_db_driver", sqlDriver{})
    58  
    59  	// Replace sql.Open() with instana.SQLOpen()
    60  	db, _ := instana.SQLOpen("your_db_driver", "driver connection string")
    61  
    62  	// Inject parent span into the context
    63  	span := sensor.Tracer().StartSpan("entry")
    64  	ctx := instana.ContextWithSpan(context.Background(), span)
    65  
    66  	// Query the database, passing the context containing the active span
    67  	db.QueryContext(ctx, "SELECT * FROM users;")
    68  
    69  	// SQL queries that are not expected to return a result set are also supported
    70  	db.ExecContext(ctx, "UPDATE users SET last_seen_at = NOW();")
    71  }