Skip to content

Commit

Permalink
Merge pull request #112 from instana/refactor_sensor_init
Browse files Browse the repository at this point in the history
Refactor sensor init
  • Loading branch information
Andrew Slotin authored Apr 24, 2020
2 parents 3e9a826 + 967a632 commit 190e57d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 93 deletions.
18 changes: 9 additions & 9 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ type agentS struct {
client *http.Client
}

func (r *agentS) init() {
r.client = &http.Client{Timeout: 5 * time.Second}
r.fsm = r.initFsm()
r.setFrom(&fromS{})
}
func newAgent(sensor *sensorS) *agentS {
sensor.logger.Debug("initializing agent")

func (r *agentS) initFsm() *fsmS {
ret := &fsmS{agent: r}
ret.init()
agent := &agentS{
sensor: sensor,
from: &fromS{},
client: &http.Client{Timeout: 5 * time.Second},
}
agent.fsm = newFSM(agent)

return ret
return agent
}

func (r *agentS) makeURL(prefix string) string {
Expand Down
24 changes: 15 additions & 9 deletions fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,30 @@ type fsmS struct {

var procSchedPIDRegex = regexp.MustCompile(`\((\d+),`)

func (r *fsmS) init() {
r.agent.sensor.logger.Warn("Stan is on the scene. Starting Instana instrumentation.")
r.agent.sensor.logger.Debug("initializing fsm")
func newFSM(agent *agentS) *fsmS {
agent.sensor.logger.Warn("Stan is on the scene. Starting Instana instrumentation.")
agent.sensor.logger.Debug("initializing fsm")

r.fsm = f.NewFSM(
ret := &fsmS{
agent: agent,
retries: maximumRetries,
}

ret.fsm = f.NewFSM(
"none",
f.Events{
{Name: eInit, Src: []string{"none", "unannounced", "announced", "ready"}, Dst: "init"},
{Name: eLookup, Src: []string{"init"}, Dst: "unannounced"},
{Name: eAnnounce, Src: []string{"unannounced"}, Dst: "announced"},
{Name: eTest, Src: []string{"announced"}, Dst: "ready"}},
f.Callbacks{
"init": r.lookupAgentHost,
"enter_unannounced": r.announceSensor,
"enter_announced": r.testAgent})
"init": ret.lookupAgentHost,
"enter_unannounced": ret.announceSensor,
"enter_announced": ret.testAgent,
})
ret.fsm.Event(eInit)

r.retries = maximumRetries
r.fsm.Event(eInit)
return ret
}

func (r *fsmS) scheduleRetry(e *f.Event, cb func(e *f.Event)) {
Expand Down
35 changes: 21 additions & 14 deletions meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,42 @@ type EntityData struct {
type meterS struct {
sensor *sensorS
numGC uint32
ticker *time.Ticker
snapshotCountdown int
}

func (r *meterS) init() {
r.ticker = time.NewTicker(1 * time.Second)
func newMeter(sensor *sensorS) *meterS {
sensor.logger.Debug("initializing meter")

meter := &meterS{
sensor: sensor,
}

ticker := time.NewTicker(1 * time.Second)
go func() {
r.snapshotCountdown = 1
for range r.ticker.C {
if r.sensor.agent.canSend() {
r.snapshotCountdown--
meter.snapshotCountdown = 1
for range ticker.C {
if meter.sensor.agent.canSend() {
meter.snapshotCountdown--
var s *SnapshotS
if r.snapshotCountdown == 0 {
r.snapshotCountdown = SnapshotPeriod
s = r.collectSnapshot()
r.sensor.logger.Debug("collected snapshot")
if meter.snapshotCountdown == 0 {
meter.snapshotCountdown = SnapshotPeriod
s = meter.collectSnapshot()
meter.sensor.logger.Debug("collected snapshot")
}

pid, _ := strconv.Atoi(r.sensor.agent.from.PID)
pid, _ := strconv.Atoi(meter.sensor.agent.from.PID)
d := &EntityData{
PID: pid,
Snapshot: s,
Metrics: r.collectMetrics(),
Metrics: meter.collectMetrics(),
}

go r.send(d)
go meter.send(d)
}
}
}()

return meter
}

func (r *meterS) send(d *EntityData) {
Expand Down
30 changes: 30 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package instana

import (
"os"
"path/filepath"
)

// Options allows the user to configure the to-be-initialized
// sensor
type Options struct {
Expand All @@ -13,3 +18,28 @@ type Options struct {
MaxBufferedProfiles int
IncludeProfilerFrames bool
}

// DefaultOptions returns the default set of options to configure Instana sensor.
// The service name is set to the name of current executable, the MaxBufferedSpans
// and ForceTransmissionStartingAt are set to instana.DefaultMaxBufferedSpans and
// instana.DefaultForceSpanSendAt correspondigly
func DefaultOptions() *Options {
opts := &Options{}
opts.setDefaults()

return opts
}

func (opts *Options) setDefaults() {
if opts.MaxBufferedSpans == 0 {
opts.MaxBufferedSpans = DefaultMaxBufferedSpans
}

if opts.ForceTransmissionStartingAt == 0 {
opts.ForceTransmissionStartingAt = DefaultForceSpanSendAt
}

if opts.Service == "" {
opts.Service = filepath.Base(os.Args[0])
}
}
80 changes: 19 additions & 61 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,88 +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 = r.initAgent()
r.meter = r.initMeter()
}

func (r *sensorS) initAgent() *agentS {
r.logger.Debug("initializing agent")

ret := &agentS{sensor: r}
ret.init()

return ret
}

func (r *sensorS) initMeter() *meterS {
r.logger.Debug("initializing meter")

ret := &meterS{sensor: r}
ret.init()

return ret
}

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 190e57d

Please sign in to comment.