Skip to content

Commit 8641268

Browse files
authored
Merge pull request #196 from keithdoggett/increment-feature-counter
Call Telemetry on Connection Pool Initialization
2 parents 876afa2 + 52f0b23 commit 8641268

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ development:
2727
In addition to the standard adapter settings, CockroachDB also supports the following:
2828

2929
- `use_follower_reads_for_type_introspection`: Use follower reads on queries to the `pg_type` catalog when set to `true`. This helps to speed up initialization by reading historical data, but may not find recently created user-defined types.
30+
- `disable_cockroachdb_telemetry`: Determines if a telemetry call is made to the database when the connection pool is initialized. Setting this to `true` will prevent the call from being made.
3031

3132
## Working with Spatial Data
3233

build/config.teamcity.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ connections:
1111
user: root
1212
requiressl: disable
1313
min_messages: warning
14+
disable_cockroachdb_telemetry: true
1415
arunit_without_prepared_statements:
1516
database: activerecord_unittest
1617
host: localhost
@@ -19,10 +20,12 @@ connections:
1920
requiressl: disable
2021
min_messages: warning
2122
prepared_statements: false
23+
disable_cockroachdb_telemetry: true
2224
arunit2:
2325
database: activerecord_unittest2
2426
host: localhost
2527
port: 26257
2628
user: root
2729
requiressl: disable
2830
min_messages: warning
31+
disable_cockroachdb_telemetry: true

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ def cockroachdb_connection(config)
5656

5757
module ActiveRecord
5858
module ConnectionAdapters
59+
module CockroachDBConnectionPool
60+
def initialize(pool_config)
61+
super(pool_config)
62+
disable_telemetry = pool_config.db_config.configuration_hash[:disable_cockroachdb_telemetry]
63+
adapter = pool_config.db_config.configuration_hash[:adapter]
64+
return if disable_telemetry || adapter != "cockroachdb"
65+
66+
with_connection do |conn|
67+
if conn.active?
68+
begin
69+
query = "SELECT crdb_internal.increment_feature_counter('ActiveRecord %d.%d')"
70+
conn.execute(query % [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR])
71+
rescue ActiveRecord::StatementInvalid
72+
# The increment_feature_counter built-in is not supported on this
73+
# CockroachDB version. Ignore.
74+
rescue StandardError => e
75+
conn.logger.warn "Unexpected error when incrementing feature counter: #{e}"
76+
end
77+
end
78+
end
79+
end
80+
end
81+
ConnectionPool.prepend(CockroachDBConnectionPool)
82+
5983
class CockroachDBAdapter < PostgreSQLAdapter
6084
ADAPTER_NAME = "CockroachDB".freeze
6185
DEFAULT_PRIMARY_KEY = "rowid"
@@ -194,6 +218,7 @@ def max_identifier_length
194218

195219
def initialize(connection, logger, conn_params, config)
196220
super(connection, logger, conn_params, config)
221+
197222
crdb_version_string = query_value("SHOW crdb_version")
198223
if crdb_version_string.include? "v1."
199224
version_num = 1

test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def setup
1616
end
1717

1818
def teardown
19-
# use connection without follower_reads
19+
# use connection without follower_reads and telemetry
2020
database_config = { "adapter" => "cockroachdb", "database" => "activerecord_unittest" }
2121
ar_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
2222
database_config.update(ar_config.configuration_hash)
@@ -38,6 +38,19 @@ def test_database_exists_returns_true_when_the_database_exists
3838
"expected database #{db_config.database} to exist"
3939
end
4040

41+
def test_using_telemetry_builtin_connects_properly
42+
database_config = { "adapter" => "cockroachdb", "database" => "activerecord_unittest" }
43+
ar_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
44+
database_config.update(ar_config.configuration_hash)
45+
database_config[:disable_cockroachdb_telemetry] = false
46+
47+
ActiveRecord::Base.establish_connection(database_config)
48+
conn = ActiveRecord::Base.connection
49+
conn_config = conn.instance_variable_get("@config")
50+
51+
assert_equal(false, conn_config[:disable_cockroachdb_telemetry])
52+
end
53+
4154
def test_using_follower_reads_connects_properly
4255
database_config = { "use_follower_reads_for_type_introspection": true, "adapter" => "cockroachdb", "database" => "activerecord_unittest" }
4356
ar_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")

test/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ connections:
55
host: localhost
66
port: 26257
77
user: root
8+
disable_cockroachdb_telemetry: true
89
arunit_without_prepared_statements:
910
min_messages: warning
1011
prepared_statements: false
1112
host: localhost
1213
port: 26257
1314
user: root
15+
disable_cockroachdb_telemetry: true
1416
arunit2:
1517
min_messages: warning
1618
host: localhost
1719
port: 26257
1820
user: root
21+
disable_cockroachdb_telemetry: true

0 commit comments

Comments
 (0)