-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
170 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# AWS Lambda | ||
|
||
The [`logfire.instrument_aws_lambda`][logfire.Logfire.instrument_aws_lambda] function can be used to | ||
instrument AWS Lambda functions to automatically send traces to **Logfire**. | ||
|
||
## Installation | ||
|
||
Install `logfire` with the `aws-lambda` extra: | ||
|
||
{{ install_logfire(extras=['aws-lambda']) }} | ||
|
||
## Usage | ||
|
||
To instrument an AWS Lambda function, call the `logfire.instrument_aws_lambda` function after defining | ||
the handler function: | ||
|
||
```python | ||
import logfire | ||
|
||
logfire.configure() # (1)! | ||
|
||
|
||
def handler(event, context): | ||
return 'Hello from Lambda' | ||
|
||
logfire.instrument_aws_lambda() # (2)! | ||
``` | ||
|
||
1. Remember to set the `LOGFIRE_TOKEN` environment variable on your Lambda function configuration. | ||
2. The `logfire.instrument_aws_lambda` function must be called after defining the handler function. | ||
Otherwise, the handler function will not be instrumented. | ||
|
||
[`logfire.instrument_aws_lambda`][logfire.Logfire.instrument_aws_lambda] uses the **OpenTelemetry AWS Lambda Instrumentation** package, | ||
which you can find more information about [here][opentelemetry-aws-lambda]. | ||
|
||
[opentelemetry-aws-lambda]: https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/aws_lambda/aws_lambda.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
try: | ||
from opentelemetry.context import Context | ||
from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor | ||
from opentelemetry.metrics import MeterProvider | ||
from opentelemetry.trace import TracerProvider | ||
except ImportError: | ||
raise RuntimeError( | ||
'`logfire.instrument_aws_lambda()` requires the `opentelemetry-instrumentation-aws-lambda` package.\n' | ||
'You can install this with:\n' | ||
" pip install 'logfire[aws_lambda]'" | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any, Callable, TypedDict, Unpack | ||
|
||
LambdaEvent = Any | ||
|
||
class AwsLambdaInstrumentKwargs(TypedDict): | ||
skip_dep_check: bool | ||
event_context_extractor: Callable[[LambdaEvent], Context] | ||
|
||
|
||
def instrument_aws_lambda( | ||
*, tracer_provider: TracerProvider, meter_provider: MeterProvider, **kwargs: Unpack[AwsLambdaInstrumentKwargs] | ||
) -> None: | ||
"""Instrument the AWS Lambda runtime so that spans are automatically created for each invocation. | ||
See the `Logfire.instrument_aws_lambda` method for details. | ||
""" | ||
return AwsLambdaInstrumentor().instrument( # type: ignore[no-any-return] | ||
tracer_provider=tracer_provider, meter_provider=meter_provider, **kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.