Skip to content

Commit

Permalink
Another fix for the loki logs query
Browse files Browse the repository at this point in the history
  • Loading branch information
totycro committed Dec 3, 2024
1 parent 7556cff commit b888445
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions pygeoapi_kubernetes_papermill/log_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import itertools
import time

from flask import Response
import requests

# NOTE: this assumes flask_app, which is the default.
Expand All @@ -43,9 +44,10 @@

@APP.get("/jobs/<job_id>/logs")
def get_job_logs(job_id):
LOGGER.debug(f"Retrieving job logs for {job_id}")
LOGGER.info(f"Retrieving job logs for {job_id}")

from pygeoapi.flask_app import api_
from pygeoapi_kubernetes_papermill.common import k8s_job_name

log_query_endpoint = getattr(api_.manager, "log_query_endpoint", None)
if not log_query_endpoint:
Expand All @@ -63,21 +65,25 @@ def get_job_logs(job_id):
# 21 days in ns (default limit in loki is 30 days)
query_time_range = 21 * 24 * 60 * 60 * 1_000_000_000

job_name = k8s_job_name(job_id)

request_params = {
"query": f'{{job="{namespace}/{job_name}"}}',
"start": time.time_ns() - query_time_range,
"end": time.time_ns(),
}
response = requests.get(
log_query_endpoint,
params={
"query": f'{{job="{namespace}/{job_id}"}}',
"start": time.time_ns() - query_time_range,
"end": time.time_ns(),
},
params=request_params,
)
response.raise_for_status()
streams = response.json()["data"]["result"]

# here let's just mix stdout/stderr
return "\n".join(
log_output = "\n".join(
entry[1]
for entry in sorted(
# here let's just mix stdout/stderr
itertools.chain.from_iterable(result["values"] for result in streams)
)
)
return Response(log_output, mimetype="text/plain")

0 comments on commit b888445

Please sign in to comment.