-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildFcm.py
64 lines (53 loc) · 2.97 KB
/
buildFcm.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
import numpy as np
# Function that build call frequency matrix Fcm
def Fcm(prom, PERIOD, APP):
app_names = APP # Get app names with the relative function
# add app name of istio ingress in app_names list for Fcm matrix
app_istio_ingress = "istio-ingressgateway"
app_names = app_names + [app_istio_ingress] # Add istio-ingress to app_names list
# Create a matrix with numpy to store calling probabilities
Fcm = np.zeros((len(app_names), len(app_names)), dtype=float)
# Find and filter significant probabilities
for i, src_app in enumerate(app_names):
for j, dst_app in enumerate(app_names):
# Check if source and destination apps are different
if src_app != dst_app:
# total requests that arrive to dst microservice from src microservice
query1 = f'sum by (destination_app) (rate(istio_requests_total{{source_app="{src_app}",reporter="destination", destination_app="{dst_app}",response_code="200"}}[{PERIOD}m]))'
#total requests that arrive to the source microservice
if src_app != "istio-ingressgateway":
# Query for istio-ingress
query2 = f'sum by (source_app) (rate(istio_requests_total{{reporter="destination", destination_app="{src_app}",response_code="200"}}[{PERIOD}m]))'
else:
query2 = f'sum by (source_app) (rate(istio_requests_total{{source_app="{src_app}",reporter="destination", destination_app="{dst_app}",response_code="200"}}[{PERIOD}m]))'
#print(query1)
#print(query2)
r1 = prom.custom_query(query=query1)
r2 = prom.custom_query(query=query2)
# Initialize variables
calling_probability = None
v = 0
s = 0
# Extract values from queries
if r1 and r2:
for result in r1:
if float(result["value"][1]) == 0:
continue
v = result["value"][1]
#print("v =", v)
for result in r2:
if float(result["value"][1]) == 0:
continue
s = result["value"][1]
#print("s =", s)
# Calculate calling probabilities
if s == 0 or s == "Nan":
calling_probability = 0 # If there isn't traffic
else:
calling_probability = float(v) / float(s)
if calling_probability > 0.98:
calling_probability = 1
calling_probability = round(calling_probability, 3) # Round to 4 decimal places
Fcm[i, j] = calling_probability # Insert the value inside Fcm matrix
#print(Fcm)
return Fcm