Skip to content

Commit

Permalink
Move sensor init into a constructor function
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Slotin committed Apr 24, 2020
1 parent 798525f commit 967a632
Showing 1 changed file with 19 additions and 43 deletions.
62 changes: 19 additions & 43 deletions sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package instana
import (
"errors"
"os"
"path/filepath"

"github.com/instana/go-sensor/autoprofile"
"github.com/instana/go-sensor/logger"
Expand All @@ -24,70 +23,47 @@ 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) {
if sensor != nil {
return
}

sensor = &sensorS{}
sensor.init(options)
sensor = newSensor(options)

// enable auto-profiling
if options.EnableAutoProfile {
Expand Down

0 comments on commit 967a632

Please sign in to comment.