Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added partial typing for apis #593

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/scout_apm/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import scout_apm.core
from scout_apm.compat import ContextDecorator, text
from scout_apm.core.config import ScoutConfig
from scout_apm.core.tracked_request import TrackedRequest
from scout_apm.core.tracked_request import TrackedRequest, Span

try:
# for typing
from typing import Any, Optional, Dict, Type, Callable
except ImportError:
pass

__all__ = [
"BackgroundTransaction",
Expand All @@ -19,6 +25,7 @@
class Context(object):
@classmethod
def add(self, key, value):
# type: (str, Any) -> None
"""Adds context to the currently executing request.

:key: Any String identifying the request context.
Expand All @@ -34,34 +41,39 @@ class Config(ScoutConfig):
pass


install = scout_apm.core.install
install = scout_apm.core.install # type: Callable[[Optional[Dict[str, Any]]], bool]


def ignore_transaction():
# type: () -> None
TrackedRequest.instance().tag("ignore_transaction", True)


class instrument(ContextDecorator):
def __init__(self, operation, kind="Custom", tags=None):
# type: (str, str, Optional[Dict[str, Any]]) -> None
self.operation = text(kind) + "/" + text(operation)
if tags is None:
self.tags = {}
else:
self.tags = tags

def __enter__(self):
# type: () -> instrument
tracked_request = TrackedRequest.instance()
self.span = tracked_request.start_span(operation=self.operation)
for key, value in self.tags.items():
self.tag(key, value)
return self

def __exit__(self, *exc):
# (Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]) -> Optional[bool]
tracked_request = TrackedRequest.instance()
tracked_request.stop_span()
return False

def tag(self, key, value):
# type: (str, Any) -> None
if self.span is not None:
self.span.tag(key, value)

Expand Down Expand Up @@ -115,22 +127,27 @@ def tag(self, key, value):
class WebTransaction(Transaction):
@classmethod
def start(cls, name, tags=None):
# type: (str, Optional[Dict[str, Any]]) -> None
super(WebTransaction, cls).start("Controller", text(name), tags)

def __enter__(self):
# type: () -> None
super(WebTransaction, self).start("Controller", self.name, self.tags)


class BackgroundTransaction(Transaction):
@classmethod
def start(cls, name, tags=None):
# type: (str, Optional[Dict[str, Any]]) -> None
super(BackgroundTransaction, cls).start("Job", text(name), tags)

def __enter__(self):
# type: () -> None
super(BackgroundTransaction, self).start("Job", self.name, self.tags)


def rename_transaction(name):
# type: (str) -> None
if name is not None:
tracked_request = TrackedRequest.instance()
tracked_request.tag("transaction.name", name)