Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions dagfactory/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ def emit_usage_metrics(metrics: dict[str, object]) -> bool:
**metrics, telemetry_version=constants.TELEMETRY_VERSION, query_string=query_string
)
logging.debug("Telemetry is enabled. Emitting the following usage metrics to %s: %s", telemetry_url, metrics)
response = httpx.get(telemetry_url, timeout=constants.TELEMETRY_TIMEOUT, follow_redirects=True)
if not response.is_success:
try:
response = httpx.get(telemetry_url, timeout=constants.TELEMETRY_TIMEOUT, follow_redirects=True)
except httpx.HTTPError as e:
logging.warning(
"Unable to emit usage metrics to %s. Status code: %s. Message: %s",
telemetry_url,
response.status_code,
response.text,
"Unable to emit usage metrics to %s. An HTTPX connection error occurred: %s.", telemetry_url, str(e)
)
return response.is_success
is_success = False
else:
is_success = response.is_success
if not is_success:
logging.warning(
"Unable to emit usage metrics to %s. Status code: %s. Message: %s",
telemetry_url,
response.status_code,
response.text,
)
return is_success


def emit_usage_metrics_if_enabled(event_type: str, additional_metrics: dict[str, object]) -> bool:
Expand Down
28 changes: 27 additions & 1 deletion tests/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from unittest.mock import patch

import httpx
import pytest

from dagfactory import telemetry
Expand Down Expand Up @@ -45,7 +46,7 @@ class MockFailedResponse:


@patch("dagfactory.telemetry.httpx.get", return_value=MockFailedResponse())
def test_emit_usage_metrics_fails(mock_httpx_get, caplog):
def test_emit_usage_metrics_is_unsuccessful(mock_httpx_get, caplog):
sample_metrics = {
"dagfactory_version": "0.2.0a1",
"airflow_version": "2.10.1",
Expand All @@ -69,6 +70,31 @@ def test_emit_usage_metrics_fails(mock_httpx_get, caplog):
assert log_msg in caplog.text


@patch("dagfactory.telemetry.httpx.get", side_effect=httpx.ConnectError(message="Something is not right"))
def test_emit_usage_metrics_fails(mock_httpx_get, caplog):
sample_metrics = {
"dagfactory_version": "0.2.0a1",
"airflow_version": "2.10.1",
"python_version": "3.11",
"platform_system": "darwin",
"platform_machine": "amd64",
"event_type": "dag_run",
"status": "success",
"dag_hash": "d151d1fa2f03270ea116cc7494f2c591",
"task_count": 3,
}
is_success = telemetry.emit_usage_metrics(sample_metrics)
mock_httpx_get.assert_called_once_with(
f"""https://astronomer.gateway.scarf.sh/dag-factory/v2/0.2.0a1/2.10.1/3.11/darwin/amd64/dag_run/success/d151d1fa2f03270ea116cc7494f2c591/3""",
timeout=5.0,
follow_redirects=True,
)
assert not is_success
log_msg = f"""Unable to emit usage metrics to https://astronomer.gateway.scarf.sh/dag-factory/v2/0.2.0a1/2.10.1/3.11/darwin/amd64/dag_run/success/d151d1fa2f03270ea116cc7494f2c591/3. An HTTPX connection error occurred: Something is not right."""
assert caplog.text.startswith("WARNING")
assert log_msg in caplog.text


@pytest.mark.integration
def test_emit_usage_metrics_succeeds(caplog):
caplog.set_level(logging.DEBUG)
Expand Down