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

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2016
     3  
     4  package instana
     5  
     6  import (
     7  	"os"
     8  	"strconv"
     9  )
    10  
    11  // Options allows the user to configure the to-be-initialized sensor
    12  type Options struct {
    13  	// Service is the global service name that will be used to identify the program in the Instana backend
    14  	Service string
    15  	// AgentHost is the Instana host agent host name
    16  	//
    17  	// Note: This setting has no effect in serverless environments. To specify the serverless acceptor endpoint,
    18  	// INSTANA_ENDPOINT_URL env var. See https://www.instana.com/docs/reference/environment_variables/#serverless-monitoring
    19  	// for more details.
    20  	AgentHost string
    21  	// AgentPort is the Instana host agent port
    22  	//
    23  	// Note: This setting has no effect in serverless environments. To specify the serverless acceptor endpoint,
    24  	// INSTANA_ENDPOINT_URL env var. See https://www.instana.com/docs/reference/environment_variables/#serverless-monitoring
    25  	// for more details.
    26  	AgentPort int
    27  	// MaxBufferedSpans is the maximum number of spans to buffer
    28  	MaxBufferedSpans int
    29  	// ForceTransmissionStartingAt is the number of spans to collect before flushing the buffer to the agent
    30  	ForceTransmissionStartingAt int
    31  	// LogLevel is the initial logging level for the logger used by Instana tracer. The valid log levels are
    32  	// logger.{Error,Warn,Info,Debug}Level provided by the github.com/instana/go-sensor/logger package.
    33  	//
    34  	// Note: this setting is only used to initialize the default logger and has no effect if a custom logger is set via instana.SetLogger()
    35  	LogLevel int
    36  	// EnableAutoProfile enables automatic continuous process profiling when set to true
    37  	EnableAutoProfile bool
    38  	// MaxBufferedProfiles is the maximum number of profiles to buffer
    39  	MaxBufferedProfiles int
    40  	// IncludeProfilerFrames is whether to include profiler calls into the profile or not
    41  	IncludeProfilerFrames bool
    42  	// Tracer contains tracer-specific configuration used by all tracers
    43  	Tracer TracerOptions
    44  	// AgentClient client to communicate with the agent. In most cases, there is no need to provide it.
    45  	// If it is nil the default implementation will be used.
    46  	AgentClient AgentClient
    47  
    48  	// Recorder records and manages spans. When this option is not set, instana.NewRecorder() will be used.
    49  	Recorder SpanRecorder
    50  
    51  	disableW3CTraceCorrelation bool
    52  }
    53  
    54  // DefaultOptions returns the default set of options to configure Instana sensor.
    55  // The service name is set to the name of current executable, the MaxBufferedSpans
    56  // and ForceTransmissionStartingAt are set to instana.DefaultMaxBufferedSpans and
    57  // instana.DefaultForceSpanSendAt correspondigly. The AgentHost and AgentPort are
    58  // taken from the env INSTANA_AGENT_HOST and INSTANA_AGENT_PORT if set, and default
    59  // to localhost and 42699 otherwise.
    60  func DefaultOptions() *Options {
    61  	opts := &Options{
    62  		Tracer: DefaultTracerOptions(),
    63  	}
    64  	opts.setDefaults()
    65  
    66  	return opts
    67  }
    68  
    69  func (opts *Options) setDefaults() {
    70  	if opts.MaxBufferedSpans == 0 {
    71  		opts.MaxBufferedSpans = DefaultMaxBufferedSpans
    72  	}
    73  
    74  	if opts.ForceTransmissionStartingAt == 0 {
    75  		opts.ForceTransmissionStartingAt = DefaultForceSpanSendAt
    76  	}
    77  
    78  	if opts.AgentHost == "" {
    79  		opts.AgentHost = agentDefaultHost
    80  
    81  		if host := os.Getenv("INSTANA_AGENT_HOST"); host != "" {
    82  			opts.AgentHost = host
    83  		}
    84  	}
    85  
    86  	if opts.AgentPort == 0 {
    87  		opts.AgentPort = agentDefaultPort
    88  
    89  		if port, err := strconv.Atoi(os.Getenv("INSTANA_AGENT_PORT")); err == nil {
    90  			opts.AgentPort = port
    91  		}
    92  	}
    93  
    94  	secretsMatcher, err := parseInstanaSecrets(os.Getenv("INSTANA_SECRETS"))
    95  	if err != nil {
    96  		defaultLogger.Warn("invalid INSTANA_SECRETS= env variable value: ", err, ", ignoring")
    97  		secretsMatcher = opts.Tracer.Secrets
    98  	}
    99  
   100  	if secretsMatcher == nil {
   101  		secretsMatcher = DefaultSecretsMatcher()
   102  	}
   103  
   104  	opts.Tracer.Secrets = secretsMatcher
   105  
   106  	if collectableHeaders, ok := os.LookupEnv("INSTANA_EXTRA_HTTP_HEADERS"); ok {
   107  		opts.Tracer.CollectableHTTPHeaders = parseInstanaExtraHTTPHeaders(collectableHeaders)
   108  	}
   109  
   110  	opts.disableW3CTraceCorrelation = os.Getenv("INSTANA_DISABLE_W3C_TRACE_CORRELATION") != ""
   111  }