Skip to content

Instantly share code, notes, and snippets.

@snjypl
Last active November 29, 2022 08:00
Show Gist options
  • Save snjypl/5ab9c43eba282a8fc65f930a1fbb54b0 to your computer and use it in GitHub Desktop.
Save snjypl/5ab9c43eba282a8fc65f930a1fbb54b0 to your computer and use it in GitHub Desktop.
airflow-provider-grafana-loki: Query loki for airflow log
from requests.auth import HTTPBasicAuth
from requests import Session
# configuration
loki_username = "LOKI_USERNAME"
loki_password = "LOKI_PASSWORD"
loki_query_endpoint = "http://localhost:3101/loki/api/v1/query_range"
dag_id = "tutorial"
task_id = 'print_date'
run_id = 'manual__2022-11-28T16:16:57.622614+00:00'
q = """
{{dag_id="{dag_id}",task_id="{task_id}"}}
| json try_number="try_number",map_index="map_index",run_id="run_id"
| try_number="1" and map_index="-1" and run_id="{run_id}"
| __error__!="JSONParserErr"
""".format(
dag_id=dag_id, task_id=task_id, run_id=run_id
)
params = {
"query": q,
"start": "2022-11-04T16:17:01.389030+00:00",
"end": "2022-12-01T17:17:01.636801+00:00",
"direction": "forward",
"limit": 1,
}
with Session() as session:
session.auth = HTTPBasicAuth(username=loki_username, password=loki_password)
r = session.get(loki_query_endpoint, params=params, timeout=100)
if r.status_code != 200:
print(r.status_code)
print(r.raw)
print(r.content)
else:
print(r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment