-
Notifications
You must be signed in to change notification settings - Fork 44
/
dashboard_scope.py
executable file
·67 lines (51 loc) · 2.3 KB
/
dashboard_scope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
#
# This example shows some examples of scope you can use for dashboards.
#
import sys
from sdcclient import SdcClient
#
# Scopes can be passed to most of dashboard-related functions, e.g. create_dashboard_from_file.
#
# NOTE: convert_scope_string_to_expression should never be used in a user script
# We're going to use it here just to demonstrate some scope options and some constraints
#
def evaluate(scope, expected):
parsed_scope = SdcClient.convert_scope_string_to_expression(scope)
print('{} is valid: {}'.format(scope, parsed_scope[0] is True))
if parsed_scope[0] != expected:
print('Unexpected parsing result!')
sys.exit(1)
# simple example: tag = value
evaluate('proc.name = "cassandra"', True)
# NOTE: For now you can still leave values without quotes.
# The API will be more strict, so please make sure you adopt the new format!
evaluate('proc.name = cassandra', True)
# other operators
evaluate('proc.name != "cassandra"', True)
evaluate('proc.name starts with "cassandra"', True)
evaluate('proc.name contains "cassandra"', True)
# list operators
evaluate('proc.name in ("cassandra", "mysql")', True)
# not-ed expressions
evaluate('not proc.name starts with "cassandra"', True)
evaluate('not proc.name contains "cassandra"', True)
evaluate('not proc.name in ("cassandra", "mysql")', True)
# you can combine multiple expressions; note that only AND'd scopes are currently supported
evaluate('kubernetes.service.name = "database" and proc.name = "cassandra"', True)
# the scope can obviously be omitted in the dashboard configuration
evaluate('', True)
evaluate(None, True)
# invalid scopes will cause errors
evaluate('proc.name == "cassandra"', False) # invalid operator
# currently, one space is required around operands and operators -- improvements will come soon
evaluate('proc.name="cassandra"', False)
#
# The current grammar is unable to validate all errors -- in these cases, the API will fail!
# Improvements will come soon!
#
# Here some errors that will not be detected by the Python library, but the API will
#
evaluate('proc.name = "cassandra" or proc.name = "mysql"', True) # not AND'd expressions are supported
evaluate('proc.name in ("cassandra\', \'mysql")', True) # mismatching quotes
evaluate('proc.name in ("cassandra", "mysql"', True) # missing parenthesis