Skip to content

Commit c3dfa5f

Browse files
Add get_series endpoint
1 parent 06e1f90 commit c3dfa5f

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

examples/get_data_promql_advanced.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def print_prometheus_results_as_table(results):
110110
#
111111
if ok:
112112
#
113-
# Read response. The JSON looks like this:
113+
# Read the response. The JSON looks like this:
114114
#
115115
# {
116116
# "data": {

examples/get_data_promql_simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def print_prometheus_results_as_table(results):
9595
#
9696
if ok:
9797
#
98-
# Read response. The JSON looks like this:
98+
# Read the response. The JSON looks like this:
9999
#
100100
# {
101101
# "data": {

examples/get_series.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script demonstrates how to use the `get_series` function to retrieve metadata
4+
# about time series that match a set of label matchers.
5+
#
6+
7+
import sys
8+
from sdcclient import SdcClient
9+
10+
def render_json_as_table(results):
11+
if not data or len(data) == 0:
12+
print("No series found for the given matchers.")
13+
return
14+
15+
# Extract all keys from the JSON objects
16+
keys = list(results[0].keys())
17+
18+
# Calculate the maximum width for each column
19+
column_widths = {key: max(len(key), max(len(str(row.get(key, ""))) for row in results)) for key in keys}
20+
21+
# Create a horizontal separator
22+
separator = "+".join("-" * (column_widths[key] + 2) for key in keys)
23+
24+
# Create the header row
25+
header = "|".join(f" {key.ljust(column_widths[key])} " for key in keys)
26+
27+
# Create the rows for each JSON object
28+
rows = []
29+
for row in results:
30+
rows.append("|".join(f" {str(row.get(key, '')).ljust(column_widths[key])} " for key in keys))
31+
32+
# Combine everything into a table
33+
print(f"+{separator}+\n|{header}|\n+{separator}+\n" + "\n".join(f"|{row}|" for row in rows) + f"\n+{separator}+")
34+
35+
36+
#
37+
# Parse arguments
38+
#
39+
if len(sys.argv) != 2:
40+
print(('usage: %s <sysdig-token>' % sys.argv[0]))
41+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
42+
sys.exit(1)
43+
44+
sdc_token = sys.argv[1]
45+
46+
sdclient = SdcClient(sdc_token)
47+
48+
#
49+
# Matchers to filter the series. Example: `up` and `process_start_time_seconds{job="prometheus"}`
50+
#
51+
match = [
52+
'up',
53+
'process_start_time_seconds{job="prometheus"}'
54+
]
55+
56+
#
57+
# Optional time range
58+
#
59+
start = None # Replace with a timestamp if needed
60+
end = None # Replace with a timestamp if needed
61+
62+
#
63+
# Optional limit
64+
#
65+
limit = 5 # Set to None to disable the limit
66+
67+
#
68+
# Fetch series
69+
#
70+
ok, response_json = sdclient.get_series(match, start=start, end=end, limit=limit)
71+
72+
#
73+
# Show the result
74+
#
75+
if ok:
76+
#
77+
# Read the response. The JSON looks like this:
78+
#
79+
# {
80+
# "data": [
81+
# {
82+
# "__name__": "up",
83+
# "_sysdig_custom_metric": "true",
84+
# "_sysdig_datasource": "prometheus_remote_write",
85+
# "agent_version": "thereal-test-x86_64",
86+
# "instance": "default.test.svc.cluster.local:9544",
87+
# "jenkins_job_build_number": "1234",
88+
# "job": "default-test-20250428112951594",
89+
# "remote_write": "true"
90+
# }
91+
# ],
92+
# "status": "success"
93+
# }
94+
#
95+
data = response_json.get("data", {})
96+
render_json_as_table(data)
97+
else:
98+
print("Error retrieving series:", response_json)

sdcclient/_common.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,37 @@ def get_data_promql_instant(self, query, time=None, timeout=None, limit=None):
510510
res = self.http.get(url, headers=self.hdrs, params=params)
511511
return self._request_result(res)
512512

513+
def get_series(self, match, start=None, end=None, limit=None):
514+
'''**Description**
515+
Retrieve metadata about time series that match a set of label matchers.
516+
517+
**Arguments**
518+
- **match**: a list of PromQL matchers (e.g., `['up', 'node_cpu_seconds_total']`).
519+
- **start**: the inclusive start timestamp of the series query as RFC3339 or a unix timestamp.
520+
- **end**: the inclusive end timestamp of the series query as RFC3339 or a unix timestamp.
521+
- **limit**: the maximum number of returned series. The limit is capped at 10,000. To disable the limit, set the value to 0.
522+
523+
**Success Return Value**
524+
A list of series that match the provided matchers.
525+
526+
**Examples**
527+
- `examples/get_series.py`
528+
'''
529+
params = {
530+
"match[]": match, # `match` should be a list of matchers
531+
}
532+
533+
if start:
534+
params["start"] = start
535+
if end:
536+
params["end"] = end
537+
if limit:
538+
params["limit"] = limit
539+
540+
url = f"{self.url}/prometheus/api/v1/series"
541+
res = self.http.get(url, headers=self.hdrs, params=params)
542+
return self._request_result(res)
543+
513544
def get_sysdig_captures(self, from_sec=None, to_sec=None, scope_filter=None):
514545
'''**Description**
515546
Returns the list of sysdig captures for the user.

0 commit comments

Comments
 (0)