1313# limitations under the License.
1414
1515import unittest
16- from unittest import mock
1716
1817from flask import Flask
1918from werkzeug .test import Client
2423from opentelemetry .ext .testutil .wsgitestutil import WsgiTestBase
2524
2625
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+
2744class TestFlaskIntegration (WsgiTestBase ):
2845 def setUp (self ):
2946 super ().setUp ()
3047
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-
3948 self .app = Flask (__name__ )
4049
4150 def hello_endpoint (helloid ):
@@ -49,100 +58,57 @@ def hello_endpoint(helloid):
4958 self .client = Client (self .app , BaseResponse )
5059
5160 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+ {
6763 "http.target" : "/hello/123" ,
68- "http.flavor" : "1.1" ,
6964 "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+ }
7966 )
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 )
8075
8176 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+ {
9279 "http.method" : "POST" ,
93- "http.server_name" : "localhost" ,
94- "http.scheme" : "http" ,
95- "host.port" : 80 ,
96- "http.host" : "localhost" ,
9780 "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+ }
10184 )
10285
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 )
11588 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 )
11694
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+ {
12898 "http.target" : "/hello/500" ,
129- "http.flavor" : "1.1" ,
13099 "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 ,
143100 "http.status_text" : "INTERNAL SERVER ERROR" ,
144- },
101+ "http.status_code" : 500 ,
102+ }
145103 )
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 )
146112
147113
148114if __name__ == "__main__" :
0 commit comments