-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathsample_app.py
More file actions
114 lines (85 loc) · 3.17 KB
/
sample_app.py
File metadata and controls
114 lines (85 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
An example of how the Datadog python tracing lib can interact
with code.
"""
# stdlib
import logging
import random
import time
# 3p
import sqlite3
# tracer
from ddtrace import tracer
from ddtrace.contrib.sqlite3 import connection_factory
from ddtrace.ext import net as netx
logging.basicConfig()
db = None
def init_database():
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS stocks
(date text, trans text, symbol text, qty real, price real)
""")
cursor.execute("DELETE FROM stocks")
db.commit()
# the decorator will set the span name to the name of your func
@tracer.wrap(name='request', service='sample-app', resource='db_calls')
def trace_db_calls():
print("trying trace_db_calls")
# this will create a first span for that query
cursor = db.cursor()
query = "INSERT INTO stocks VALUES ('2006-01-05', 'BUY', 'RHAT', 100, 35.14)"
cursor.execute(query)
db.commit()
# a new span will be created for that other call
cursor = db.cursor()
cursor.execute("SELECT * FROM stocks WHERE symbol=?", ('RHAT',))
return "returning result (%s)" % str(cursor.fetchone())
@tracer.wrap(name='request', service='sample-app', resource='ctx_manager')
def trace_ctx_manager():
print("trying trace_ctx_manager")
# some complicated work
def fibo(n):
from math import sqrt
return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))
# Use the context manager to create a span from a block code easily
with tracer.trace("compute.fibo") as span:
result = fibo(100)
# set metadata about your span anytime
span.set_tag("fibo.compute_method", "golden ratio")
span.set_tag("fibo.result", result)
return "returning result (%d)" % result
@tracer.wrap(name='request', service='sample-app', resource='rpc')
def trace_rpc():
print("trying trace_rpc")
# to allow tracing across apps/hosts you need to pass
# as metadata to your remote call
# * the trace ID (and the other side needs to set it as its trace ID as well)
# * the current span ID (and the other side needs to set it as its parent ID)
span = tracer.current_span()
# use the ext modules to get "standard" metadata keys
span.set_tag(netx.TARGET_HOST, "remote.server.datadoghq.com")
span.set_tag(netx.TARGET_PORT, 4242)
return rpc_response("hello", trace_id=span.trace_id, parent_id=span.span_id)
def rpc_response(request, trace_id=None, parent_id=None):
""" would live typically on another host"""
with tracer.trace('rpc.server') as span:
if trace_id and parent_id:
span.trace_id = trace_id
span.parent_id = parent_id
return "returning result (%s)" % request
def run():
# use an integration to trace by default all of our DB calls
traced_factory = connection_factory(tracer, service="master-db")
global db
db = sqlite3.connect(":memory:", factory=traced_factory)
init_database()
funcs = [trace_db_calls, trace_ctx_manager, trace_rpc]
try:
while True:
print(random.choice(funcs)())
time.sleep(1)
except KeyboardInterrupt:
print "exiting"
if __name__ == '__main__':
run()