gcp.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import functools
  2. import sys
  3. from copy import deepcopy
  4. from datetime import datetime, timedelta, timezone
  5. from os import environ
  6. import sentry_sdk
  7. from sentry_sdk.api import continue_trace
  8. from sentry_sdk.consts import OP
  9. from sentry_sdk.integrations import Integration
  10. from sentry_sdk.integrations._wsgi_common import _filter_headers
  11. from sentry_sdk.scope import should_send_default_pii
  12. from sentry_sdk.tracing import TransactionSource
  13. from sentry_sdk.utils import (
  14. AnnotatedValue,
  15. capture_internal_exceptions,
  16. event_from_exception,
  17. logger,
  18. TimeoutThread,
  19. reraise,
  20. )
  21. from typing import TYPE_CHECKING
  22. # Constants
  23. TIMEOUT_WARNING_BUFFER = 1.5 # Buffer time required to send timeout warning to Sentry
  24. MILLIS_TO_SECONDS = 1000.0
  25. if TYPE_CHECKING:
  26. from typing import Any
  27. from typing import TypeVar
  28. from typing import Callable
  29. from typing import Optional
  30. from sentry_sdk._types import EventProcessor, Event, Hint
  31. F = TypeVar("F", bound=Callable[..., Any])
  32. def _wrap_func(func):
  33. # type: (F) -> F
  34. @functools.wraps(func)
  35. def sentry_func(functionhandler, gcp_event, *args, **kwargs):
  36. # type: (Any, Any, *Any, **Any) -> Any
  37. client = sentry_sdk.get_client()
  38. integration = client.get_integration(GcpIntegration)
  39. if integration is None:
  40. return func(functionhandler, gcp_event, *args, **kwargs)
  41. configured_time = environ.get("FUNCTION_TIMEOUT_SEC")
  42. if not configured_time:
  43. logger.debug(
  44. "The configured timeout could not be fetched from Cloud Functions configuration."
  45. )
  46. return func(functionhandler, gcp_event, *args, **kwargs)
  47. configured_time = int(configured_time)
  48. initial_time = datetime.now(timezone.utc)
  49. with sentry_sdk.isolation_scope() as scope:
  50. with capture_internal_exceptions():
  51. scope.clear_breadcrumbs()
  52. scope.add_event_processor(
  53. _make_request_event_processor(
  54. gcp_event, configured_time, initial_time
  55. )
  56. )
  57. scope.set_tag("gcp_region", environ.get("FUNCTION_REGION"))
  58. timeout_thread = None
  59. if (
  60. integration.timeout_warning
  61. and configured_time > TIMEOUT_WARNING_BUFFER
  62. ):
  63. waiting_time = configured_time - TIMEOUT_WARNING_BUFFER
  64. timeout_thread = TimeoutThread(waiting_time, configured_time)
  65. # Starting the thread to raise timeout warning exception
  66. timeout_thread.start()
  67. headers = {}
  68. if hasattr(gcp_event, "headers"):
  69. headers = gcp_event.headers
  70. transaction = continue_trace(
  71. headers,
  72. op=OP.FUNCTION_GCP,
  73. name=environ.get("FUNCTION_NAME", ""),
  74. source=TransactionSource.COMPONENT,
  75. origin=GcpIntegration.origin,
  76. )
  77. sampling_context = {
  78. "gcp_env": {
  79. "function_name": environ.get("FUNCTION_NAME"),
  80. "function_entry_point": environ.get("ENTRY_POINT"),
  81. "function_identity": environ.get("FUNCTION_IDENTITY"),
  82. "function_region": environ.get("FUNCTION_REGION"),
  83. "function_project": environ.get("GCP_PROJECT"),
  84. },
  85. "gcp_event": gcp_event,
  86. }
  87. with sentry_sdk.start_transaction(
  88. transaction, custom_sampling_context=sampling_context
  89. ):
  90. try:
  91. return func(functionhandler, gcp_event, *args, **kwargs)
  92. except Exception:
  93. exc_info = sys.exc_info()
  94. sentry_event, hint = event_from_exception(
  95. exc_info,
  96. client_options=client.options,
  97. mechanism={"type": "gcp", "handled": False},
  98. )
  99. sentry_sdk.capture_event(sentry_event, hint=hint)
  100. reraise(*exc_info)
  101. finally:
  102. if timeout_thread:
  103. timeout_thread.stop()
  104. # Flush out the event queue
  105. client.flush()
  106. return sentry_func # type: ignore
  107. class GcpIntegration(Integration):
  108. identifier = "gcp"
  109. origin = f"auto.function.{identifier}"
  110. def __init__(self, timeout_warning=False):
  111. # type: (bool) -> None
  112. self.timeout_warning = timeout_warning
  113. @staticmethod
  114. def setup_once():
  115. # type: () -> None
  116. import __main__ as gcp_functions
  117. if not hasattr(gcp_functions, "worker_v1"):
  118. logger.warning(
  119. "GcpIntegration currently supports only Python 3.7 runtime environment."
  120. )
  121. return
  122. worker1 = gcp_functions.worker_v1
  123. worker1.FunctionHandler.invoke_user_function = _wrap_func(
  124. worker1.FunctionHandler.invoke_user_function
  125. )
  126. def _make_request_event_processor(gcp_event, configured_timeout, initial_time):
  127. # type: (Any, Any, Any) -> EventProcessor
  128. def event_processor(event, hint):
  129. # type: (Event, Hint) -> Optional[Event]
  130. final_time = datetime.now(timezone.utc)
  131. time_diff = final_time - initial_time
  132. execution_duration_in_millis = time_diff / timedelta(milliseconds=1)
  133. extra = event.setdefault("extra", {})
  134. extra["google cloud functions"] = {
  135. "function_name": environ.get("FUNCTION_NAME"),
  136. "function_entry_point": environ.get("ENTRY_POINT"),
  137. "function_identity": environ.get("FUNCTION_IDENTITY"),
  138. "function_region": environ.get("FUNCTION_REGION"),
  139. "function_project": environ.get("GCP_PROJECT"),
  140. "execution_duration_in_millis": execution_duration_in_millis,
  141. "configured_timeout_in_seconds": configured_timeout,
  142. }
  143. extra["google cloud logs"] = {
  144. "url": _get_google_cloud_logs_url(final_time),
  145. }
  146. request = event.get("request", {})
  147. request["url"] = "gcp:///{}".format(environ.get("FUNCTION_NAME"))
  148. if hasattr(gcp_event, "method"):
  149. request["method"] = gcp_event.method
  150. if hasattr(gcp_event, "query_string"):
  151. request["query_string"] = gcp_event.query_string.decode("utf-8")
  152. if hasattr(gcp_event, "headers"):
  153. request["headers"] = _filter_headers(gcp_event.headers)
  154. if should_send_default_pii():
  155. if hasattr(gcp_event, "data"):
  156. request["data"] = gcp_event.data
  157. else:
  158. if hasattr(gcp_event, "data"):
  159. # Unfortunately couldn't find a way to get structured body from GCP
  160. # event. Meaning every body is unstructured to us.
  161. request["data"] = AnnotatedValue.removed_because_raw_data()
  162. event["request"] = deepcopy(request)
  163. return event
  164. return event_processor
  165. def _get_google_cloud_logs_url(final_time):
  166. # type: (datetime) -> str
  167. """
  168. Generates a Google Cloud Logs console URL based on the environment variables
  169. Arguments:
  170. final_time {datetime} -- Final time
  171. Returns:
  172. str -- Google Cloud Logs Console URL to logs.
  173. """
  174. hour_ago = final_time - timedelta(hours=1)
  175. formatstring = "%Y-%m-%dT%H:%M:%SZ"
  176. url = (
  177. "https://console.cloud.google.com/logs/viewer?project={project}&resource=cloud_function"
  178. "%2Ffunction_name%2F{function_name}%2Fregion%2F{region}&minLogLevel=0&expandAll=false"
  179. "&timestamp={timestamp_end}&customFacets=&limitCustomFacetWidth=true"
  180. "&dateRangeStart={timestamp_start}&dateRangeEnd={timestamp_end}"
  181. "&interval=PT1H&scrollTimestamp={timestamp_end}"
  182. ).format(
  183. project=environ.get("GCP_PROJECT"),
  184. function_name=environ.get("FUNCTION_NAME"),
  185. region=environ.get("FUNCTION_REGION"),
  186. timestamp_end=final_time.strftime(formatstring),
  187. timestamp_start=hour_ago.strftime(formatstring),
  188. )
  189. return url