Skip to content

Commit 619b3ca

Browse files
Add get_labels endpoint
1 parent c3dfa5f commit 619b3ca

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

examples/get_labels.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script demonstrates how to use the `get_labels` function to retrieve metadata
4+
# about label names and render the output as a table.
5+
#
6+
7+
import sys
8+
from sdcclient import SdcClient
9+
10+
def render_labels_as_table(labels):
11+
if not labels:
12+
print("No labels found.")
13+
return
14+
15+
# Calculate the maximum width for the label column
16+
max_width = max(len(label) for label in labels)
17+
18+
# Create a horizontal separator
19+
separator = "+" + "-" * (max_width + 2) + "+"
20+
21+
# Create the header row
22+
header = f"| {'Label'.ljust(max_width)} |"
23+
24+
# Create the rows for each label
25+
rows = [f"| {label.ljust(max_width)} |" for label in labels]
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) != 2:
35+
print(('usage: %s <sysdig-token>' % 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+
# Optional matchers to filter the labels
45+
#
46+
match = [
47+
'up'
48+
] # Replace with a list of matchers if needed
49+
50+
#
51+
# Optional limit
52+
#
53+
limit = 10 # Set to None to disable the limit
54+
55+
#
56+
# Fetch labels
57+
#
58+
ok, response_json = sdclient.get_labels(match=match, limit=limit)
59+
60+
#
61+
# Show the result
62+
#
63+
if ok:
64+
#
65+
# Read the response. The JSON looks like this:
66+
#
67+
# {
68+
# "status": "success",
69+
# "data": [
70+
# "agent_id",
71+
# "k8s_app",
72+
# "kube_pod_uid",
73+
# "kubernetes_io_cluster_service",
74+
# "container_image_tag",
75+
# "cloud_provider_tag_team",
76+
# "cloud_provider_tag_expirationDate",
77+
# "kube_pod_label_kubernetes_azure_com_managedby",
78+
# ]
79+
# }
80+
#
81+
labels = response_json.get("data", [])
82+
render_labels_as_table(labels)
83+
else:
84+
print("Error retrieving labels:", response_json)

sdcclient/_common.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,31 @@ def get_series(self, match, start=None, end=None, limit=None):
541541
res = self.http.get(url, headers=self.hdrs, params=params)
542542
return self._request_result(res)
543543

544+
def get_labels(self, match=None, limit=None):
545+
'''**Description**
546+
Retrieve metadata about label names.
547+
548+
**Arguments**
549+
- **match**: a list of PromQL matchers to filter the labels.
550+
- **limit**: the maximum number of returned labels. A value of 0 disables the limit.
551+
552+
**Success Return Value**
553+
A list of available labels.
554+
555+
**Examples**
556+
- `examples/get_labels.py`
557+
'''
558+
params = {}
559+
560+
if match:
561+
params["match[]"] = match # `match` should be a list of matchers
562+
if limit:
563+
params["limit"] = limit
564+
565+
url = f"{self.url}/prometheus/api/v1/labels"
566+
res = self.http.get(url, headers=self.hdrs, params=params)
567+
return self._request_result(res)
568+
544569
def get_sysdig_captures(self, from_sec=None, to_sec=None, scope_filter=None):
545570
'''**Description**
546571
Returns the list of sysdig captures for the user.

0 commit comments

Comments
 (0)