Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
5aff96d6
Unverified
Commit
5aff96d6
authored
5 years ago
by
Cam Saul
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix API log timing for StreamingResponses (#11944)
parent
e5d8439a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/async/streaming_response.clj
+16
-5
16 additions, 5 deletions
src/metabase/async/streaming_response.clj
src/metabase/middleware/log.clj
+17
-8
17 additions, 8 deletions
src/metabase/middleware/log.clj
with
33 additions
and
13 deletions
src/metabase/async/streaming_response.clj
+
16
−
5
View file @
5aff96d6
...
...
@@ -107,7 +107,7 @@
(
.flush
writer
))
(
catch
Throwable
_
))))
(
defn-
write-to-stream!
[
f
{
:keys
[
write-keepalive-newlines?
]}
^
OutputStream
os
]
(
defn-
write-to-stream!
[
f
{
:keys
[
write-keepalive-newlines?
]}
^
OutputStream
os
finished-chan
]
(
with-open-chan
[
canceled-chan
(
a/promise-chan
)]
(
with-open
[
os
os
os
(
jetty-eof-canceling-output-stream
os
canceled-chan
)
...
...
@@ -118,9 +118,13 @@
(
catch
Throwable
e
(
write-error!
os
{
:message
(
.getMessage
e
)}))
(
finally
(
.flush
os
))))))
(
.flush
os
)
(
a/>!!
finished-chan
(
if
(
a/poll!
canceled-chan
)
:canceled
:done
))
(
a/close!
finished-chan
))))))
(
p.types/deftype+
StreamingResponse
[
f
options
]
(
p.types/deftype+
StreamingResponse
[
f
options
donechan
]
pretty/PrettyPrintable
(
pretty
[
_
]
(
list
(
symbol
(
str
(
.getCanonicalName
StreamingResponse
)
\.
))
f
options
))
...
...
@@ -128,7 +132,7 @@
;; both sync and async responses
ring.protocols/StreamableResponseBody
(
write-body-to-stream
[
_
_
os
]
(
write-to-stream!
f
options
os
))
(
write-to-stream!
f
options
os
donechan
))
;; async responses only
compojure.response/Sendable
...
...
@@ -138,6 +142,12 @@
:headers
(
:headers
options
)
:status
202
}))))
(
defn
finished-chan
"Fetch a promise channel that will get a message when a `StreamingResponse` is completely finished. Provided primarily
for logging purposes."
[
^
StreamingResponse
response
]
(
.donechan
response
))
(
defmacro
streaming-response
"Return an streaming response that writes keepalive newline bytes.
...
...
@@ -159,4 +169,5 @@
[
options
[
os-binding
canceled-chan-binding
:as
bindings
]
&
body
]
{
:pre
[(
=
(
count
bindings
)
2
)]}
`
(
->StreamingResponse
(
fn
[
~
(
vary-meta
os-binding
assoc
:tag
'java.io.OutputStream
)
~
canceled-chan-binding
]
~@
body
)
~
options
))
~
options
(
a/promise-chan
)))
This diff is collapsed.
Click to expand it.
src/metabase/middleware/log.clj
+
17
−
8
View file @
5aff96d6
...
...
@@ -6,12 +6,15 @@
[
metabase
[
server
:as
server
]
[
util
:as
u
]]
[
metabase.async.util
:as
async.u
]
[
metabase.async
[
streaming-response
:as
streaming-response
]
[
util
:as
async.u
]]
[
metabase.middleware.util
:as
middleware.u
]
[
metabase.query-processor.middleware.async
:as
qp.middleware.async
]
[
metabase.util.i18n
:refer
[
trs
]]
[
toucan.db
:as
db
])
(
:import
clojure.core.async.impl.channels.ManyToManyChannel
metabase.async.streaming_response.StreamingResponse
org.eclipse.jetty.util.thread.QueuedThreadPool
))
;; To simplify passing large amounts of arguments around most functions in this namespace take an "info" map that
...
...
@@ -132,18 +135,24 @@
;; [async] wait for the pipe to close the canceled/finished channel and log the API response
(
a/go
(
let
[
result
(
a/<!
chan
)]
(
log-info
(
assoc
info
:async-status
(
if
(
nil?
result
)
"canceled"
"completed"
)))))
;; [sync] return response as-is
response
)
(
log-info
(
assoc
info
:async-status
(
if
(
nil?
result
)
"canceled"
"completed"
))))))
(
defn-
log-streaming-response
[{{
streaming-response
:body,
:as
response
}
:response,
:as
info
}]
;; [async] wait for the streaming response to be canceled/finished channel and log the API response
(
let
[
finished-chan
(
streaming-response/finished-chan
streaming-response
)]
(
a/go
(
let
[
result
(
a/<!
finished-chan
)]
(
log-info
(
assoc
info
:async-status
(
if
(
:canceled
result
)
"canceled"
"completed"
)))))))
(
defn-
logged-response
"Log an API response. Returns resonse, possibly modified (i.e., core.async channels will be wrapped); this value
should be passed to the normal `respond` function."
[{{
:keys
[
body
]
,
:as
response
}
:response,
:as
info
}]
(
if
(
instance?
ManyToManyChannel
body
)
(
log-core-async-response
info
)
(
do
(
log-info
info
)
response
)))
(
condp
instance?
body
ManyToManyChannel
(
log-core-async-response
info
)
StreamingResponse
(
log-streaming-response
info
)
(
log-info
info
))
response
)
;;; +----------------------------------------------------------------------------------------------------------------+
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment