Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

Is then an exemple of defining and registering per-function FuncExtensionBase custom extension? #1204

Closed springcomp closed 1 year ago

springcomp commented 1 year ago

Question

I would like to define a custom extension that is per function. This extension would capture the HttpTrigger request and perform some custom processing.

I could not find any sample for that. Is there any sample?

bhagyshricompany commented 1 year ago

Thanks for reaching out. There is already doc available for this.you can refer this below doc. https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-the-http-request

springcomp commented 1 year ago

Thanks for reaching out. There is already doc available for this.you can refer this below doc. https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-the-http-request

Thanks for the feedback. My question however relates to Python and not .Net.

Is there any example for creating a custom class inheriting from Fun extension base?

bhagyshricompany commented 1 year ago

you can try for sample below. import azure.functions as func

class MyCustomFunction(func.Function): def init(self): super().init()

def __call__(self, req: func.HttpRequest) -> func.HttpResponse:

    return func.HttpResponse("Hello, World!")
springcomp commented 1 year ago

@bhagyshricompany thanks for the feedback.

Can you please share a more complete sample? I fail to see where you are showing a FuncExtensionBase-derived class ?

bhagyshricompany commented 1 year ago

extensions/my_extension/init.py

from .my_extension import MyExtension

def main(req, context): my_extension = MyExtension() result = my_extension.func_name(req) return result

extensions/my_extension/my_extension.py

from azure.functions import FunctionExtensionBase

class MyExtension(FunctionExtensionBase):

def func_name(self, input):
    # Implementation of your function here
    return result

To use this custom extension in your Azure Function, you need to include the extensions directory in your function's PYTHONPATH environment variable. You can do this by including the following line in your function's init.py file: import os import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(file), '..', 'extensions')))

Please make sure that the my_extension.py file is located in the extensions/my_extension directory relative to the location of the init.py file.

springcomp commented 1 year ago

@bhagyshricompany thanks a lot.

This demonstrates how one would go in consuming a custom extension.

However, I’m specifically looking for an example of how to create such a custom extension.

For instance, the official document shows how to create a custom extension using an AppExtensionBase base class.

import typing
from logging import Logger
from time import time
from azure.functions import AppExtensionBase, Context, HttpResponse
class TimerExtension(AppExtensionBase):
    """A Python worker extension to record elapsed time in a function invocation
    """

…

The document then has this text:

This code inherits from AppExtensionBase so that the extension applies to every function in the app. You could have also implemented the extension on a function-level scope by inheriting from FuncExtensionBase.

However, I cannot find any example of creating such a custom extension from an FuncExtensionBase class.

bhagyshricompany commented 1 year ago

ok will update for this asap.

bhagyshricompany commented 1 year ago

You can refer below Link for good sample: Tutorial: https://learn.microsoft.com/azure/azure-functions/develop-python-worker-extensions

 

Example extension: https://github.com/census-ecosystem/opencensus-python-extensions-azure/tree/main/extensions/functions

 

Hazhzeng/python-worker-extension-timer (github.com)

ghost commented 1 year ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

springcomp commented 1 year ago

I think the answer gives me a lot of information for going further. Thanks a lot.

springcomp commented 1 year ago

For the record, here is an actual example using FuncExtensionBase: https://github.com/Hazhzeng/functions-ext-profile/blob/master/functions_ext_profile/__init__.py