Skip to content

Commit da08d0a

Browse files
authored
Merge pull request #8 from g-n-a-d/master
add sharding ignorances
2 parents 4ae475c + fc25323 commit da08d0a

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

ansible/roles/prometheus/defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ prometheus_web_config:
4141
basic_auth_users: {}
4242

4343
# Prometheus customized arguments
44+
# -------------------------------
4445
prometheus_enable_target_sharding: false
46+
# Job names to ignore sharding
47+
prometheus_sharding_ignorances: []
4548

4649
# Configuration file options
4750
# --------------------------

ansible/roles/prometheus/library/inject_sharding_config.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,44 @@ def save_config_file(data, path):
2121
with open(path, 'w') as f:
2222
json.dump(data, f, indent=2, ensure_ascii=False)
2323

24-
def add_sharding(config, modulus, hash_value):
24+
def add_sharding(config, modulus, hash_value, ignorances):
2525
for job in config.get('scrape_configs', []):
26-
job['relabel_configs'] = [
27-
{
28-
'source_labels': ['__address__'],
29-
'modulus': modulus,
30-
'target_label': '__tmp_hash__',
31-
'action': 'hashmod'
32-
},
33-
{
34-
'source_labels': ['__tmp_hash__'],
35-
'regex': hash_value,
36-
'action': 'keep'
37-
}
38-
]
26+
if job.get('job_name', '') not in ignorances:
27+
job['relabel_configs'].extend([
28+
{
29+
'source_labels': ['__address__'],
30+
'modulus': modulus,
31+
'target_label': '__tmp_hash__',
32+
'action': 'hashmod'
33+
},
34+
{
35+
'source_labels': ['__tmp_hash__'],
36+
'regex': hash_value,
37+
'action': 'keep'
38+
}
39+
])
3940
return config
4041

4142
def main():
4243
module = AnsibleModule(
4344
argument_spec=dict(
4445
source=dict(type='str', required=True),
4546
modulus=dict(type='int', required=True),
46-
hash_value=dict(type='int', required=True)
47+
hash_value=dict(type='int', required=True),
48+
ignorances=list(type='str', required=True)
4749
)
4850
)
4951

5052
source = module.params['source']
5153
modulus = module.params['modulus']
5254
hash_value = module.params['hash_value']
55+
ignorances = module.params['ignorances']
5356

5457
config = load_config_file(source)
5558
if not isinstance(config, dict):
5659
module.exit_json(changed=False)
5760
return
58-
sharded_config = add_sharding(config, modulus, hash_value)
61+
sharded_config = add_sharding(config, modulus, hash_value, ignorances)
5962
save_config_file(sharded_config, source)
6063

6164
module.exit_json(changed=True)

ansible/roles/prometheus/tasks/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
source: "{{ prometheus_config_dir }}/prometheus.yml"
9292
modulus: "{{ groups['prometheus'] | length }}"
9393
hash_value: "{{ groups['prometheus'].index(inventory_hostname) }}"
94+
ignorances: "{{ prometheus_sharding_ignorances }}"
9495
when: prometheus_enable_target_sharding | bool
9596

9697
- name: Find file_sd configurations on prometheus hosts
@@ -109,6 +110,7 @@
109110
source: "{{ item.path }}"
110111
modulus: "{{ groups['prometheus'] | length }}"
111112
hash_value: "{{ groups['prometheus'].index(inventory_hostname) }}"
113+
ignorances: "{{ prometheus_sharding_ignorances }}"
112114
loop: "{{ host_file_sd.files }}"
113115
when:
114116
- prometheus_enable_target_sharding | bool

0 commit comments

Comments
 (0)