Skip to content

Commit f10ae73

Browse files
committed
feat: add typing on some public api functions
1 parent b930647 commit f10ae73

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

elasticapm/__init__.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from elasticapm.base import Client, get_client # noqa: F401
3333
from elasticapm.conf import setup_logging # noqa: F401
34+
from elasticapm.contrib.asyncio.traces import async_capture_span # noqa: F401 E402
3435
from elasticapm.contrib.serverless import capture_serverless # noqa: F401
3536
from elasticapm.instrumentation.control import instrument, uninstrument # noqa: F401
3637
from elasticapm.traces import ( # noqa: F401
@@ -49,7 +50,30 @@
4950
)
5051
from elasticapm.utils.disttracing import trace_parent_from_headers, trace_parent_from_string # noqa: F401
5152

52-
__all__ = ("VERSION", "Client")
53+
__all__ = (
54+
"VERSION",
55+
"Client",
56+
"get_client",
57+
"setup_logging",
58+
"capture_serverless",
59+
"instrument",
60+
"uninstrument",
61+
"capture_span",
62+
"get_span_id",
63+
"get_trace_id",
64+
"get_trace_parent_header",
65+
"get_transaction_id",
66+
"label",
67+
"set_context",
68+
"set_custom_context",
69+
"set_transaction_name",
70+
"set_transaction_outcome",
71+
"set_transaction_result",
72+
"set_user_context",
73+
"trace_parent_from_headers",
74+
"trace_parent_from_string",
75+
"async_capture_span",
76+
)
5377

5478
_activation_method = None
5579

@@ -64,5 +88,3 @@
6488

6589
if sys.version_info <= (3, 5):
6690
raise DeprecationWarning("The Elastic APM agent requires Python 3.6+")
67-
68-
from elasticapm.contrib.asyncio.traces import async_capture_span # noqa: F401 E402

elasticapm/base.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@
4444
import warnings
4545
from copy import deepcopy
4646
from datetime import timedelta
47-
from typing import Optional, Sequence, Tuple
47+
from types import TracebackType
48+
from typing import Any, Optional, Sequence, Tuple, Type, Union
4849

4950
import elasticapm
5051
from elasticapm.conf import Config, VersionedConfig, constants
5152
from elasticapm.conf.constants import ERROR
5253
from elasticapm.metrics.base_metrics import MetricsRegistry
53-
from elasticapm.traces import DroppedSpan, Tracer, execution_context
54+
from elasticapm.traces import DroppedSpan, Tracer, Transaction, execution_context
5455
from elasticapm.utils import cgroup, cloud, compat, is_master_process, stacks, varmap
5556
from elasticapm.utils.disttracing import TraceParent
5657
from elasticapm.utils.encoding import enforce_label_format, keyword_field, shorten, transform
@@ -261,15 +262,22 @@ def capture(self, event_type, date=None, context=None, custom=None, stack=None,
261262
self.queue(ERROR, data, flush=not handled)
262263
return data["id"]
263264

264-
def capture_message(self, message=None, param_message=None, **kwargs):
265+
def capture_message(self, message: Optional[str] = None, param_message=None, **kwargs: Any) -> str:
265266
"""
266267
Creates an event from ``message``.
267268
268269
>>> client.capture_message('My event just happened!')
269270
"""
270271
return self.capture("Message", message=message, param_message=param_message, **kwargs)
271272

272-
def capture_exception(self, exc_info=None, handled=True, **kwargs):
273+
def capture_exception(
274+
self,
275+
exc_info: Union[
276+
None, bool, Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]]
277+
] = None,
278+
handled: bool = True,
279+
**kwargs: Any
280+
) -> str:
273281
"""
274282
Creates an event from an exception.
275283
@@ -317,7 +325,9 @@ def begin_transaction(
317325
transaction_type, trace_parent=trace_parent, start=start, auto_activate=auto_activate, links=links
318326
)
319327

320-
def end_transaction(self, name=None, result="", duration=None):
328+
def end_transaction(
329+
self, name: Optional[str] = None, result: str = "", duration: Optional[Union[float, timedelta]] = None
330+
) -> Transaction:
321331
"""
322332
End the current transaction.
323333

elasticapm/traces.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31+
import decimal
3132
import functools
3233
import random
3334
import re
@@ -989,10 +990,12 @@ def begin_transaction(
989990
execution_context.set_transaction(transaction)
990991
return transaction
991992

992-
def end_transaction(self, result=None, transaction_name=None, duration=None):
993+
def end_transaction(
994+
self, result: Optional[str] = None, transaction_name: Optional[str] = None, duration: Optional[timedelta] = None
995+
) -> Transaction:
993996
"""
994997
End the current transaction and queue it for sending
995-
:param result: result of the transaction, e.g. "OK" or 200
998+
:param result: result of the transaction, e.g. "OK" or "HTTP 2xx"
996999
:param transaction_name: name of the transaction
9971000
:param duration: override duration, mostly useful for testing
9981001
:return:
@@ -1130,7 +1133,7 @@ def handle_exit(
11301133
logger.debug("ended non-existing span %s of type %s", self.name, self.type)
11311134

11321135

1133-
def label(**labels) -> None:
1136+
def label(**labels: Union[str, bool, int, float, decimal.Decimal]) -> None:
11341137
"""
11351138
Labels current transaction. Keys should be strings, values can be strings, booleans,
11361139
or numerical values (int, float, Decimal)
@@ -1211,43 +1214,43 @@ def set_transaction_outcome(outcome=None, http_status_code=None, override=True)
12111214
transaction.outcome = outcome
12121215

12131216

1214-
def get_transaction_id():
1217+
def get_transaction_id() -> Optional[str]:
12151218
"""
12161219
Returns the current transaction ID
12171220
"""
12181221
transaction = execution_context.get_transaction()
12191222
if not transaction:
1220-
return
1223+
return None
12211224
return transaction.id
12221225

12231226

1224-
def get_trace_parent_header():
1227+
def get_trace_parent_header() -> Optional[str]:
12251228
"""
12261229
Return the trace parent header for the current transaction.
12271230
"""
12281231
transaction = execution_context.get_transaction()
12291232
if not transaction or not transaction.trace_parent:
1230-
return
1233+
return None
12311234
return transaction.trace_parent.to_string()
12321235

12331236

1234-
def get_trace_id():
1237+
def get_trace_id() -> Optional[str]:
12351238
"""
12361239
Returns the current trace ID
12371240
"""
12381241
transaction = execution_context.get_transaction()
12391242
if not transaction:
1240-
return
1243+
return None
12411244
return transaction.trace_parent.trace_id if transaction.trace_parent else None
12421245

12431246

1244-
def get_span_id():
1247+
def get_span_id() -> Optional[str]:
12451248
"""
12461249
Returns the current span ID
12471250
"""
12481251
span = execution_context.get_span()
12491252
if not span:
1250-
return
1253+
return None
12511254
return span.id
12521255

12531256

0 commit comments

Comments
 (0)