Skip to content

Commit

Permalink
Merge pull request #9742 from ycedres/promtail-container-adjustments
Browse files Browse the repository at this point in the history
Adjust promtail container exec script
  • Loading branch information
ycedres authored Feb 14, 2025
2 parents 1bdc8b6 + d19045e commit 596f436
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@ FROM opensuse/leap:latest
COPY promtail-linux-amd64 /usr/bin/promtail
RUN chmod a+x /usr/bin/promtail

RUN zypper -n in python311 python311-requests
COPY complete_checker.py /usr/bin/complete_checker.py

COPY run.py /usr/bin/run.py
RUN chmod +x /usr/bin/run.py

ENTRYPOINT ["/usr/bin/run.py"]
ENTRYPOINT ["promtail"]
CMD ["--config.file=/etc/promtail/config.yml"]

This file was deleted.

39 changes: 0 additions & 39 deletions health-check/src/uyuni_health_check/containers/promtail/run.py

This file was deleted.

165 changes: 0 additions & 165 deletions health-check/src/uyuni_health_check/loki/loki_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def run_loki(supportconfig_path=None, verbose=False):
]

podman(podman_args, verbose)
wait_promtail_init()


def render_promtail_cfg(supportconfig_path=None, promtail_template=None):
Expand Down Expand Up @@ -130,167 +129,3 @@ def render_promtail_cfg(supportconfig_path=None, promtail_template=None):

# Write rendered promtail configuration file
config.write_config("promtail", "config.yaml", promtail_template.render(**opts))


def check_series_in_loki(
loki_url,
job_name="promtail-complete-job",
flag="complete",
message="Promtail finished!d",
):
query = f'{{job="{job_name}", flag="{flag}"}} |= "{message}"'
end = int(time.time())
start = end - 60 * 60

response = requests.get(
f"{loki_url}/loki/api/v1/query",
params={
"query": query,
"start": start * 1_000_000_000,
"end": end * 1_000_000_000,
},
)

if response.status_code == 200:
data = response.json()
return len(data["data"]["result"]) > 0
else:
print("Failed to query Loki:", response.text)
return False


def wait_promtail_init():
loki_url = "http://localhost:3100"
start_time = time.time()
timeout = 60
console.log("[bold]Waiting for Promtail to process logs")

while not check_series_in_loki(loki_url):
elapsed_time = time.time() - start_time
if elapsed_time >= timeout:
console.log("Timeout waiting for promtail to finish!")
break
time.sleep(10)
console.log("Promtail finished processing logs")


def wait_loki_init(verbose=False):
"""
Try to figure out when loki is ready to answer our requests.
There are two things to wait for:
- loki to be up
- promtail to have read the logs and the loki ingester having handled them
"""
metrics = {}
timeout = False
request_message_bytes_sum = 0
loki_ingester_chunk_entries_count = 0
start_time = time.time()
ready = False

# Wait for promtail to be ready
# TODO Add a timeout here in case something went really bad
# TODO checking the lags won't work when working on older logs,
# we could try to compare the positions with the size of the files in such a case
while (
not metrics
or metrics["active"] < PROMTAIL_TARGETS
or (not metrics["lags"] and metrics["active_files"] == 0)
or any([v >= 10 for v in metrics["lags"].values()])
or (metrics["lags"] and metrics["active_files"])
or (metrics["encoded_bytes_total"] == metrics["sent_bytes_total"] == 0)
or (metrics["encoded_bytes_total"] != metrics["sent_bytes_total"] != 0)
or (
metrics["encoded_bytes_total"]
!= metrics["sent_bytes_total"]
!= request_message_bytes_sum
)
or loki_ingester_chunk_entries_count == 0
or not ready
and not timeout
):
if verbose:
console.log("Waiting for promtail metrics to be collected")
time.sleep(5)
response = requests.get("http://localhost:3100/metrics")
if verbose:
console.log("loki metrics 3100 status code", response.status_code)
if response.status_code == 200:
content = response.content.decode()
request_message_bytes_sum = re.findall(
r'loki_request_message_bytes_sum{.*"loki_api_v1_push"} (\d+)', content
)
request_message_bytes_sum = (
int(request_message_bytes_sum[0]) if request_message_bytes_sum else 0
)
loki_ingester_chunk_entries_count = re.findall(
r"loki_ingester_chunk_entries_count (\d+)", content
)
loki_ingester_chunk_entries_count = (
int(loki_ingester_chunk_entries_count[0])
if loki_ingester_chunk_entries_count
else 0
)

response = requests.get("http://localhost:9081/metrics")
if verbose:
console.log("promtail metrics 9081 status code", response.status_code)
if response.status_code == 200:
content = response.content.decode()
active = re.findall(r"promtail_targets_active_total (\d+)", content)
encoded_bytes_total = re.findall(
r"promtail_encoded_bytes_total{.*} (\d+)", content
)
sent_bytes_total = re.findall(
r"promtail_sent_bytes_total{.*} (\d+)", content
)
active_files = re.findall(r"promtail_files_active_total (\d+)", content)
lags = re.findall(
'promtail_stream_lag_seconds{filename="([^"]+)".*} ([0-9.]+)', content
)
metrics = {
"lags": {row[0]: float(row[1]) for row in lags},
"active": int(active[0]) if active else 0,
"active_files": int(active_files[0]) if active_files else 0,
"encoded_bytes_total": (
int(encoded_bytes_total[0]) if encoded_bytes_total else 0
),
"sent_bytes_total": int(sent_bytes_total[0]) if sent_bytes_total else 0,
}

# check if loki is ready
if verbose:
console.log("Waiting for loki to be ready")
response = requests.get("http://localhost:3100/ready")
if verbose:
console.log("loki 3100 status code", response.status_code)
if response.status_code == 200:
content = response.content.decode()
if content == "ready\n":
ready = True

# check if promtail is ready
if verbose:
console.log("Waiting for promtail to be ready")
response = requests.get("http://localhost:9081/ready")
if verbose:
console.log("promtail ready 9081 status code", response.status_code)
if response.status_code == 200:
content = response.content.decode()
if content == "Ready":
ready = True
else:
ready = False

# check timeout
if (time.time() - start_time) > LOKI_WAIT_TIMEOUT:
timeout = True
if timeout:
raise HealthException(
"[red bold]Timeout has been reached waiting for Loki and promtail. Something unexpected may happen. Please check and try again."
)
else:
console.print(metrics)
console.print(loki_ingester_chunk_entries_count)
console.print(request_message_bytes_sum)
console.log("[bold]Loki and promtail are now ready to receive requests")

0 comments on commit 596f436

Please sign in to comment.