mirror of
https://github.com/usestrix/strix.git
synced 2026-08-16 17:27:26 +02:00
Deduplicate scan ended telemetry
This commit is contained in:
@@ -135,6 +135,9 @@ class ReportState:
|
||||
self._sarif_repo_ctx: dict[str, Any] | None = None
|
||||
self._sarif_repo_ctx_ready: bool = False
|
||||
|
||||
self.posthog_scan_ended_sent: bool = False
|
||||
self.scarf_scan_ended_sent: bool = False
|
||||
|
||||
def get_run_dir(self) -> Path:
|
||||
if self._run_dir is None:
|
||||
run_dir_name = self.run_name if self.run_name else self.run_id
|
||||
|
||||
@@ -82,6 +82,10 @@ def finding(severity: str) -> None:
|
||||
|
||||
|
||||
def end(report_state: "ReportState", exit_reason: str = "completed") -> None:
|
||||
if report_state.posthog_scan_ended_sent:
|
||||
return
|
||||
report_state.posthog_scan_ended_sent = True
|
||||
|
||||
vulnerabilities_counts = {"critical": 0, "high": 0, "medium": 0, "low": 0, "info": 0}
|
||||
for v in report_state.vulnerability_reports:
|
||||
sev = v.get("severity", "info").lower()
|
||||
|
||||
@@ -85,6 +85,10 @@ def finding(severity: str) -> None:
|
||||
|
||||
|
||||
def end(report_state: ReportState, exit_reason: str = "completed") -> None:
|
||||
if report_state.scarf_scan_ended_sent:
|
||||
return
|
||||
report_state.scarf_scan_ended_sent = True
|
||||
|
||||
vulnerabilities_counts = {"critical": 0, "high": 0, "medium": 0, "low": 0, "info": 0}
|
||||
for v in report_state.vulnerability_reports:
|
||||
sev = v.get("severity", "info").lower()
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Tests for scan telemetry emission."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from strix.report.state import ReportState
|
||||
from strix.telemetry import posthog, scarf
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("backend", "sent_attribute"),
|
||||
[
|
||||
(posthog, "posthog_scan_ended_sent"),
|
||||
(scarf, "scarf_scan_ended_sent"),
|
||||
],
|
||||
)
|
||||
def test_scan_ended_is_sent_once_per_backend(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
backend: Any,
|
||||
sent_attribute: str,
|
||||
) -> None:
|
||||
state = ReportState(run_name="test-run")
|
||||
sent_events: list[tuple[str, dict[str, Any]]] = []
|
||||
monkeypatch.setattr(
|
||||
backend,
|
||||
"_send",
|
||||
lambda event, properties: sent_events.append((event, properties)),
|
||||
)
|
||||
|
||||
backend.end(state, exit_reason="finished_by_tool")
|
||||
backend.end(state, exit_reason="user_exit")
|
||||
|
||||
assert getattr(state, sent_attribute) is True
|
||||
assert len(sent_events) == 1
|
||||
assert sent_events[0][0] == "scan_ended"
|
||||
assert sent_events[0][1]["exit_reason"] == "finished_by_tool"
|
||||
Reference in New Issue
Block a user