Skip to content

Commit

Permalink
Use corrolation_id for logging across commands. (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv authored Feb 21, 2024
1 parent 2b452f3 commit 930aae7
Show file tree
Hide file tree
Showing 2 changed files with 23 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
18 changes: 18 additions & 0 deletions mreg_cli/utilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import json
import logging
import os
import re
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 +44,22 @@ 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 = re.sub(r"\s+", "_", suffix)
correlation_id = f"{uuid4()}-{suffix}"

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


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

0 comments on commit 930aae7

Please sign in to comment.