|
| 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) |
0 commit comments