1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
# frozen_string_literal: true
require "spec_helper"
module Appsignal
module_function
def instrument(key, &block)
instrumented << key
yield
end
def instrumented
@instrumented ||= []
end
end
describe GraphQL::Tracing::AppsignalTrace do
class IntBox
def initialize(value)
@value = value
end
attr_reader :value
end
module AppsignalTraceTest
class Thing < GraphQL::Schema::Object
field :str, String
def str; "blah"; end
end
class Named < GraphQL::Schema::Union
possible_types Thing
def self.resolve_type(obj, ctx)
Thing
end
end
class Query < GraphQL::Schema::Object
include GraphQL::Types::Relay::HasNodeField
field :int, Integer, null: false
def int
IntBox.new(1)
end
field :thing, Thing
def thing; :thing; end
field :named, Named, resolver_method: :thing
end
class TestSchema < GraphQL::Schema
query(Query)
trace_with(GraphQL::Tracing::AppsignalTrace)
lazy_resolve(IntBox, :value)
end
end
before do
Appsignal.instrumented.clear
end
it "traces events" do
_res = AppsignalTraceTest::TestSchema.execute("{ int thing { str } named { ... on Thing { str } } }")
expected_trace = [
"execute.graphql",
"analyze.graphql",
(USING_C_PARSER ? "lex.graphql" : nil),
"parse.graphql",
"validate.graphql",
"analyze.graphql",
"execute.graphql",
"Query.authorized.graphql",
"Query.thing.graphql",
"Thing.authorized.graphql",
"Query.named.graphql",
"Named.resolve_type.graphql",
"Thing.authorized.graphql",
"execute.graphql",
].compact
assert_equal expected_trace, Appsignal.instrumented
end
describe "With Datadog Trace" do
class AppsignalAndDatadogTestSchema < GraphQL::Schema
query(AppsignalTraceTest::Query)
trace_with(GraphQL::Tracing::DataDogTrace)
trace_with(GraphQL::Tracing::AppsignalTrace)
lazy_resolve(IntBox, :value)
end
class AppsignalAndDatadogReverseOrderTestSchema < GraphQL::Schema
query(AppsignalTraceTest::Query)
# Include these modules in different order than above:
trace_with(GraphQL::Tracing::AppsignalTrace)
trace_with(GraphQL::Tracing::DataDogTrace)
lazy_resolve(IntBox, :value)
end
before do
Datadog.clear_all
end
it "traces with both systems" do
_res = AppsignalAndDatadogTestSchema.execute("{ int thing { str } named { ... on Thing { str } } }")
expected_appsignal_trace = [
"execute.graphql",
(USING_C_PARSER ? "lex.graphql" : nil),
"parse.graphql",
"analyze.graphql",
"validate.graphql",
"analyze.graphql",
"execute.graphql",
"Query.authorized.graphql",
"Query.thing.graphql",
"Thing.authorized.graphql",
"Query.named.graphql",
"Named.resolve_type.graphql",
"Thing.authorized.graphql",
"execute.graphql",
].compact
expected_datadog_trace = [
"graphql.execute_multiplex",
(USING_C_PARSER ? "graphql.lex" : nil),
"graphql.parse",
"graphql.analyze_multiplex",
"graphql.validate",
"graphql.analyze_query",
"graphql.execute_query",
"graphql.authorized",
"graphql.execute_field",
"graphql.authorized",
"graphql.execute_field",
"graphql.resolve_type",
"graphql.authorized",
"graphql.execute_query_lazy",
].compact
assert_equal expected_appsignal_trace, Appsignal.instrumented
assert_equal expected_datadog_trace, Datadog::SPAN_TAGS
.select { |t| t[0].is_a?(String) }
.each_slice(2).map { |(p1, p2)| "#{p1[1]}.#{p2[1]}" }
end
it "works when the modules are included in reverse order" do
_res = AppsignalAndDatadogReverseOrderTestSchema.execute("{ int thing { str } named { ... on Thing { str } } }")
expected_appsignal_trace = [
(USING_C_PARSER ? "lex.graphql" : nil),
"parse.graphql",
"execute.graphql",
"analyze.graphql",
"validate.graphql",
"analyze.graphql",
"execute.graphql",
"Query.authorized.graphql",
"Query.thing.graphql",
"Thing.authorized.graphql",
"Query.named.graphql",
"Named.resolve_type.graphql",
"Thing.authorized.graphql",
"execute.graphql",
].compact
expected_datadog_trace = [
"graphql.execute_multiplex",
(USING_C_PARSER ? "graphql.lex" : nil),
"graphql.parse",
"graphql.analyze_multiplex",
"graphql.validate",
"graphql.analyze_query",
"graphql.execute_query",
"graphql.authorized",
"graphql.execute_field",
"graphql.authorized",
"graphql.execute_field",
"graphql.resolve_type",
"graphql.authorized",
"graphql.execute_query_lazy",
].compact
assert_equal expected_appsignal_trace, Appsignal.instrumented
assert_equal expected_datadog_trace, Datadog::SPAN_TAGS
.select { |t| t[0].is_a?(String) }
.each_slice(2).map { |(p1, p2)| "#{p1[1]}.#{p2[1]}" }
end
end
end
|