33
33
starlette = pytest .importorskip ("starlette" ) # isort:skip
34
34
35
35
import mock
36
-
37
36
from starlette .applications import Starlette
38
37
from starlette .responses import PlainTextResponse
39
38
from starlette .testclient import TestClient
40
- from elasticapm .utils .disttracing import TraceParent
41
- from elasticapm import async_capture_span
42
39
40
+ from elasticapm import async_capture_span
43
41
from elasticapm .conf import constants
44
42
from elasticapm .contrib .starlette import ElasticAPM
43
+ from elasticapm .utils .disttracing import TraceParent
45
44
46
45
pytestmark = [pytest .mark .starlette ]
47
46
50
49
def app (elasticapm_client ):
51
50
app = Starlette ()
52
51
53
- @app .route ("/" )
52
+ @app .route ("/" , methods = [ "GET" , "POST" ] )
54
53
async def hi (request ):
55
54
with async_capture_span ("test" ):
56
55
pass
@@ -68,11 +67,14 @@ async def raise_exception(request):
68
67
def test_get (app , elasticapm_client ):
69
68
client = TestClient (app )
70
69
71
- response = client .get ('/' , headers = {
72
- constants .TRACEPARENT_HEADER_NAME : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" ,
73
- constants .TRACESTATE_HEADER_NAME : "foo=bar,bar=baz" ,
74
- "REMOTE_ADDR" : "127.0.0.1" ,
75
- })
70
+ response = client .get (
71
+ "/" ,
72
+ headers = {
73
+ constants .TRACEPARENT_HEADER_NAME : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" ,
74
+ constants .TRACESTATE_HEADER_NAME : "foo=bar,bar=baz" ,
75
+ "REMOTE_ADDR" : "127.0.0.1" ,
76
+ },
77
+ )
76
78
77
79
assert response .status_code == 200
78
80
@@ -93,15 +95,52 @@ def test_get(app, elasticapm_client):
93
95
assert span ["name" ] == "test"
94
96
95
97
96
- def test_exception (app , elasticapm_client ):
98
+ @pytest .mark .parametrize ("elasticapm_client" , [{"capture_body" : "all" }], indirect = True )
99
+ def test_post (app , elasticapm_client ):
97
100
client = TestClient (app )
98
101
99
- with pytest .raises (ValueError ):
100
- client .get ('/raise-exception' , headers = {
102
+ response = client .post (
103
+ "/" ,
104
+ headers = {
101
105
constants .TRACEPARENT_HEADER_NAME : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" ,
102
106
constants .TRACESTATE_HEADER_NAME : "foo=bar,bar=baz" ,
103
107
"REMOTE_ADDR" : "127.0.0.1" ,
104
- })
108
+ },
109
+ data = {"foo" : "bar" },
110
+ )
111
+
112
+ assert response .status_code == 200
113
+
114
+ assert len (elasticapm_client .events [constants .TRANSACTION ]) == 1
115
+ transaction = elasticapm_client .events [constants .TRANSACTION ][0 ]
116
+ spans = elasticapm_client .spans_for_transaction (transaction )
117
+ assert len (spans ) == 1
118
+ span = spans [0 ]
119
+
120
+ assert transaction ["name" ] == "POST /"
121
+ assert transaction ["result" ] == "HTTP 2xx"
122
+ assert transaction ["type" ] == "request"
123
+ assert transaction ["span_count" ]["started" ] == 1
124
+ request = transaction ["context" ]["request" ]
125
+ request ["method" ] == "GET"
126
+ request ["socket" ] == {"remote_address" : "127.0.0.1" , "encrypted" : False }
127
+ assert request ["body" ]["foo" ] == "bar"
128
+
129
+ assert span ["name" ] == "test"
130
+
131
+
132
+ def test_exception (app , elasticapm_client ):
133
+ client = TestClient (app )
134
+
135
+ with pytest .raises (ValueError ):
136
+ client .get (
137
+ "/raise-exception" ,
138
+ headers = {
139
+ constants .TRACEPARENT_HEADER_NAME : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" ,
140
+ constants .TRACESTATE_HEADER_NAME : "foo=bar,bar=baz" ,
141
+ "REMOTE_ADDR" : "127.0.0.1" ,
142
+ },
143
+ )
105
144
106
145
assert len (elasticapm_client .events [constants .TRANSACTION ]) == 1
107
146
transaction = elasticapm_client .events [constants .TRANSACTION ][0 ]
@@ -129,10 +168,13 @@ def test_traceparent_handling(app, elasticapm_client, header_name):
129
168
with mock .patch (
130
169
"elasticapm.contrib.flask.TraceParent.from_string" , wraps = TraceParent .from_string
131
170
) as wrapped_from_string :
132
- response = client .get ('/' , headers = {
133
- header_name : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" ,
134
- constants .TRACESTATE_HEADER_NAME : "foo=bar,baz=bazzinga" ,
135
- })
171
+ response = client .get (
172
+ "/" ,
173
+ headers = {
174
+ header_name : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" ,
175
+ constants .TRACESTATE_HEADER_NAME : "foo=bar,baz=bazzinga" ,
176
+ },
177
+ )
136
178
137
179
assert response .status_code == 200
138
180
0 commit comments