|
4 | 4 | from flask import Flask, jsonify, request |
5 | 5 | from graphene import ObjectType, Schema, String |
6 | 6 |
|
| 7 | +import sentry_sdk |
7 | 8 | from sentry_sdk.consts import OP |
8 | 9 | from sentry_sdk.integrations.fastapi import FastApiIntegration |
9 | 10 | from sentry_sdk.integrations.flask import FlaskIntegration |
@@ -254,6 +255,63 @@ def graphql_server_sync(): |
254 | 255 | assert "graphql.document" not in span["data"] |
255 | 256 |
|
256 | 257 |
|
| 258 | +@pytest.mark.parametrize( |
| 259 | + "send_default_pii", |
| 260 | + [True, False], |
| 261 | +) |
| 262 | +def test_graphql_streamed_span_holds_query_information( |
| 263 | + sentry_init, capture_items, send_default_pii |
| 264 | +): |
| 265 | + sentry_init( |
| 266 | + integrations=[GrapheneIntegration(), FlaskIntegration()], |
| 267 | + traces_sample_rate=1.0, |
| 268 | + default_integrations=False, |
| 269 | + send_default_pii=send_default_pii, |
| 270 | + _experiments={"trace_lifecycle": "stream"}, |
| 271 | + ) |
| 272 | + items = capture_items("span") |
| 273 | + |
| 274 | + schema = Schema(query=Query) |
| 275 | + |
| 276 | + sync_app = Flask(__name__) |
| 277 | + |
| 278 | + @sync_app.route("/graphql", methods=["POST"]) |
| 279 | + def graphql_server_sync(): |
| 280 | + data = request.get_json() |
| 281 | + result = schema.execute(data["query"], operation_name=data.get("operationName")) |
| 282 | + return jsonify(result.data), 200 |
| 283 | + |
| 284 | + query = { |
| 285 | + "query": "query GreetingQuery { hello }", |
| 286 | + "operationName": "GreetingQuery", |
| 287 | + } |
| 288 | + client = sync_app.test_client() |
| 289 | + client.post("/graphql", json=query) |
| 290 | + |
| 291 | + sentry_sdk.get_client().flush() |
| 292 | + |
| 293 | + spans = [item.payload for item in items] |
| 294 | + assert len(spans) == 2 |
| 295 | + |
| 296 | + graphql_span, flask_segment = spans |
| 297 | + |
| 298 | + assert graphql_span["name"] == query["operationName"] |
| 299 | + assert graphql_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY |
| 300 | + assert ( |
| 301 | + graphql_span["attributes"]["graphql.operation.name"] == query["operationName"] |
| 302 | + ) |
| 303 | + assert graphql_span["attributes"]["graphql.operation.type"] == "query" |
| 304 | + assert graphql_span["is_segment"] is False |
| 305 | + |
| 306 | + if send_default_pii is True: |
| 307 | + assert graphql_span["attributes"]["graphql.document"] == query["query"] |
| 308 | + else: |
| 309 | + assert "graphql.document" not in graphql_span["attributes"] |
| 310 | + |
| 311 | + assert flask_segment["is_segment"] is True |
| 312 | + assert graphql_span["parent_span_id"] == flask_segment["span_id"] |
| 313 | + |
| 314 | + |
257 | 315 | def test_breadcrumbs_hold_query_information_on_error(sentry_init, capture_events): |
258 | 316 | sentry_init( |
259 | 317 | integrations=[ |
@@ -293,3 +351,50 @@ def graphql_server_sync(): |
293 | 351 | assert breadcrumb["data"]["operation_name"] == query["operationName"] |
294 | 352 | assert breadcrumb["data"]["operation_type"] == "query" |
295 | 353 | assert breadcrumb["type"] == "default" |
| 354 | + |
| 355 | + |
| 356 | +def test_breadcrumbs_hold_query_information_on_error_with_span_streaming( |
| 357 | + sentry_init, capture_items |
| 358 | +): |
| 359 | + sentry_init( |
| 360 | + integrations=[ |
| 361 | + GrapheneIntegration(), |
| 362 | + ], |
| 363 | + default_integrations=False, |
| 364 | + _experiments={"trace_lifecycle": "stream"}, |
| 365 | + ) |
| 366 | + items = capture_items("span", "event") |
| 367 | + |
| 368 | + schema = Schema(query=Query) |
| 369 | + |
| 370 | + sync_app = Flask(__name__) |
| 371 | + |
| 372 | + @sync_app.route("/graphql", methods=["POST"]) |
| 373 | + def graphql_server_sync(): |
| 374 | + data = request.get_json() |
| 375 | + result = schema.execute(data["query"], operation_name=data.get("operationName")) |
| 376 | + return jsonify(result.data), 200 |
| 377 | + |
| 378 | + query = { |
| 379 | + "query": "query ErrorQuery { goodbye }", |
| 380 | + "operationName": "ErrorQuery", |
| 381 | + } |
| 382 | + client = sync_app.test_client() |
| 383 | + client.post("/graphql", json=query) |
| 384 | + |
| 385 | + sentry_sdk.get_client().flush() |
| 386 | + |
| 387 | + events = [item.payload for item in items if item.type == "event"] |
| 388 | + assert len(events) == 1 |
| 389 | + |
| 390 | + (event,) = events |
| 391 | + assert len(event["breadcrumbs"]) == 1 |
| 392 | + |
| 393 | + breadcrumbs = event["breadcrumbs"]["values"] |
| 394 | + assert len(breadcrumbs) == 1 |
| 395 | + |
| 396 | + (breadcrumb,) = breadcrumbs |
| 397 | + assert breadcrumb["category"] == "graphql.operation" |
| 398 | + assert breadcrumb["data"]["operation_name"] == query["operationName"] |
| 399 | + assert breadcrumb["data"]["operation_type"] == "query" |
| 400 | + assert breadcrumb["type"] == "default" |
0 commit comments