<jupyter_start><jupyter_text>Creates SLI and derives SLO from SLI SLI Definition**Percentage of transactions for mutating API calls that get completed by the Kube API Server below 1 sec**`The percentage of sufficiently fast requests of mutating API calls, as measured at the API Server.“Sufficiently fast” is defined as < 1 secondEvaluation interval is 5minMutating calls are: POST, PUT, DELETE and PATCH requests`For each 5min interval, we calculatecount of mutating http_requests with a duration less than or equal to "1.0" seconds divided by count of all mutating http_requestsAnd we plot this over a period of time as defined in SLO```sum(increase(apiserver_request_duration_seconds_bucket{apiserver="kube-apiserver",le="1",verb=~"POST|PUT|DELETE|PATCH"}[5m])) / sum(increase(apiserver_request_duration_seconds_count{apiserver="kube-apiserver",verb=~"POST|PUT|DELETE|PATCH"}[5m]))````increase` could have replaced by `rate` as well. Result would be same. SLO Definition99% ≤1 second over any five-minute period AND 99% of those are satisfied in a rolling 7 and 28 day period.In that 7 day period, we consider a 5 min interval a success if the SLI above returned 99% or higherAnd aggregate of all 5 min interval to pass that threshold must be 99% of all 5 min interval in the SLO time periodNote - only considering 7 day scenario below. It would work perfectly well if we changed the settings to 28 days.  Sets the connection details<jupyter_code># URL of the route call rbac-query-proxy 
# created in namespace:open-cluster-management-observability
# after installing Observability on Advanaced Cluster Managment Install
url = "https://prometheus-k8s-openshift-monitoring.apps.xxx.com/"
# bearer token obtained from command `oc whoami -t`
token = "<PASSWORD>"
#connects to Thanos or Prometheus as dictated by the URL
pc = PrometheusConnect(url=url, headers={"Authorization": "Bearer {}".format(token)}, disable_ssl=True);<jupyter_output><empty_output><jupyter_text>Sets the time range and time step for all queries below<jupyter_code>#datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
#today = datetime.datetime.today()
#end_time=datetime.datetime(year=today.year, month=today.month,day=today.day, hour=0, second=0) 
#start_time=(end_time - datetime.timedelta(minutes=60))
start_time=(datetime.datetime.now() - datetime.timedelta(minutes=10080))
end_time=datetime.datetime.now()
#interval between data points gathered
step='30m'<jupyter_output><empty_output><jupyter_text>Calculates the SLI<jupyter_code># PromQL being run: count(ALERTS{alertstate="firing"}) by (cluster,alertname)
sli = pc.custom_query_range(
    #query='sum(rate(apiserver_request_total{apiserver="kube-apiserver",code!~"5.."}[5m]))/sum(rate(apiserver_request_total{apiserver="kube-apiserver"}[5m]))',
    query='sum(increase(apiserver_request_duration_seconds_bucket{apiserver="kube-apiserver",le="1",verb=~"POST|PUT|DELETE|PATCH"}[5m])) / sum(increase(apiserver_request_duration_seconds_count{apiserver="kube-apiserver",verb=~"POST|PUT|DELETE|PATCH"}[5m]))',
    start_time=start_time,
    end_time=end_time,
    step=step,
)

sli_df = MetricRangeDataFrame(sli);
sli_df["value"]=sli_df["value"].astype(float)
sli_df.index= pandas.to_datetime(sli_df.index, unit="s")

print(sli_df.head())
print(sli_df.count())


ax=sli_df.plot(title="SLI: Percentage of mutating calls that completed in le 1sec")
ax.legend(bbox_to_anchor=(0., 1.06, 1., .102), loc='lower left', ncol=2, mode="expand", borderaxespad=0.)
plt.show()<jupyter_output>value
timestamp                 
2021-08-19 15:17:33    1.0
2021-08-19 15:47:33    1.0
2021-08-19 16:17:33    1.0
2021-08-19 16:47:33    1.0
2021-08-19 17:17:33    1.0
value    337
dtype: int64<jupyter_text>Create a new column called result```if (SLI value in one interval (or 1 observation or 1 step0)) >=0.990 :then     set result value =1else     set result value = 0```<jupyter_code>sli_df['result'] = np.where(sli_df['value'] >= 0.9900, 1, 0)
print(sli_df.head())
#print(sli_df.count())
print("Breakdown of result columm--")
print(sli_df['result'].value_counts())

#ax=sli_df['result'].plot(title="SLI Alertnate view: Intervals where All mutating calls that completed in le 1sec",color="green")
#ax.legend(bbox_to_anchor=(0., 1.06, 1., .102), loc='lower left', ncol=2, mode="expand", borderaxespad=0.)
#ax.set(facecolor = "red")

f, ax = plt.subplots()
ax.bar(sli_df.index, sli_df['result'],color="green", width=0.05)
ax.set(title="SLI Alertnate view: Intervals where All mutating calls that completed in le 1sec")
#ax.set(facecolor = "red")
plt.xticks(rotation=90)
plt.show()<jupyter_output>value  result
timestamp                         
2021-08-19 15:17:33    1.0       1
2021-08-19 15:47:33    1.0       1
2021-08-19 16:17:33    1.0       1
2021-08-19 16:47:33    1.0       1
2021-08-19 17:17:33    1.0       1
Breakdown of result columm--
1    337
Name: result, dtype: int64<jupyter_text>SLO Calculation<jupyter_code>total_hi = sli_df['result'].sum()
print("Sum of all times when the result=1: ", total_hi)
total_count = sli_df['result'].count()
print("Count of all observations (should be (12*24*7)+1=2017): ",total_count)
pct_slo = total_hi/total_count
print("SLO Value for last 7 days: ", pct_slo * 100)<jupyter_output>Sum of all times when the result=1:  337
Count of all observations (should be (12*24*7)+1=2017):  337
SLO Value for last 7 days:  100.0
{'fraction_non_alphanumeric': 0.12831858407079647, 'fraction_numerical': 0.056784660766961655, 'mean_word_length': 5.689272503082614, 'pattern_counts': {'":': 1, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '270192', 'license': 'Apache-2.0', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1868, 'n_words': 507, 'path': 'SLO_SLI.ipynb', 'repo_name': 'Randy424/multicluster-notebooks', 'size': '5439'}