Skip to content

Commit 3c46f36

Browse files
committed
Rename internal variables in CompiledMetric to avoid tag name conflicts
Prefix and suffix internal variables with __ in dynamically generated methods to prevent conflicts with user-defined tag names like `cache`, `datagram`, `start`, `stop`, `sample_rate`, etc. Renamed variables: - return_value -> __return_value__ - cache_key -> __cache_key__ - datagram -> __datagram__ - cache -> __cache__ - cached_datagram -> __cached_datagram__ - new_datagram -> __new_datagram__ - sample_rate -> __sample_rate__ - start -> __start__ - stop -> __stop__
1 parent 53c9c32 commit 3c46f36

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

lib/statsd/instrument/compiled_metric.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def generate_block_handler
114114
# Not doing so would impact performance and CPU usage.
115115
# See Datadog's documentation for more details: https://github.com/DataDog/datadog-go/blob/20af2dbfabbbe6bd0347780cd57ed931f903f223/statsd/aggregator.go#L281-L283
116116
<<~RUBY
117-
sample_rate ||= @sample_rate
118-
if sample_rate && !sample?(sample_rate)
117+
__sample_rate__ ||= @sample_rate
118+
if __sample_rate__ && !sample?(__sample_rate__)
119119
if block_given?
120120
return yield
121121
end
@@ -124,12 +124,12 @@ def generate_block_handler
124124
end
125125
126126
if block_given?
127-
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
127+
__start__ = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
128128
begin
129-
return_value = yield
129+
__return_value__ = yield
130130
ensure
131-
stop = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
132-
__value__ = stop - start
131+
__stop__ = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
132+
__value__ = __stop__ - __start__
133133
end
134134
end
135135
RUBY
@@ -146,42 +146,42 @@ def define_dynamic_method(tags)
146146

147147
method_code = <<~RUBY
148148
def self.#{method}(__value__ = #{default_val.inspect}, #{tag_names.map { |name| "#{name}:" }.join(", ")})
149-
return_value = StatsD::Instrument::VOID
149+
__return_value__ = StatsD::Instrument::VOID
150150
#{generate_block_handler if allow_block}
151151
152152
# Compute hash of tag values for cache lookup
153-
cache_key = #{tag_names.map { |name| "#{name}.hash" }.join(" ^ ")}
153+
__cache_key__ = #{tag_names.map { |name| "#{name}.hash" }.join(" ^ ")}
154154
155155
# Look up or create a PrecompiledDatagram
156-
datagram =
157-
if (cache = @tag_combination_cache)
158-
cached_datagram = cache[cache_key] ||=
156+
__datagram__ =
157+
if (__cache__ = @tag_combination_cache)
158+
__cached_datagram__ = __cache__[__cache_key__] ||=
159159
begin
160-
new_datagram = PrecompiledDatagram.new([#{tag_names.join(", ")}], @datagram_blueprint, @sample_rate)
160+
__new_datagram__ = PrecompiledDatagram.new([#{tag_names.join(", ")}], @datagram_blueprint, @sample_rate)
161161
162162
# Clear cache if it grows too large to prevent memory bloat
163-
if cache.size >= @max_cache_size
163+
if __cache__.size >= @max_cache_size
164164
StatsD.increment("statsd_instrument.compiled_metric.cache_exceeded_total", tags: { metric_name: @name, max_size: @max_cache_size })
165165
@tag_combination_cache = nil
166166
end
167167
168-
new_datagram
168+
__new_datagram__
169169
end
170170
171171
# Hash collision detection
172-
if #{tag_names.map.with_index { |name, i| "#{name} != cached_datagram.tag_values[#{i}]" }.join(" || ")}
172+
if #{tag_names.map.with_index { |name, i| "#{name} != __cached_datagram__.tag_values[#{i}]" }.join(" || ")}
173173
# Hash collision - fall back to creating a new datagram
174174
StatsD.increment("statsd_instrument.compiled_metric.hash_collision_detected", tags: { metric_name: @name })
175-
cached_datagram = nil
175+
__cached_datagram__ = nil
176176
end
177177
178-
cached_datagram
178+
__cached_datagram__
179179
end
180180
181-
datagram ||= PrecompiledDatagram.new([#{tag_names.join(", ")}], @datagram_blueprint, @sample_rate)
181+
__datagram__ ||= PrecompiledDatagram.new([#{tag_names.join(", ")}], @datagram_blueprint, @sample_rate)
182182
183-
@singleton_client.emit_precompiled_#{method}_metric(datagram, __value__)
184-
return_value
183+
@singleton_client.emit_precompiled_#{method}_metric(__datagram__, __value__)
184+
__return_value__
185185
end
186186
RUBY
187187

@@ -198,10 +198,10 @@ def define_static_method
198198

199199
instance_eval(<<~RUBY, __FILE__, __LINE__ + 1)
200200
def self.#{method}(__value__ = #{default_val.inspect})
201-
return_value = StatsD::Instrument::VOID
201+
__return_value__ = StatsD::Instrument::VOID
202202
#{generate_block_handler if allow_block}
203203
@singleton_client.emit_precompiled_#{method}_metric(@static_datagram, __value__)
204-
return_value
204+
__return_value__
205205
end
206206
RUBY
207207
end

0 commit comments

Comments
 (0)