Skip to content

Commit 9247f62

Browse files
authored
Disable app insights when running locally (#619)
* Disable app insights when running locally * Rename to `APPINSIGHTS_ENABLED` * Fix `TokenLogger.py` for App Insights * Add comment to env var
1 parent 428ffa8 commit 9247f62

File tree

9 files changed

+169
-334
lines changed

9 files changed

+169
-334
lines changed

code/app.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import os
12
from azure.monitor.opentelemetry import configure_azure_monitor
23
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
34

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

711
from create_app import create_app # noqa: E402
812

code/backend/Admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

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

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

1518
logger = logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(
1619
logging.WARNING

code/backend/batch/utilities/helpers/EnvHelper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ def __init__(self, **kwargs) -> None:
128128
"AZURE_FORM_RECOGNIZER_KEY"
129129
)
130130
# Azure App Insights
131-
self.APPINSIGHTS_ENABLED = self.get_env_var_bool("APPINSIGHTS_ENABLED")
131+
# APPINSIGHTS_ENABLED will be True when the application runs in App Service
132+
self.APPINSIGHTS_ENABLED = self.get_env_var_bool("APPINSIGHTS_ENABLED", "False")
132133

133134
self.APPINSIGHTS_CONNECTION_STRING = os.getenv(
134135
"APPINSIGHTS_CONNECTION_STRING", ""
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
import logging
2-
from opencensus.ext.azure.log_exporter import AzureLogHandler
3-
from ..helpers.EnvHelper import EnvHelper
42

53

4+
# TODO: We probably don't need a separate class for this
65
class TokenLogger:
76
def __init__(self, name: str = __name__):
8-
env_helper: EnvHelper = EnvHelper()
97
self.logger = logging.getLogger(name)
10-
if env_helper.APPINSIGHTS_ENABLED:
11-
self.logger.addHandler(
12-
AzureLogHandler(
13-
connection_string=env_helper.APPINSIGHTS_CONNECTION_STRING
14-
)
15-
)
168
self.logger.setLevel(logging.INFO)
179

1810
def get_logger(self):
1911
return self.logger
2012

2113
def log(self, message: str, custom_dimensions: dict):
2214
# Setting log properties
23-
log_properties = {"custom_dimensions": custom_dimensions}
24-
self.logger.info(message, extra=log_properties)
15+
self.logger.info(message, extra=custom_dimensions)

code/tests/utilities/test_EnvHelper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_load_config_from_blob_storage(monkeypatch: MonkeyPatch, value, expected
4747

4848
@pytest.mark.parametrize(
4949
"value,expected",
50-
[("true", True), ("false", False), ("this is the way", False), (None, True)],
50+
[("true", True), ("false", False), ("this is the way", False), (None, False)],
5151
)
5252
def test_app_insights_enabled(monkeypatch: MonkeyPatch, value, expected):
5353
# given

infra/core/host/appservice.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ module configAppSettings 'appservice-appsettings.bicep' = {
9090
name: appService.name
9191
appSettings: union(appSettings,
9292
{
93+
APPINSIGHTS_ENABLED: string(!empty(applicationInsightsName))
9394
SCM_DO_BUILD_DURING_DEPLOYMENT: string(scmDoBuildDuringDeployment)
9495
ENABLE_ORYX_BUILD: string(enableOryxBuild)
9596
},

infra/main.json

Lines changed: 152 additions & 152 deletions
Large diffs are not rendered by default.

poetry.lock

Lines changed: 1 addition & 165 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ beautifulsoup4 = "4.12.3"
3030
fake-useragent = "1.5.1"
3131
chardet = "5.2.0"
3232
azure-search-documents = "11.4.0"
33-
opencensus-ext-azure = "1.1.13"
3433
azure-ai-contentsafety = "1.0.0"
3534
python-docx = "1.1.0"
3635
azure-keyvault-secrets = "4.8.0"

0 commit comments

Comments
 (0)