Skip to content

Commit 5763038

Browse files
fix(prevent): Add conditional logging in ow webhook forwarding (#103081)
Make the logging conditional on the feature flag
1 parent a2cfcd5 commit 5763038

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/sentry/overwatch_webhooks/webhook_forwarder.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,35 @@ class OverwatchOrganizationContext:
3939
logger = logging.getLogger("sentry.overwatch_webhook_forwarder")
4040

4141

42+
def verbose_log(msg: str, *, extra: dict | None = None) -> None:
43+
if bool(options.get("overwatch.forward-webhooks.verbose", False)):
44+
logger.info(msg, extra=extra)
45+
46+
4247
class OverwatchGithubWebhookForwarder:
4348
integration: Integration
4449

4550
def __init__(self, integration: Integration):
4651
self.integration = integration
4752

4853
def should_forward_to_overwatch(self, headers: Mapping[str, str]) -> bool:
49-
return headers.get(GITHUB_WEBHOOK_TYPE_HEADER) in GITHUB_EVENTS_TO_FORWARD_OVERWATCH
54+
event_type = headers.get(GITHUB_WEBHOOK_TYPE_HEADER)
55+
verbose_log(
56+
"overwatch.debug.should_forward_to_overwatch.checked",
57+
extra={
58+
"event_type": event_type,
59+
"should_forward": event_type in GITHUB_EVENTS_TO_FORWARD_OVERWATCH,
60+
},
61+
)
62+
return event_type in GITHUB_EVENTS_TO_FORWARD_OVERWATCH
5063

5164
def _get_org_summaries_by_region_for_integration(
5265
self, integration: Integration
5366
) -> dict[str, list[OrganizationSummary]]:
5467
org_integrations = OrganizationIntegration.objects.filter(
5568
integration=integration, status=ObjectStatus.ACTIVE
5669
)
57-
logger.info(
70+
verbose_log(
5871
"overwatch.debug.org_integrations.fetched",
5972
extra={
6073
"integration_id": integration.id,
@@ -64,7 +77,7 @@ def _get_org_summaries_by_region_for_integration(
6477
)
6578
organization_ids = [org_integration.organization_id for org_integration in org_integrations]
6679
org_mappings = OrganizationMapping.objects.filter(organization_id__in=organization_ids)
67-
logger.info(
80+
verbose_log(
6881
"overwatch.debug.org_mappings.fetched",
6982
extra={
7083
"org_mapping_ids": [om.organization_id for om in org_mappings],
@@ -87,15 +100,15 @@ def _get_org_summaries_by_region_for_integration(
87100
organization_mapping=org_mapping,
88101
)
89102
)
90-
logger.info(
103+
verbose_log(
91104
"overwatch.debug.organizations.grouped_by_region",
92105
extra={
93106
"region_name": region_name,
94107
"org_id": org_integration.organization_id,
95108
},
96109
)
97110

98-
logger.info(
111+
verbose_log(
99112
"overwatch.debug.org_contexts_by_region.final",
100113
extra={
101114
"regions": list(org_contexts_by_region.keys()),
@@ -116,7 +129,7 @@ def forward_if_applicable(self, event: Mapping[str, Any], headers: Mapping[str,
116129
orgs_by_region = self._get_org_summaries_by_region_for_integration(
117130
integration=self.integration
118131
)
119-
logger.info(
132+
verbose_log(
120133
"overwatch.debug.orgs_by_region",
121134
extra={
122135
"regions": list(orgs_by_region.keys()),
@@ -125,7 +138,7 @@ def forward_if_applicable(self, event: Mapping[str, Any], headers: Mapping[str,
125138
)
126139

127140
if not orgs_by_region or not self.should_forward_to_overwatch(headers):
128-
logger.info(
141+
verbose_log(
129142
"overwatch.debug.skipped_forwarding",
130143
extra={
131144
"orgs_by_region_empty": not orgs_by_region,
@@ -137,7 +150,7 @@ def forward_if_applicable(self, event: Mapping[str, Any], headers: Mapping[str,
137150
# We can conditionally opt into forwarding on a per-region basis,
138151
# similar to codecov's current implementation.
139152
for region_name, org_summaries in orgs_by_region.items():
140-
logger.info(
153+
verbose_log(
141154
"overwatch.debug.check_region",
142155
extra={
143156
"region_name": region_name,
@@ -151,7 +164,7 @@ def forward_if_applicable(self, event: Mapping[str, Any], headers: Mapping[str,
151164
raw_app_id = headers.get(
152165
GITHUB_INSTALLATION_TARGET_ID_HEADER,
153166
) or headers.get(DJANGO_HTTP_GITHUB_INSTALLATION_TARGET_ID_HEADER)
154-
logger.info(
167+
verbose_log(
155168
"overwatch.debug.raw_app_id",
156169
extra={"region_name": region_name, "raw_app_id": raw_app_id},
157170
)
@@ -160,7 +173,7 @@ def forward_if_applicable(self, event: Mapping[str, Any], headers: Mapping[str,
160173
try:
161174
app_id = int(raw_app_id)
162175
except (TypeError, ValueError):
163-
logger.info(
176+
verbose_log(
164177
"overwatch.debug.app_id_parse_error",
165178
extra={"region_name": region_name, "raw_app_id": raw_app_id},
166179
)
@@ -179,7 +192,7 @@ def forward_if_applicable(self, event: Mapping[str, Any], headers: Mapping[str,
179192
integration_provider=self.integration.provider,
180193
region=get_region_by_name(region_name),
181194
)
182-
logger.info("overwatch.debug.enqueue_webhook", extra={"region_name": region_name})
195+
verbose_log("overwatch.debug.enqueue_webhook", extra={"region_name": region_name})
183196
publisher.enqueue_webhook(webhook_detail)
184197
metrics.incr(
185198
"overwatch.forward-webhooks.success",

0 commit comments

Comments
 (0)