Skip to content

Commit

Permalink
feat!: Support slo-generator config v2 format (core changes) (#126)
Browse files Browse the repository at this point in the history
* feat!: Support slo-generator config v2 format (core changes)
* Add step property
* Fix kind filtering
* Fix Istio cluster naming for Service Monitoring API + modularize logging format based on debug flag
* Fix linting
  • Loading branch information
Olivier Cervello authored and lvaylet committed Nov 25, 2022
1 parent 8a2525b commit ecdf9bd
Show file tree
Hide file tree
Showing 14 changed files with 653 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""
`stackdriver.py`
Stackdriver Monitoring backend implementation.
`cloud_monitoring.py`
Cloud Monitoring backend implementation.
"""
import logging
import pprint
Expand All @@ -27,15 +27,16 @@
LOGGER = logging.getLogger(__name__)


class StackdriverBackend:
"""Backend for querying metrics from Stackdriver Monitoring.
class CloudMonitoringBackend:
"""Backend for querying metrics from Cloud Monitoring.
Args:
project_id (str): Stackdriver host project id.
project_id (str): Cloud Monitoring host project id.
client (google.cloud.monitoring_v3.MetricServiceClient, optional):
Existing Stackdriver Service Monitoring client. Initialize a new
client if omitted.
Existing Cloud Monitoring Metrics client. Initialize a new client
if omitted.
"""

def __init__(self, project_id, client=None):
self.client = client
if client is None:
Expand All @@ -54,8 +55,7 @@ def good_bad_ratio(self, timestamp, window, slo_config):
Returns:
tuple: A tuple (good_event_count, bad_event_count)
"""
conf = slo_config['backend']
measurement = conf['measurement']
measurement = slo_config['spec']['service_level_indicator']
filter_good = measurement['filter_good']
filter_bad = measurement.get('filter_bad')
filter_valid = measurement.get('filter_valid')
Expand All @@ -65,24 +65,24 @@ def good_bad_ratio(self, timestamp, window, slo_config):
window=window,
filter=filter_good)
good_ts = list(good_ts)
good_event_count = SD.count(good_ts)
good_event_count = CM.count(good_ts)

# Query 'bad events' timeseries
if filter_bad:
bad_ts = self.query(timestamp=timestamp,
window=window,
filter=filter_bad)
bad_ts = list(bad_ts)
bad_event_count = SD.count(bad_ts)
bad_event_count = CM.count(bad_ts)
elif filter_valid:
valid_ts = self.query(timestamp=timestamp,
window=window,
filter=filter_valid)
valid_ts = list(valid_ts)
bad_event_count = SD.count(valid_ts) - good_event_count
bad_event_count = CM.count(valid_ts) - good_event_count
else:
raise Exception(
"Oneof `filter_bad` or `filter_valid` is required.")
"One of `filter_bad` or `filter_valid` is required.")

LOGGER.debug(f'Good events: {good_event_count} | '
f'Bad events: {bad_event_count}')
Expand All @@ -100,8 +100,7 @@ def distribution_cut(self, timestamp, window, slo_config):
Returns:
tuple: A tuple (good_event_count, bad_event_count).
"""
conf = slo_config['backend']
measurement = conf['measurement']
measurement = slo_config['spec']['service_level_indicator']
filter_valid = measurement['filter_valid']
threshold_bucket = int(measurement['threshold_bucket'])
good_below_threshold = measurement.get('good_below_threshold', True)
Expand Down Expand Up @@ -168,7 +167,7 @@ def query(self,
aligner='ALIGN_SUM',
reducer='REDUCE_SUM',
group_by=[]):
"""Query timeseries from Stackdriver Monitoring.
"""Query timeseries from Cloud Monitoring.
Args:
timestamp (int): Current timestamp.
Expand All @@ -181,8 +180,8 @@ def query(self,
Returns:
list: List of timeseries objects.
"""
measurement_window = SD.get_window(timestamp, window)
aggregation = SD.get_aggregation(window,
measurement_window = CM.get_window(timestamp, window)
aggregation = CM.get_aggregation(window,
aligner=aligner,
reducer=reducer,
group_by=group_by)
Expand Down Expand Up @@ -261,4 +260,4 @@ def get_aggregation(window,
return aggregation


SD = StackdriverBackend
CM = CloudMonitoringBackend
Loading

0 comments on commit ecdf9bd

Please sign in to comment.