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

vdk-logging-json: pass op_id attribute to logs #569

Merged
merged 2 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@


class EcsJsonFormatter(StdlibFormatter):
def __init__(self, job_name, attempt_id):
def __init__(self, job_name: str, attempt_id: str, op_id: str):
super().__init__()
self.__job_name = job_name
self.__attempt_id = attempt_id
self.__op_id = op_id

def format(self, record):
result = self.format_to_ecs(record)
Expand All @@ -40,6 +41,7 @@ def format(self, record):
# bind extra fields
result["jobname"] = self.__job_name
result["attemptid"] = self.__attempt_id
result["opid"] = self.__op_id

return json.dumps(result)

Expand All @@ -51,14 +53,18 @@ def vdk_start(plugin_registry: IPluginRegistry, command_line_args: List):
EcsJsonFormatter(
job_name="",
attempt_id="",
op_id="",
)
)


@hookimpl(trylast=True)
def initialize_job(context: JobContext) -> None:
attempt_id = context.core_context.state.get(CommonStoreKeys.ATTEMPT_ID)
op_id = context.core_context.state.get(CommonStoreKeys.OP_ID)
job_name = context.name

for handler in logging.getLogger().handlers:
handler.setFormatter(EcsJsonFormatter(job_name=job_name, attempt_id=attempt_id))
handler.setFormatter(
EcsJsonFormatter(job_name=job_name, attempt_id=attempt_id, op_id=op_id)
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def test_formatter():
formatter = EcsJsonFormatter(job_name="", attempt_id="")
formatter = EcsJsonFormatter(job_name="", attempt_id="", op_id="")

log_record = logging.LogRecord(
name="",
Expand All @@ -24,21 +24,21 @@ def test_formatter():
assert result[43:] == (
'"message": "test-string", "level": "LEVEL 1", '
'"lineno": 1, "filename": "", "modulename": "", '
'"funcname": "", "jobname": "", "attemptid": ""}'
'"funcname": "", "jobname": "", "attemptid": "", "opid": ""}'
)

log_record.msg = "test\nstring\nwith\nnewlines"
result = formatter.format(log_record)
assert result[43:] == (
'"message": "test\\nstring\\nwith\\nnewlines", '
'"level": "LEVEL 1", "lineno": 1, "filename": "", '
'"modulename": "", "funcname": "", "jobname": "", "attemptid": ""}'
'"modulename": "", "funcname": "", "jobname": "", "attemptid": "", "opid": ""}'
)

log_record.msg = 'test"string"with"double"quotes'
result = formatter.format(log_record)
assert result[43:] == (
'"message": "test\\"string\\"with\\"double\\"quotes", '
'"level": "LEVEL 1", "lineno": 1, "filename": "", '
'"modulename": "", "funcname": "", "jobname": "", "attemptid": ""}'
'"modulename": "", "funcname": "", "jobname": "", "attemptid": "", "opid": ""}'
)