Skip to content

Commit 67502b1

Browse files
committed
implemented OpenTracing traces. Needs polishing, safeguards and Python3 compatibility. Happy tho
1 parent 11a0b35 commit 67502b1

File tree

11 files changed

+144
-19
lines changed

11 files changed

+144
-19
lines changed

example/simple.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
import sys
22
import instana.sensor as s
33
import instana.options as o
4-
import logging as l
4+
import logging
5+
import opentracing as ot
6+
import instana.tracer
7+
import time
8+
import instana.data as d
9+
import instana.http as h
510

611
def main(argv):
7-
sensor = s.Sensor(o.Options(service='python-simple',
8-
log_level=l.DEBUG))
12+
instana.tracer.init(o.Options(service='python-simple',
13+
log_level=logging.DEBUG))
14+
15+
while (True):
16+
simple()
17+
18+
def simple():
19+
parent_span = ot.tracer.start_span(operation_name="parent")
20+
parent_span.log_kv({"type": h.HTTP_SERVER,
21+
"data": d.Data(http=h.HttpData(host="localhost",
22+
url="/python/simple/one",
23+
status=200,
24+
method="GET"))})
25+
child_span = ot.tracer.start_span(operation_name="child", child_of=parent_span)
26+
child_span.log_kv({"type": h.HTTP_CLIENT,
27+
"data": d.Data(http=h.HttpData(host="localhost",
28+
url="/python/simple/two",
29+
status=204,
30+
method="POST"))})
31+
child_span.set_baggage_item("someBaggage", "someValue")
32+
time.sleep(.45)
33+
child_span.finish()
34+
time.sleep(.55)
35+
parent_span.finish()
936

1037
if __name__ == "__main__":
1138
main(sys.argv)

instana/agent.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import urllib2
44
import instana.log as l
55
import instana.fsm as f
6+
import instana.agent_const as a
67

78
class From(object):
89
pid = ""
@@ -19,15 +20,8 @@ def get_method(self):
1920
return "PUT"
2021

2122
class Agent(object):
22-
AGENT_DISCOVERY_URL = "/com.instana.plugin.python.discovery"
23-
AGENT_TRACES_URL = "/com.instana.plugin.python/traces."
24-
AGENT_DATA_URL = "/com.instana.plugin.python."
25-
AGENT_DEFAULT_HOST = "localhost"
26-
AGENT_DEFAULT_PORT = 42699
27-
AGENT_HEADER = "Instana Agent"
28-
2923
sensor = None
30-
host = AGENT_DEFAULT_HOST
24+
host = a.AGENT_DEFAULT_HOST
3125
fsm = None
3226
from_ = None
3327

@@ -100,7 +94,7 @@ def make_url(self, prefix):
10094
def make_host_url(self, host, prefix):
10195
port = self.sensor.options.agent_port
10296
if port == 0:
103-
port = self.AGENT_DEFAULT_PORT
97+
port = a.AGENT_DEFAULT_PORT
10498

10599
return self.make_full_url(host, port, prefix)
106100

instana/agent_const.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
AGENT_DISCOVERY_URL = "/com.instana.plugin.python.discovery"
2+
AGENT_TRACES_URL = "/com.instana.plugin.python/traces."
3+
AGENT_DATA_URL = "/com.instana.plugin.python."
4+
AGENT_DEFAULT_HOST = "localhost"
5+
AGENT_DEFAULT_PORT = 42699
6+
AGENT_HEADER = "Instana Agent"

instana/data.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Data(object):
2+
http = None
3+
service = None
4+
5+
def __init__(self, **kwds):
6+
self.__dict__.update(kwds)

instana/fsm.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import threading as t
55
import fysom as f
66
import instana.log as l
7+
import instana.agent_const as a
78

89
class Discovery(object):
910
pid = 0
@@ -46,15 +47,15 @@ def lookup_agent_host(self, e):
4647
if self.agent.sensor.options.agent_host != "":
4748
host = self.agent.sensor.options.agent_host
4849
else:
49-
host = self.agent.AGENT_DEFAULT_HOST
50+
host = a.AGENT_DEFAULT_HOST
5051

5152
h = self.check_host(host)
52-
if h == self.agent.AGENT_HEADER:
53+
if h == a.AGENT_HEADER:
5354
self.agent.set_host(host)
5455
self.fsm.lookup()
5556
else:
5657
self.check_host(self.get_default_gateway())
57-
if h == self.agent.AGENT_HEADER:
58+
if h == a.AGENT_HEADER:
5859
self.agent.set_host(host)
5960
self.fsm.lookup()
6061

