13
13
# limitations under the License.
14
14
15
15
import unittest
16
- from unittest import mock
17
16
18
17
from flask import Flask
19
18
from werkzeug .test import Client
24
23
from opentelemetry .ext .testutil .wsgitestutil import WsgiTestBase
25
24
26
25
26
+ def expected_attributes (override_attributes ):
27
+ default_attributes = {
28
+ "component" : "http" ,
29
+ "http.method" : "GET" ,
30
+ "http.server_name" : "localhost" ,
31
+ "http.scheme" : "http" ,
32
+ "host.port" : 80 ,
33
+ "http.host" : "localhost" ,
34
+ "http.target" : "/" ,
35
+ "http.flavor" : "1.1" ,
36
+ "http.status_text" : "OK" ,
37
+ "http.status_code" : 200 ,
38
+ }
39
+ for key , val in override_attributes .items ():
40
+ default_attributes [key ] = val
41
+ return default_attributes
42
+
43
+
27
44
class TestFlaskIntegration (WsgiTestBase ):
28
45
def setUp (self ):
29
46
super ().setUp ()
30
47
31
- self .span_attrs = {}
32
-
33
- def setspanattr (key , value ):
34
- self .assertIsInstance (key , str )
35
- self .span_attrs [key ] = value
36
-
37
- self .span .set_attribute = setspanattr
38
-
39
48
self .app = Flask (__name__ )
40
49
41
50
def hello_endpoint (helloid ):
@@ -49,100 +58,57 @@ def hello_endpoint(helloid):
49
58
self .client = Client (self .app , BaseResponse )
50
59
51
60
def test_simple (self ):
52
- resp = self .client .get ("/hello/123" )
53
- self .assertEqual (200 , resp .status_code )
54
- self .assertEqual ([b"Hello: 123" ], list (resp .response ))
55
-
56
- self .start_span .assert_called_with (
57
- "hello_endpoint" ,
58
- trace_api .INVALID_SPAN_CONTEXT ,
59
- kind = trace_api .SpanKind .SERVER ,
60
- attributes = {
61
- "component" : "http" ,
62
- "http.method" : "GET" ,
63
- "http.server_name" : "localhost" ,
64
- "http.scheme" : "http" ,
65
- "host.port" : 80 ,
66
- "http.host" : "localhost" ,
61
+ expected_attrs = expected_attributes (
62
+ {
67
63
"http.target" : "/hello/123" ,
68
- "http.flavor" : "1.1" ,
69
64
"http.route" : "/hello/<int:helloid>" ,
70
- },
71
- start_time = mock .ANY ,
72
- )
73
-
74
- # TODO: Change this test to use the SDK, as mocking becomes painful
75
-
76
- self .assertEqual (
77
- self .span_attrs ,
78
- {"http.status_code" : 200 , "http.status_text" : "OK" },
65
+ }
79
66
)
67
+ resp = self .client .get ("/hello/123" )
68
+ self .assertEqual (200 , resp .status_code )
69
+ self .assertEqual ([b"Hello: 123" ], list (resp .response ))
70
+ span_list = self .memory_exporter .get_finished_spans ()
71
+ self .assertEqual (len (span_list ), 1 )
72
+ self .assertEqual (span_list [0 ].name , "hello_endpoint" )
73
+ self .assertEqual (span_list [0 ].kind , trace_api .SpanKind .SERVER )
74
+ self .assertEqual (span_list [0 ].attributes , expected_attrs )
80
75
81
76
def test_404 (self ):
82
- resp = self .client .post ("/bye" )
83
- self .assertEqual (404 , resp .status_code )
84
- resp .close ()
85
-
86
- self .start_span .assert_called_with (
87
- "/bye" ,
88
- trace_api .INVALID_SPAN_CONTEXT ,
89
- kind = trace_api .SpanKind .SERVER ,
90
- attributes = {
91
- "component" : "http" ,
77
+ expected_attrs = expected_attributes (
78
+ {
92
79
"http.method" : "POST" ,
93
- "http.server_name" : "localhost" ,
94
- "http.scheme" : "http" ,
95
- "host.port" : 80 ,
96
- "http.host" : "localhost" ,
97
80
"http.target" : "/bye" ,
98
- "http.flavor " : "1.1 " ,
99
- } ,
100
- start_time = mock . ANY ,
81
+ "http.status_text " : "NOT FOUND " ,
82
+ "http.status_code" : 404 ,
83
+ }
101
84
)
102
85
103
- # Nope, this uses Tracer.use_span(end_on_exit)
104
- # self.assertEqual(1, self.span.end.call_count)
105
- # TODO: Change this test to use the SDK, as mocking becomes painful
106
-
107
- self .assertEqual (
108
- self .span_attrs ,
109
- {"http.status_code" : 404 , "http.status_text" : "NOT FOUND" },
110
- )
111
-
112
- def test_internal_error (self ):
113
- resp = self .client .get ("/hello/500" )
114
- self .assertEqual (500 , resp .status_code )
86
+ resp = self .client .post ("/bye" )
87
+ self .assertEqual (404 , resp .status_code )
115
88
resp .close ()
89
+ span_list = self .memory_exporter .get_finished_spans ()
90
+ self .assertEqual (len (span_list ), 1 )
91
+ self .assertEqual (span_list [0 ].name , "/bye" )
92
+ self .assertEqual (span_list [0 ].kind , trace_api .SpanKind .SERVER )
93
+ self .assertEqual (span_list [0 ].attributes , expected_attrs )
116
94
117
- self .start_span .assert_called_with (
118
- "hello_endpoint" ,
119
- trace_api .INVALID_SPAN_CONTEXT ,
120
- kind = trace_api .SpanKind .SERVER ,
121
- attributes = {
122
- "component" : "http" ,
123
- "http.method" : "GET" ,
124
- "http.server_name" : "localhost" ,
125
- "http.scheme" : "http" ,
126
- "host.port" : 80 ,
127
- "http.host" : "localhost" ,
95
+ def test_internal_error (self ):
96
+ expected_attrs = expected_attributes (
97
+ {
128
98
"http.target" : "/hello/500" ,
129
- "http.flavor" : "1.1" ,
130
99
"http.route" : "/hello/<int:helloid>" ,
131
- },
132
- start_time = mock .ANY ,
133
- )
134
-
135
- # Nope, this uses Tracer.use_span(end_on_exit)
136
- # self.assertEqual(1, self.span.end.call_count)
137
- # TODO: Change this test to use the SDK, as mocking becomes painful
138
-
139
- self .assertEqual (
140
- self .span_attrs ,
141
- {
142
- "http.status_code" : 500 ,
143
100
"http.status_text" : "INTERNAL SERVER ERROR" ,
144
- },
101
+ "http.status_code" : 500 ,
102
+ }
145
103
)
104
+ resp = self .client .get ("/hello/500" )
105
+ self .assertEqual (500 , resp .status_code )
106
+ resp .close ()
107
+ span_list = self .memory_exporter .get_finished_spans ()
108
+ self .assertEqual (len (span_list ), 1 )
109
+ self .assertEqual (span_list [0 ].name , "hello_endpoint" )
110
+ self .assertEqual (span_list [0 ].kind , trace_api .SpanKind .SERVER )
111
+ self .assertEqual (span_list [0 ].attributes , expected_attrs )
146
112
147
113
148
114
if __name__ == "__main__" :
0 commit comments