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
@@ -28,14 +27,6 @@ class TestFlaskIntegration(WsgiTestBase):
28
27
def setUp (self ):
29
28
super ().setUp ()
30
29
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
30
self .app = Flask (__name__ )
40
31
41
32
def hello_endpoint (helloid ):
@@ -49,88 +40,60 @@ def hello_endpoint(helloid):
49
40
self .client = Client (self .app , BaseResponse )
50
41
51
42
def test_simple (self ):
43
+ expected_attrs = {
44
+ "component" : "http" ,
45
+ "http.method" : "GET" ,
46
+ "http.host" : "localhost" ,
47
+ "http.url" : "http://localhost/hello/123" ,
48
+ "http.route" : "/hello/<int:helloid>" ,
49
+ "http.status_code" : 200 ,
50
+ "http.status_text" : "OK" ,
51
+ }
52
52
resp = self .client .get ("/hello/123" )
53
53
self .assertEqual (200 , resp .status_code )
54
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
- start_time = mock .ANY ,
61
- )
62
-
63
- # TODO: Change this test to use the SDK, as mocking becomes painful
64
-
65
- self .assertEqual (
66
- self .span_attrs ,
67
- {
68
- "component" : "http" ,
69
- "http.method" : "GET" ,
70
- "http.host" : "localhost" ,
71
- "http.url" : "http://localhost/hello/123" ,
72
- "http.route" : "/hello/<int:helloid>" ,
73
- "http.status_code" : 200 ,
74
- "http.status_text" : "OK" ,
75
- },
76
- )
55
+ span_list = self .memory_exporter .get_finished_spans ()
56
+ self .assertEqual (len (span_list ), 1 )
57
+ self .assertEqual (span_list [0 ].name , "hello_endpoint" )
58
+ self .assertEqual (span_list [0 ].kind , trace_api .SpanKind .SERVER )
59
+ self .assertEqual (span_list [0 ].attributes , expected_attrs )
77
60
78
61
def test_404 (self ):
62
+ expected_attrs = {
63
+ "component" : "http" ,
64
+ "http.method" : "POST" ,
65
+ "http.host" : "localhost" ,
66
+ "http.url" : "http://localhost/bye" ,
67
+ "http.status_code" : 404 ,
68
+ "http.status_text" : "NOT FOUND" ,
69
+ }
79
70
resp = self .client .post ("/bye" )
80
71
self .assertEqual (404 , resp .status_code )
81
72
resp .close ()
82
-
83
- self .start_span .assert_called_with (
84
- "/bye" ,
85
- trace_api .INVALID_SPAN_CONTEXT ,
86
- kind = trace_api .SpanKind .SERVER ,
87
- start_time = mock .ANY ,
88
- )
89
-
90
- # Nope, this uses Tracer.use_span(end_on_exit)
91
- # self.assertEqual(1, self.span.end.call_count)
92
- # TODO: Change this test to use the SDK, as mocking becomes painful
93
-
94
- self .assertEqual (
95
- self .span_attrs ,
96
- {
97
- "component" : "http" ,
98
- "http.method" : "POST" ,
99
- "http.host" : "localhost" ,
100
- "http.url" : "http://localhost/bye" ,
101
- "http.status_code" : 404 ,
102
- "http.status_text" : "NOT FOUND" ,
103
- },
104
- )
73
+ span_list = self .memory_exporter .get_finished_spans ()
74
+ self .assertEqual (len (span_list ), 1 )
75
+ self .assertEqual (span_list [0 ].name , "/bye" )
76
+ self .assertEqual (span_list [0 ].kind , trace_api .SpanKind .SERVER )
77
+ self .assertEqual (span_list [0 ].attributes , expected_attrs )
105
78
106
79
def test_internal_error (self ):
80
+ expected_attrs = {
81
+ "component" : "http" ,
82
+ "http.method" : "GET" ,
83
+ "http.host" : "localhost" ,
84
+ "http.url" : "http://localhost/hello/500" ,
85
+ "http.route" : "/hello/<int:helloid>" ,
86
+ "http.status_code" : 500 ,
87
+ "http.status_text" : "INTERNAL SERVER ERROR" ,
88
+ }
107
89
resp = self .client .get ("/hello/500" )
108
90
self .assertEqual (500 , resp .status_code )
109
91
resp .close ()
110
-
111
- self .start_span .assert_called_with (
112
- "hello_endpoint" ,
113
- trace_api .INVALID_SPAN_CONTEXT ,
114
- kind = trace_api .SpanKind .SERVER ,
115
- start_time = mock .ANY ,
116
- )
117
-
118
- # Nope, this uses Tracer.use_span(end_on_exit)
119
- # self.assertEqual(1, self.span.end.call_count)
120
- # TODO: Change this test to use the SDK, as mocking becomes painful
121
-
122
- self .assertEqual (
123
- self .span_attrs ,
124
- {
125
- "component" : "http" ,
126
- "http.method" : "GET" ,
127
- "http.host" : "localhost" ,
128
- "http.url" : "http://localhost/hello/500" ,
129
- "http.route" : "/hello/<int:helloid>" ,
130
- "http.status_code" : 500 ,
131
- "http.status_text" : "INTERNAL SERVER ERROR" ,
132
- },
133
- )
92
+ span_list = self .memory_exporter .get_finished_spans ()
93
+ self .assertEqual (len (span_list ), 1 )
94
+ self .assertEqual (span_list [0 ].name , "hello_endpoint" )
95
+ self .assertEqual (span_list [0 ].kind , trace_api .SpanKind .SERVER )
96
+ self .assertEqual (span_list [0 ].attributes , expected_attrs )
134
97
135
98
136
99
if __name__ == "__main__" :
0 commit comments