mirror of
https://github.com/wahyd4/gitlabhq.git
synced 2026-08-11 21:56:19 +10:00
This removes the need for Sidekiq and any overhead/problems introduced by TCP. There are a few things to take into account: 1. When writing data to InfluxDB you may still get an error if the server becomes unavailable during the write. Because of this we're catching all exceptions and just ignore them (for now). 2. Writing via UDP apparently requires the timestamp to be in nanoseconds. Without this data either isn't written properly. 3. Due to the restrictions on UDP buffer sizes we're writing metrics one by one, instead of writing all of them at once.
48 lines
1.3 KiB
Ruby
48 lines
1.3 KiB
Ruby
module Gitlab
|
|
module Metrics
|
|
# Class for producing SQL queries with sensitive data stripped out.
|
|
class ObfuscatedSQL
|
|
REPLACEMENT = /
|
|
\d+(\.\d+)? # integers, floats
|
|
| '.+?' # single quoted strings
|
|
| \/.+?(?<!\\)\/ # regexps (including escaped slashes)
|
|
/x
|
|
|
|
MYSQL_REPLACEMENTS = /
|
|
".+?" # double quoted strings
|
|
/x
|
|
|
|
# Regex to replace consecutive placeholders with a single one indicating
|
|
# the length. This can be useful when a "IN" statement uses thousands of
|
|
# IDs (storing this would just be a waste of space).
|
|
CONSECUTIVE = /(\?(\s*,\s*)?){2,}/
|
|
|
|
# sql - The raw SQL query as a String.
|
|
def initialize(sql)
|
|
@sql = sql
|
|
end
|
|
|
|
# Returns a new, obfuscated SQL query.
|
|
def to_s
|
|
regex = REPLACEMENT
|
|
|
|
if Gitlab::Database.mysql?
|
|
regex = Regexp.union(regex, MYSQL_REPLACEMENTS)
|
|
end
|
|
|
|
sql = @sql.gsub(regex, '?').gsub(CONSECUTIVE) do |match|
|
|
"#{match.count(',') + 1} values"
|
|
end
|
|
|
|
# InfluxDB escapes double quotes upon output, so lets get rid of them
|
|
# whenever we can.
|
|
if Gitlab::Database.postgresql?
|
|
sql = sql.delete('"')
|
|
end
|
|
|
|
sql.tr("\n", ' ')
|
|
end
|
|
end
|
|
end
|
|
end
|