Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logfire.instrument_aws_lambda #657

Merged
merged 11 commits into from
Dec 10, 2024
Merged

Add logfire.instrument_aws_lambda #657

merged 11 commits into from
Dec 10, 2024

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Dec 10, 2024

Suggestions on how to test this?

Copy link

cloudflare-workers-and-pages bot commented Dec 10, 2024

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2434e73
Status: ✅  Deploy successful!
Preview URL: https://a88aae38.logfire-docs.pages.dev
Branch Preview URL: https://aws-lambda-integration.logfire-docs.pages.dev

View logs

@Kludex Kludex force-pushed the aws-lambda-integration branch from 5cab65b to 202a692 Compare December 10, 2024 10:18
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (406f85d) to head (2434e73).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #657   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          135       137    +2     
  Lines        10685     10735   +50     
  Branches      1466      1471    +5     
=========================================
+ Hits         10685     10735   +50     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@alexmojaki alexmojaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What have you learned from manual testing? Didn't it not work in the python runtime?

I think basic automated testing like asserting that something got patched is good enough.

```

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be enforced with code, e.g. require calling logfire.instrument_aws_lambda(handler) or check globals()['handler'].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do it with globals(), but the function was not there, I guess I needed to go up the stack and get the locals up? It was easier to add the lambda_handler.

@Kludex
Copy link
Member Author

Kludex commented Dec 10, 2024

What have you learned from manual testing? Didn't it not work in the python runtime?

I made it work, I had to put the instrument method after the lambda function.

@alexmojaki
Copy link
Contributor

And does it do something more useful than logfire.instrument?

@Kludex
Copy link
Member Author

Kludex commented Dec 10, 2024

And does it do something more useful than logfire.instrument?

It does.

Screenshot 2024-12-10 at 13 49 18

But... I wouldn't be able to explain how useful this information is.

@Kludex Kludex force-pushed the aws-lambda-integration branch from be098ce to 71e2f1d Compare December 10, 2024 13:26
@@ -1451,12 +1452,31 @@ def instrument_sqlite3(
},
)

def instrument_aws_lambda(self, lambda_function: Any, **kwargs: Unpack[AwsLambdaInstrumentKwargs]) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be type-hinted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callable[[Any, Any], Any], or maybe Callable[[dict[str, Any], dict[str, Any]], Any]...

Copy link
Member Author

@Kludex Kludex Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure if the one on the right covers all cases...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the basic one on the left is fine. Also maybe a note in docs about this param.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you want to mention?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess just mention what they're supposed to pass in

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please suggest, and apply whatever you'd like to see.

The example contains the function as first parameter...

@Kludex Kludex requested a review from alexmojaki December 10, 2024 13:51
@@ -1451,12 +1452,33 @@ def instrument_sqlite3(
},
)

def instrument_aws_lambda(
self, lambda_function: LambdaFunction, **kwargs: Unpack[AwsLambdaInstrumentKwargs]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self, lambda_function: LambdaFunction, **kwargs: Unpack[AwsLambdaInstrumentKwargs]
self, lambda_function_handler: LambdaFunctionHandler, **kwargs: Unpack[AwsLambdaInstrumentKwargs]

Copy link
Member Author

@Kludex Kludex Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples on AWS use the lambda_function name for the function, I prefer to use the same name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A function handler can be any name; however, the default name in the Lambda console is lambda_function.lambda_handler. This function handler name reflects the function name (lambda_handler) and the file where the handler code is stored (lambda_function.py).

https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah, lambda_handler, not lambda_function. mb

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything else in this PR besides this? I'll merge after fixing this otherwise.

@Kludex Kludex enabled auto-merge (squash) December 10, 2024 17:42
@Kludex Kludex merged commit dae0932 into main Dec 10, 2024
15 checks passed
@Kludex Kludex deleted the aws-lambda-integration branch December 10, 2024 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants