Skip to content

Commit 1809063

Browse files
Add get_metadata.py function.
1 parent bafc58a commit 1809063

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

examples/get_metadata.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script demonstrates how to use the `get_metadata` function to retrieve metadata
4+
# about metrics and render the output as a table.
5+
#
6+
7+
import sys
8+
from sdcclient import SdcClient
9+
10+
def render_metadata_as_table(metadata):
11+
if not metadata:
12+
print("No metadata found.")
13+
return
14+
15+
# Extract all metric names and their metadata
16+
rows = []
17+
for metric, details in metadata.items():
18+
for detail in details:
19+
rows.append({
20+
"Metric": metric,
21+
"Type": detail.get("type", ""),
22+
"Unit": detail.get("unit", ""),
23+
"Help": detail.get("help", "")
24+
})
25+
26+
# Extract column names
27+
columns = ["Metric", "Type", "Unit", "Help"]
28+
29+
# Calculate the maximum width for each column
30+
column_widths = {col: max(len(col), max(len(str(row[col])) for row in rows)) for col in columns}
31+
32+
# Create a horizontal separator
33+
separator = "+" + "+".join("-" * (column_widths[col] + 2) for col in columns) + "+"
34+
35+
# Create the header row
36+
header = "|" + "|".join(f" {col.ljust(column_widths[col])} " for col in columns) + "|"
37+
38+
# Create the rows for each metadata entry
39+
table_rows = [
40+
"|" + "|".join(f" {str(row[col]).ljust(column_widths[col])} " for col in columns) + "|"
41+
for row in rows
42+
]
43+
44+
# Combine everything into a table
45+
print(f"{separator}\n{header}\n{separator}\n" + "\n".join(table_rows) + f"\n{separator}")
46+
47+
48+
#
49+
# Parse arguments
50+
#
51+
if len(sys.argv) != 2:
52+
print(('usage: %s <sysdig-token>' % sys.argv[0]))
53+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
54+
sys.exit(1)
55+
56+
sdc_token = sys.argv[1]
57+
58+
sdclient = SdcClient(sdc_token)
59+
60+
61+
#
62+
# Optional metric name to filter metadata
63+
#
64+
metric_name = "up" # Replace with a specific metric name if needed
65+
66+
#
67+
# Optional limit
68+
#
69+
limit = 10 # Set to None to disable the limit
70+
71+
#
72+
# Fetch metadata
73+
#
74+
ok, response_json = sdclient.get_metadata(metric_name=metric_name, limit=limit)
75+
76+
#
77+
# Show the result
78+
#
79+
if ok:
80+
#
81+
# Read the response. The JSON looks like this:
82+
#
83+
# {
84+
# "data": [
85+
# "up": [
86+
# {
87+
# "type": "gauge",
88+
# "unit": "number",
89+
# "help": ""
90+
# }
91+
# ]
92+
# ...
93+
# ],
94+
# "status": "success"
95+
# }
96+
#
97+
metadata = response_json.get("data", [])
98+
render_metadata_as_table(metadata)
99+
else:
100+
print("Error retrieving metadata:", response_json)

sdcclient/_common.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,31 @@ def get_label_values(self, label_name, match=None, limit=None):
592592
res = self.http.get(url, headers=self.hdrs, params=params)
593593
return self._request_result(res)
594594

595+
def get_metadata(self, metric_name=None, limit=None):
596+
'''**Description**
597+
Retrieve metadata about metrics.
598+
599+
**Arguments**
600+
- **metric_name**: the metric name to filter metadata for. If omitted, metadata for all metrics is retrieved.
601+
- **limit**: the maximum number of returned metadata entries. A value of 0 disables the limit.
602+
603+
**Success Return Value**
604+
A list of metadata entries for the specified metric(s).
605+
606+
**Examples**
607+
- `examples/get_metadata.py`
608+
'''
609+
params = {}
610+
611+
if metric_name:
612+
params["metric"] = metric_name
613+
if limit:
614+
params["limit"] = limit
615+
616+
url = f"{self.url}/prometheus/api/v1/metadata"
617+
res = self.http.get(url, headers=self.hdrs, params=params)
618+
return self._request_result(res)
619+
595620
def get_sysdig_captures(self, from_sec=None, to_sec=None, scope_filter=None):
596621
'''**Description**
597622
Returns the list of sysdig captures for the user.

0 commit comments

Comments
 (0)