Tracing
CLASS | DESCRIPTION |
---|---|
Tracer |
Tracer using AWS-XRay to provide decorators with known defaults for Lambda functions |
Tracer ¶
Tracer(
service: str | None = None,
disabled: bool | None = None,
auto_patch: bool | None = None,
patch_modules: Sequence[str] | None = None,
provider: BaseProvider | None = None,
)
Tracer using AWS-XRay to provide decorators with known defaults for Lambda functions
When running locally, it detects whether it's running via SAM CLI, and if it is it returns dummy segments/subsegments instead.
By default, it patches all available libraries supported by X-Ray SDK. Patching is automatically disabled when running locally via SAM CLI or by any other means.
Ref: https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/thirdparty.html
Tracer keeps a copy of its configuration as it can be instantiated more than once. This
is useful when you are using your own middlewares and want to utilize an existing Tracer.
Make sure to set auto_patch=False
in subsequent Tracer instances to avoid double patching.
Environment variables
POWERTOOLS_TRACE_DISABLED : str
disable tracer (e.g. "true", "True", "TRUE"
)
POWERTOOLS_SERVICE_NAME : str
service name
POWERTOOLS_TRACER_CAPTURE_RESPONSE : str
disable auto-capture response as metadata (e.g. "true", "True", "TRUE"
)
POWERTOOLS_TRACER_CAPTURE_ERROR : str
disable auto-capture error as metadata (e.g. "true", "True", "TRUE"
)
PARAMETER | DESCRIPTION |
---|---|
service
|
Service name that will be appended in all tracing metadata
TYPE:
|
auto_patch
|
Patch existing imported modules during initialization, by default True
TYPE:
|
disabled
|
Flag to explicitly disable tracing, useful when running/testing locally
TYPE:
|
patch_modules
|
Tuple of modules supported by tracing provider to patch, by default all modules are patched |
provider
|
Tracing provider, by default it is aws_xray_sdk.core.xray_recorder
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tracer
|
Tracer instance with imported modules patched |
Example
A Lambda function using Tracer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Booking Lambda function using Tracer that adds additional annotation/metadata
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
A Lambda function using service name via POWERTOOLS_SERVICE_NAME
1 2 3 4 5 6 7 8 9 |
|
Reuse an existing instance of Tracer anywhere in the code
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Limitations
- Async handler not supported
METHOD | DESCRIPTION |
---|---|
capture_lambda_handler |
Decorator to create subsegment for lambda handlers |
capture_method |
Decorator to create subsegment for arbitrary functions |
ignore_endpoint |
If you want to ignore certain httplib requests you can do so based on the hostname or URL that is being |
patch |
Patch modules for instrumentation. |
put_annotation |
Adds annotation to existing segment or subsegment |
put_metadata |
Adds metadata to existing segment or subsegment |
Source code in aws_lambda_powertools/tracing/tracer.py
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 |
|
capture_lambda_handler ¶
capture_lambda_handler(
lambda_handler: (
Callable[[T, Any], Any]
| Callable[[T, Any, Any], Any]
| None
) = None,
capture_response: bool | None = None,
capture_error: bool | None = None,
)
Decorator to create subsegment for lambda handlers
As Lambda follows (event, context) signature we can remove some of the boilerplate and also capture any exception any Lambda function throws or its response as metadata
PARAMETER | DESCRIPTION |
---|---|
lambda_handler
|
Method to annotate on
TYPE:
|
capture_response
|
Instructs tracer to not include handler's response as metadata
TYPE:
|
capture_error
|
Instructs tracer to not include handler's error as metadata, by default True
TYPE:
|
Example
Lambda function using capture_lambda_handler decorator
1 2 3 4 |
|
Preventing Tracer to log response as metadata
1 2 3 4 |
|
RAISES | DESCRIPTION |
---|---|
err
|
Exception raised by method |
Source code in aws_lambda_powertools/tracing/tracer.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
capture_method ¶
capture_method(method: AnyCallableT) -> AnyCallableT
capture_method(
method: None = None,
capture_response: bool | None = None,
capture_error: bool | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
capture_method(
method: AnyCallableT | None = None,
capture_response: bool | None = None,
capture_error: bool | None = None,
) -> AnyCallableT
Decorator to create subsegment for arbitrary functions
It also captures both response and exceptions as metadata
and creates a subsegment named ## <method_module.method_qualifiedname>
see here: Qualified name for classes and functions¶
When running async functions concurrently, methods may impact each others subsegment, and can trigger and AlreadyEndedException from X-Ray due to async nature.
For this use case, either use capture_method
only where
async.gather
is called, or use in_subsegment_async
context manager via our escape hatch mechanism - See examples.
PARAMETER | DESCRIPTION |
---|---|
method
|
Method to annotate on
TYPE:
|
capture_response
|
Instructs tracer to not include method's response as metadata
TYPE:
|
capture_error
|
Instructs tracer to not include handler's error as metadata, by default True
TYPE:
|
Example
Custom function using capture_method decorator
1 2 3 |
|
Custom async method using capture_method decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Custom generator function using capture_method decorator
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Custom generator context manager using capture_method decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Tracing nested async calls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Safely tracing concurrent async calls with decorator
This may not needed once this bug is closed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Safely tracing each concurrent async calls with escape hatch
This may not needed once this bug is closed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
RAISES | DESCRIPTION |
---|---|
err
|
Exception raised by method |
Source code in aws_lambda_powertools/tracing/tracer.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
|
ignore_endpoint ¶
ignore_endpoint(
hostname: str | None = None,
urls: list[str] | None = None,
)
If you want to ignore certain httplib requests you can do so based on the hostname or URL that is being requested.
NOTE: If the provider is not xray, nothing will be added to ignore list
Documentation
- https://github.com/aws/aws-xray-sdk-python#ignoring-httplib-requests
PARAMETER | DESCRIPTION |
---|---|
hostname
|
The hostname is matched using the Python fnmatch library which does Unix glob style matching.
TYPE:
|
urls
|
List of urls to ignore. Example |
Source code in aws_lambda_powertools/tracing/tracer.py
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 |
|
patch ¶
patch(modules: Sequence[str] | None = None)
Patch modules for instrumentation.
Patches all supported modules by default if none are given.
PARAMETER | DESCRIPTION |
---|---|
modules
|
List of modules to be patched, optional by default |
Source code in aws_lambda_powertools/tracing/tracer.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
put_annotation ¶
put_annotation(key: str, value: str | Number | bool)
Adds annotation to existing segment or subsegment
PARAMETER | DESCRIPTION |
---|---|
key
|
Annotation key
TYPE:
|
value
|
Value for annotation |
Example
Custom annotation for a pseudo service named payment
1 2 |
|
Source code in aws_lambda_powertools/tracing/tracer.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
put_metadata ¶
put_metadata(
key: str, value: Any, namespace: str | None = None
)
Adds metadata to existing segment or subsegment
PARAMETER | DESCRIPTION |
---|---|
key
|
Metadata key
TYPE:
|
value
|
Value for metadata
TYPE:
|
namespace
|
Namespace that metadata will lie under, by default None
TYPE:
|
Example
Custom metadata for a pseudo service named payment
1 2 3 |
|
Source code in aws_lambda_powertools/tracing/tracer.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|