Skip to content

Commit 17f8d01

Browse files
committed
Add adapter name guard before telemetry call and make call in the current thread
1 parent a4c4580 commit 17f8d01

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

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: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,22 @@ module CockroachDBConnectionPool
6060
def initialize(pool_config)
6161
super(pool_config)
6262
disable_telemetry = pool_config.db_config.configuration_hash[:disable_cockroachdb_telemetry]
63-
return if disable_telemetry
64-
65-
Thread.new do
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, NotImplementedError => e
75-
conn.logger.warn "Unexpected error when incrementing feature counter: #{e}"
76-
end
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}"
7776
end
7877
end
79-
end.join # close thread after execution
78+
end
8079
end
8180
end
8281
ConnectionPool.prepend(CockroachDBConnectionPool)

test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def setup
1515
@connection_handler = ActiveRecord::Base.connection_handler
1616
end
1717

18+
def teardown
19+
# use connection without follower_reads
20+
database_config = { "adapter" => "cockroachdb", "database" => "activerecord_unittest" }
21+
ar_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
22+
database_config.update(ar_config.configuration_hash)
23+
24+
ActiveRecord::Base.establish_connection(database_config)
25+
end
26+
1827
def test_database_exists_returns_false_when_the_database_does_not_exist
1928
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
2029
config = db_config.configuration_hash.dup
@@ -28,6 +37,19 @@ def test_database_exists_returns_true_when_the_database_exists
2837
assert ActiveRecord::ConnectionAdapters::CockroachDBAdapter.database_exists?(db_config.configuration_hash),
2938
"expected database #{db_config.database} to exist"
3039
end
40+
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
3153
end
3254
end
3355
end

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)