Skip to content

Commit

Permalink
Apply review suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
pankajastro committed Nov 29, 2024
1 parent f1b14a6 commit c9f9b71
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dagfactory/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
TELEMETRY_URL = "https://astronomer.gateway.scarf.sh/{telemetry_version}/{dagfactory_version}/{airflow_version}/{python_version}/{platform_system}/{platform_machine}/{event_type}/{status}/{dag_hash}/{task_count}"
TELEMETRY_URL = "https://astronomer.gateway.scarf.sh/dag-factory/{telemetry_version}/{dagfactory_version}/{airflow_version}/{python_version}/{platform_system}/{platform_machine}/{event_type}/{status}/{dag_hash}/{task_count}?{query_string}"
TELEMETRY_VERSION = "v1"
TELEMETRY_TIMEOUT = 5.0
6 changes: 5 additions & 1 deletion dagfactory/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import platform
from urllib import parse
from urllib.parse import urlencode

import httpx
from airflow import __version__ as airflow_version
Expand Down Expand Up @@ -38,7 +39,10 @@ def emit_usage_metrics(metrics: dict[str, object]) -> bool:
The metrics must contain the necessary fields to build the TELEMETRY_URL.
"""
telemetry_url = constants.TELEMETRY_URL.format(**metrics, telemetry_version=constants.TELEMETRY_VERSION)
query_string = urlencode(metrics)
telemetry_url = constants.TELEMETRY_URL.format(
**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:
Expand Down
6 changes: 4 additions & 2 deletions tests/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from unittest.mock import patch
from urllib.parse import urlencode

import pytest

Expand Down Expand Up @@ -56,14 +57,15 @@ def test_emit_usage_metrics_fails(mock_httpx_get, caplog):
"dag_hash": "d151d1fa2f03270ea116cc7494f2c591",
"task_count": 3,
}
query_string = urlencode(sample_metrics)
is_success = telemetry.emit_usage_metrics(sample_metrics)
mock_httpx_get.assert_called_once_with(
"https://astronomer.gateway.scarf.sh/v1/0.2.0a1/2.10.1/3.11/darwin/amd64/dag_run/success/d151d1fa2f03270ea116cc7494f2c591/3",
f"""https://astronomer.gateway.scarf.sh/dag-factory/v1/0.2.0a1/2.10.1/3.11/darwin/amd64/dag_run/success/d151d1fa2f03270ea116cc7494f2c591/3?{query_string}""",
timeout=5.0,
follow_redirects=True,
)
assert not is_success
log_msg = "Unable to emit usage metrics to https://astronomer.gateway.scarf.sh/v1/0.2.0a1/2.10.1/3.11/darwin/amd64/dag_run/success/d151d1fa2f03270ea116cc7494f2c591/3. Status code: 404. Message: Non existent URL"
log_msg = f"""Unable to emit usage metrics to https://astronomer.gateway.scarf.sh/dag-factory/v1/0.2.0a1/2.10.1/3.11/darwin/amd64/dag_run/success/d151d1fa2f03270ea116cc7494f2c591/3?{query_string}. Status code: 404. Message: Non existent URL"""
assert caplog.text.startswith("WARNING")
assert log_msg in caplog.text

Expand Down

0 comments on commit c9f9b71

Please sign in to comment.