-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathretrieve_loki_data.yml
More file actions
88 lines (77 loc) · 3.08 KB
/
retrieve_loki_data.yml
File metadata and controls
88 lines (77 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
# Query Loki to retrieve data and output to retrieve_loki_op.json
# Count the entries in the log file
- name: Read the json data log file to calculate the no. of entries
ansible.builtin.slurp:
src: "{{ ck_output_file_remote }}"
register: source_file_data
- name: Set Expected Log Count
ansible.builtin.set_fact:
expected_log_count: "{{ (source_file_data['content'] | b64decode | from_json).streams | map(attribute='values') | map('length') | sum }}"
- name: Expected Count
ansible.builtin.debug:
msg: "Input file has {{ expected_log_count }} data entries that Loki has to return"
# Calculate Time
- name: Calculate Start Time in nanoseconds
ansible.builtin.command: date -d "{{ lookback }} days ago" +%s000000000
register: nano_time
changed_when: false
- name: Set Start Time
ansible.builtin.set_fact:
start_time: "{{ nano_time.stdout }}"
- name: Display Query Parameters
ansible.builtin.debug:
msg:
- "Query: {{ logql_query }}"
- "Start Time: {{ start_time }}"
- "Limit: {{ limit }}"
# Query Loki
- name: Retrieve Logs from Loki via API
block:
- name: Query Loki API
ansible.builtin.uri:
url: "{{ loki_query_url }}?query={{ logql_query | urlencode }}&start={{ start_time }}&limit={{ limit }}"
method: GET
client_cert: "{{ cert_dir }}/tls.crt"
client_key: "{{ cert_dir }}/tls.key"
ca_path: "{{ cert_dir }}/ca.crt"
validate_certs: false
return_content: yes
body_format: json
register: loki_response
# Wait condition
until:
- loki_response.status == 200
- loki_response.json.status == 'success'
- loki_response.json.data.result | length > 0
- (loki_response.json.data.result | map(attribute='values') | map('length') | sum) >= expected_log_count|int
retries: 10
delay: 150
# Save data
- name: Save Loki Data to JSON file
ansible.builtin.copy:
content: "{{ loki_response.json | to_nice_json }}"
dest: "{{ ck_loki_retrieve_file }}"
mode: '0644'
# Validate
- name: Verify Data Integrity
vars:
actual_count: "{{ loki_response.json.data.result | map(attribute='values') | map('length') | sum }}"
ansible.builtin.assert:
that:
- loki_response.json.status == 'success'
- loki_response.json.data.result | length > 0
- actual_count|int == expected_log_count|int
fail_msg: "Query did not return all data entries. Expected {{ expected_log_count }} log entries, but Loki only returned {{ actual_count }}"
success_msg: "Query returned all data entries. Input file had {{ expected_log_count }} entries and Loki returned {{ actual_count }}"
rescue:
- name: Debug failure
ansible.builtin.debug:
msg:
- "Status: {{ loki_response.status | default('Unknown') }}"
- "Body: {{ loki_response.content | default('No Content') }}"
- "Msg: {{ loki_response.msg | default('Request failed') }}"
# Failure
- name: Report Retrieval Failure
ansible.builtin.fail:
msg: "Retrieval Failed"