generated from dbt-labs/dbt-oss-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generic snowplow tracker with file logger for testing
- Loading branch information
1 parent
9798ca7
commit e671471
Showing
8 changed files
with
275 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from pathlib import Path | ||
import uuid | ||
from typing import Any, Dict | ||
|
||
import yaml | ||
|
||
# the C version is faster, but it doesn't always exist | ||
try: | ||
from yaml import CSafeLoader as SafeLoader | ||
except ImportError: | ||
from yaml import SafeLoader | ||
|
||
|
||
class Cookie: | ||
def __init__(self, directory: Path) -> None: | ||
self.id: str = str(uuid.uuid4()) | ||
self.path: Path = directory / ".user.yml" | ||
self.save() | ||
|
||
def as_dict(self) -> Dict[str, Any]: | ||
return {"id": self.id} | ||
|
||
def save(self) -> None: | ||
with open(self.path, "w") as fh: | ||
yaml.dump(self.as_dict(), fh) | ||
|
||
def load(self) -> Dict[str, Any]: | ||
with open(self.path, "r") as fh: | ||
try: | ||
return yaml.load(fh, Loader=SafeLoader) | ||
except yaml.reader.ReaderError: | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from dataclasses import dataclass | ||
import logging | ||
from logging.handlers import RotatingFileHandler | ||
from typing import Optional | ||
|
||
from snowplow_tracker import Emitter, Tracker | ||
from snowplow_tracker.typing import FailureCallback | ||
|
||
from dbt_common.events.base_types import EventMsg | ||
|
||
|
||
@dataclass | ||
class TrackerConfig: | ||
invocation_id: Optional[str] = None | ||
endpoint: Optional[str] = None | ||
protocol: Optional[str] = None | ||
on_failure: Optional[FailureCallback] = None | ||
name: Optional[str] = None | ||
output_file_name: Optional[str] = None | ||
output_file_max_bytes: Optional[int] = 10 * 1024 * 1024 # 10 mb | ||
|
||
|
||
class _Tracker: | ||
def __init__(self, config: TrackerConfig) -> None: | ||
self.invocation_id: Optional[str] = config.invocation_id | ||
|
||
if all([config.name, config.output_file_name]): | ||
file_handler = RotatingFileHandler( | ||
filename=str(config.output_file_name), | ||
encoding="utf8", | ||
maxBytes=config.output_file_max_bytes, # type: ignore | ||
backupCount=5, | ||
) | ||
self._tracker = self._python_file_logger(config.name, file_handler) | ||
|
||
elif all([config.endpoint, config.protocol]): | ||
self._tracker = self._snowplow_tracker(config.endpoint, config.protocol) | ||
|
||
def track(self, msg: EventMsg) -> str: | ||
raise NotImplementedError() | ||
|
||
def _python_file_logger(self, name: str, handler: logging.Handler) -> logging.Logger: | ||
log = logging.getLogger(name) | ||
log.setLevel(logging.DEBUG) | ||
handler.setFormatter(logging.Formatter(fmt="%(message)s")) | ||
log.handlers.clear() | ||
log.propagate = False | ||
log.addHandler(handler) | ||
return log | ||
|
||
def _snowplow_tracker( | ||
self, | ||
endpoint: str, | ||
protocol: Optional[str] = "https", | ||
on_failure: Optional[FailureCallback] = None, | ||
) -> Tracker: | ||
emitter = Emitter( | ||
endpoint, | ||
protocol, | ||
method="post", | ||
batch_size=30, | ||
on_failure=on_failure, | ||
byte_limit=None, | ||
request_timeout=5.0, | ||
) | ||
tracker = Tracker( | ||
emitters=emitter, | ||
namespace="cf", | ||
app_id="dbt", | ||
) | ||
return tracker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from datetime import datetime | ||
from pathlib import Path | ||
from typing import Any, Dict, Optional, Union | ||
|
||
import pytz | ||
|
||
from dbt_common.events.functions import get_invocation_id | ||
|
||
|
||
class User: | ||
def __init__(self, directory: Union[str, Path]) -> None: | ||
self.cookie: Dict[str, Any] = {} | ||
self.directory: Path = Path(directory) | ||
self.invocation_id: str = get_invocation_id() | ||
self.run_started_at: datetime = datetime.now(tz=pytz.utc) | ||
|
||
@property | ||
def id(self) -> Optional[str]: | ||
if self.cookie: | ||
return self.cookie.get("id") | ||
|
||
@property | ||
def do_not_track(self) -> bool: | ||
return self.cookie != {} | ||
|
||
def state(self): | ||
return "do not track" if self.do_not_track else "tracking" | ||
|
||
@property | ||
def profile(self) -> Path: | ||
return Path(self.directory) / "profiles.yml" | ||
|
||
def enable_tracking(self, cookie: Dict[str, Any]): | ||
self.cookie = cookie | ||
|
||
def disable_tracking(self): | ||
self.cookie = {} |