Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add app name to logging handler #233

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from corsheaders.defaults import default_headers
from opencensus.trace import config_integration

from main.utils_azure_insights import (
create_azure_log_handler_config,
create_azure_trace_config,
)

from .azure_settings import Azure

azure = Azure()
Expand All @@ -29,6 +34,7 @@
DEBUG = os.getenv("DEBUG", "false").lower() == "true"
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

APP_NAME = os.getenv("APP_NAME", "iiif-auth-proxy")
APP_BASE_URL = os.getenv("APP_BASE_URL", "https://bouwdossiers.amsterdam.nl/")
ALLOWED_HOSTS = ["*"]

Expand Down Expand Up @@ -301,22 +307,15 @@

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-auth-proxy'
)""",
}
}

OPENCENSUS = create_azure_trace_config(
APPLICATIONINSIGHTS_CONNECTION_STRING, APP_NAME
)
LOGGING["handlers"]["azure"] = create_azure_log_handler_config(
APPLICATIONINSIGHTS_CONNECTION_STRING, APP_NAME
)
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")
48 changes: 48 additions & 0 deletions src/main/utils_azure_insights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from functools import partial

from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.ext.azure.trace_exporter import AzureExporter


def add_app_name_to_telemetry(app_name, envelope):
envelope.data.baseData.properties["Application name"] = app_name
envelope.tags["ai.cloud.role"] = f"{app_name} - {envelope.tags['ai.cloud.role']}"
return True


def create_azure_trace_config(connection_string, app_name):
exporter = AzureExporter(
connection_string=connection_string,
service_name=app_name,
)
exporter.add_telemetry_processor(partial(add_app_name_to_telemetry, app_name))
return {
"TRACE": {
"SAMPLER": "opencensus.trace.samplers.ProbabilitySampler(rate=1)",
"EXPORTER": exporter,
}
}


class AzureLogHandlerWithAppName(AzureLogHandler):
app_name = None

@staticmethod
def set_app_name(name):
AzureLogHandlerWithAppName.app_name = name

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.app_name:
telemetry_callback = partial(add_app_name_to_telemetry, self.app_name)
self.add_telemetry_processor(telemetry_callback)


def create_azure_log_handler_config(connection_string, app_name):
AzureLogHandlerWithAppName.set_app_name(app_name)
return {
"level": "DEBUG",
"()": AzureLogHandlerWithAppName,
"connection_string": connection_string,
"formatter": "json",
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/zip_consumer/queue_zip_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from iiif import image_server
from iiif.metadata import get_metadata
from main import utils
from main.utils_azure import (
from main.utils_azure_storage import (
create_storage_account_temp_url,
get_blob_from_storage_account,
get_queue_client,
Expand Down
2 changes: 1 addition & 1 deletion src/zip_consumer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from auth_mail import authentication
from iiif import parsing
from main import utils
from main.utils_azure import store_blob_on_storage_account
from main.utils_azure_storage import store_blob_on_storage_account
from zip_consumer import zip_tools

log = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion src/zip_consumer/zip_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uuid import uuid4
from zipfile import ZipFile

from main.utils_azure import get_queue_client
from main.utils_azure_storage import get_queue_client

TMP_BOUWDOSSIER_ZIP_FOLDER = "/tmp/bouwdossier-zips/"
ZIP_MESSAGE_VERSION_NAME = "zip_job_v1"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from azure.core.exceptions import ResourceExistsError
from django.conf import settings

from main.utils_azure import get_blob_service_client, get_queue_client
from main.utils_azure_storage import get_blob_service_client, get_queue_client


def create_blob_container(container_name):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_zip_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
create_mail_login_token,
)
from auth_mail.generate_token import create_authz_token
from main.utils_azure import get_blob_from_storage_account, get_queue_client
from main.utils_azure_storage import get_blob_from_storage_account, get_queue_client
from tests.test_settings import (
IMAGE_BINARY_DATA,
PRE_WABO_IMG_URL_BASE,
Expand Down
Loading