15
15
16
16
from timeit import default_timer
17
17
18
- from tornado .testing import AsyncHTTPTestCase
19
-
20
- from opentelemetry import trace
21
18
from opentelemetry .instrumentation .tornado import TornadoInstrumentor
22
- from opentelemetry .sdk .metrics .export import (
23
- HistogramDataPoint ,
24
- NumberDataPoint ,
25
- )
26
- from opentelemetry .test .test_base import TestBase
27
-
28
- from .tornado_test_app import make_app
29
-
19
+ from opentelemetry .sdk .metrics .export import HistogramDataPoint
30
20
31
- class TornadoTest (AsyncHTTPTestCase , TestBase ):
32
- # pylint:disable=no-self-use
33
- def get_app (self ):
34
- tracer = trace .get_tracer (__name__ )
35
- app = make_app (tracer )
36
- return app
37
-
38
- def get_sorted_metrics (self ):
39
- resource_metrics = (
40
- self .memory_metrics_reader .get_metrics_data ().resource_metrics
41
- )
42
- for metrics in resource_metrics :
43
- for scope_metrics in metrics .scope_metrics :
44
- all_metrics = list (scope_metrics .metrics )
45
- return self .sorted_metrics (all_metrics )
46
-
47
- @staticmethod
48
- def sorted_metrics (metrics ):
49
- """
50
- Sorts metrics by metric name.
51
- """
52
- return sorted (
53
- metrics ,
54
- key = lambda m : m .name ,
55
- )
21
+ from .test_instrumentation import ( # pylint: disable=no-name-in-module,import-error
22
+ TornadoTest ,
23
+ )
56
24
57
- def assert_metric_expected (
58
- self , metric , expected_value , expected_attributes
59
- ):
60
- data_point = next (iter (metric .data .data_points ))
61
25
62
- if isinstance (data_point , HistogramDataPoint ):
63
- self .assertEqual (
64
- data_point .sum ,
65
- expected_value ,
66
- )
67
- elif isinstance (data_point , NumberDataPoint ):
68
- self .assertEqual (
69
- data_point .value ,
70
- expected_value ,
26
+ class TestTornadoMetricsInstrumentation (TornadoTest ):
27
+ # Return Sequence with one histogram
28
+ def create_histogram_data_points (self , sum_data_point , attributes ):
29
+ return [
30
+ self .create_histogram_data_point (
31
+ sum_data_point , 1 , sum_data_point , sum_data_point , attributes
71
32
)
33
+ ]
72
34
73
- self .assertDictEqual (
74
- expected_attributes ,
75
- dict (data_point .attributes ),
76
- )
77
-
78
- def assert_duration_metric_expected (
79
- self , metric , duration_estimated , expected_attributes
80
- ):
81
- data_point = next (iter (metric .data .data_points ))
82
-
83
- self .assertAlmostEqual (
84
- data_point .sum ,
85
- duration_estimated ,
86
- delta = 200 ,
87
- )
88
-
89
- self .assertDictEqual (
90
- expected_attributes ,
91
- dict (data_point .attributes ),
92
- )
93
-
94
- def setUp (self ):
95
- super ().setUp ()
96
- TornadoInstrumentor ().instrument (
97
- server_request_hook = getattr (self , "server_request_hook" , None ),
98
- client_request_hook = getattr (self , "client_request_hook" , None ),
99
- client_response_hook = getattr (self , "client_response_hook" , None ),
100
- meter_provider = self .meter_provider ,
101
- )
102
-
103
- def tearDown (self ):
104
- TornadoInstrumentor ().uninstrument ()
105
- super ().tearDown ()
106
-
107
-
108
- class TestTornadoInstrumentor (TornadoTest ):
109
35
def test_basic_metrics (self ):
110
36
start_time = default_timer ()
111
37
response = self .fetch ("/" )
@@ -132,104 +58,122 @@ def test_basic_metrics(self):
132
58
)
133
59
self .assert_metric_expected (
134
60
server_active_request ,
135
- 0 ,
136
- {
137
- "http.method" : "GET" ,
138
- "http.flavor" : "HTTP/1.1" ,
139
- "http.scheme" : "http" ,
140
- "http.target" : "/" ,
141
- "http.host" : response .request .headers ["host" ],
142
- },
61
+ [
62
+ self .create_number_data_point (
63
+ 0 ,
64
+ attributes = {
65
+ "http.method" : "GET" ,
66
+ "http.flavor" : "HTTP/1.1" ,
67
+ "http.scheme" : "http" ,
68
+ "http.target" : "/" ,
69
+ "http.host" : response .request .headers ["host" ],
70
+ },
71
+ ),
72
+ ],
143
73
)
144
74
145
75
self .assertEqual (server_duration .name , "http.server.duration" )
146
- self .assert_duration_metric_expected (
76
+ self .assert_metric_expected (
147
77
server_duration ,
148
- client_duration_estimated ,
149
- {
150
- "http.status_code" : response .code ,
151
- "http.method" : "GET" ,
152
- "http.flavor" : "HTTP/1.1" ,
153
- "http.scheme" : "http" ,
154
- "http.target" : "/" ,
155
- "http.host" : response .request .headers ["host" ],
156
- },
78
+ self .create_histogram_data_points (
79
+ client_duration_estimated ,
80
+ attributes = {
81
+ "http.method" : "GET" ,
82
+ "http.flavor" : "HTTP/1.1" ,
83
+ "http.scheme" : "http" ,
84
+ "http.target" : "/" ,
85
+ "http.host" : response .request .headers ["host" ],
86
+ "http.status_code" : response .code ,
87
+ },
88
+ ),
89
+ est_value_delta = 200 ,
157
90
)
158
91
159
92
self .assertEqual (server_request_size .name , "http.server.request.size" )
160
93
self .assert_metric_expected (
161
94
server_request_size ,
162
- 0 ,
163
- {
164
- "http.status_code" : 200 ,
165
- "http.method" : "GET" ,
166
- "http.flavor" : "HTTP/1.1" ,
167
- "http.scheme" : "http" ,
168
- "http.target" : "/" ,
169
- "http.host" : response .request .headers ["host" ],
170
- },
95
+ self .create_histogram_data_points (
96
+ 0 ,
97
+ attributes = {
98
+ "http.status_code" : 200 ,
99
+ "http.method" : "GET" ,
100
+ "http.flavor" : "HTTP/1.1" ,
101
+ "http.scheme" : "http" ,
102
+ "http.target" : "/" ,
103
+ "http.host" : response .request .headers ["host" ],
104
+ },
105
+ ),
171
106
)
172
107
173
108
self .assertEqual (
174
109
server_response_size .name , "http.server.response.size"
175
110
)
176
111
self .assert_metric_expected (
177
112
server_response_size ,
178
- len (response .body ),
179
- {
180
- "http.status_code" : response .code ,
181
- "http.method" : "GET" ,
182
- "http.flavor" : "HTTP/1.1" ,
183
- "http.scheme" : "http" ,
184
- "http.target" : "/" ,
185
- "http.host" : response .request .headers ["host" ],
186
- },
113
+ self .create_histogram_data_points (
114
+ len (response .body ),
115
+ attributes = {
116
+ "http.status_code" : response .code ,
117
+ "http.method" : "GET" ,
118
+ "http.flavor" : "HTTP/1.1" ,
119
+ "http.scheme" : "http" ,
120
+ "http.target" : "/" ,
121
+ "http.host" : response .request .headers ["host" ],
122
+ },
123
+ ),
187
124
)
188
125
189
126
self .assertEqual (client_duration .name , "http.client.duration" )
190
- self .assert_duration_metric_expected (
127
+ self .assert_metric_expected (
191
128
client_duration ,
192
- client_duration_estimated ,
193
- {
194
- "http.status_code" : response .code ,
195
- "http.method" : "GET" ,
196
- "http.url" : response .effective_url ,
197
- },
129
+ self .create_histogram_data_points (
130
+ client_duration_estimated ,
131
+ attributes = {
132
+ "http.status_code" : response .code ,
133
+ "http.method" : "GET" ,
134
+ "http.url" : response .effective_url ,
135
+ },
136
+ ),
137
+ est_value_delta = 200 ,
198
138
)
199
139
200
140
self .assertEqual (client_request_size .name , "http.client.request.size" )
201
141
self .assert_metric_expected (
202
142
client_request_size ,
203
- 0 ,
204
- {
205
- "http.status_code" : response .code ,
206
- "http.method" : "GET" ,
207
- "http.url" : response .effective_url ,
208
- },
143
+ self .create_histogram_data_points (
144
+ 0 ,
145
+ attributes = {
146
+ "http.status_code" : response .code ,
147
+ "http.method" : "GET" ,
148
+ "http.url" : response .effective_url ,
149
+ },
150
+ ),
209
151
)
210
152
211
153
self .assertEqual (
212
154
client_response_size .name , "http.client.response.size"
213
155
)
214
156
self .assert_metric_expected (
215
157
client_response_size ,
216
- len (response .body ),
217
- {
218
- "http.status_code" : response .code ,
219
- "http.method" : "GET" ,
220
- "http.url" : response .effective_url ,
221
- },
158
+ self .create_histogram_data_points (
159
+ len (response .body ),
160
+ attributes = {
161
+ "http.status_code" : response .code ,
162
+ "http.method" : "GET" ,
163
+ "http.url" : response .effective_url ,
164
+ },
165
+ ),
222
166
)
223
167
224
168
def test_metric_uninstrument (self ):
225
169
self .fetch ("/" )
226
170
TornadoInstrumentor ().uninstrument ()
227
171
self .fetch ("/" )
228
172
229
- metrics_list = self .memory_metrics_reader . get_metrics_data ()
230
- for resource_metric in metrics_list . resource_metrics :
231
- for scope_metric in resource_metric . scope_metrics :
232
- for metric in scope_metric . metrics :
233
- for point in list (metric .data .data_points ):
234
- if isinstance (point , HistogramDataPoint ):
235
- self .assertEqual (point .count , 1 )
173
+ metrics = self .get_sorted_metrics ()
174
+ self . assertEqual ( len ( metrics ), 7 )
175
+
176
+ for metric in metrics :
177
+ for point in list (metric .data .data_points ):
178
+ if isinstance (point , HistogramDataPoint ):
179
+ self .assertEqual (point .count , 1 )
0 commit comments