Package com.atlassian.bitbucket.request
Interface RequestContext
- All Superinterfaces:
RequestInfoProvider
,RequestMetadata
Provides information about a request, such as the requestId, sessionId, remoteAddress and more.
RequestContext
is currently used as an abstraction for HTTP and SSH requests, but can easily be used for other protocols as well.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addCleanupCallback
(Runnable callback) Registers a cleanup task that is executed when the current request ends.void
Adds a label to the request context that will be included in the access logs.long
long
getId()
Returns a highly unique (though not guaranteeably unique) request ID.boolean
isActive()
void
setBytesRead
(long bytesRead) Report on the number of bytes read for this request.void
setBytesWritten
(long bytesWritten) Report on the number of bytes written for this request.void
setResponseCode
(int responseCode) The response code of the request.Wraps the providedRunnable
so that it executes with this request context, including its full state (e.g.<T> Callable<T>
Wraps the providedCallable
so that it executes with this request context, including its full state (e.g.Methods inherited from interface com.atlassian.bitbucket.request.RequestInfoProvider
getRawRequest, getRawResponse
Methods inherited from interface com.atlassian.bitbucket.request.RequestMetadata
getAction, getDetails, getProtocol, getRemoteAddress, getSessionId, hasSessionId, isSecure
-
Method Details
-
addCleanupCallback
Registers a cleanup task that is executed when the current request ends. Note that the callback will only be called for the current request.- Parameters:
callback
- callback that should be called when the request ends- Throws:
IllegalStateException
- if no request isactive
-
addLabel
Adds a label to the request context that will be included in the access logs. Use this purely for diagnostic / analysis purposes.- Parameters:
label
- the label to add- Throws:
IllegalStateException
- if no request isactive
-
getId
Returns a highly unique (though not guaranteeably unique) request ID.The request ID contains:
- The minute of the current day
- The number of requests, including the current one, which have been serviced by the application since it was started
- The number of requests which were being processed concurrently at the time the ID was generated
long
for the counter wraps (which at 1,000 requests per second will take 252 million years).It is worth noting that the uniqueness period required for request IDs is 1 day, which is the period at which log files are rotated. The goal of this ID is not to be universally unique so much as it is to allow support to easily trace the logging related to a single request, within the log files.
- Returns:
- the generated request ID
- Throws:
IllegalStateException
- if no request isactive
-
isActive
boolean isActive()- Returns:
true
if the request is in progress.false
if there is no current request or the request has finished
-
getBytesRead
long getBytesRead()- Returns:
- the number of bytes read from the client for this request
- Since:
- 4.4
-
getBytesWritten
long getBytesWritten()- Returns:
- the number of bytes written to the client for this request
- Since:
- 4.4
-
getDuration
- Returns:
- a
Duration
representing the amount of wall time taken to process this request orOptional.empty()
if the duration has not been set - Since:
- 4.4
-
setBytesRead
void setBytesRead(long bytesRead) Report on the number of bytes read for this request.- Parameters:
bytesRead
- the number of bytes read- Throws:
IllegalStateException
- if no request isactive
-
setBytesWritten
void setBytesWritten(long bytesWritten) Report on the number of bytes written for this request.- Parameters:
bytesWritten
- the number of bytes written- Throws:
IllegalStateException
- if no request isactive
-
setResponseCode
void setResponseCode(int responseCode) The response code of the request. HTTP(S) requests the HTTP response code is used. SSH requests the exit code of the git process is used.- Parameters:
responseCode
- the request response code- Throws:
IllegalStateException
- if no request isactive
-
wrap
Wraps the providedCallable
so that it executes with this request context, including its full state (e.g. authenticated user, etc.).- Parameters:
callable
- the runnable to wrap- Returns:
- the wrapped runnable
- Since:
- 7.9
-
wrap
Wraps the providedRunnable
so that it executes with this request context, including its full state (e.g. authenticated user, etc.).- Parameters:
runnable
- the runnable to wrap- Returns:
- the wrapped runnable
- Since:
- 7.9
-