Skip to content

Commit

Permalink
Disable app insights when running locally (#619)
Browse files Browse the repository at this point in the history
* Disable app insights when running locally

* Rename to `APPINSIGHTS_ENABLED`

* Fix `TokenLogger.py` for App Insights

* Add comment to env var
  • Loading branch information
cecheta authored Apr 9, 2024
1 parent 428ffa8 commit 9247f62
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 334 deletions.
8 changes: 6 additions & 2 deletions code/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

configure_azure_monitor()
HTTPXClientInstrumentor().instrument() # httpx is used by openai
# We cannot use EnvHelper here as Application Insights should be configured first
# for instrumentation to work correctly
if os.getenv("APPINSIGHTS_ENABLED", "false").lower() == "true":
configure_azure_monitor()
HTTPXClientInstrumentor().instrument() # httpx is used by openai

from create_app import create_app # noqa: E402

Expand Down
5 changes: 4 additions & 1 deletion code/backend/Admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

configure_azure_monitor()
# We cannot use EnvHelper here as Application Insights needs to be configured first
# for instrumentation to work correctly
if os.getenv("APPINSIGHTS_ENABLED", "false").lower() == "true":
configure_azure_monitor()

logger = logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(
logging.WARNING
Expand Down
3 changes: 2 additions & 1 deletion code/backend/batch/utilities/helpers/EnvHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def __init__(self, **kwargs) -> None:
"AZURE_FORM_RECOGNIZER_KEY"
)
# Azure App Insights
self.APPINSIGHTS_ENABLED = self.get_env_var_bool("APPINSIGHTS_ENABLED")
# APPINSIGHTS_ENABLED will be True when the application runs in App Service
self.APPINSIGHTS_ENABLED = self.get_env_var_bool("APPINSIGHTS_ENABLED", "False")

self.APPINSIGHTS_CONNECTION_STRING = os.getenv(
"APPINSIGHTS_CONNECTION_STRING", ""
Expand Down
13 changes: 2 additions & 11 deletions code/backend/batch/utilities/loggers/TokenLogger.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
from ..helpers.EnvHelper import EnvHelper


# TODO: We probably don't need a separate class for this
class TokenLogger:
def __init__(self, name: str = __name__):
env_helper: EnvHelper = EnvHelper()
self.logger = logging.getLogger(name)
if env_helper.APPINSIGHTS_ENABLED:
self.logger.addHandler(
AzureLogHandler(
connection_string=env_helper.APPINSIGHTS_CONNECTION_STRING
)
)
self.logger.setLevel(logging.INFO)

def get_logger(self):
return self.logger

def log(self, message: str, custom_dimensions: dict):
# Setting log properties
log_properties = {"custom_dimensions": custom_dimensions}
self.logger.info(message, extra=log_properties)
self.logger.info(message, extra=custom_dimensions)
2 changes: 1 addition & 1 deletion code/tests/utilities/test_EnvHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_load_config_from_blob_storage(monkeypatch: MonkeyPatch, value, expected

@pytest.mark.parametrize(
"value,expected",
[("true", True), ("false", False), ("this is the way", False), (None, True)],
[("true", True), ("false", False), ("this is the way", False), (None, False)],
)
def test_app_insights_enabled(monkeypatch: MonkeyPatch, value, expected):
# given
Expand Down
1 change: 1 addition & 0 deletions infra/core/host/appservice.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ module configAppSettings 'appservice-appsettings.bicep' = {
name: appService.name
appSettings: union(appSettings,
{
APPINSIGHTS_ENABLED: string(!empty(applicationInsightsName))
SCM_DO_BUILD_DURING_DEPLOYMENT: string(scmDoBuildDuringDeployment)
ENABLE_ORYX_BUILD: string(enableOryxBuild)
},
Expand Down
304 changes: 152 additions & 152 deletions infra/main.json

Large diffs are not rendered by default.

166 changes: 1 addition & 165 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ beautifulsoup4 = "4.12.3"
fake-useragent = "1.5.1"
chardet = "5.2.0"
azure-search-documents = "11.4.0"
opencensus-ext-azure = "1.1.13"
azure-ai-contentsafety = "1.0.0"
python-docx = "1.1.0"
azure-keyvault-secrets = "4.8.0"
Expand Down

0 comments on commit 9247f62

Please sign in to comment.