Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/datadog/tracing/contrib/graphql/unified_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def add_query_error_events(span, errors)
@type_key => parsed_error.type,
@stacktrace_key => parsed_error.backtrace,
@message_key => graphql_error['message'],
@locations_key => serialize_error_locations(graphql_error['locations']),
@locations_key => self.class.serialize_error_locations(graphql_error['locations']),
@path_key => graphql_error['path'],
)
)
Expand All @@ -285,7 +285,11 @@ def add_query_error_events(span, errors)
# ]
# is serialized as:
# ["3:10", "7:8"]
def serialize_error_locations(locations)
def self.serialize_error_locations(locations)
# locations are only provided by the `graphql` library when the error can
# be associated to a particular point in the query.
return [] if locations.nil?

locations.map do |location|
"#{location["line"]}:#{location["column"]}"
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/tracing/contrib/graphql/unified_trace.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module Datadog

def add_query_error_events: (SpanOperation span, Array[GraphQL::Error] errors) -> void

def serialize_error_locations: (Array[Hash[String, Integer]] locations) -> Array[String]
def self.serialize_error_locations: (Array[Hash[String, Integer]]? locations) -> Array[String]
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions spec/datadog/tracing/contrib/graphql/unified_trace_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'datadog/tracing/contrib/support/spec_helper'
require 'datadog/tracing/contrib/graphql/unified_trace'

RSpec.describe Datadog::Tracing::Contrib::GraphQL::UnifiedTrace do
describe '.serialize_error_locations' do
subject(:result) { described_class.serialize_error_locations(locations) }

context 'when locations is nil' do
let(:locations) { nil }

it 'returns an empty array' do
expect(result).to eq([])
end
end

context 'when locations is an array' do
let(:locations) do
[
{ 'line' => 3, 'column' => 10 },
{ 'line' => 7, 'column' => 8 }
]
end

it 'maps locations to formatted strings' do
expect(result).to eq(['3:10', '7:8'])
end
end

context 'when locations is an empty array' do
let(:locations) { [] }

it 'returns an empty array' do
expect(result).to eq([])
end
end
end
end