Skip to content

Commit

Permalink
Add adapter telemetry method for core model run snowplow event. (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
VersusFacit authored Oct 28, 2024
1 parent 951d98d commit d83314d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20241016-033159.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Add adapter telemetry.
time: 2024-10-16T03:31:59.334011-07:00
custom:
Author: versusfacit
Issue: "301"
1 change: 1 addition & 0 deletions dbt/adapters/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
BaseRelation,
RelationType,
SchemaSearchMap,
AdapterTrackingRelationInfo,
)
27 changes: 27 additions & 0 deletions dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from contextlib import contextmanager
from datetime import datetime
from enum import Enum
from importlib import import_module
from multiprocessing.context import SpawnContext
from typing import (
Any,
Expand Down Expand Up @@ -61,12 +62,14 @@
ComponentName,
InformationSchema,
SchemaSearchMap,
AdapterTrackingRelationInfo,
)
from dbt.adapters.cache import RelationsCache, _make_ref_key_dict
from dbt.adapters.capability import Capability, CapabilityDict
from dbt.adapters.contracts.connection import Credentials
from dbt.adapters.contracts.macros import MacroResolverProtocol
from dbt.adapters.contracts.relation import RelationConfig

from dbt.adapters.events.types import (
CacheMiss,
CatalogGenerationError,
Expand Down Expand Up @@ -1744,6 +1747,30 @@ def capabilities(cls) -> CapabilityDict:
def supports(cls, capability: Capability) -> bool:
return bool(cls.capabilities()[capability])

@classmethod
def get_adapter_run_info(cls, config: RelationConfig) -> AdapterTrackingRelationInfo:
adapter_class_name, *_ = cls.__name__.split("Adapter")
adapter_name = adapter_class_name.lower()

if adapter_name == "base":
adapter_version = ""
else:
adapter_version = import_module(f"dbt.adapters.{adapter_name}.__version__").version

return AdapterTrackingRelationInfo(
adapter_name=adapter_name,
base_adapter_version=import_module("dbt.adapters.__about__").version,
adapter_version=adapter_version,
model_adapter_details=cls._get_adapter_specific_run_info(config),
)

@classmethod
def _get_adapter_specific_run_info(cls, config) -> Dict[str, Any]:
"""
Adapter maintainers should overwrite this method to return any run metadata that should be captured during a run.
"""
return {}


COLUMNS_EQUAL_SQL = """
with diff_count as (
Expand Down
8 changes: 8 additions & 0 deletions dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,11 @@ def flatten(self, allow_multiple_databases: bool = False) -> "SchemaSearchMap":
)

return new


@dataclass(frozen=True, eq=False, repr=False)
class AdapterTrackingRelationInfo(FakeAPIObject, Hashable):
adapter_name: str
base_adapter_version: str
adapter_version: str
model_adapter_details: Any
15 changes: 15 additions & 0 deletions tests/unit/test_adapter_telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dbt.adapters.__about__

from dbt.adapters.base.impl import BaseAdapter
from dbt.adapters.base.relation import AdapterTrackingRelationInfo


def test_telemetry_returns():
res = BaseAdapter.get_adapter_run_info({})

assert res.adapter_name == "base"
assert res.base_adapter_version == dbt.adapters.__about__.version
assert res.adapter_version == ""
assert res.model_adapter_details == {}

assert type(res) is AdapterTrackingRelationInfo

0 comments on commit d83314d

Please sign in to comment.