Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Support slo-generator config v2 format (core changes) #126

Merged
merged 9 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,23 @@ 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.")
raise Exception("Oneof `filter_bad` or `filter_valid` is required.")
ocervell marked this conversation as resolved.
Show resolved Hide resolved

LOGGER.debug(f'Good events: {good_event_count} | '
f'Bad events: {bad_event_count}')
Expand All @@ -100,8 +99,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 +166,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 +179,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 +259,4 @@ def get_aggregation(window,
return aggregation


SD = StackdriverBackend
CM = CloudMonitoringBackend
Loading