Skip to content

Commit 9acdbaf

Browse files
authored
Add telemetry to SQLAlchemy. (#140)
Telemetry is enabled by default, set disable_cockroachdb_telemetry in create_engine's connect_args field to disable. Example: engine = create_engine('cockroachdb://...', connect_args={"disable_cockroachdb_telemetry": True})
1 parent 96f1587 commit 9acdbaf

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Version 1.3.4
2+
- Add telemetry to SQLAlchemy CockroachDB
3+
- Telemetry is enabled by default, set disable_cockroachdb_telemetry in create_engine's connect_args field to disable.
4+
- ```Example: engine = create_engine('cockroachdb://...', connect_args={"disable_cockroachdb_telemetry": True})```
5+
6+
17
# Version 1.3.3
28
Released April 26, 2021
39

cockroachdb/sqlalchemy/dialect.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from .stmt_compiler import CockroachCompiler, CockroachIdentifierPreparer
1515
from .ddl_compiler import CockroachDDLCompiler
1616

17+
import pkg_resources
18+
1719
# Map type names (as returned by information_schema) to sqlalchemy type
1820
# objects.
1921
#
@@ -100,6 +102,20 @@ class CockroachDBDialect(PGDialect_psycopg2):
100102
preparer = CockroachIdentifierPreparer
101103
ddl_compiler = CockroachDDLCompiler
102104

105+
# Override connect so we can take disable_cockroachdb_telemetry as a connect_arg to sqlalchemy.
106+
def connect(
107+
self,
108+
dsn=None,
109+
connection_factory=None,
110+
cursor_factory=None,
111+
disable_cockroachdb_telemetry=False,
112+
**kwargs,
113+
):
114+
self.disable_cockroachdb_telemetry = disable_cockroachdb_telemetry
115+
return super(CockroachDBDialect, self).connect(
116+
dsn, connection_factory,
117+
cursor_factory, **kwargs)
118+
103119
def __init__(self, *args, **kwargs):
104120
if kwargs.get("use_native_hstore", False):
105121
raise NotImplementedError("use_native_hstore is not supported")
@@ -126,11 +142,23 @@ def initialize(self, connection):
126142
self._is_v192plus = self._is_v191plus and (" v19.1." not in sversion)
127143
self._is_v201plus = self._is_v192plus and (" v19.2." not in sversion)
128144
self._is_v202plus = self._is_v201plus and (" v20.1." not in sversion)
145+
self._is_v211plus = self._is_v202plus and (" v20.2." not in sversion)
129146
self._has_native_json = self._is_v2plus
130147
self._has_native_jsonb = self._is_v2plus
131148
self._supports_savepoints = self._is_v201plus
132149
self.supports_native_enum = self._is_v202plus
133150

151+
# Increment third party tool telemetry counter for SQLAlchemy.
152+
# The crdb_internal.increment_feature_counter only exists in v21.1 of CRDB
153+
# and onwards.
154+
if self._is_v211plus and not self.disable_cockroachdb_telemetry:
155+
version = pkg_resources.require("sqlalchemy-cockroachdb")[0].version
156+
telemetry_query = (
157+
"SELECT crdb_internal.increment_feature_counter"
158+
+ "('SQLAlchemy {version}')".format(version=version)
159+
)
160+
connection.execute(telemetry_query)
161+
134162
def _get_server_version_info(self, conn):
135163
# PGDialect expects a postgres server version number here,
136164
# although we've overridden most of the places where it's

0 commit comments

Comments
 (0)