1
- import numpy as np
2
- import matplotlib .pyplot as plt
3
1
import random
4
-
5
2
from collections import defaultdict
6
3
4
+ import matplotlib .pyplot as plt
5
+ import numpy as np
7
6
from opentelemetry import metrics
8
7
from opentelemetry .sdk .metrics import Counter , MeterProvider
9
8
from opentelemetry .sdk .metrics .export .aggregate import SumAggregator
10
9
from opentelemetry .sdk .metrics .export .controller import PushController
11
- from opentelemetry .sdk .metrics .export .in_memory_metrics_exporter import InMemoryMetricsExporter
10
+ from opentelemetry .sdk .metrics .export .in_memory_metrics_exporter import (
11
+ InMemoryMetricsExporter ,
12
+ )
12
13
from opentelemetry .sdk .metrics .view import View , ViewConfig
13
14
14
- ## set up opentelemetry
15
+ # set up opentelemetry
15
16
16
17
# Sets the global MeterProvider instance
17
18
metrics .set_meter_provider (MeterProvider ())
47
48
48
49
meter .register_view (counter_view )
49
50
50
- ## generate the random metric data
51
+ # generate the random metric data
52
+
51
53
52
54
def unknown_customer_calls ():
53
55
"""Generate customer call data to our application"""
@@ -58,23 +60,49 @@ def unknown_customer_calls():
58
60
random .seed (1 )
59
61
60
62
# customer 123 is a big user, and made 1000 requests in this timeframe
61
- requests = np .random .normal (1000 , 250 , 1000 ) # 1000 requests with average 1000 bytes, covariance 100
63
+ requests = np .random .normal (
64
+ 1000 , 250 , 1000
65
+ ) # 1000 requests with average 1000 bytes, covariance 100
62
66
63
67
for request in requests :
64
- bytes_counter .add (int (request ), {"environment" : "production" , "method" : "REST" , "customer_id" : 123 })
68
+ bytes_counter .add (
69
+ int (request ),
70
+ {
71
+ "environment" : "production" ,
72
+ "method" : "REST" ,
73
+ "customer_id" : 123 ,
74
+ },
75
+ )
65
76
66
77
# customer 247 is another big user, making fewer, but bigger requests
67
- requests = np .random .normal (5000 , 1250 , 200 ) # 200 requests with average size of 5k bytes
78
+ requests = np .random .normal (
79
+ 5000 , 1250 , 200
80
+ ) # 200 requests with average size of 5k bytes
68
81
69
82
for request in requests :
70
- bytes_counter .add (int (request ), {"environment" : "production" , "method" : "REST" , "customer_id" : 247 })
83
+ bytes_counter .add (
84
+ int (request ),
85
+ {
86
+ "environment" : "production" ,
87
+ "method" : "REST" ,
88
+ "customer_id" : 247 ,
89
+ },
90
+ )
71
91
72
92
# There are many other smaller customers
73
93
for customer_id in range (250 ):
74
94
requests = np .random .normal (1000 , 250 , np .random .randint (1 , 10 ))
75
95
method = "REST" if np .random .randint (2 ) else "gRPC"
76
96
for request in requests :
77
- bytes_counter .add (int (request ), {"environment" : "production" , "method" : method , "customer_id" : customer_id })
97
+ bytes_counter .add (
98
+ int (request ),
99
+ {
100
+ "environment" : "production" ,
101
+ "method" : method ,
102
+ "customer_id" : customer_id ,
103
+ },
104
+ )
105
+
78
106
79
107
unknown_customer_calls ()
80
108
@@ -94,10 +122,15 @@ def unknown_customer_calls():
94
122
customer_bytes_map [exemplar .dropped_labels ] += exemplar .value
95
123
96
124
97
- customer_bytes_list = sorted (list (customer_bytes_map .items ()), key = lambda t : t [1 ], reverse = True )
125
+ customer_bytes_list = sorted (
126
+ list (customer_bytes_map .items ()), key = lambda t : t [1 ], reverse = True
127
+ )
98
128
99
129
# Save our top 5 customers and sum all of the rest into "Others".
100
- top_5_customers = [("Customer {}" .format (dict (val [0 ])["customer_id" ]), val [1 ]) for val in customer_bytes_list [:5 ]] + [("Other Customers" , sum ([val [1 ] for val in customer_bytes_list [5 :]]))]
130
+ top_5_customers = [
131
+ ("Customer {}" .format (dict (val [0 ])["customer_id" ]), val [1 ])
132
+ for val in customer_bytes_list [:5 ]
133
+ ] + [("Other Customers" , sum ([val [1 ] for val in customer_bytes_list [5 :]]))]
101
134
102
135
# unzip the data into X (sizes of each customer's contribution) and labels
103
136
labels , X = zip (* top_5_customers )
@@ -107,26 +140,45 @@ def unknown_customer_calls():
107
140
plt .show ()
108
141
109
142
# Estimate how many bytes customer 123 sent
110
- customer_123_bytes = customer_bytes_map [(("customer_id" , 123 ), ("method" , "REST" ))]
143
+ customer_123_bytes = customer_bytes_map [
144
+ (("customer_id" , 123 ), ("method" , "REST" ))
145
+ ]
111
146
112
147
# Since the exemplars were randomly sampled, all sample_counts will be the same
113
148
sample_count = exemplars [0 ].sample_count
114
149
print ("sample count" , sample_count , "custmer" , customer_123_bytes )
115
150
full_customer_123_bytes = sample_count * customer_123_bytes
116
151
117
152
# With seed == 1 we get 1008612 - quite close to the statistical mean of 1000000! (more exemplars would make this estimation even more accurate)
118
- print ("Customer 123 sent about {} bytes this interval" .format (int (full_customer_123_bytes )))
153
+ print (
154
+ "Customer 123 sent about {} bytes this interval" .format (
155
+ int (full_customer_123_bytes )
156
+ )
157
+ )
119
158
120
159
# Determine the top 25 customers by how many bytes they sent in exemplars
121
160
top_25_customers = customer_bytes_list [:25 ]
122
161
123
162
# out of those 25 customers, determine how many used grpc, and come up with a ratio
124
- percent_grpc = len (list (filter (lambda customer_value : customer_value [0 ][1 ][1 ] == "gRPC" , top_25_customers ))) / len (top_25_customers )
125
-
126
- print ("~{}% of the top 25 customers (by bytes in) used gRPC this interval" .format (int (percent_grpc * 100 )))
163
+ percent_grpc = len (
164
+ list (
165
+ filter (
166
+ lambda customer_value : customer_value [0 ][1 ][1 ] == "gRPC" ,
167
+ top_25_customers ,
168
+ )
169
+ )
170
+ ) / len (top_25_customers )
171
+
172
+ print (
173
+ "~{}% of the top 25 customers (by bytes in) used gRPC this interval" .format (
174
+ int (percent_grpc * 100 )
175
+ )
176
+ )
127
177
128
178
# Determine the 50th, 90th, and 99th percentile of byte size sent in
129
- quantiles = np .quantile ([exemplar .value for exemplar in exemplars ], [0.5 , 0.9 , 0.99 ])
179
+ quantiles = np .quantile (
180
+ [exemplar .value for exemplar in exemplars ], [0.5 , 0.9 , 0.99 ]
181
+ )
130
182
print ("50th Percentile Bytes In:" , int (quantiles [0 ]))
131
183
print ("90th Percentile Bytes In:" , int (quantiles [1 ]))
132
184
print ("99th Percentile Bytes In:" , int (quantiles [2 ]))
0 commit comments