Skip to content

Commit 9d7f3e2

Browse files
seedofjoybeniwohli
authored andcommitted
Fix GraphQL instrumentation: span spamming (elastic#1015)
Do not create spans for scalar fields with a required flag, because it causing span spamming. Before this commit it's worked only for optional fields.
1 parent 70f104d commit 9d7f3e2

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

elasticapm/instrumentation/packages/graphql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def call(self, module, method, wrapped, instance, args, kwargs):
5252
query = args[2]
5353

5454
if "ResolveInfo" == type(query).__name__:
55-
if str(query.return_type) in [
55+
if str(query.return_type).rstrip("!") in [
5656
"Boolean",
5757
"Context",
5858
"Date",

tests/instrumentation/graphql_tests.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

3131

32-
import os
33-
3432
import pytest
3533

3634
from elasticapm.conf.constants import TRANSACTION
@@ -42,12 +40,8 @@
4240
pytestmark = pytest.mark.graphene
4341

4442

45-
class Query(graphene.ObjectType):
46-
rand = graphene.String()
47-
48-
4943
class Success(graphene.ObjectType):
50-
yeah = graphene.String()
44+
yeah = graphene.String(required=True)
5145

5246

5347
class Error(graphene.ObjectType):
@@ -88,7 +82,9 @@ def resolve_err(self, *args, **kwargs):
8882
return Error(message="oops")
8983

9084

91-
@pytest.mark.skipif(not hasattr(graphql, "VERSION") or graphql.VERSION[0] >= 3, reason="Executor is reimplementated in graphql-core 3")
85+
@pytest.mark.skipif(
86+
not hasattr(graphql, "VERSION") or graphql.VERSION[0] >= 3, reason="Executor is reimplementated in graphql-core 3"
87+
)
9288
@pytest.mark.integrationtest
9389
def test_create_post(instrument, elasticapm_client):
9490
query_string = """
@@ -112,16 +108,17 @@ def test_create_post(instrument, elasticapm_client):
112108
transactions = elasticapm_client.events[TRANSACTION]
113109
spans = elasticapm_client.spans_for_transaction(transactions[0])
114110
expected_signatures = {
115-
"GraphQL.mutation __typename",
116111
"GraphQL.mutation createPost",
117112
"GraphQL.mutation result",
118113
"test_graphene",
119114
}
120115
assert {t["name"] for t in spans} == expected_signatures
121-
assert transactions[0]['name'] == 'GraphQL MUTATION createPost'
116+
assert transactions[0]["name"] == "GraphQL MUTATION createPost"
122117

123118

124-
@pytest.mark.skipif(not hasattr(graphql, "VERSION") or graphql.VERSION[0] >= 3, reason="Executor is reimplementated in graphql-core 3")
119+
@pytest.mark.skipif(
120+
not hasattr(graphql, "VERSION") or graphql.VERSION[0] >= 3, reason="Executor is reimplementated in graphql-core 3"
121+
)
125122
@pytest.mark.integrationtest
126123
def test_fetch_data(instrument, elasticapm_client):
127124
query_string = "{succ{yeah},err{__typename}}"
@@ -136,10 +133,9 @@ def test_fetch_data(instrument, elasticapm_client):
136133
transactions = elasticapm_client.events[TRANSACTION]
137134
spans = elasticapm_client.spans_for_transaction(transactions[0])
138135
expected_signatures = {
139-
"GraphQL.query __typename",
140136
"GraphQL.query err",
141137
"GraphQL.query succ",
142138
"test_graphene",
143139
}
144140
assert {t["name"] for t in spans} == expected_signatures
145-
assert transactions[0]['name'] == 'GraphQL QUERY succ+err'
141+
assert transactions[0]["name"] == "GraphQL QUERY succ+err"

0 commit comments

Comments
 (0)