Commit 864f6408 authored by Stan Hu's avatar Stan Hu
Browse files

Reduce CPU utilization of row count query

Previously, scraping the /database metrics used a significant amount of
CPU because each query would rebuild the PG to Ruby data type map. We
now memoize this map so we only need to build this map once.

Closes https://gitlab.com/gitlab-org/gitlab-exporter/-/issues/75
parent c0e2f044
...@@ -175,6 +175,14 @@ module GitLab ...@@ -175,6 +175,14 @@ module GitLab
def execute(query) def execute(query)
with_connection_pool do |conn| with_connection_pool do |conn|
conn.exec(query).map_types!(type_map_for_results(conn))
end
rescue PG::UndefinedTable, PG::UndefinedColumn
nil
end
def type_map_for_results(conn)
@type_map_for_results ||= begin
tm = PG::BasicTypeMapForResults.new(conn) tm = PG::BasicTypeMapForResults.new(conn)
# Remove warning message: # Remove warning message:
...@@ -187,10 +195,8 @@ module GitLab ...@@ -187,10 +195,8 @@ module GitLab
tm.add_coder(old_coder.dup.tap { |c| c.oid = value[:oid] }) tm.add_coder(old_coder.dup.tap { |c| c.oid = value[:oid] })
end end
conn.exec(query).map_types!(tm) tm
end end
rescue PG::UndefinedTable, PG::UndefinedColumn
nil
end end
# Not private so I can test it without meta programming tricks # Not private so I can test it without meta programming tricks
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment