go.undefinedlabs.com/scopeagent@v0.4.2/instrumentation/nethttp/patch.go (about)

     1  package nethttp
     2  
     3  import (
     4  	"go.undefinedlabs.com/scopeagent/env"
     5  	"net/http"
     6  	"sync"
     7  )
     8  
     9  type Option func(*Transport)
    10  
    11  var once sync.Once
    12  
    13  // Enables the payload instrumentation in the transport
    14  func WithPayloadInstrumentation() Option {
    15  	return func(t *Transport) {
    16  		t.PayloadInstrumentation = true
    17  	}
    18  }
    19  
    20  // Enables stacktrace
    21  func WithStacktrace() Option {
    22  	return func(t *Transport) {
    23  		t.Stacktrace = true
    24  	}
    25  }
    26  
    27  // Patches the default http client with the instrumented transport
    28  func PatchHttpDefaultClient(options ...Option) {
    29  	once.Do(func() {
    30  		transport := &Transport{RoundTripper: http.DefaultTransport}
    31  		for _, option := range options {
    32  			option(transport)
    33  		}
    34  		transport.PayloadInstrumentation = transport.PayloadInstrumentation || env.ScopeInstrumentationHttpPayloads.Value
    35  		transport.Stacktrace = transport.Stacktrace || env.ScopeInstrumentationHttpStacktrace.Value
    36  		http.DefaultClient = &http.Client{Transport: transport}
    37  	})
    38  }