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

Use corrolation_id for logging across commands. #213

Merged
merged 3 commits into from
Feb 21, 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
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
Loading