Skip to content

Commit

Permalink
Merge pull request #171 from Amsterdam/feature/applications-insights
Browse files Browse the repository at this point in the history
Add application insights to logging
  • Loading branch information
TerryvanWalen authored Apr 15, 2024
2 parents 322f0df + d2111ad commit e043600
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
62 changes: 57 additions & 5 deletions src/main/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import os
import sys

from corsheaders.defaults import default_headers
from opencensus.trace import config_integration

from .azure_settings import Azure

Expand Down Expand Up @@ -157,6 +159,10 @@
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, "..", "static"))

# Django Logging settings
base_log_fmt = {"time": "%(asctime)s", "name": "%(name)s", "level": "%(levelname)s"}
log_fmt = base_log_fmt.copy()
log_fmt["message"] = "%(message)s"

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
Expand All @@ -165,25 +171,35 @@
"handlers": ["console"],
},
"formatters": {
"console": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"},
"json": {"format": json.dumps(log_fmt)},
},
"handlers": {
"console": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "console",
"formatter": "json",
},
},
"loggers": {
"bouwdossiers-metadata-server": {
"bag": {
"level": "WARNING",
"handlers": ["console"],
"propagate": False,
},
"bouwdossiers": {
"level": "WARNING",
"handlers": ["console"],
"propagate": False,
},
"importer": {
"level": "WARNING",
"handlers": ["console"],
"propagate": True,
"propagate": False,
},
"main": {
"level": "WARNING",
"handlers": ["console"],
"propagate": True,
"propagate": False,
},
"django": {
"handlers": ["console"],
Expand All @@ -198,9 +214,45 @@
"handlers": ["console"],
"propagate": False,
},
"opencensus": {
"handlers": ["console"],
"level": "WARNING",
"propagate": False
},
"azure.core.pipeline.policies.http_logging_policy": {
"handlers": ["console"],
"level": "WARNING",
"propagate": False,
},
},
}

APPLICATIONINSIGHTS_CONNECTION_STRING = os.getenv(
"APPLICATIONINSIGHTS_CONNECTION_STRING"
)

if APPLICATIONINSIGHTS_CONNECTION_STRING:
MIDDLEWARE.append("opencensus.ext.django.middleware.OpencensusMiddleware")
OPENCENSUS = {
"TRACE": {
"SAMPLER": "opencensus.trace.samplers.ProbabilitySampler(rate=1)",
"EXPORTER": f"""opencensus.ext.azure.trace_exporter.AzureExporter(
connection_string='{APPLICATIONINSIGHTS_CONNECTION_STRING}',
service_name='app-iiif-metadata-server'
)""",
}
}
config_integration.trace_integrations(["logging"])
LOGGING["handlers"]["azure"] = {
"level": "DEBUG",
"class": "opencensus.ext.azure.log_exporter.AzureLogHandler",
"connection_string": APPLICATIONINSIGHTS_CONNECTION_STRING,
"formatter": "json"
}
LOGGING["root"]["handlers"].append("azure")
for logger_name, logger_details in LOGGING["loggers"].items():
LOGGING["loggers"][logger_name]["handlers"].append("azure")


OBJECTSTORE = dict(
VERSION="2.0",
Expand Down
4 changes: 3 additions & 1 deletion src/main/uwsgi.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ wsgi-file = main/wsgi.py
route = /iiif-metadata/static/(.*) static:/static/$1

buffer-size = 32768
harakiri = 30
harakiri = 30

lazy-apps = true

0 comments on commit e043600

Please sign in to comment.