|
18 | 18 | from os import environ |
19 | 19 | from typing import Collection |
20 | 20 | from opentelemetry import context |
21 | | -from wrapt import wrap_function_wrapper as _wrap |
22 | | -from opentelemetry.instrumentation.utils import unwrap |
23 | 21 |
|
24 | 22 | from opentelemetry.instrumentation.instrumentor import BaseInstrumentor |
25 | 23 |
|
|
28 | 26 |
|
29 | 27 | from opentelemetry import trace |
30 | 28 | from opentelemetry.trace import ( |
31 | | - INVALID_SPAN, |
32 | | - INVALID_SPAN_CONTEXT, |
33 | 29 | get_current_span, |
34 | 30 | get_tracer_provider, |
35 | | - get_tracer, |
36 | | - SpanKind |
| 31 | + get_tracer |
37 | 32 | ) |
38 | 33 |
|
39 | | -ATTRIBUTE_THREAD_NAME = "currentthread.name" |
40 | | -DEFAULT_THREAD_NAME = "thread" |
41 | | -ATTRIBUTE_TARGET_NAME = "currenttarget.name" |
42 | | -DEFAULT_TARGET_NAME = "None" |
43 | | - |
44 | | -def _with_tracer_wrapper(func): |
45 | | - """Helper for providing tracer for wrapper functions.""" |
46 | | - |
47 | | - def _with_tracer(tracer): |
48 | | - def wrapper(wrapped, instance, args, kwargs): |
49 | | - return func(tracer, wrapped, instance, args, kwargs) |
50 | | - |
51 | | - return wrapper |
52 | | - |
53 | | - return _with_tracer |
54 | | - |
55 | | -def _wrap_target(ctx, target_func, tracer): |
56 | | - """Helper for providing tracer for wrapper functions.""" |
57 | | - context.attach(ctx) |
58 | | - with tracer.start_as_current_span( |
59 | | - "threading.Thread.target", |
60 | | - kind=SpanKind.INTERNAL, |
61 | | - ) as span: |
62 | | - if span.is_recording(): |
63 | | - span.set_attribute(ATTRIBUTE_TARGET_NAME, target_func.__name__) |
64 | | - return target_func |
65 | | - |
66 | | -@_with_tracer_wrapper |
67 | | -def _wrap_thread(tracer, wrapped, instance, args, kwargs): |
68 | | - """Wrap `Threading.thread`""" |
69 | | - |
70 | | - target_func = kwargs.get("target") |
71 | | - |
72 | | - with tracer.start_as_current_span( |
73 | | - "threading.Thread", |
74 | | - kind=SpanKind.INTERNAL, |
75 | | - ) as span: |
76 | | - if span.is_recording(): |
77 | | - ctx = context.get_current() |
78 | | - kwargs["target"] = _wrap_target(ctx, target_func, tracer) |
79 | | - span.set_attribute(ATTRIBUTE_THREAD_NAME, wrapped.__name__) |
80 | | - return wrapped(*args, **kwargs) |
81 | | - |
82 | 34 | class _InstrumentedThread(threading.Thread): |
83 | 35 | def __init__(self, *args, **kwargs): |
84 | 36 | super().__init__(*args, **kwargs) |
|
0 commit comments