Skip to content

Commit 203a224

Browse files
authored
Merge pull request #40 from instana/better-offline-handling
Better offline handling
2 parents e2750ad + 3b71f61 commit 203a224

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

instana/agent.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import instana.fsm as f
44
import instana.agent_const as a
55
import threading
6+
from datetime import datetime
67

78
try:
89
import urllib.request as urllib2
@@ -36,6 +37,7 @@ class Agent(object):
3637
port = a.AGENT_DEFAULT_PORT
3738
fsm = None
3839
from_ = From()
40+
last_seen = None
3941

4042
def __init__(self, sensor):
4143
log.debug("initializing agent")
@@ -50,6 +52,13 @@ def to_json(self, o):
5052
except Exception as e:
5153
log.info("to_json: ", e, o)
5254

55+
def is_timed_out(self):
56+
if self.last_seen and self.can_send:
57+
diff = datetime.now() - self.last_seen
58+
if diff.seconds > 60:
59+
return True
60+
return False
61+
5362
def can_send(self):
5463
return self.fsm.fsm.current == "good2go"
5564

@@ -90,6 +99,7 @@ def full_request_response(self, url, method, o, body, header):
9099
if self.can_send():
91100
self.reset()
92101
else:
102+
self.last_seen = datetime.now()
93103
if body:
94104
b = response.read()
95105

@@ -125,6 +135,7 @@ def make_full_url(self, host, port, prefix):
125135
return s
126136

127137
def reset(self):
138+
self.last_seen = None
128139
self.from_ = From()
129140
self.fsm.reset()
130141

instana/fsm.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ def __init__(self, agent):
4141

4242
self.agent = agent
4343
self.fsm = f.Fysom({
44-
"initial": {'state': "lostandalone", 'event': 'init', 'defer': True},
4544
"events": [
46-
("startup", "*", "lostandalone"),
47-
("lookup", "lostandalone", "found"),
45+
("lookup", "*", "found"),
4846
("announce", "found", "announced"),
4947
("ready", "announced", "good2go")],
5048
"callbacks": {
5149
"onlookup": self.lookup_agent_host,
5250
"onannounce": self.announce_sensor,
51+
"onready": self.start_metric_reporting,
5352
"onchangestate": self.printstatechange}})
5453

55-
timer = t.Timer(2, self.boot)
54+
timer = t.Timer(2, self.fsm.lookup)
5655
timer.daemon = True
5756
timer.name = "Startup"
5857
timer.start()
@@ -61,13 +60,12 @@ def printstatechange(self, e):
6160
log.debug('========= (%i#%s) FSM event: %s, src: %s, dst: %s ==========' %
6261
(os.getpid(), t.current_thread().name, e.event, e.src, e.dst))
6362

64-
def boot(self):
65-
self.fsm.init()
66-
self.fsm.lookup()
67-
6863
def reset(self):
6964
self.fsm.lookup()
7065

66+
def start_metric_reporting(self, e):
67+
self.agent.sensor.meter.run()
68+
7169
def lookup_agent_host(self, e):
7270
if self.agent.sensor.options.agent_host != "":
7371
host = self.agent.sensor.options.agent_host

instana/meter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class Meter(object):
114114

115115
def __init__(self, sensor):
116116
self.sensor = sensor
117-
self.run()
118117

119118
def run(self):
120119
self.timer = t.Thread(target=self.collect_and_report)
@@ -125,6 +124,10 @@ def run(self):
125124
def collect_and_report(self):
126125
while 1:
127126
self.process()
127+
if (self.sensor.agent.is_timed_out()):
128+
log.warn("Host agent offline for >1 min. Going to sit in a corner...")
129+
self.sensor.agent.reset()
130+
break
128131
time.sleep(1)
129132

130133
def process(self):

0 commit comments

Comments
 (0)