|
7 | 7 | from botocore.config import Config |
8 | 8 | from botocore.exceptions import ClientError, EndpointConnectionError |
9 | 9 | from botocore.response import StreamingBody |
| 10 | +from botocore.stub import Stubber |
10 | 11 |
|
11 | 12 | import sentry_sdk |
12 | 13 | from sentry_sdk.consts import OP, SPANDATA |
@@ -268,6 +269,165 @@ def _assert_one_failed_span(spans, span_streaming): |
268 | 269 | _assert_span_finished(spans[0], span_streaming) |
269 | 270 |
|
270 | 271 |
|
| 272 | +def _capture_stubbed_client_span( |
| 273 | + client, |
| 274 | + method_name, |
| 275 | + api_params, |
| 276 | + capture_items, |
| 277 | + span_streaming, |
| 278 | +): |
| 279 | + with Stubber(client) as stubber: |
| 280 | + stubber.add_response(method_name, {}, api_params) |
| 281 | + spans_by_op = _capture_boto3_spans_by_op( |
| 282 | + lambda: getattr(client, method_name)(**api_params), |
| 283 | + capture_items, |
| 284 | + span_streaming, |
| 285 | + ) |
| 286 | + |
| 287 | + client_spans = spans_by_op.get(OP.HTTP_CLIENT, []) |
| 288 | + assert len(client_spans) == 1 |
| 289 | + return client_spans[0] |
| 290 | + |
| 291 | + |
| 292 | +def _span_attributes(span, span_streaming): |
| 293 | + return span["attributes"] if span_streaming else span["data"] |
| 294 | + |
| 295 | + |
| 296 | +@pytest.mark.parametrize( |
| 297 | + ( |
| 298 | + "service_name", |
| 299 | + "method_name", |
| 300 | + "api_params", |
| 301 | + "span_name", |
| 302 | + "rpc_service", |
| 303 | + "rpc_method", |
| 304 | + "endpoint_url", |
| 305 | + "server_address", |
| 306 | + "server_port", |
| 307 | + ), |
| 308 | + [ |
| 309 | + ( |
| 310 | + "s3", |
| 311 | + "head_object", |
| 312 | + {"Bucket": "bucket", "Key": "foo"}, |
| 313 | + "aws.s3.HeadObject", |
| 314 | + "S3", |
| 315 | + "HeadObject", |
| 316 | + "http://localhost:4566", |
| 317 | + "localhost", |
| 318 | + 4566, |
| 319 | + ), |
| 320 | + ( |
| 321 | + "events", |
| 322 | + "list_event_buses", |
| 323 | + {}, |
| 324 | + "aws.eventbridge.ListEventBuses", |
| 325 | + "EventBridge", |
| 326 | + "ListEventBuses", |
| 327 | + None, |
| 328 | + "events.eu-north-1.amazonaws.com", |
| 329 | + 443, |
| 330 | + ), |
| 331 | + ], |
| 332 | +) |
| 333 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 334 | +def test_client_call_has_common_attributes( |
| 335 | + capture_items, |
| 336 | + client_factory, |
| 337 | + span_streaming, |
| 338 | + service_name, |
| 339 | + method_name, |
| 340 | + api_params, |
| 341 | + span_name, |
| 342 | + rpc_service, |
| 343 | + rpc_method, |
| 344 | + endpoint_url, |
| 345 | + server_address, |
| 346 | + server_port, |
| 347 | +): |
| 348 | + client = client_factory(service_name=service_name, endpoint_url=endpoint_url) |
| 349 | + span = _capture_stubbed_client_span( |
| 350 | + client, |
| 351 | + method_name, |
| 352 | + api_params, |
| 353 | + capture_items, |
| 354 | + span_streaming, |
| 355 | + ) |
| 356 | + attributes = _span_attributes(span, span_streaming) |
| 357 | + |
| 358 | + assert span["name" if span_streaming else "description"] == span_name |
| 359 | + assert attributes[SPANDATA.RPC_SERVICE] == rpc_service |
| 360 | + assert attributes[SPANDATA.RPC_METHOD] == rpc_method |
| 361 | + assert attributes[SPANDATA.RPC_SYSTEM_NAME] == "aws-api" |
| 362 | + assert attributes[SPANDATA.CLOUD_REGION] == "eu-north-1" |
| 363 | + assert attributes[SPANDATA.SERVER_ADDRESS] == server_address |
| 364 | + assert attributes[SPANDATA.SERVER_PORT] == server_port |
| 365 | + |
| 366 | + |
| 367 | +def test_client_call_attributes_are_available_at_span_creation( |
| 368 | + sentry_init, capture_items |
| 369 | +): |
| 370 | + # attribute-based filtering happens during span creation, at the same boundary |
| 371 | + # where creation attributes are made available for sampling decisions. |
| 372 | + sentry_init( |
| 373 | + traces_sample_rate=1.0, |
| 374 | + integrations=[Boto3Integration()], |
| 375 | + trace_lifecycle="stream", |
| 376 | + ignore_spans=[ |
| 377 | + { |
| 378 | + "attributes": { |
| 379 | + SPANDATA.RPC_METHOD: "HeadObject", |
| 380 | + SPANDATA.RPC_SERVICE: "S3", |
| 381 | + SPANDATA.RPC_SYSTEM_NAME: "aws-api", |
| 382 | + SPANDATA.SERVER_ADDRESS: "s3.eu-north-1.amazonaws.com", |
| 383 | + SPANDATA.SERVER_PORT: 443, |
| 384 | + } |
| 385 | + } |
| 386 | + ], |
| 387 | + ) |
| 388 | + client = session.client("s3") |
| 389 | + items = capture_items("span") |
| 390 | + |
| 391 | + with Stubber(client) as stubber: |
| 392 | + stubber.add_response("head_object", {}, {"Bucket": "bucket", "Key": "foo"}) |
| 393 | + with sentry_sdk.traces.start_span(name="parent"): |
| 394 | + client.head_object(Bucket="bucket", Key="foo") |
| 395 | + |
| 396 | + sentry_sdk.flush() |
| 397 | + client_spans = [ |
| 398 | + item.payload |
| 399 | + for item in items |
| 400 | + if item.payload["attributes"].get(SPANDATA.SENTRY_ORIGIN) |
| 401 | + == Boto3Integration.origin |
| 402 | + ] |
| 403 | + assert client_spans == [] |
| 404 | + |
| 405 | + |
| 406 | +def test_client_call_omits_missing_region( |
| 407 | + sentry_init, |
| 408 | + capture_items, |
| 409 | + monkeypatch, |
| 410 | +): |
| 411 | + sentry_init( |
| 412 | + traces_sample_rate=1.0, |
| 413 | + integrations=[Boto3Integration()], |
| 414 | + trace_lifecycle="stream", |
| 415 | + server_name="", |
| 416 | + ) |
| 417 | + client = session.client("s3") |
| 418 | + monkeypatch.setattr(type(client.meta), "region_name", property(lambda _: None)) |
| 419 | + |
| 420 | + span = _capture_stubbed_client_span( |
| 421 | + client, |
| 422 | + "head_object", |
| 423 | + {"Bucket": "bucket", "Key": "foo"}, |
| 424 | + capture_items, |
| 425 | + span_streaming=True, |
| 426 | + ) |
| 427 | + |
| 428 | + assert SPANDATA.CLOUD_REGION not in span["attributes"] |
| 429 | + |
| 430 | + |
271 | 431 | @pytest.mark.parametrize("span_streaming", [True, False]) |
272 | 432 | def test_retry_attempts_share_one_client_span( |
273 | 433 | capture_items, |
|
0 commit comments