Skip to content

Error Logs for failed tranformer requests #360

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

Merged
merged 10 commits into from
Apr 30, 2024
83 changes: 83 additions & 0 deletions servicex/app/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import asyncio
from pathlib import Path
from typing import Optional, List
import webbrowser
import re
from enum import Enum

import rich
import typer
Expand Down Expand Up @@ -138,3 +141,83 @@

for path in result_files:
print(path.as_posix())


class TimeFrame(str, Enum):
r"""
Time Frame levels: 'day', 'week' & 'month'
"""
day = ("day",)
week = ("week",)
month = ("month")


class LogLevel(str, Enum):
r"""
Level of the log messages: INFO & ERROR
"""
info = ("INFO",)
error = ("ERROR",)


def add_query(key, value):
"""
Creates query string from the key and value pairs
"""
query_string = "(query:(match_phrase:({0}:'{1}')))".format(key, value)
return query_string


def select_time(time_frame=TimeFrame.day):
"""
Takes input as 'day','week','month' and returns the time filter
"""
time_string = time_frame
if time_frame.lower() == TimeFrame.day:
time_string = "time:(from:now%2Fd,to:now%2Fd)"
elif time_frame.lower() == TimeFrame.week:
time_string = "time:(from:now%2Fw,to:now%2Fw)"
elif time_frame.lower() == TimeFrame.month:
time_string = "time:(from:now-30d%2Fd,to:now)"
else:
rich.print("Got a time frame apart from 'day', 'week', 'month'")

Check warning on line 183 in servicex/app/transforms.py

View check run for this annotation

Codecov / codecov/patch

servicex/app/transforms.py#L183

Added line #L183 was not covered by tests
return time_string


def create_kibana_link_parameters(log_url, transform_id=None, log_level=None, time_frame=None):
"""
Create the _a and _g parameters for the kibana dashboard link
"""
a_parameter = f"&_a=(filters:!({add_query('requestId', transform_id)},"\
f"{add_query('level', log_level.value.lower())}))"
g_parameter = f"&_g=({select_time(time_frame.value.lower())})"
kibana_link = re.sub(r"\&\_g\=\(\)", g_parameter + a_parameter, log_url)
return kibana_link


@transforms_app.command(no_args_is_help=True)
def logs(
url: Optional[str] = url_cli_option,
backend: Optional[str] = backend_cli_option,
transform_id: str = typer.Option(None, "-t", "--transform-id", help="Transform ID"),
log_level: Optional[LogLevel] = typer.Option("ERROR", "-l", "--log-level",
help="Level of Logs",
case_sensitive=False),
time_frame: Optional[TimeFrame] = typer.Option("month", "-f", "--time-frame",
help="Time Frame",
case_sensitive=False)
):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have an option to select the log level you want to see. It should default to error, but developers might appreciate the full info logs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added an Enum with two log level: INFO and ERROR.
And the user can use these options while requesting the log

"""
Open the URL to the Kibana dashboard of the logs of a tranformer
"""
sx = ServiceXClient(backend=backend, url=url)
transforms = sx.get_transform_status(transform_id)
if transforms and transforms.request_id == transform_id:
kibana_link = create_kibana_link_parameters(transforms.log_url,

Check warning on line 216 in servicex/app/transforms.py

View check run for this annotation

Codecov / codecov/patch

servicex/app/transforms.py#L213-L216

Added lines #L213 - L216 were not covered by tests
transform_id=transform_id,
log_level=log_level,
time_frame=time_frame)
print(kibana_link)
webbrowser.open(kibana_link)

Check warning on line 221 in servicex/app/transforms.py

View check run for this annotation

Codecov / codecov/patch

servicex/app/transforms.py#L220-L221

Added lines #L220 - L221 were not covered by tests
else:
rich.print("Invalid Request ID")

Check warning on line 223 in servicex/app/transforms.py

View check run for this annotation

Codecov / codecov/patch

servicex/app/transforms.py#L223

Added line #L223 was not covered by tests
60 changes: 60 additions & 0 deletions tests/test_servicex_app_transforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from servicex.app.transforms import LogLevel, TimeFrame
from servicex.app.transforms import add_query, select_time, create_kibana_link_parameters


def test_add_query():
key = "abc"
value = "123-345-567"
query = "(query:(match_phrase:(abc:'123-345-567')))"
assert add_query(key, value) == query

key = "requestId"
value = "d2ede739-9779-4075-95b1-0c7fae1de408"
query = "(query:(match_phrase:(requestId:'d2ede739-9779-4075-95b1-0c7fae1de408')))"
assert add_query(key, value) == query


def test_select_time():
time_frame = TimeFrame.week
time_filter = "time:(from:now%2Fw,to:now%2Fw)"
assert time_filter == select_time(time_frame)

time_frame = "month"
time_filter = "time:(from:now-30d%2Fd,to:now)"
assert time_filter == select_time(time_frame)

time_frame = "daY"
time_filter = "time:(from:now%2Fd,to:now%2Fd)"
assert time_filter == select_time(time_frame)


def test_create_kibana_link_parameters():
initial_log_url = "https://atlas-kibana.mwt2.org:5601/s/servicex/app"\
"/dashboards?auth_provider_hint=anonymous1#/view/"\
"2d2b3b40-f34e-11ed-a6d8-9f6a16cd6d78?embed=true&_g=()"\
"&show-time-filter=true&hide-filter-bar=true"
transform_id = "d2ede739-9779-4075-95b1-0c7fae1de408"
log_level = LogLevel.error
time_frame = TimeFrame.day
final_url = "https://atlas-kibana.mwt2.org:5601/s/servicex/app/dashboards?"\
"auth_provider_hint=anonymous1#/view/2d2b3b40-f34e-11ed-a6d8-9f6a16cd6d78?"\
"embed=true&_g=(time:(from:now%2Fd,to:now%2Fd))"\
"&_a=(filters:!((query:(match_phrase:"\
"(requestId:'d2ede739-9779-4075-95b1-0c7fae1de408'))),"\
"(query:(match_phrase:(level:'error')))))&show-time-filter=true"\
"&hide-filter-bar=true"
assert create_kibana_link_parameters(initial_log_url, transform_id,
log_level, time_frame) == final_url

transform_id = "93713b34-2f0b-4d53-8412-8afa98626516"
log_level = LogLevel.info
time_frame = TimeFrame.month
final_url = "https://atlas-kibana.mwt2.org:5601/s/servicex/app/dashboards?"\
"auth_provider_hint=anonymous1#/view/2d2b3b40-f34e-11ed-a6d8-9f6a16cd6d78?"\
"embed=true&_g=(time:(from:now-30d%2Fd,to:now))"\
"&_a=(filters:!((query:(match_phrase:"\
"(requestId:'93713b34-2f0b-4d53-8412-8afa98626516'))),"\
"(query:(match_phrase:(level:'info')))))&show-time-filter=true"\
"&hide-filter-bar=true"
assert create_kibana_link_parameters(initial_log_url, transform_id,
log_level, time_frame) == final_url
Loading