You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+90-1Lines changed: 90 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Please see the examples directory. Overall, basic usage requires that a tracer g
21
21
import sqlalchemy_opentracing
22
22
23
23
sqlalchemy_opentracing.init_tracing(tracer) # A OpenTracing compatible tracer.
24
-
sqlalchemy_opentracing.register_connectable(engine) # A valid SQLAlchemy Engine object.
24
+
sqlalchemy_opentracing.register_engine(engine) # A valid SQLAlchemy Engine object.
25
25
26
26
with engine.begin() as conn:
27
27
sel = select([users])
@@ -40,6 +40,95 @@ By default, only statements marked to be traced are taken into account (explicit
40
40
sel = select([users])
41
41
conn.execute(sel)
42
42
43
+
The resulting spans will have an operation name related to the sql statement (such as `create-table` or `insert`), and will include exception information (if any), the dialect/backend (such as sqlite), and a few other hints.
44
+
45
+
Tracing under a Connection
46
+
===========================
47
+
48
+
It is possible to trace all statements being executed under a connection's transaction lifetime. For this, instead of marking a statement as traced, the connection is passed to set_traced() or set_parent_span():
Either a commit or a rollback on a connection's transaction will finish its tracing. If the same Connection object is used afterwards, no tracing will be done for it (unless registered for tracing again). When using (emulated) nested transactions, the tracing needs to be marked at top-level transaction time, and tracing will happen for all statements under the nested transactions:
It is also possible to trace all actual SQL statements happening during a Session's execution life time - that is, from being fresh to have its statements executed and committed (or rollbacked). For this, the Session object is passed to set_traced or set_parent_span():
Similar to what happens for Connection, either a commit or a rollback will finish its tracing, and further work on it will not be reported.
96
+
97
+
Tracing raw SQL statements
98
+
==========================
99
+
100
+
Executing raw SQL statements can be done through either a Connection or a Session, through their execute() method. Since there's no way to mark each statement individually, tracing them can be done through either tracing all statements, or through tracing a Connection's transaction or Session:
# this statement will be traced as part of the session's execution
107
+
session.execute('INSERT INTO users VALUES (?, ?)', 1, 'John')
108
+
109
+
110
+
Raw SQL statements will be traced having its operation name as `textclause`, to indicate their explicit text nature.
111
+
112
+
Manually cancel tracing
113
+
=======================
114
+
115
+
Sometimes no commit nor rollback may happen for a Connection or Session (for example, when doing bulk insertion/update). In this case, manually canceling tracing for an object can be done through clear_traced():
# this will generate tracing of a single INSERT statement.
125
+
users = [User(name='User-%s'% i) for i inxrange(100)]
126
+
session.bulk_save_objects(users)
127
+
128
+
sqlalchemy_opentracing.clear_traced(session)
129
+
130
+
Manually canceling tracing will not clear any tracing already done - it will simply stop any further tracing for the current statement, Connection or Session object.
This directory contains examples of tracing applications using the sqlalchemy package. To run the examples, make sure you've installed the packages `opentracing` and `lightstep`. If you have a lightstep token and would like to view the created spans, then uncomment to proper lines under the given examples. If you would like to use a different OpenTracing implementation, you may also replace the lightstep tracer with the tracer of your choice.
0 commit comments