|
| 1 | +# frozen_string_literal: true |
| 2 | +require "spec_helper" |
| 3 | + |
| 4 | +describe ::GraphQL::Tracing::OpenTelemetryTracing do |
| 5 | + module OpenTelemetryTest |
| 6 | + class Thing < GraphQL::Schema::Object |
| 7 | + implements GraphQL::Types::Relay::Node |
| 8 | + end |
| 9 | + |
| 10 | + class Query < GraphQL::Schema::Object |
| 11 | + include GraphQL::Types::Relay::HasNodeField |
| 12 | + |
| 13 | + field :int, Integer, null: false |
| 14 | + |
| 15 | + def int |
| 16 | + 1 |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + class SchemaWithPerRequestTracing < GraphQL::Schema |
| 21 | + query(Query) |
| 22 | + use(GraphQL::Tracing::OpenTelemetryTracing) |
| 23 | + orphan_types(Thing) |
| 24 | + |
| 25 | + def self.object_from_id(_id, _ctx) |
| 26 | + :thing |
| 27 | + end |
| 28 | + |
| 29 | + def self.resolve_type(_type, _obj, _ctx) |
| 30 | + Thing |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + before do |
| 36 | + OpenTelemetry::Instrumentation::GraphQL::Instrumentation.clear_all |
| 37 | + end |
| 38 | + |
| 39 | + it "captures all keys when tracing is enabled in config and in query execution context" do |
| 40 | + query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }') |
| 41 | + query.context.namespace(:opentelemetry)[:enable_platform_field] = true |
| 42 | + query.context.namespace(:opentelemetry)[:enable_platform_authorized] = true |
| 43 | + query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = true |
| 44 | + |
| 45 | + query.result |
| 46 | + |
| 47 | + assert_span("Query.authorized") |
| 48 | + assert_span("Query.node") |
| 49 | + assert_span("Node.resolve_type") |
| 50 | + assert_span("Thing.authorized") |
| 51 | + assert_span("DynamicFields.authorized") |
| 52 | + end |
| 53 | + |
| 54 | + it "does not capture the keys when tracing is not enabled in config but is enabled in query execution context" do |
| 55 | + OpenTelemetry::Instrumentation::GraphQL::Instrumentation.instance.stub(:config, { |
| 56 | + schemas: [], |
| 57 | + enable_platform_field: false, |
| 58 | + enable_platform_authorized: false, |
| 59 | + enable_platform_resolve_type: false |
| 60 | + }) do |
| 61 | + |
| 62 | + query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }') |
| 63 | + query.context.namespace(:opentelemetry)[:enable_platform_field] = true |
| 64 | + query.context.namespace(:opentelemetry)[:enable_platform_authorized] = true |
| 65 | + query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = true |
| 66 | + |
| 67 | + query.result |
| 68 | + |
| 69 | + refute_span("Query.authorized") |
| 70 | + refute_span("Query.node") |
| 71 | + refute_span("Node.resolve_type") |
| 72 | + refute_span("Thing.authorized") |
| 73 | + refute_span("DynamicFields.authorized") |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + it "does not capture any key when tracing is not enabled in config and tracing is not set in context" do |
| 78 | + OpenTelemetry::Instrumentation::GraphQL::Instrumentation.instance.stub(:config, { |
| 79 | + schemas: [], |
| 80 | + enable_platform_field: false, |
| 81 | + enable_platform_authorized: false, |
| 82 | + enable_platform_resolve_type: false |
| 83 | + }) do |
| 84 | + |
| 85 | + query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }') |
| 86 | + |
| 87 | + query.result |
| 88 | + |
| 89 | + refute_span("Query.authorized") |
| 90 | + refute_span("Query.node") |
| 91 | + refute_span("Node.resolve_type") |
| 92 | + refute_span("Thing.authorized") |
| 93 | + refute_span("DynamicFields.authorized") |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + it "does not capture any key when tracing is not enabled in config and context" do |
| 98 | + OpenTelemetry::Instrumentation::GraphQL::Instrumentation.instance.stub(:config, { |
| 99 | + schemas: [], |
| 100 | + enable_platform_field: false, |
| 101 | + enable_platform_authorized: false, |
| 102 | + enable_platform_resolve_type: false |
| 103 | + }) do |
| 104 | + |
| 105 | + query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }') |
| 106 | + query.context.namespace(:opentelemetry)[:enable_platform_field] = false |
| 107 | + query.context.namespace(:opentelemetry)[:enable_platform_authorized] = false |
| 108 | + query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = false |
| 109 | + |
| 110 | + query.result |
| 111 | + |
| 112 | + refute_span("Query.authorized") |
| 113 | + refute_span("Query.node") |
| 114 | + refute_span("Node.resolve_type") |
| 115 | + refute_span("Thing.authorized") |
| 116 | + refute_span("DynamicFields.authorized") |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + it "captures all keys when tracing is enabled in config but is not set in context" do |
| 121 | + query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }') |
| 122 | + |
| 123 | + query.result |
| 124 | + |
| 125 | + assert_span("Query.authorized") |
| 126 | + assert_span("Query.node") |
| 127 | + assert_span("Node.resolve_type") |
| 128 | + assert_span("Thing.authorized") |
| 129 | + assert_span("DynamicFields.authorized") |
| 130 | + end |
| 131 | + |
| 132 | + it "does not capture any key when tracing is enabled in config but is not enabled in context" do |
| 133 | + query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }') |
| 134 | + query.context.namespace(:opentelemetry)[:enable_platform_field] = false |
| 135 | + query.context.namespace(:opentelemetry)[:enable_platform_authorized] = false |
| 136 | + query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = false |
| 137 | + |
| 138 | + query.result |
| 139 | + |
| 140 | + refute_span("Query.authorized") |
| 141 | + refute_span("Query.node") |
| 142 | + refute_span("Node.resolve_type") |
| 143 | + refute_span("Thing.authorized") |
| 144 | + refute_span("DynamicFields.authorized") |
| 145 | + end |
| 146 | + |
| 147 | + private |
| 148 | + |
| 149 | + def assert_span(span) |
| 150 | + assert OpenTelemetry::Instrumentation::GraphQL::Instrumentation::EVENTS.include?(span) |
| 151 | + end |
| 152 | + |
| 153 | + def refute_span(span) |
| 154 | + refute OpenTelemetry::Instrumentation::GraphQL::Instrumentation::EVENTS.include?(span) |
| 155 | + end |
| 156 | +end |
0 commit comments