Skip to content

Commit

Permalink
fix: Reduce allocations on GraphQL hot paths (#1544)
Browse files Browse the repository at this point in the history
* Reduce allocations on GraphQL hot paths

* Update sdk/lib/opentelemetry/sdk/internal.rb

Co-authored-by: Francis Bogsanyi <francis.bogsanyi@shopify.com>

* Update sdk/lib/opentelemetry/sdk/internal.rb

Co-authored-by: Francis Bogsanyi <francis.bogsanyi@shopify.com>

* Appease the cop

---------

Co-authored-by: Francis Bogsanyi <francis.bogsanyi@shopify.com>
  • Loading branch information
rmosolgo and fbogsany authored Nov 23, 2023
1 parent d365dfe commit 98c629e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions sdk/lib/opentelemetry/sdk/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ def valid_value?(value)
end

def valid_attributes?(owner, kind, attrs)
attrs.nil? || attrs.all? do |k, v|
attrs.nil? || attrs.each do |k, v|
if !valid_key?(k)
OpenTelemetry.handle_error(message: "invalid #{kind} attribute key type #{k.class} on span '#{owner}'")
false
return false
elsif !valid_value?(v)
OpenTelemetry.handle_error(message: "invalid #{kind} attribute value type #{v.class} for key '#{k}' on span '#{owner}'")
false
else
true
return false
end
end

true
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions sdk/lib/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def to_span_data
end

# @api private
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
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
super(span_context: context)
@mutex = Mutex.new
@name = name
Expand All @@ -297,7 +297,7 @@ def initialize(context, parent_context, parent_span, name, kind, parent_span_id,
@total_recorded_events = 0
@total_recorded_links = links&.size || 0
@total_recorded_attributes = attributes&.size || 0
@attributes = attributes.nil? ? nil : Hash[attributes] # We need a mutable copy of attributes.
@attributes = attributes
trim_span_attributes(@attributes)
@events = nil
@links = trim_links(links, span_limits.link_count_limit, span_limits.link_attribute_count_limit)
Expand All @@ -317,7 +317,7 @@ def initialize(context, parent_context, parent_span, name, kind, parent_span_id,
# SpanData.
@monotonic_start_timestamp = monotonic_now
@realtime_start_timestamp = if parent_span.recording?
relative_realtime(parent_span.realtime_start_timestamp, parent_span.monotonic_start_timestamp)
relative_realtime(parent_span.realtime_start_timestamp, parent_span.monotonic_start_timestamp, @monotonic_start_timestamp)
else
realtime_now
end
Expand Down Expand Up @@ -419,15 +419,15 @@ def append_event(events, event) # rubocop:disable Metrics/CyclomaticComplexity,
def relative_timestamp(timestamp)
return time_in_nanoseconds(timestamp) unless timestamp.nil?

relative_realtime(realtime_start_timestamp, monotonic_start_timestamp)
relative_realtime(realtime_start_timestamp, monotonic_start_timestamp, monotonic_now)
end

def time_in_nanoseconds(timestamp)
(timestamp.to_r * 1_000_000_000).to_i
end

def relative_realtime(realtime_base, monotonic_base)
realtime_base + (monotonic_now - monotonic_base)
def relative_realtime(realtime_base, monotonic_base, now)
realtime_base + (now - monotonic_base)
end

def realtime_now
Expand Down
2 changes: 1 addition & 1 deletion sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def internal_start_span(name, kind, attributes, links, start_timestamp, parent_c
if result.recording? && !@stopped
trace_flags = result.sampled? ? OpenTelemetry::Trace::TraceFlags::SAMPLED : OpenTelemetry::Trace::TraceFlags::DEFAULT
context = OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, trace_flags: trace_flags, tracestate: result.tracestate)
attributes = attributes&.merge(result.attributes) || result.attributes
attributes = attributes&.merge(result.attributes) || result.attributes.dup
Span.new(
context,
parent_context,
Expand Down

0 comments on commit 98c629e

Please sign in to comment.