Skip to content

Commit 19543ed

Browse files
authored
Merge pull request #44 from draios/better-paging-coverage
Document paging parameter in get_data() and improve corresponding examples
2 parents 5a5709f + 4700c89 commit 19543ed

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

examples/get_data_advanced.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env python
22
#
3-
# This script shows an advanced Sysdig Cloud data request that leverages
3+
# This script shows an advanced Sysdig Monitor data request that leverages
44
# filtering and segmentation.
55
#
6-
# The request returns the last 10 minutes of CPU utilization for all of the
7-
# containers inside the given host, with 1 minute data granularity
6+
# The request returns the last 10 minutes of CPU utilization for the 5
7+
# busiest containers inside the given host, with 1 minute data granularity
88
#
99

1010
import os
1111
import sys
12+
import json
1213
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1314
from sdcclient import SdcClient
1415

@@ -34,10 +35,10 @@
3435
metrics = [
3536
# The first metric we request is the container name. This is a segmentation
3637
# metric, and you can tell by the fact that we don't specify any aggregation
37-
# criteria. This entry tells Sysdig Cloud that we want to see the CPU
38+
# criteria. This entry tells Sysdig Monitor that we want to see the CPU
3839
# utilization for each container separately.
3940
{"id": "container.name"},
40-
# The second metric we reuest is the CPU. We aggregate it as an average.
41+
# The second metric we request is the CPU. We aggregate it as an average.
4142
{"id": "cpu.used.percent",
4243
"aggregations": {
4344
"time": "avg",
@@ -51,19 +52,26 @@
5152
#
5253
filter = "host.hostName = '%s'" % hostname
5354

55+
#
56+
# Paging (from and to included; by default you get from=0 to=9)
57+
# Here we'll get the top 5.
58+
#
59+
paging = { "from": 0, "to": 4 }
60+
5461
#
5562
# Fire the query.
5663
#
57-
res = sdclient.get_data(metrics, # metrics list
58-
-600, # start_ts = 600 seconds ago
59-
0, # end_ts = now
60-
60, # 1 data point per minute
61-
filter, # The filter
62-
'container') # The source for our metrics is the container
64+
res = sdclient.get_data(metrics=metrics, # List of metrics to query
65+
start_ts=-600, # Start of query span is 600 seconds ago
66+
end_ts=0, # End the query span now
67+
sampling_s=60, # 1 data point per minute
68+
filter=filter, # The filter specifying the target host
69+
paging=paging, # Paging to limit to just the 5 most busy
70+
datasource_type='container') # The source for our metrics is the container
6371

6472
#
6573
# Show the result!
6674
#
67-
print res[1]
75+
print json.dumps(res[1], sort_keys=True, indent=4)
6876
if not res[0]:
6977
sys.exit(1)

examples/get_data_simple.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
#
3-
# This script shows the basics of getting data out of Sysdig Cloud by creating a
4-
# very simple request that has no filter an no segmentation.
3+
# This script shows the basics of getting data out of Sysdig Monitor by creating a
4+
# very simple request that has no filter and no segmentation.
55
#
66
# The request queries for the average CPU across all of the instrumented hosts for
77
# the last 10 minutes, with 1 minute data granularity
@@ -54,15 +54,10 @@
5454
#
5555
sampling = 60
5656

57-
#
58-
# Paging (from and to included; by default you get from=0 to=9)
59-
#
60-
paging = { "from": 0, "to": 9 }
61-
6257
#
6358
# Load data
6459
#
65-
res = sdclient.get_data(metrics, start, end, sampling, filter = filter, paging = paging)
60+
res = sdclient.get_data(metrics, start, end, sampling, filter = filter)
6661

6762
#
6863
# Show the result

sdcclient/_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def get_data(self, metrics, start_ts, end_ts=0, sampling_s=0,
412412
- **sampling_s**: the duration of the samples that will be returned. 0 means that the whole data will be returned as a single sample.
413413
- **filter**: a boolean expression combining Sysdig Monitor segmentation criteria that defines what the query will be applied to. For example: *kubernetes.namespace.name='production' and container.image='nginx'*.
414414
- **datasource_type**: specify the metric source for the request, can be ``container`` or ``host``. Most metrics, for example ``cpu.used.percent`` or ``memory.bytes.used``, are reported by both hosts and containers. By default, host metrics are used, but if the request contains a container-specific grouping key in the metric list/filter (e.g. ``container.name``), then the container source is used. In cases where grouping keys are missing or apply to both hosts and containers (e.g. ``tag.Name``), *datasource_type* can be explicitly set to avoid any ambiguity and allow the user to select precisely what kind of data should be used for the request. `examples/get_data_datasource.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_data_datasource.py>`_ contains a few examples that should clarify the use of this argument.
415-
- **paging**:
415+
- **paging**: if segmentation of the query generates values for several different entities (e.g. containers/hosts), this parameter specifies which to include in the returned result. It's specified as a dictionary of inclusive values for ``from`` and ``to`` with the default being ``{ "from": 0, "to": 9 }``, which will return values for the "top 10" entities. The meaning of "top" is query-dependent, based on points having been sorted via the specified group aggregation, with the results sorted in ascending order if the group aggregation is ``min`` or ``none``, and descending order otherwise.
416416
417417
**Success Return Value**
418418
A dictionary with the requested data. Data is organized in a list of time samples, each of which includes a UTC timestamp and a list of values, whose content and order reflect what was specified in the *metrics* argument.

0 commit comments

Comments
 (0)