This document explains how to set up and run real integration tests with Application Insights.
- Azure Application Insights Resource: You need access to an Azure Application Insights instance
- Azure Credentials: Appropriate permissions to query the Application Insights resource
- Python Dependencies: Azure SDK packages installed
Install the required Azure dependencies:
pip install azure-monitor-query azure-identityOr install with the azure extras:
pip install .[azure]Set the following environment variables before running the tests:
AZURE_APPLICATION_INSIGHTS_RESOURCE_ID: The full resource ID of your Application Insights instance/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Insights/components/{app-insights-name}
AZURE_TENANT_ID: Your Azure tenant IDAZURE_CLIENT_ID: Your Azure client ID (for service principal authentication)AZURE_CLIENT_SECRET: Your Azure client secret (for service principal authentication)
If these are not provided, the tests will use DefaultAzureCredential which tries multiple authentication methods.
The tests support multiple authentication methods:
-
Service Principal (recommended for CI/CD):
export AZURE_TENANT_ID="your-tenant-id" export AZURE_CLIENT_ID="your-client-id" export AZURE_CLIENT_SECRET="your-client-secret"
-
Azure CLI (for local development):
az login
-
Managed Identity (when running on Azure):
- No additional configuration needed
-
Visual Studio Code (for local development):
- Install Azure extension and sign in
pytest tests/integration/test_span_hydrator.py::TestRealApplicationInsightsIntegration -v# Run connection tests only
pytest tests/integration/test_span_hydrator.py::TestRealApplicationInsightsIntegration::test_connection_to_application_insights -v
# Run data discovery tests
pytest tests/integration/test_span_hydrator.py::TestRealDataScenarios -v
# Run performance tests (these take longer)
pytest tests/integration/test_span_hydrator.py::TestPerformanceWithRealData -v# Run all integration tests
pytest -m "integration" -v
# Run tests that use real data
pytest -m "real_data" -v
# Run slow tests
pytest -m "slow" -v$env:AZURE_APPLICATION_INSIGHTS_RESOURCE_ID = "/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/my-rg/providers/Microsoft.Insights/components/my-app-insights"
$env:AZURE_TENANT_ID = "your-tenant-id"
$env:AZURE_CLIENT_ID = "your-client-id"
$env:AZURE_CLIENT_SECRET = "your-client-secret"
pytest tests/integration/test_span_hydrator.py::TestRealApplicationInsightsIntegration -vexport AZURE_APPLICATION_INSIGHTS_RESOURCE_ID="/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/my-rg/providers/Microsoft.Insights/components/my-app-insights"
export AZURE_TENANT_ID="your-tenant-id"
export AZURE_CLIENT_ID="your-client-id"
export AZURE_CLIENT_SECRET="your-client-secret"
pytest tests/integration/test_span_hydrator.py::TestRealApplicationInsightsIntegration -vtest_connection_to_application_insights: Verifies basic connectivity
test_query_recent_spans: Tests span retrieval functionalitytest_query_child_spans_with_real_data: Tests child span queriestest_query_child_spans_with_gen_ai_filter: Tests filtered queries
test_error_handling_with_invalid_time_range: Tests large time rangestest_connector_authentication_error_handling: Tests auth error handling
test_discover_available_data: Explores available data in your instancetest_discover_gen_ai_data: Looks for GenAI-specific data
test_query_performance: Measures single query performancetest_multiple_queries_performance: Measures multiple query performance
If you have specific span IDs in your Application Insights data:
- Edit the
manual_test_with_known_span_id()function in the test file - Replace
"your-actual-span-id-here"with a real span ID - Run the function directly:
python tests/integration/test_span_hydrator.py-
Authentication Errors:
- Verify your credentials are correct
- Check that you have read permissions on the Application Insights resource
- Try using
az loginfor local testing
-
Resource Not Found:
- Verify the resource ID format is correct
- Check that the Application Insights resource exists
- Ensure you have access to the subscription and resource group
-
No Data Returned:
- Check the time range (try increasing from hours to days)
- Verify that your Application Insights instance has data
- Use the data discovery tests to explore available data
-
Timeout Errors:
- Increase the timeout in
ApplicationInsightsConfig - Reduce the time range for queries
- Check your network connection to Azure
- Increase the timeout in
Enable debug logging to see detailed query information:
import logging
logging.basicConfig(level=logging.DEBUG)Test your queries directly using the Azure portal or Azure CLI:
az monitor log-analytics query \
--workspace "/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Insights/components/{name}" \
--analytics-query "dependencies | take 5"For automated testing in CI/CD pipelines:
- Store credentials as secrets in your CI system
- Use service principal authentication
- Run with appropriate test markers:
# In CI pipeline
pytest -m "integration and not slow" tests/integration/test_span_hydrator.py