Skip to content

Commit 98c629e

Browse files
rmosolgofbogsany
andauthored
fix: Reduce allocations on GraphQL hot paths (#1544)
* Reduce allocations on GraphQL hot paths * Update sdk/lib/opentelemetry/sdk/internal.rb Co-authored-by: Francis Bogsanyi <[email protected]> * Update sdk/lib/opentelemetry/sdk/internal.rb Co-authored-by: Francis Bogsanyi <[email protected]> * Appease the cop --------- Co-authored-by: Francis Bogsanyi <[email protected]>
1 parent d365dfe commit 98c629e

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

sdk/lib/opentelemetry/sdk/internal.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ def valid_value?(value)
4949
end
5050

5151
def valid_attributes?(owner, kind, attrs)
52-
attrs.nil? || attrs.all? do |k, v|
52+
attrs.nil? || attrs.each do |k, v|
5353
if !valid_key?(k)
5454
OpenTelemetry.handle_error(message: "invalid #{kind} attribute key type #{k.class} on span '#{owner}'")
55-
false
55+
return false
5656
elsif !valid_value?(v)
5757
OpenTelemetry.handle_error(message: "invalid #{kind} attribute value type #{v.class} for key '#{k}' on span '#{owner}'")
58-
false
59-
else
60-
true
58+
return false
6159
end
6260
end
61+
62+
true
6363
end
6464
end
6565
end

sdk/lib/opentelemetry/sdk/trace/span.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def to_span_data
282282
end
283283

284284
# @api private
285-
def initialize(context, parent_context, parent_span, name, kind, parent_span_id, span_limits, span_processors, attributes, links, start_timestamp, resource, instrumentation_scope) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
285+
def initialize(context, parent_context, parent_span, name, kind, parent_span_id, span_limits, span_processors, attributes, links, start_timestamp, resource, instrumentation_scope) # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
286286
super(span_context: context)
287287
@mutex = Mutex.new
288288
@name = name
@@ -297,7 +297,7 @@ def initialize(context, parent_context, parent_span, name, kind, parent_span_id,
297297
@total_recorded_events = 0
298298
@total_recorded_links = links&.size || 0
299299
@total_recorded_attributes = attributes&.size || 0
300-
@attributes = attributes.nil? ? nil : Hash[attributes] # We need a mutable copy of attributes.
300+
@attributes = attributes
301301
trim_span_attributes(@attributes)
302302
@events = nil
303303
@links = trim_links(links, span_limits.link_count_limit, span_limits.link_attribute_count_limit)
@@ -317,7 +317,7 @@ def initialize(context, parent_context, parent_span, name, kind, parent_span_id,
317317
# SpanData.
318318
@monotonic_start_timestamp = monotonic_now
319319
@realtime_start_timestamp = if parent_span.recording?
320-
relative_realtime(parent_span.realtime_start_timestamp, parent_span.monotonic_start_timestamp)
320+
relative_realtime(parent_span.realtime_start_timestamp, parent_span.monotonic_start_timestamp, @monotonic_start_timestamp)
321321
else
322322
realtime_now
323323
end
@@ -419,15 +419,15 @@ def append_event(events, event) # rubocop:disable Metrics/CyclomaticComplexity,
419419
def relative_timestamp(timestamp)
420420
return time_in_nanoseconds(timestamp) unless timestamp.nil?
421421

422-
relative_realtime(realtime_start_timestamp, monotonic_start_timestamp)
422+
relative_realtime(realtime_start_timestamp, monotonic_start_timestamp, monotonic_now)
423423
end
424424

425425
def time_in_nanoseconds(timestamp)
426426
(timestamp.to_r * 1_000_000_000).to_i
427427
end
428428

429-
def relative_realtime(realtime_base, monotonic_base)
430-
realtime_base + (monotonic_now - monotonic_base)
429+
def relative_realtime(realtime_base, monotonic_base, now)
430+
realtime_base + (now - monotonic_base)
431431
end
432432

433433
def realtime_now

sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def internal_start_span(name, kind, attributes, links, start_timestamp, parent_c
141141
if result.recording? && !@stopped
142142
trace_flags = result.sampled? ? OpenTelemetry::Trace::TraceFlags::SAMPLED : OpenTelemetry::Trace::TraceFlags::DEFAULT
143143
context = OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, trace_flags: trace_flags, tracestate: result.tracestate)
144-
attributes = attributes&.merge(result.attributes) || result.attributes
144+
attributes = attributes&.merge(result.attributes) || result.attributes.dup
145145
Span.new(
146146
context,
147147
parent_context,

0 commit comments

Comments
 (0)