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

Extension to instrument Azure Functions #1766

Merged
merged 7 commits into from Mar 21, 2023

Conversation

beniwohli
Copy link
Contributor

What does this pull request do?

We use a Worker Extension to insert a middleware of sorts into the execution flow. This allows us to access the context independent of the function definition.

Related issues

Closes #1675
Closes #1722

We use a Worker Extension to insert a middleware of sorts into the
execution flow. This allows us to access the context independent
of the function definition.
Copy link
Contributor

@basepi basepi left a comment

Choose a reason for hiding this comment

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

Looks great! Just some docs nits, mostly.

[[azure-functions-support]]
=== Monitoring Azure Functions

Incorporating Elastic APM into your Azure Functions App is straight forward!
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm leaning towards deleting this line. I'm not sure it adds anything useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, true, it got cargo-culted from one docs page to the next. Should probably remove all of them.

docs/serverless-azure-functions.asciidoc Outdated Show resolved Hide resolved

Elastic APM uses https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=asgi%2Capplication-level&pivots=python-mode-configuration#python-worker-extensions[Worker Extensions]
to instrument Azure Functions.
This feature has to be enabled in your Azure Functions App.
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
This feature has to be enabled in your Azure Functions App.
This feature is not enabled by default, and must be enabled in your Azure Functions App.

ElasticAPMExtension.configure()
----

Put them somewhere at the top of your Python file, before the function definitions.
Copy link
Contributor

Choose a reason for hiding this comment

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

How critical is their placement? Do they need to be before any init code or just before the function defs?


You need to add `elastic-apm` as a dependency for your Functions app.
Simply add `elastic-apm` to your `requirements.txt` file.
We recommend to pin the version to the current newest version of the agent, and periodically update it.
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
We recommend to pin the version to the current newest version of the agent, and periodically update it.
We recommend pinning the version to the current newest version of the agent, and periodically updating the version.

The APM Python agent is configured through https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal#settings[App Settings].
These are then picked up by the agent as environment variables.

For the minimal configuration, you will need the _APM Server URL_ to set the destination for APM data and an _{apm-guide-ref}/secret-token.html[APM Secret Token]_.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we link to our configuration docs for these?

you can retrieve the `Client` object for capturing exceptions/messages
using <<api-get-client,`get_client`>>.

NOTE: Currently, only HTTP and timer triggers are supported.
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this should be at the top? I don't know how much Azure function usage falls outside of these triggers but it seems like an important note.

client = None

@classmethod
def init(cls):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be gating this to obey enabled and instrument configs? I'm assuming this is getting called in the AppExtensionBase somewhere, perhaps we should put in a docstring noting where/how this is called?


@classmethod
def post_function_load_app_level(cls, function_name: str, function_directory: str, *args, **kwargs):
with codecs.open(os.path.join(function_directory, "function.json")) as f:
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason you're using codecs.open instead of just open? My understanding is that codecs.open is obsolete in py3.x but I could have missed something.

@beniwohli beniwohli merged commit 9242427 into elastic:main Mar 21, 2023
94 checks passed
APM-Agents (OLD) automation moved this from In Progress to Done Mar 21, 2023
@beniwohli beniwohli deleted the azure-functions branch March 21, 2023 15:53
@beniwohli
Copy link
Contributor Author

@basepi regarding your comment about gating instrument() with enabled/instrument, I'll wait for #1780 to merge and then use the same WrapperConfig approach in a separate PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[META 745] Implement Azure Functions metadata detection Support for azure functions
2 participants