@@ -85,7 +85,7 @@ def add_field(self, name, value):
85
85
86
86
`beeline.add_field("my field", "my value")`
87
87
88
- If a field is being attributed to the wrong span/event,
88
+ If a field is being attributed to the wrong span/event,
89
89
make sure that `new_event` and `close_event` calls are matched.
90
90
'''
91
91
# fetch the current event from our tracer
@@ -113,7 +113,7 @@ def tracer(self, name, trace_id=None, parent_id=None):
113
113
return self .tracer_impl (name = name , trace_id = trace_id , parent_id = parent_id )
114
114
115
115
def new_event (self , data = None , trace_name = '' ):
116
- ''' DEPRECATED: Helper method that wraps `start_trace` and
116
+ ''' DEPRECATED: Helper method that wraps `start_trace` and
117
117
`start_span`. It is better to use these methods as it provides
118
118
better control and context around how traces are implemented in your
119
119
app.
@@ -164,6 +164,15 @@ def send_all(self):
164
164
self .tracer_impl .finish_span (span )
165
165
span = self .tracer_impl .get_active_span ()
166
166
167
+ def traced (self , name , trace_id = None , parent_id = None ):
168
+ def wrapped (fn , * args , ** kwargs ):
169
+ def inner (* args , ** kwargs ):
170
+ with self .tracer (name = name , trace_id = trace_id , parent_id = parent_id ):
171
+ return fn (* args , ** kwargs )
172
+ return inner
173
+
174
+ return wrapped
175
+
167
176
def _run_hooks_and_send (self , ev ):
168
177
''' internal - run any defined hooks on the event and send '''
169
178
presampled = False
@@ -308,7 +317,7 @@ def add_context_field(name, value):
308
317
309
318
def remove_context_field (name ):
310
319
''' Remove a single field from the current span.
311
-
320
+
312
321
```
313
322
beeline.add_context({ "first_field": "a", "second_field": "b"})
314
323
beeline.remove_context_field("second_field")
@@ -323,7 +332,7 @@ def remove_context_field(name):
323
332
def add_trace_field (name , value ):
324
333
''' Similar to `add_context_field` - adds a field to the current span, but
325
334
also to all other future spans in this trace. Trace context fields will be
326
- propagated to downstream services if using instrumented libraries
335
+ propagated to downstream services if using instrumented libraries
327
336
like `requests`.
328
337
329
338
Args:
@@ -334,8 +343,8 @@ def add_trace_field(name, value):
334
343
_GBL .tracer_impl .add_trace_field (name = name , value = value )
335
344
336
345
def remove_trace_field (name ):
337
- ''' Removes a trace context field from the current span. This will not
338
- affect other existing spans, but will prevent the field from being
346
+ ''' Removes a trace context field from the current span. This will not
347
+ affect other existing spans, but will prevent the field from being
339
348
propagated to new spans.
340
349
341
350
Args:
@@ -446,7 +455,7 @@ def marshal_trace_context():
446
455
447
456
448
457
def new_event (data = None , trace_name = '' ):
449
- ''' DEPRECATED: Helper method that wraps `start_trace` and
458
+ ''' DEPRECATED: Helper method that wraps `start_trace` and
450
459
`start_span`. It is better to use these methods as it provides
451
460
better control and context around how traces are implemented in your
452
461
app.
@@ -464,7 +473,7 @@ def new_event(data=None, trace_name=''):
464
473
'''
465
474
if _GBL :
466
475
_GBL .new_event (data = data , trace_name = trace_name )
467
-
476
+
468
477
def send_event ():
469
478
''' DEPRECATED: Sends the currently active event (current span),
470
479
if it exists.
@@ -508,3 +517,41 @@ def close():
508
517
_GBL .close ()
509
518
510
519
_GBL = None
520
+
521
+ def traced (name , trace_id = None , parent_id = None ):
522
+ '''
523
+ Function decorator to wrap an entire function in a trace span. If no trace
524
+ is active in the current thread, starts a new trace, and the wrapping span
525
+ will be a root span. If a trace is active, creates a child span of the
526
+ existing trace.
527
+
528
+ Example use:
529
+
530
+ ```
531
+ @traced(name="my_expensive_function")
532
+ def my_func(n):
533
+ recursive_fib(n)
534
+
535
+ my_func(100)
536
+ ```
537
+
538
+ Args:
539
+ - `name`: a descriptive name for the this trace span, i.e. "function_name". This is required.
540
+ - `trace_id`: the trace_id to use. If None, will be automatically generated.
541
+ Use this if you want to explicitly resume a trace in this application that was
542
+ initiated in another application, and you have the upstream trace_id.
543
+ - `parent_id`: If trace_id is set, will populate the root span's parent
544
+ with this id.
545
+ '''
546
+
547
+ _beeline = get_beeline ()
548
+ if not _beeline :
549
+ # just pass through if not initialized
550
+ def wrapped (fn , * args , ** kwargs ):
551
+ def inner (* args , ** kwargs ):
552
+ return fn (* args , ** kwargs )
553
+ return inner
554
+
555
+ return wrapped
556
+
557
+ return _beeline .traced (name , trace_id , parent_id )
0 commit comments