Skip to content

Commit

Permalink
Use corrolation_id for logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv committed Feb 21, 2024
1 parent 2b452f3 commit b88b900
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mreg_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from mreg_cli.help_formatter import CustomHelpFormatter
from mreg_cli.outputmanager import OutputManager
from mreg_cli.types import CommandFunc, Flag
from mreg_cli.utilities.api import create_and_set_corrolation_id
from mreg_cli.utilities.api import logout as _force_logout

if TYPE_CHECKING:
Expand Down Expand Up @@ -246,6 +247,10 @@ def process_command_line(self, line: str) -> None:
# Set the command that generated the output
# Also remove filters and other noise.
cmd = output.from_command(line)
# Create and set the corrolation id, using the cleaned command
# as the suffix. This is used to track the command in the logs
# on the server side.
create_and_set_corrolation_id(cmd)
# Run the command
cli.parse(cmd)
# Render the output
Expand Down
22 changes: 22 additions & 0 deletions mreg_cli/utilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
from typing import TYPE_CHECKING, Any, Dict, List, NoReturn, Optional, Union, cast, overload
from urllib.parse import urljoin
from uuid import uuid4

import requests

Expand Down Expand Up @@ -42,6 +43,27 @@ def error(msg: Union[str, Exception], code: int = os.EX_UNAVAILABLE) -> NoReturn
sys.exit(code)


def create_and_set_corrolation_id(suffix: str) -> str:
"""Set currently active corrolation id.
This will take a suffix and append it to a generated UUIDv4 and set it as the corrolation id.
:param suffix: The suffix to use for the corrolation id.
:returns: The generated corrolation id.
"""
suffix = suffix.replace(" ", "_")
corrolation_id = f"{uuid4()}-{suffix}"

session.headers.update({"X-Correlation-ID": corrolation_id})
return corrolation_id


def set_headers(headers: Dict[str, str]) -> None:
"""Set headers for the session."""
session.headers.update(headers)


def set_file_permissions(f: str, mode: int) -> None:
"""Set file permissions on a file."""
try:
Expand Down

0 comments on commit b88b900

Please sign in to comment.