Coverage for src/couchers/models/logging.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-08 00:20 +0000

1from sqlalchemy import BigInteger, Boolean, Column, DateTime, Float, String, func 

2from sqlalchemy import LargeBinary as Binary 

3from sqlalchemy.sql import expression 

4 

5from couchers.config import config 

6from couchers.models.base import Base 

7 

8 

9class APICall(Base): 

10 """ 

11 API call logs 

12 """ 

13 

14 __tablename__ = "api_calls" 

15 __table_args__ = {"schema": "logging"} 

16 

17 id = Column(BigInteger, primary_key=True) 

18 

19 # whether the call was made using an api key or session cookies 

20 is_api_key = Column(Boolean, nullable=False, server_default=expression.false()) 

21 

22 # backend version (normally e.g. develop-31469e3), allows us to figure out which proto definitions were used 

23 # note that `default` is a python side default, not hardcoded into DB schema 

24 version = Column(String, nullable=False, default=config["VERSION"]) 

25 

26 # approximate time of the call 

27 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

28 

29 # the method call name, e.g. "/org.couchers.api.core.API/ListFriends" 

30 method = Column(String, nullable=False) 

31 

32 # gRPC status code name, e.g. FAILED_PRECONDITION, None if success 

33 status_code = Column(String, nullable=True) 

34 

35 # handler duration (excluding serialization, etc) 

36 duration = Column(Float, nullable=False) 

37 

38 # user_id of caller, None means not logged in 

39 user_id = Column(BigInteger, nullable=True) 

40 

41 # sanitized request bytes 

42 request = Column(Binary, nullable=True) 

43 

44 # sanitized response bytes 

45 response = Column(Binary, nullable=True) 

46 

47 # whether response bytes have been truncated 

48 response_truncated = Column(Boolean, nullable=False, server_default=expression.false()) 

49 

50 # the exception traceback, if any 

51 traceback = Column(String, nullable=True) 

52 

53 # human readable perf report 

54 perf_report = Column(String, nullable=True) 

55 

56 # details of the browser, if available 

57 ip_address = Column(String, nullable=True) 

58 user_agent = Column(String, nullable=True)