Skip to content

feat: supporting EventHub trigger SDK bindings #101

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

Merged
merged 28 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
21 changes: 21 additions & 0 deletions azurefunctions-extensions-bindings-eventhub/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) Microsoft Corporation.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions azurefunctions-extensions-bindings-eventhub/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
recursive-include azure *.py *.pyi
recursive-include tests *.py
include LICENSE README.md
90 changes: 90 additions & 0 deletions azurefunctions-extensions-bindings-eventhub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Azure Functions Extensions Bindings EventHub library for Python
This library allows an EventHub Trigger binding in Python Function Apps to recognize and bind to the types from the
Azure EventHub sdk (EventData).

EventHub types can be generated from:

[Source code](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-eventhub)
[Package (PyPi)](https://pypi.org/project/azurefunctions-extensions-bindings-eventhub/)
| API reference documentation
| Product documentation
| [Samples](hhttps://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-eventhub/samples)


## Getting started

### Prerequisites
* Python 3.9 or later is required to use this package. For more details, please read our page on [Python Functions version support policy](https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=isolated-process%2Cv4&pivots=programming-language-python#languages).

* You must have an [Azure subscription](https://azure.microsoft.com/free/) and an
[Azure storage account](https://docs.microsoft.com/azure/storage/common/storage-account-overview) to use this package.

### Install the package
Install the Azure Functions Extensions Bindings EventHub library for Python with pip:

```bash
pip install azurefunctions-extensions-bindings-eventhub
```

### Create a storage account
If you wish to create a new storage account, you can use the
[Azure Portal](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal),
[Azure PowerShell](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell),
or [Azure CLI](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli):

```bash
# Create a new resource group to hold the storage account -
# if using an existing resource group, skip this step
az group create --name my-resource-group --location westus2

# Create the storage account
az storage account create -n my-storage-account-name -g my-resource-group
```

### Bind to the SDK-type
The Azure Functions Extensions Bindings EventHub library for Python allows you to create a function app with an EventHub Trigger
and define the type as an EventData. Instead of receiving an InputStream, when the function is executed, the type returned will be the defined SDK-type and have all of the properties and methods available as seen in the Azure EventHub library for Python.


```python
import logging
import azure.functions as func
import azurefunctions.extensions.bindings.eventhub as eh

@app.event_hub_message_trigger(
arg_name="eh_data", event_hub_name="EVENTHUB_NAME", connection="AzureWebJobsStorage"
)
def eventhub_trigger(eh_data: eh.EventHubData):
logging.info(
"Python EventHub trigger processed an event %s",
eh_data.body_as_str()
)
```

## Troubleshooting
### General
The SDK-types raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/README.md).

This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the `error_code` attribute, i.e, `exception.error_code`.

## Next steps

### More sample code

Get started with our [EventHub samples](hhttps://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-eventhub/samples).

Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with EventHubs:

* [eventhub_samples_eventhubdata](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-eventhub/samples/eventhub_samples_eventhubdata) - Examples for using the EventHubData type:
* From EventHubTrigger

### Additional documentation
For more information on the Azure EventHub SDK, see the [Azure EventHub documentation](https://learn.microsoft.com/en-us/azure/event-hubs/) on learn.microsoft.com
and the [Azure EventHub README](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/eventhub/azure-eventhub/README.md).

## Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from .eventHubData import EventHubData
from .eventHubDataConverter import EventHubDataConverter

__all__ = [
"EventHubData",
"EventHubDataConverter",
]

__version__ = "1.0.0b2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import re
from typing import Union
import uamqp

from azure.eventhub import EventData
from azurefunctions.extensions.base import Datum, SdkType


class EventHubData(SdkType):
def __init__(self, *, data: Union[bytes, Datum]) -> None:
# model_binding_data properties
self._data = data
self._version = None
self._source = None
self._content_type = None
self._content = None
self.decoded_message = None
if self._data:
self._version = data.version
self._source = data.source
self._content_type = data.content_type
self._content = data.content
self.decoded_message = self.__get_eventhub_content(self._content)

def __get_eventhub_content(self, content):
if content:
return uamqp.Message().decode_from_bytes(content)
else:
return None

def get_sdk_type(self):
# https://github.com/Azure/azure-sdk-for-python/issues/39711
if self.decoded_message:
return EventData._from_message(self.decoded_message)
else:
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import Any

from azurefunctions.extensions.base import Datum, InConverter, OutConverter

from .eventHubData import EventHubData


class EventHubDataConverter(
InConverter,
OutConverter,
binding="eventHub",
trigger="eventHubTrigger",
):
@classmethod
def check_input_type_annotation(cls, pytype: type) -> bool:
return issubclass(
pytype, (EventHubData)
)

@classmethod
def decode(cls, data: Datum, *, trigger_metadata, pytype) -> Any:
if data is None or data.type is None:
return None

data_type = data.type

if data_type == "model_binding_data":
data = data.value
else:
raise ValueError(
f'unexpected type of data received for the "eventhub" binding '
f": {data_type!r}"
)

# Determines which sdk type to return based on pytype
if pytype == EventHubData:
return EventHubData(data=data).get_sdk_type()
else:
return None
49 changes: 49 additions & 0 deletions azurefunctions-extensions-bindings-eventhub/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[build-system]
requires = ["setuptools >= 61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "azurefunctions-extensions-bindings-eventhub"
dynamic = ["version"]
requires-python = ">=3.9"
authors = [{ name = "Azure Functions team at Microsoft Corp.", email = "[email protected]"}]
description = "EventHub Python worker extension for Azure Functions."
readme = "README.md"
license = {text = "MIT License"}
classifiers= [
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Operating System :: MacOS :: MacOS X',
'Environment :: Web Environment',
'Development Status :: 5 - Production/Stable',
]
dependencies = [
'azurefunctions-extensions-base',
'azure-eventhub~=5.13.0',
'azure-identity~=1.19.0'
]

[project.optional-dependencies]
dev = [
'pytest',
'pytest-cov',
'coverage',
'pytest-instafail',
'pre-commit'
]

[tool.setuptools.dynamic]
version = {attr = "azurefunctions.extensions.bindings.eventhub.__version__"}

[tool.setuptools.packages.find]
exclude = [
'azurefunctions.extensions.bindings','azurefunctions.extensions',
'azurefunctions', 'tests', 'samples'
]

58 changes: 58 additions & 0 deletions azurefunctions-extensions-bindings-eventhub/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
page_type: sample
languages:
- python
products:
- azure
- azure-functions
- azure-functions-extensions
- azurefunctions-extensions-bindings-eventhub
urlFragment: extension-eventhub-samples
---

# Azure Functions Extension EventHub library for Python samples

These are code samples that show common scenario operations with the Azure Functions Extension EventHub library.

These samples relate to the Azure EventHub library being used as part of a Python Function App. For
examples on how to use the Azure EventHub library, please see [Azure EventHub samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/eventhub/azure-eventhub/samples)

* [eventhub_samples_eventhubdata](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-eventhub/samples/eventhub_samples_eventhubdata) - Examples for using the EventData type:
* From EventHubTrigger

## Prerequisites
* Python 3.9 or later is required to use this package. For more details, please read our page on [Python Functions version support policy](https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=isolated-process%2Cv4&pivots=programming-language-python#languages).
* You must have an [Azure subscription](https://azure.microsoft.com/free/) and an
[Azure storage account](https://docs.microsoft.com/azure/storage/common/storage-account-overview) to use this package.

## Setup

1. Install [Core Tools](https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Cisolated-process%2Cnode-v4%2Cpython-v2%2Chttp-trigger%2Ccontainer-apps&pivots=programming-language-python)
2. Install the Azure Functions Extension EventHub library for Python with [pip](https://pypi.org/project/pip/):

```bash
pip install azurefunctions-extensions-bindings-eventhub
```

3. Clone or download this sample repository
4. Open the sample folder in Visual Studio Code or your IDE of choice.

## Running the samples

1. Open a terminal window and `cd` to the directory that the sample you wish to run is saved in.
2. Set the environment variables specified in the sample file you wish to run.
3. Install the required dependencies
```bash
pip install -r requirements.txt
```
4. Start the Functions runtime
```bash
func start
```
5. Execute the function by uploading an event to the EventHub that is being targeted.

## Next steps

Visit the [SDK-type bindings in Python reference documentation]() to learn more about how to use SDK-type bindings in a Python Function App and the
[API reference documentation](https://learn.microsoft.com/en-us/python/api/azure-eventhub/azure.eventhub?view=azure-python) to learn more about
what you can do with the Azure EventHub library.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import logging
import azure.functions as func
import azurefunctions.extensions.bindings.eventhub as eh

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

"""
FOLDER: eventhub_samples_eventhubdata
DESCRIPTION:
These samples demonstrate how to obtain EventHubData from an EventHub Trigger.
USAGE:
There are different ways to connect to an EventHub via the connection property and
envionrment variables specifiied in local.settings.json

The connection property can be:
- The name of an application setting containing a connection string
- The name of a shared prefix for multiple application settings, together defining an identity-based connection

For more information, see:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cfunctionsv2%2Cextensionv5&pivots=programming-language-python
"""


@app.event_hub_message_trigger(
arg_name="eh_data", event_hub_name="EVENTHUB_NAME", connection="AzureWebJobsStorage"
)
def eventhub_trigger(eh_data: eh.EventHubData):
logging.info(
"Python EventHub trigger processed an event %s",
eh_data.body_as_str()
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
Loading