Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit a0e2330

Browse files
Louis Yecopybara-github
authored andcommitted
Supports breakpoint canary in GCP
The breakpoint canary feature is controlled using the new 'breakpoint_enable_canary' and 'breakpoint_allow_canary_override' parameters when calling enable() or the same-name flags when run as a module. PiperOrigin-RevId: 315558797 Change-Id: I13c7a2077055ecfb6e8c28c726a8315b60ce505e
1 parent d3bf11d commit a0e2330

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,9 @@ Default Credentials](https://cloud.google.com/docs/authentication/production)
271271
which are automatically available on machines hosted on GCP, or can be set via
272272
`gcloud auth application-default login` or the `GOOGLE_APPLICATION_CREDENTIALS`
273273
environment variable.
274+
275+
`breakpoint_enable_canary`: Whether to enable the
276+
[breakpoint canary feature](https://cloud.google.com/debugger/docs/using/snapshots#with_canarying).
277+
It expects a boolean value (`True`/`False`) or a string, with `'True'`
278+
interpreted as `True` and any other string interpreted as `False`). If not
279+
provided, the breakpoint canarying will not be enabled.

src/googleclouddebugger/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def _StartDebugger():
7272
_flags.get('project_id'),
7373
_flags.get('project_number'),
7474
_flags.get('service_account_json_file'))
75+
_hub_client.SetupCanaryMode(
76+
_flags.get('breakpoint_enable_canary'),
77+
_flags.get('breakpoint_allow_canary_override'))
7578
_hub_client.InitializeDebuggeeLabels(_flags)
7679
_hub_client.Start()
7780

src/googleclouddebugger/gcp_hub_client.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@
7575
# of 40 seconds.
7676
_HTTP_TIMEOUT_SECONDS = 100
7777

78+
# The map from the values of flags (breakpoint_enable_canary,
79+
# breakpoint_allow_canary_override) to canary mode.
80+
_CANARY_MODE_MAP = {
81+
(True, True): 'CANARY_MODE_DEFAULT_ENABLED',
82+
(True, False): 'CANARY_MODE_ALWAYS_ENABLED',
83+
(False, True): 'CANARY_MODE_DEFAULT_DISABLED',
84+
(False, False): 'CANARY_MODE_ALWAYS_DISABLED',
85+
}
86+
7887

7988
class NoProjectIdError(Exception):
8089
"""Used to indicate the project id cannot be determined."""
@@ -101,6 +110,8 @@ def __init__(self):
101110
self._debuggee_labels = {}
102111
self._service_account_auth = False
103112
self._debuggee_id = None
113+
self._agent_id = None
114+
self._canary_mode = None
104115
self._wait_token = 'init'
105116
self._breakpoints = []
106117
self._main_thread = None
@@ -218,6 +229,21 @@ def SetupAuth(self,
218229
self._project_id = project_id
219230
self._project_number = project_number or project_id
220231

232+
def SetupCanaryMode(self, breakpoint_enable_canary,
233+
breakpoint_allow_canary_override):
234+
"""Sets up canaryMode for the debuggee according to input parameters.
235+
236+
Args:
237+
breakpoint_enable_canary: str or bool, whether to enable breakpoint
238+
canary. Any string except 'True' is interpreted as False.
239+
breakpoint_allow_canary_override: str or bool, whether to allow the
240+
individually set breakpoint to override the canary behavior. Any
241+
string except 'True' is interpreted as False.
242+
"""
243+
enable_canary = breakpoint_enable_canary in ('True', True)
244+
allow_canary_override = breakpoint_allow_canary_override in ('True', True)
245+
self._canary_mode = _CANARY_MODE_MAP[enable_canary, allow_canary_override]
246+
221247
def Start(self):
222248
"""Starts the worker thread."""
223249
self._shutdown = False
@@ -327,8 +353,11 @@ def _RegisterDebuggee(self, service):
327353
self._project_number = project_number or self._project_number
328354

329355
self._debuggee_id = response['debuggee']['id']
330-
native.LogInfo('Debuggee registered successfully, ID: %s' % (
331-
self._debuggee_id))
356+
self._agent_id = response['agentId']
357+
native.LogInfo(
358+
'Debuggee registered successfully, ID: %s, agent ID: %s, '
359+
'canary mode: %s' % (self._debuggee_id, self._agent_id,
360+
response['debuggee'].get('canaryMode')))
332361
self.register_backoff.Succeeded()
333362
return (False, 0) # Proceed immediately to list active breakpoints.
334363
except BaseException:
@@ -355,7 +384,9 @@ def _ListActiveBreakpoints(self, service):
355384
"""
356385
try:
357386
response = service.debuggees().breakpoints().list(
358-
debuggeeId=self._debuggee_id, waitToken=self._wait_token,
387+
debuggeeId=self._debuggee_id,
388+
agentId=self._agent_id,
389+
waitToken=self._wait_token,
359390
successOnTimeout=True).execute()
360391
if not response.get('waitExpired'):
361392
self._wait_token = response.get('nextWaitToken')
@@ -469,6 +500,7 @@ def _GetDebuggee(self):
469500
'description': self._GetDebuggeeDescription(),
470501
'labels': self._debuggee_labels,
471502
'agentVersion': agent_version,
503+
'canaryMode': self._canary_mode,
472504
}
473505

474506
source_context = self._ReadAppJsonFile('source-context.json')

0 commit comments

Comments
 (0)