Skip to content

Commit c436911

Browse files
committed
Merge branch 'master' into autoinstrumentation-hooks
2 parents 2e73ea7 + 6fa7774 commit c436911

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

instana/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__copyright__ = 'Copyright 2017 Instana Inc.'
1010
__credits__ = ['Pavlo Baron', 'Peter Giacomo Lombardo']
1111
__license__ = 'MIT'
12-
__version__ = '0.6.10'
12+
__version__ = '0.6.11'
1313
__maintainer__ = 'Peter Giacomo Lombardo'
1414
__email__ = '[email protected]'
1515

instana/agent.py

Lines changed: 12 additions & 2 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
@@ -35,14 +36,14 @@ class Agent(object):
3536
host = a.AGENT_DEFAULT_HOST
3637
port = a.AGENT_DEFAULT_PORT
3738
fsm = None
38-
from_ = None
39+
from_ = From()
40+
last_seen = None
3941

4042
def __init__(self, sensor):
4143
log.debug("initializing agent")
4244

4345
self.sensor = sensor
4446
self.fsm = f.Fsm(self)
45-
self.reset()
4647

4748
def to_json(self, o):
4849
try:
@@ -51,6 +52,13 @@ def to_json(self, o):
5152
except Exception as e:
5253
log.info("to_json: ", e, o)
5354

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+
5462
def can_send(self):
5563
return self.fsm.fsm.current == "good2go"
5664

@@ -91,6 +99,7 @@ def full_request_response(self, url, method, o, body, header):
9199
if self.can_send():
92100
self.reset()
93101
else:
102+
self.last_seen = datetime.now()
94103
if body:
95104
b = response.read()
96105

@@ -126,6 +135,7 @@ def make_full_url(self, host, port, prefix):
126135
return s
127136

128137
def reset(self):
138+
self.last_seen = None
129139
self.from_ = From()
130140
self.fsm.reset()
131141

instana/fsm.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import subprocess
2-
import os
31
import sys
2+
import os
43
import socket
4+
import subprocess
55
import threading as t
66
import fysom as f
77
from instana import log
@@ -41,24 +41,31 @@ def __init__(self, agent):
4141

4242
self.agent = agent
4343
self.fsm = f.Fysom({
44-
"initial": "lostandalone",
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

54+
timer = t.Timer(2, self.fsm.lookup)
55+
timer.daemon = True
56+
timer.name = "Startup"
57+
timer.start()
58+
5559
def printstatechange(self, e):
5660
log.debug('========= (%i#%s) FSM event: %s, src: %s, dst: %s ==========' %
5761
(os.getpid(), t.current_thread().name, e.event, e.src, e.dst))
5862

5963
def reset(self):
6064
self.fsm.lookup()
6165

66+
def start_metric_reporting(self, e):
67+
self.agent.sensor.meter.run()
68+
6269
def lookup_agent_host(self, e):
6370
if self.agent.sensor.options.agent_host != "":
6471
host = self.agent.sensor.options.agent_host
@@ -110,6 +117,7 @@ def announce_sensor(self, e):
110117
log.debug("announcing sensor to the agent")
111118
s = None
112119
pid = os.getpid()
120+
cmdline = []
113121

114122
if os.path.isfile("/proc/self/cmdline"):
115123
with open("/proc/self/cmdline") as cmd:

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):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33
setup(name='instana',
4-
version='0.6.10',
4+
version='0.6.11',
55
download_url='https://github.com/instana/python-sensor',
66
url='https://www.instana.com/',
77
license='MIT',

0 commit comments

Comments
 (0)