Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :metrics option to capture headers #625

Merged
merged 24 commits into from
Mar 29, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function Client(options) {
queryTimeout: null,
http2SessionIdleTime: http2SessionIdleTime.value,
checkNewVersion: false,
metrics: false,
})

if (http2SessionIdleTime.shouldOverride) {
Expand Down Expand Up @@ -281,6 +282,22 @@ Client.prototype.close = function(opts) {
return this._http.close(opts)
}

/**
* Executes a query via the FaunaDB Query API.
* See the [docs](https://app.fauna.com/documentation/reference/queryapi),
* and the query functions in this documentation.
* @param expression {module:query~ExprArg}
* The query to execute. Created from {@link module:query} functions.
* @param {?Object} options
* Object that configures the current query, overriding FaunaDB client options.
* @param {?string} options.secret FaunaDB secret (see [Reference Documentation](https://app.fauna.com/documentation/intro/security))
* @return {external:Promise<Object>} An object containing the FaunaDB response object and the list of query metrics incurred by the request.
elersong marked this conversation as resolved.
Show resolved Hide resolved
*/
Client.prototype.queryWithMetrics = function(expression, options) {
options = Object.assign({}, options, { withMetrics: true })
elersong marked this conversation as resolved.
Show resolved Hide resolved
return this._execute('POST', '', query.wrap(expression), null, options)
}

Client.prototype._execute = function(method, path, data, query, options) {
query = util.defaults(query, null)

Expand Down Expand Up @@ -326,8 +343,29 @@ Client.prototype._execute = function(method, path, data, query, options) {
endTime
)
self._handleRequestResult(response, result, options)

const metricsHeaders = [
'x-query-bytes-in',
'x-query-bytes-out',
'x-query-time',
'x-read-ops',
'x-write-ops',
'x-compute-ops',
'x-storage-bytes-read',
'x-storage-bytes-write',
'x-txn-retries'
elersong marked this conversation as resolved.
Show resolved Hide resolved
]

return responseObject['resource']
if (options && options.metrics) {
return {
value: responseObject['resource'],
elersong marked this conversation as resolved.
Show resolved Hide resolved
metrics: Object.fromEntries(
Object.entries(response.headers).filter(([key]) => metricsHeaders.includes(key))
)
}
} else {
return responseObject['resource']
}
})
}

Expand Down