Skip to content

Commit 5ebea1e

Browse files
committed
[telemetry_chargeback] Add Loki data retrieval and processing tasks
- Add retrieve_loki_data.yml for querying Loki API with retry logic - Update ingest_loki_data.yml for uploading data to Loki - Update load_loki_data.yml for loading Loki data - Update README.md with task documentation
1 parent ddb2060 commit 5ebea1e

3 files changed

Lines changed: 93 additions & 28 deletions

File tree

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
---
22
# Ingest data generated by gen_synth_loki_data.yml to Loki
33

4-
- name: "Ingest data log to Loki via API"
5-
block:
4+
- name: "TEST Push data to Loki"
5+
ansible.builtin.uri:
6+
## loki_push_url will be used in future role developement
7+
url: "{{ loki_push_url }}"
8+
method: POST
9+
src: "{{ cloudkitty_data_file }}"
10+
body_format: json
11+
client_cert: "{{ cert_dir }}/tls.crt"
12+
client_key: "{{ cert_dir }}/tls.key"
13+
validate_certs: false
14+
status_code: 204
15+
return_content: true
16+
register: loki_response
17+
ignore_errors: true
18+
failed_when: loki_response.status != 204
619

7-
- name: "Push data to Loki"
8-
ansible.builtin.uri:
9-
## loki_push_url will be used in future role developement
10-
url: "{{ loki_push_url }}"
11-
method: POST
12-
src: "{{ cloudkitty_data_file }}"
13-
body_format: json
14-
client_cert: "{{ cert_dir }}/tls.crt"
15-
client_key: "{{ cert_dir }}/tls.key"
16-
validate_certs: false
17-
status_code: 204
18-
return_content: true
19-
register: loki_response
20-
ignore_errors: false
21-
failed_when: loki_response.status != 204
22-
23-
# Success
24-
- name: "Confirm ingestion success"
25-
ansible.builtin.debug:
26-
msg: "Ingestion Successful!"
27-
28-
rescue:
29-
# Rescue block
30-
- name: "Debug failure"
31-
ansible.builtin.debug:
32-
msg: "Ingest failed. Loki Status: {{ loki_response.status | default('N/A') }}"
20+
- name: "Ingest Status Message"
21+
ansible.builtin.debug:
22+
msg: "Ingestion Successful"
23+
when: loki_response.status | default(0) == 204

roles/telemetry_chargeback/tasks/load_loki_data.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44

55
- name: "Flush data to Loki storage for {{ scenario_name }}"
66
ansible.builtin.include_tasks: flush_loki_data.yml
7+
8+
- name: "Retrieve data log from Loki for {{ scenario_name }}"
9+
ansible.builtin.include_tasks: retrieve_loki_data.yml
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
- name: "Expected Count {{ scenario_name }}"
3+
ansible.builtin.debug:
4+
msg: "Input file has {{ synth_data_rates.data_log.log_count }} data entries that Loki has to return"
5+
6+
# Query Loki
7+
- name: "Retrieve Logs from Loki via API {{ scenario_name }}"
8+
block:
9+
- name: "Query Loki API"
10+
ansible.builtin.uri:
11+
url: "{{ loki_query_url }}?query={{ logql_query | urlencode }}&start={{ synth_data_rates.time.begin_step.nanosec }}&limit={{ limit }}"
12+
method: GET
13+
client_cert: "{{ cert_dir }}/tls.crt"
14+
client_key: "{{ cert_dir }}/tls.key"
15+
ca_path: "{{ cert_dir }}/ca.crt"
16+
validate_certs: false
17+
return_content: true
18+
body_format: json
19+
register: loki_response
20+
# Wait condition
21+
until:
22+
- loki_response.status == 200
23+
- loki_response.json.status == 'success'
24+
- loki_response.json.data.result | length > 0
25+
- (loki_response.json.data.result | map(attribute='values') | map('length') | sum) >= (synth_data_rates.data_log.log_count | int)
26+
retries: 25
27+
delay: 60
28+
29+
- name: "Save Loki Data to JSON file"
30+
ansible.builtin.copy:
31+
content: "{{ loki_response.json | to_json }}"
32+
dest: "{{ artifacts_dir_zuul }}/{{ scenario_name }}{{ cloudkitty_loki_data_suffix }}"
33+
mode: '0644'
34+
35+
# Validate
36+
- name: "Verify Data Integrity {{ scenario_name }}"
37+
vars:
38+
actual_count: "{{ loki_response.json.data.result | map(attribute='values') | map('length') | sum }}"
39+
ansible.builtin.assert:
40+
that:
41+
- loki_response.json.status == 'success'
42+
- loki_response.json.data.result | length > 0
43+
- actual_count | int == (synth_data_rates.data_log.log_count | int)
44+
fail_msg: >-
45+
Query did not return all data entries. Expected
46+
{{ synth_data_rates.data_log.log_count }} log entries, but Loki
47+
only returned {{ actual_count }}
48+
success_msg: "Query returned all data entries. Input file had {{ synth_data_rates.data_log.log_count }} entries and Loki returned {{ actual_count }}"
49+
50+
rescue:
51+
- name: "Debug failure"
52+
ansible.builtin.debug:
53+
msg:
54+
- "Status: {{ loki_response.status | default('Unknown') }}"
55+
- "Body: {{ loki_response.content | default('No Content') }}"
56+
- "Msg: {{ loki_response.msg | default('Request failed') }}"
57+
58+
# Failure
59+
- name: "Report Retrieval Failure"
60+
ansible.builtin.fail:
61+
msg: "Retrieval Failed"
62+
63+
- name: "Generate chargeback stats from Loki-retrieved data file: {{ scenario_name }}"
64+
ansible.builtin.command:
65+
cmd: >
66+
python3 "{{ cloudkitty_summary_script }}"
67+
-j "{{ artifacts_dir_zuul }}/{{ scenario_name }}{{ cloudkitty_loki_data_suffix }}"
68+
-o "{{ artifacts_dir_zuul }}/{{ scenario_name }}{{ cloudkitty_loki_totals_metrics_suffix }}"
69+
--debug "{{ cloudkitty_debug_dir }}"
70+
register: synth_rating_info
71+
changed_when: synth_rating_info.rc == 0

0 commit comments

Comments
 (0)