Skip to content
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Commit 00fa425

Browse files
author
Stephen Gutekanst
committed
python/appdash: add the much-needed HTTP server and client events.
1 parent 0acb722 commit 00fa425

1 file changed

Lines changed: 144 additions & 1 deletion

File tree

‎python/appdash/event.py‎

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,147 @@ def marshal(self):
9797
"ClientRecv": self.clientRecv,
9898
}
9999

100-
# TODO(slimsag): add HTTPEvent
100+
# _flattenReqRes flattens the request or response info dictionary. It has very
101+
# defined functionality primarily to avoid code duplication four times below in
102+
# marshaling of HTTP events.
103+
def _flattenReqRes(reqRes, into):
104+
for k, v in reqRes.items():
105+
if k == "Headers":
106+
into["Request.Headers." + k] = str(v)
107+
else:
108+
into["Request." + k] = str(v)
109+
return into
110+
111+
# HTTPServerEvent represents a HTTP event where a client's request was served.
112+
#
113+
# e = HTTPServerEvent(
114+
# send = sendTimestamp,
115+
# recv = recvTimestamp,
116+
# route = "/endpoint-B",
117+
# user = "",
118+
# requestInfo = {
119+
# "Method": "GET",
120+
# "URI": "/endpoint-B",
121+
# "Proto": "HTTP/1.1",
122+
# "Headers": {}, # dictionary of strings in proper HTTP case "Foo-Bar".
123+
# "Host": "localhost:8699",
124+
# "RemoteAddr": "127.0.0.1:35787",
125+
# "ContentLength": 0,
126+
# },
127+
# responseInfo = {
128+
# "Method": "GET",
129+
# "URI": "/endpoint-B",
130+
# "Proto": "HTTP/1.1",
131+
# "Headers": { # dictionary of strings in proper HTTP case "Foo-Bar".
132+
# "Span-Id": "8d4bdb285382e850/ef1ba6f3fa12d5d2/d9564f71aae8aba2",
133+
# },
134+
# "Host": "localhost:8699",
135+
# "RemoteAddr": "127.0.0.1:35787",
136+
# "ContentLength": 28,
137+
# },
138+
# )
139+
#
140+
class HTTPServerEvent:
141+
schema = "HTTPServer"
142+
143+
# Request headers and information.
144+
requestInfo = {}
145+
146+
# Response headers and information.
147+
responseInfo = {}
148+
149+
# The route, e.g. "/endpoint-B".
150+
route = ""
151+
152+
# The user (if any).
153+
user = ""
154+
155+
# RFC3339-UTC timestamp of when the query was sent, and later a result received.
156+
serverRecv = ""
157+
serverSend = ""
158+
159+
def __init__(self, send, recv=None, requestInfo = {}, responseInfo = {}, route = "", user=""):
160+
self.requestInfo = requestInfo
161+
self.responseInfo = responseInfo
162+
self.route = route
163+
self.user = user
164+
self.serverSend = timeString(send)
165+
166+
# If user didn't specify a recv time, use right now.
167+
if recv:
168+
self.serverRecv = timeString(recv)
169+
else:
170+
self.serverRecv = timeString(time.time())
171+
172+
def marshal(self):
173+
d = {
174+
"Route": self.route,
175+
"User": self.user,
176+
"ServerRecv": self.serverRecv,
177+
"ServerSend": self.serverSend,
178+
}
179+
d = _flattenReqRes(self.requestInfo, d)
180+
d = _flattenReqRes(self.responseInfo, d)
181+
return d
182+
183+
# HTTPClientEvent represents a HTTP event where a outbound request to a server
184+
# was made.
185+
#
186+
# e = HTTPClientEvent(
187+
# send = sendTimestamp,
188+
# recv = recvTimestamp,
189+
# requestInfo = {
190+
# "Method": "GET",
191+
# "URI": "/endpoint-B",
192+
# "Proto": "HTTP/1.1",
193+
# "Headers": {}, # dictionary of strings in proper HTTP case "Foo-Bar".
194+
# "Host": "localhost:8699",
195+
# "RemoteAddr": "127.0.0.1:35787",
196+
# "ContentLength": 0,
197+
# },
198+
# responseInfo = {
199+
# "Method": "GET",
200+
# "URI": "/endpoint-B",
201+
# "Proto": "HTTP/1.1",
202+
# "Headers": { # dictionary of strings in proper HTTP case "Foo-Bar".
203+
# "Span-Id": "8d4bdb285382e850/ef1ba6f3fa12d5d2/d9564f71aae8aba2",
204+
# },
205+
# "Host": "localhost:8699",
206+
# "RemoteAddr": "127.0.0.1:35787",
207+
# "ContentLength": 28,
208+
# },
209+
# )
210+
#
211+
class HTTPClientEvent:
212+
schema = "HTTPClient"
213+
214+
# Request headers and information.
215+
requestInfo = {}
216+
217+
# Response headers and information.
218+
responseInfo = {}
219+
220+
# RFC3339-UTC timestamp of when the query was sent, and later a result received.
221+
serverRecv = ""
222+
serverSend = ""
223+
224+
def __init__(self, send, recv=None, requestInfo = {}, responseInfo = {}):
225+
self.requestInfo = requestInfo
226+
self.responseInfo = responseInfo
227+
self.serverSend = timeString(send)
228+
229+
# If user didn't specify a recv time, use right now.
230+
if recv:
231+
self.serverRecv = timeString(recv)
232+
else:
233+
self.serverRecv = timeString(time.time())
234+
235+
def marshal(self):
236+
d = {
237+
"ServerRecv": self.serverRecv,
238+
"ServerSend": self.serverSend,
239+
}
240+
d = _flattenReqRes(self.requestInfo, d)
241+
d = _flattenReqRes(self.responseInfo, d)
242+
return d
243+

0 commit comments

Comments
 (0)