Skip to content

Commit bafc58a

Browse files
Add get_label_values.py function.
1 parent 619b3ca commit bafc58a

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

examples/get_label_values.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script demonstrates how to use the `get_label_values` function to retrieve values
4+
# for a specific label and render the output as a table.
5+
#
6+
7+
import sys
8+
from sdcclient import SdcClient
9+
10+
def render_label_values_as_table(label_values):
11+
if not label_values:
12+
print("No values found for the specified label.")
13+
return
14+
15+
# Calculate the maximum width for the value column
16+
max_width = max(len(value) for value in label_values)
17+
18+
# Create a horizontal separator
19+
separator = "+" + "-" * (max_width + 2) + "+"
20+
21+
# Create the header row
22+
header = f"| {'Value'.ljust(max_width)} |"
23+
24+
# Create the rows for each label value
25+
rows = [f"| {value.ljust(max_width)} |" for value in label_values]
26+
27+
# Combine everything into a table
28+
print(f"{separator}\n{header}\n{separator}\n" + "\n".join(rows) + f"\n{separator}")
29+
30+
31+
#
32+
# Parse arguments
33+
#
34+
if len(sys.argv) != 3:
35+
print(('usage: %s <sysdig-token> <label-name>' % sys.argv[0]))
36+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
37+
sys.exit(1)
38+
39+
sdc_token = sys.argv[1]
40+
41+
sdclient = SdcClient(sdc_token)
42+
43+
#
44+
# The label name to fetch values for
45+
#
46+
label_name = "job"
47+
48+
#
49+
# Optional matchers to filter the label values
50+
#
51+
match = None # Replace with a list of matchers if needed
52+
53+
#
54+
# Optional limit
55+
#
56+
limit = 10 # Set to None to disable the limit
57+
58+
#
59+
# Fetch label values
60+
#
61+
ok, response_json = sdclient.get_label_values(label_name, match=match, limit=limit)
62+
63+
#
64+
# Show the result
65+
#
66+
if ok:
67+
#
68+
# Read the response. The JSON looks like this:
69+
#
70+
# {
71+
# "data": [
72+
# "fluentd-default",
73+
# "harbor-registry-default",
74+
# "k8s-cadvisor-default",
75+
# "k8s-kubelet-default",
76+
# "k8s-pods",
77+
# "k8s-pvc-default",
78+
# ],
79+
# "status": "success"
80+
# }
81+
#
82+
label_values = response_json.get("data", [])
83+
render_label_values_as_table(label_values)
84+
else:
85+
print("Error retrieving label values:", response_json)

sdcclient/_common.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,32 @@ def get_labels(self, match=None, limit=None):
566566
res = self.http.get(url, headers=self.hdrs, params=params)
567567
return self._request_result(res)
568568

569+
def get_label_values(self, label_name, match=None, limit=None):
570+
'''**Description**
571+
Retrieve the values for a specific label.
572+
573+
**Arguments**
574+
- **label_name**: the name of the label to retrieve values for.
575+
- **match**: a list of PromQL matchers to filter the label values.
576+
- **limit**: the maximum number of returned values. A value of 0 disables the limit.
577+
578+
**Success Return Value**
579+
A list of values for the specified label.
580+
581+
**Examples**
582+
- `examples/get_label_values.py`
583+
'''
584+
params = {}
585+
586+
if match:
587+
params["match[]"] = match # `match` should be a list of matchers
588+
if limit:
589+
params["limit"] = limit
590+
591+
url = f"{self.url}/prometheus/api/v1/label/{label_name}/values"
592+
res = self.http.get(url, headers=self.hdrs, params=params)
593+
return self._request_result(res)
594+
569595
def get_sysdig_captures(self, from_sec=None, to_sec=None, scope_filter=None):
570596
'''**Description**
571597
Returns the list of sysdig captures for the user.

0 commit comments

Comments
 (0)