Skip to content

Commit a4c4580

Browse files
committed
move telemetry thread to pool initialize
1 parent d9bfffc commit a4c4580

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ 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+
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
77+
end
78+
end
79+
end.join # close thread after execution
80+
end
81+
end
82+
ConnectionPool.prepend(CockroachDBConnectionPool)
83+
5984
class CockroachDBAdapter < PostgreSQLAdapter
6085
ADAPTER_NAME = "CockroachDB".freeze
6186
DEFAULT_PRIMARY_KEY = "rowid"
@@ -194,6 +219,7 @@ def max_identifier_length
194219

195220
def initialize(connection, logger, conn_params, config)
196221
super(connection, logger, conn_params, config)
222+
197223
crdb_version_string = query_value("SHOW crdb_version")
198224
if crdb_version_string.include? "v1."
199225
version_num = 1
@@ -209,26 +235,6 @@ def initialize(connection, logger, conn_params, config)
209235
version_num = 202
210236
end
211237
@crdb_version = version_num
212-
213-
if @config[:disable_cockroachdb_telemetry]
214-
return
215-
end
216-
Thread.new do
217-
pool.with_connection do |conn|
218-
if !conn.active?
219-
return
220-
end
221-
begin
222-
query = "SELECT crdb_internal.increment_feature_counter('ActiveRecord %d.%d')"
223-
conn.execute(query % [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR])
224-
rescue ActiveRecord::StatementInvalid
225-
# The increment_feature_counter built-in is not supported on this
226-
# CockroachDB version. Ignore.
227-
rescue => error
228-
logger.warn "Unexpected error when incrementing feature counter: #{error}"
229-
end
230-
end
231-
end
232238
end
233239

234240
def self.database_exists?(config)

0 commit comments

Comments
 (0)