zmoog / public-notes

Apache License 2.0
0 stars 1 forks source link

Figure out how to collect Azure Functions logs #36

Open zmoog opened 1 year ago

zmoog commented 1 year ago

I want to start collecting Azure Functions logs and sending them to an Event Hub to prepare the ingestion using the Elastic Agent.


Summary

This issue thread is based on the lesson learned during #17

zmoog commented 1 year ago

Install dependencies

zmoog commented 1 year ago

Create a FunctionApp project

Create a virtualenv for this project:

$ python3 -m venv venv
$ func init hello-world --python
Found Python version 3.8.10 (python3).
Did you know? There is a new Python programming model in public preview. For fewer files and a decorator based approach, learn how you can try it out today at https://aka.ms/pythonprogrammingmodel
Writing requirements.txt
Writing getting_started.md
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing /home/zmoog/code/projects/zmoog/public-notes/36/hello-world/.vscode/extensions.json

Here's the result:

$ tree hello-world/
hello-world/
├── getting_started.md
├── host.json
├── local.settings.json
└── requirements.txt

Now move into the function app project and install the dependencies:

cd hello-world

$ pip install -r requirements.txt
Collecting azure-functions
  Downloading azure_functions-1.14.0-py3-none-any.whl (165 kB)
     |████████████████████████████████| 165 kB 2.4 MB/s
Installing collected packages: azure-functions
Successfully installed azure-functions-1.14.0

Time to create a new function:

$ func new --name hello --template "HTTP trigger" --authlevel "anonymous"
Select a number for template:HTTP trigger
Function name: [HttpTrigger] Writing /home/zmoog/code/projects/zmoog/public-notes/36/hello-world/hello/__init__.py
Writing /home/zmoog/code/projects/zmoog/public-notes/36/hello-world/hello/function.json
The function "hello" was created successfully from the "HTTP trigger" template.
zmoog commented 1 year ago

Run in locally

$ func start
Found Python version 3.8.10 (python3).

Azure Functions Core Tools
Core Tools Version:       4.0.4895 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.13.0.19486

Functions:

        hello: [GET,POST] http://localhost:7071/api/hello

For detailed output, run func with --verbose flag.
[2023-05-17T08:04:33.279Z] Worker process started and initialized.
[2023-05-17T08:04:36.188Z] Host lock lease acquired by instance ID '000000000000000000000000000C2005'.

Let's test the function and the HTTP trigger are running fine:

$ curl -i http://localhost:7071/api/hello
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Wed, 17 May 2023 08:06:26 GMT
Server: Kestrel
Transfer-Encoding: chunked

This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
zmoog commented 1 year ago

Deploy on Azure

To deploy the FunctionApp on Azure we need to set up some infrastructure first. To keep things as simple as possible, we can use the Azure CLI.

Create the resources

$ az group create --name mbranca-test-rg --location eastus
{
  "id": "/subscriptions/3e5e69b7-ff74-421e-bc42-66cbbbf888df/resourceGroups/mbranca-test-rg",
  "location": "eastus",
  "managedBy": null,
  "name": "mbranca-test-rg",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

$ az storage account create --name mbrancatestsa --sku Standard_LRS --resource-group mbranca-test-rg
{..}

$ az functionapp create --consumption-plan-location eastus --runtime python --runtime-version 3.9 --functions-version 4 --name mbranca-test-function --os-type linux --storage-account mbrancatestsa --resource-group mbranca-test-rg
{..}

Deploy

With all the cloud resources ready to go, we can deploy the function code in Azure:

$ func azure functionapp publish mbranca-test-function
....
Deployment successful. deployer = Push-Deployer deploymentPath = Functions App ZipDeploy. Extract zip. Remote build.
Remote build succeeded!
Syncing triggers...
Functions in mbranca-test-function:
    hello - [httpTrigger]
        Invoke url: https://mbranca-test-function.azurewebsites.net/api/hello

Test

Quick test to check everything is okay:

$ $ curl -i https://mbranca-test-function.azurewebsites.net/api/hello
HTTP/2 200
content-type: text/plain; charset=utf-8
date: Wed, 17 May 2023 08:23:51 GMT
server: Kestrel
request-context: appId=cid-v1:513cc8f4-05ea-4cd3-a52e-b50bc3e57638

This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
zmoog commented 1 year ago

Generate logs

Here's a simple mechanism to invoke the Azure Functions every 10 seconds in the terminal:

watch -n 5 curl -i https://mbranca-test-function.azurewebsites.net/api/hello

This will generate a steady flow of logs for our exploration and testing.

zmoog commented 1 year ago

Here are the logs from the Azure Portal perspective:

CleanShot 2023-05-17 at 10 34 09@2x

zmoog commented 1 year ago

Route the logs to an event hub

  1. Visit Monitoring > Diagnostic settings
  2. Click on Add diagnostic settings

CleanShot 2023-05-17 at 10 37 10@2x

Then select all logs and route them to an event hub:

CleanShot 2023-05-17 at 10 37 36@2x

zmoog commented 1 year ago

Get the logs using the eventhub cli tool

Install the eventhub cli tool:

pip install eventhubs

You can set up the CLI tool using the environment variables for the whole session:

export EVENTHUB_CONNECTION_STRING="Endpoint=sb:// ... "
export EVENTHUB_NAMESPACE="... "
export EVENTHUB_NAME=".."

Start receiving the logs!

$ eh -v eventdata receive
Receiving events from functionapplogs
Received event from partition 0: {"records": [{ "time": "2023-05-17T08:53:31Z", "reso .... 
zmoog commented 1 year ago

Clean up

Be a good cloud citizen, and tear down the cloud resources when you don't need them anymore:

$  az group delete --resource-group mbranca-test-rg
Are you sure you want to perform this operation? (y/n): y

Deleting the resource group should be enough.