Mercurial > code > home > repos > victoriametrics
annotate scrape_job.py @ 82:5a526531305f
rules
author | drewp@bigasterisk.com |
---|---|
date | Wed, 07 Aug 2024 15:10:28 -0700 |
parents | adde35eb4773 |
children |
rev | line source |
---|---|
61 | 1 import json |
2 from pathlib import Path | |
3 import subprocess | |
4 | |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
5 class FromName: |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
6 pass |
61 | 7 |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
8 def jobConfig(name, targets, scrape_interval=None, ping_job=False, metrics_path=None, params=None, https=False): |
61 | 9 """one scrape job config""" |
10 ret = { | |
11 "job_name": name, | |
12 "relabel_configs": [ | |
13 { | |
14 "target_label": "namespace", | |
15 "replacement": "default" | |
16 }, | |
17 { | |
18 "source_labels": ["__meta_kubernetes_pod_node_name"], | |
19 "target_label": "node" | |
20 }, | |
21 ] | |
22 } | |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
23 |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
24 if targets is FromName: |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
25 targets = [name] |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
26 |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
27 if targets: |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
28 ret["static_configs"] = [{ |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
29 "targets": targets, |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
30 }] |
61 | 31 |
32 if metrics_path: | |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
33 ret.setdefault('relabel_configs', []).append({ |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
34 "action": "replace", |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
35 "target_label": "__metrics_path__", |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
36 "replacement": metrics_path, |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
37 }) |
61 | 38 |
39 if scrape_interval: | |
40 ret['scrape_interval'] = scrape_interval | |
41 | |
42 if params: | |
43 ret['params'] = params | |
44 | |
45 if ping_job: | |
46 ret['metrics_path'] = '/probe' | |
47 ret['params'] = {'module': ['icmp']} | |
48 ret["relabel_configs"] = [ | |
49 { | |
50 "source_labels": ["__address__"], | |
51 "target_label": "__param_target" | |
52 }, | |
53 { | |
54 "source_labels": ["__param_target"], | |
55 "target_label": "instance" | |
56 }, | |
57 { | |
58 "target_label": "__address__", | |
59 "replacement": "prober" | |
60 }, | |
61 ] | |
62 | |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
63 if https: |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
64 ret['scheme'] = 'https' |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
65 ret["tls_config"] = {"ca_file": "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"} |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
66 ret["bearer_token_file"] = "/var/run/secrets/kubernetes.io/serviceaccount/token" |
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
67 |
61 | 68 return ret |
69 | |
70 | |
71 def current_deployments(): | |
72 deploys = json.loads(subprocess.check_output(['kubectl', 'get', 'deploy', '-o=json'])) | |
73 for deploy in deploys['items']: | |
74 name = deploy['metadata']['name'] | |
75 yield name | |
76 | |
77 | |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
78 def scrape_deployments(skip_names): |
61 | 79 ret = [] |
80 for name in current_deployments(): | |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
81 if name in skip_names: |
61 | 82 continue |
83 targets = [name] | |
84 ret.append(jobConfig(name=name, targets=targets)) | |
85 return ret | |
86 | |
87 | |
88 def writeJobConfigs(outDir: Path, jobConfs: list, retention: str): | |
62
8134cd480817
make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
61
diff
changeset
|
89 outDir.mkdir(exist_ok=True, parents=True) |
61 | 90 filenames_written = [] |
91 for job in jobConfs: | |
92 filename = f'job_{job["job_name"]}.yaml' | |
93 (outDir / filename).write_text(json.dumps([job], indent=2, sort_keys=True)) | |
94 filenames_written.append(filename) | |
95 | |
96 (outDir / f'scrape_{retention}.yaml').write_text(json.dumps({ | |
97 "global": { | |
98 "scrape_interval": "1m", | |
99 "scrape_timeout": "10s" | |
100 }, | |
64
def1aa2bfa3f
more targets polish. reorg code into next/
drewp@bigasterisk.com
parents:
62
diff
changeset
|
101 "scrape_config_files": sorted(filenames_written), |
61 | 102 }, indent=2)) |