Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pridhi-arora committed Feb 16, 2023
1 parent f528051 commit 3242e3b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from os import environ
from typing import Collection
from opentelemetry import context
from wrapt import wrap_function_wrapper as _wrap
from opentelemetry.instrumentation.utils import unwrap

from opentelemetry.instrumentation.instrumentor import BaseInstrumentor

Expand All @@ -28,57 +26,11 @@

from opentelemetry import trace
from opentelemetry.trace import (
INVALID_SPAN,
INVALID_SPAN_CONTEXT,
get_current_span,
get_tracer_provider,
get_tracer,
SpanKind
get_tracer
)

ATTRIBUTE_THREAD_NAME = "currentthread.name"
DEFAULT_THREAD_NAME = "thread"
ATTRIBUTE_TARGET_NAME = "currenttarget.name"
DEFAULT_TARGET_NAME = "None"

def _with_tracer_wrapper(func):
"""Helper for providing tracer for wrapper functions."""

def _with_tracer(tracer):
def wrapper(wrapped, instance, args, kwargs):
return func(tracer, wrapped, instance, args, kwargs)

return wrapper

return _with_tracer

def _wrap_target(ctx, target_func, tracer):
"""Helper for providing tracer for wrapper functions."""
context.attach(ctx)
with tracer.start_as_current_span(
"threading.Thread.target",
kind=SpanKind.INTERNAL,
) as span:
if span.is_recording():
span.set_attribute(ATTRIBUTE_TARGET_NAME, target_func.__name__)
return target_func

@_with_tracer_wrapper
def _wrap_thread(tracer, wrapped, instance, args, kwargs):
"""Wrap `Threading.thread`"""

target_func = kwargs.get("target")

with tracer.start_as_current_span(
"threading.Thread",
kind=SpanKind.INTERNAL,
) as span:
if span.is_recording():
ctx = context.get_current()
kwargs["target"] = _wrap_target(ctx, target_func, tracer)
span.set_attribute(ATTRIBUTE_THREAD_NAME, wrapped.__name__)
return wrapped(*args, **kwargs)

class _InstrumentedThread(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-contrib-instrumentations/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies = [
"opentelemetry-instrumentation-sqlite3==0.37b0.dev",
"opentelemetry-instrumentation-starlette==0.37b0.dev",
"opentelemetry-instrumentation-system-metrics==0.37b0.dev",
"opentelemetry-instrumentation-threading==0.37b0.dev",
"opentelemetry-instrumentation-tornado==0.37b0.dev",
"opentelemetry-instrumentation-tortoiseorm==0.37b0.dev",
"opentelemetry-instrumentation-urllib==0.37b0.dev",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@
"opentelemetry-instrumentation-dbapi==0.37b0.dev",
"opentelemetry-instrumentation-logging==0.37b0.dev",
"opentelemetry-instrumentation-sqlite3==0.37b0.dev",
"opentelemetry-instrumentation-threading==0.37b0.dev",
"opentelemetry-instrumentation-urllib==0.37b0.dev",
"opentelemetry-instrumentation-wsgi==0.37b0.dev",

]

0 comments on commit 3242e3b

Please sign in to comment.