@@ -78,7 +79,7 @@ def announce_sensor(self, e):
7879
name=sys.executable,
7980
args=sys.argv[0:])
8081

81-
(b, h) = self.agent.request_response(self.agent.make_url(self.agent.AGENT_DISCOVERY_URL), "PUT", d)
82+
(b, h) = self.agent.request_response(self.agent.make_url(a.AGENT_DISCOVERY_URL), "PUT", d)
8283
if not b:
8384
l.error("Cannot announce sensor. Scheduling retry.")
8485
self.schedule_retry(self.announce_sensor, e)
@@ -92,7 +93,7 @@ def schedule_retry(self, f, e):
9293
def test_agent(self, e):
9394
l.debug("testing communication with the agent")
9495

95-
(b, h) = self.agent.head(self.agent.make_url(self.agent.AGENT_DATA_URL))
96+
(b, h) = self.agent.head(self.agent.make_url(a.AGENT_DATA_URL))
9697

9798
if not b:
9899
self.schedule_retry(self.test_agent, e)

instana/http.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
HTTP_CLIENT = "g.hc"
2+
HTTP_SERVER = "g.http"
3+
4+
class HttpData(object):
5+
host = None
6+
url = None
7+
status = 0
8+
method = None
9+
10+
def __init__(self, **kwds):
11+
self.__dict__.update(kwds)

instana/meter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import gc
77
import sys
8+
import instana.agent_const as a
89

910
class Snapshot(object):
1011
name = None
@@ -94,7 +95,7 @@ def process(self):
9495
d = EntityData(pid=os.getpid(), snapshot=s, metrics=m)
9596

9697
thread.start_new_thread(self.sensor.agent.request,
97-
(self.sensor.agent.make_url(self.sensor.agent.AGENT_DATA_URL), "POST", d))
98+
(self.sensor.agent.make_url(a.AGENT_DATA_URL), "POST", d))
9899

99100
self.tick()
100101

instana/recorder.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from basictracer import Sampler, SpanRecorder
2+
import instana.log as l
3+
import instana.agent_const as a
4+
import thread
5+
import time
6+
7+
class InstanaSpan(object):
8+
t = 0
9+
p = None
10+
s = 0
11+
ts = 0
12+
d = 0
13+
n = None
14+
f = None
15+
data = None
16+
17+
def __init__(self, **kwds):
18+
self.__dict__.update(kwds)
19+
20+
class InstanaRecorder(SpanRecorder):
21+
sensor = None
22+
23+
def __init__(self, sensor):
24+
super(InstanaRecorder, self).__init__()
25+
self.sensor = sensor
26+
27+
def record_span(self, span):
28+
if self.sensor.agent.can_send():
29+
data = self.get_span_log_field(span, "data")
30+
if not data.service:
31+
data.service = self.sensor.service_name
32+
33+
thread.start_new_thread(self.sensor.agent.request,
34+
(self.sensor.agent.make_url(a.AGENT_TRACES_URL), "POST",
35+
[InstanaSpan(t=span.context.trace_id,
36+
p=span.parent_id,
37+
s=span.context.span_id,
38+
ts=int(round(span.start_time * 1000)),
39+
d=int(round(span.duration * 1000)),
40+
n=self.get_string_span_log_field(span, "type"),
41+
f=self.sensor.agent.from_,
42+
data=data)]))
43+
44+
def get_string_span_log_field(self, span, field):
45+
ret = self.get_span_log_field(span, field)
46+
if not ret:
47+
return ""
48+
49+
return ret
50+
51+
def get_span_log_field(self, span, field):
52+
for e in span.logs:
53+
if field in e.key_values:
54+
return e.key_values[field]
55+
56+
return None
57+
58+
class InstanaSampler(Sampler):
59+
def sampled(self, trace_id):
60+
return False

instana/tracer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from basictracer import BasicTracer
2+
import instana.recorder as r
3+
import opentracing
4+
import instana.options as o
5+
import instana.sensor as s
6+
7+
class InstanaTracer(BasicTracer):
8+
sensor = None
9+
10+
def __init__(self, options=o.Options()):
11+
self.sensor = s.Sensor(options)
12+
super(InstanaTracer, self).__init__(r.InstanaRecorder(self.sensor), r.InstanaSampler())
13+
14+
def init(options):
15+
opentracing.tracer = InstanaTracer(options)

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
nose>=1.0
22
fysom>=2.1.2
3+
opentracing>=1.2.1
4+
basictracer>=2.2.0

0 commit comments

Comments
 (0)