Skip to content

Commit

Permalink
added environ var to process trace spans based on specific modules (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Hidayathulla Buramdin <[email protected]>
  • Loading branch information
hidayathb and hiburami authored Oct 21, 2024
1 parent 0e14f7c commit 416ed7d
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions otel_extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def __init__(
self.span_name = span_name
self.service_name = service_name
self.span_attributes = span_attributes if span_attributes is not None else {}
self.process_modules = os.environ.get("OTEL_PROCESS_MODULES", "")

def __call__(self, wrapped_function: DecoratedFuncType) -> DecoratedFuncType:
module = inspect.getmodule(wrapped_function)
Expand All @@ -288,20 +289,43 @@ def __call__(self, wrapped_function: DecoratedFuncType) -> DecoratedFuncType:
module_name = module.__name__
span_name = self.span_name or wrapped_function.__qualname__

create_span = True
if hasattr(wrapped_function, '__module__'):
func_module_path = wrapped_function.__module__
self.span_attributes['module.name'] = func_module_path
create_span = self._module_allowed(func_module_path)

@wraps(wrapped_function)
def new_f(*args: Any, **kwargs: Any) -> Any:
with get_tracer(module_name, service_name=self.service_name).start_as_current_span(span_name) as span:
span.set_attributes(self.span_attributes)
if create_span:
with get_tracer(module_name, service_name=self.service_name).start_as_current_span(span_name) as span:
span.set_attributes(self.span_attributes)
return wrapped_function(*args, **kwargs)
else:
return wrapped_function(*args, **kwargs)

@wraps(wrapped_function)
async def new_f_async(*args: Any, **kwargs: Any) -> Any:
with get_tracer(module_name, service_name=self.service_name).start_as_current_span(span_name) as span:
span.set_attributes(self.span_attributes)
if create_span:
with get_tracer(module_name, service_name=self.service_name).start_as_current_span(span_name) as span:
span.set_attributes(self.span_attributes)
return await wrapped_function(*args, **kwargs)
else:
return await wrapped_function(*args, **kwargs)

return cast(DecoratedFuncType, new_f_async) if is_async else cast(DecoratedFuncType, new_f)

def _module_allowed(self, func_module_path):
if self.process_modules is None or self.process_modules == "":
return True

modules = self.process_modules.split(",")
module_path = str(func_module_path).lower()
for mod in modules:
if str(mod).lower() in module_path:
return True
return False


@overload
def instrumented(wrapped_function: DecoratedFuncType) -> DecoratedFuncType:
Expand Down

0 comments on commit 416ed7d

Please sign in to comment.