diff --git a/sensor.go b/sensor.go index 6e1c3dca0..99a7ee95d 100644 --- a/sensor.go +++ b/sensor.go @@ -3,7 +3,6 @@ package instana import ( "errors" "os" - "path/filepath" "github.com/instana/go-sensor/autoprofile" "github.com/instana/go-sensor/logger" @@ -24,61 +23,39 @@ type sensorS struct { var sensor *sensorS -func (r *sensorS) init(options *Options) { - // sensor can be initialized explicitly or implicitly through OpenTracing global init - if r.meter != nil { - return +func newSensor(options *Options) *sensorS { + if options == nil { + options = DefaultOptions() + } else { + options.setDefaults() } - if r.logger == nil { - r.setLogger(defaultLogger) + s := &sensorS{ + options: options, + serviceName: options.Service, } + s.setLogger(defaultLogger) - r.setOptions(options) - r.configureServiceName() - r.agent = newAgent(r) - r.meter = newMeter(r) -} - -func (r *sensorS) setOptions(options *Options) { - r.options = options - if r.options == nil { - r.options = &Options{} + // override service name with an env value if set + if name, ok := os.LookupEnv("INSTANA_SERVICE_NAME"); ok { + s.serviceName = name } - if r.options.MaxBufferedSpans == 0 { - r.options.MaxBufferedSpans = DefaultMaxBufferedSpans + // handle the legacy (instana.Options).LogLevel value if we use logger.Logger to log + if l, ok := s.logger.(*logger.Logger); ok { + setLogLevel(l, options.LogLevel) } - if r.options.ForceTransmissionStartingAt == 0 { - r.options.ForceTransmissionStartingAt = DefaultForceSpanSendAt - } + s.agent = newAgent(s) + s.meter = newMeter(s) - // handle the legacy (instana.Options).LogLevel value if we use logger.Logger to log - if l, ok := r.logger.(*logger.Logger); ok { - setLogLevel(l, r.options.LogLevel) - } + return s } func (r *sensorS) setLogger(l LeveledLogger) { r.logger = l } -func (r *sensorS) configureServiceName() { - if name, ok := os.LookupEnv("INSTANA_SERVICE_NAME"); ok { - r.serviceName = name - return - } - - if r.options != nil { - r.serviceName = r.options.Service - } - - if r.serviceName == "" { - r.serviceName = filepath.Base(os.Args[0]) - } -} - // InitSensor intializes the sensor (without tracing) to begin collecting // and reporting metrics. func InitSensor(options *Options) { @@ -86,8 +63,7 @@ func InitSensor(options *Options) { return } - sensor = &sensorS{} - sensor.init(options) + sensor = newSensor(options) // enable auto-profiling if options.EnableAutoProfile {