Showing posts with label function app. Show all posts
Showing posts with label function app. Show all posts

Friday, February 21, 2025

How does azure function app with python code updates azure app insights

Azure Application Insights helps monitor logs, exceptions, performance metrics, and telemetry data for Azure Functions. To integrate Python-based Azure Functions with Application Insights, follow these steps:
--------

1. Prerequisites

Azure Function App (Python)

Azure Application Insights Resource

Instrumentation Key or Connection String

-------

2. Enable Application Insights for Azure Function App

Option 1: Enable via Azure CLI

az monitor app-insights component create --app <APP_INSIGHTS_NAME> --resource-group <RESOURCE_GROUP> --location eastus
az functionapp config appsettings set --name <FUNCTION_APP_NAME> --resource-group <RESOURCE_GROUP> \
    --settings "APPINSIGHTS_INSTRUMENTATIONKEY=<YOUR_INSTRUMENTATION_KEY>"

OR use the Connection String (recommended for newer versions):

az functionapp config appsettings set --name <FUNCTION_APP_NAME> --resource-group <RESOURCE_GROUP> \
    --settings "APPLICATIONINSIGHTS_CONNECTION_STRING=<YOUR_CONNECTION_STRING>"

------

3. Install & Configure Application Insights in Python

Install the Azure Monitor OpenTelemetry SDK:

pip install opentelemetry-sdk opentelemetry-exporter-azure-monitor

Modify your __init__.py to include Application Insights logging:

import logging
import azure.functions as func
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.exporter.azuremonitor import AzureMonitorSpanExporter

# Setup Application Insights Telemetry
instrumentation_key = "<YOUR_INSTRUMENTATION_KEY>"  # Or fetch from env variable
tracer = TracerProvider()
tracer.add_span_processor(SimpleSpanProcessor(AzureMonitorSpanExporter.from_connection_string(instrumentation_key)))

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info("Processing request...")
    return func.HttpResponse("Hello from Azure Function with App Insights!")

---

4. Verify Logs in Application Insights

1. Go to Azure Portal → Application Insights


2. Navigate to Logs → Run the following Kusto Query:

traces
| where timestamp > ago(10m)
| order by timestamp desc


3. Check if your function's logs appear.

How to Deploy a Python Azure Function from GitLab using a CI/CD pipeline

To deploy a Python Azure Function from GitLab using a CI/CD pipeline, follow these steps:
-------

1. Prerequisites

-Azure Subscription and an Azure Function App created
-Azure CLI installed
-GitLab repository with Python function code
-Deployment credentials (Service Principal or Publish Profile)

---

2. Setup Deployment Credentials in GitLab

Option 1: Use Azure Service Principal (Recommended)

1. Create a Service Principal in Azure:

az ad sp create-for-rbac --name "gitlab-deploy" --role contributor --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>

Output:

{
    "appId": "xxxx-xxxx-xxxx-xxxx",
    "displayName": "gitlab-deploy",
    "password": "xxxx-xxxx-xxxx-xxxx",
    "tenant": "xxxx-xxxx-xxxx-xxxx"
}


2. Store these credentials in GitLab CI/CD Variables:

AZURE_APP_ID: xxxx-xxxx-xxxx-xxxx

AZURE_PASSWORD: xxxx-xxxx-xxxx-xxxx

AZURE_TENANT_ID: xxxx-xxxx-xxxx-xxxx

AZURE_SUBSCRIPTION_ID: <Your Azure Subscription ID>

AZURE_FUNCTIONAPP_NAME: <Your Function App Name>

---

3. Create .gitlab-ci.yml for CI/CD Pipeline

Add the following .gitlab-ci.yml file to the root of your repository:

stages:
  - deploy

variables:
  AZURE_RESOURCE_GROUP: "my-resource-group"
  FUNCTION_APP_NAME: "$AZURE_FUNCTIONAPP_NAME"

deploy_to_azure:
  image: mcr.microsoft.com/azure-cli
  stage: deploy
  script:
    - echo "Logging into Azure..."
    - az login --service-principal -u "$AZURE_APP_ID" -p "$AZURE_PASSWORD" --tenant "$AZURE_TENANT_ID"
    - az account set --subscription "$AZURE_SUBSCRIPTION_ID"
    
    - echo "Deploying Function App..."
    - func azure functionapp publish $FUNCTION_APP_NAME
  only:
    - main

This script:

Logs into Azure using Service Principal

Sets the subscription

Deploys the function app from the GitLab repository

---------

4. Enable Git Deployment in Azure

Ensure Git-based deployment is enabled on the Azure Function App:

az functionapp deployment source config --name <YOUR_FUNCTION_APP_NAME> --resource-group <YOUR_RESOURCE_GROUP> --repo-url <YOUR_GITLAB_REPO_URL> --branch main

-------------

5. Commit and Push Changes

git add .gitlab-ci.yml
git commit -m "Added GitLab CI/CD for Azure Function"
git push origin main

This will trigger the pipeline and deploy your function to Azure.

---------

6. Verify Deployment

After deployment, check logs with:

az functionapp log tail --name $FUNCTION_APP_NAME --resource-group $AZURE_RESOURCE_GROUP

Or visit the Azure Portal -> Function App -> "Deployment Center" to verify the latest deployment.
Related Posts Plugin for WordPress, Blogger...