Skip to content

Commit

Permalink
Log http request duration
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Sep 27, 2022
1 parent 1bf951c commit 92846d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/10142
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Estimate duration of network requests in httplogger

We now include an estimated duration in the httploger.

https://github.com/owncloud/client/pull/10142
30 changes: 15 additions & 15 deletions src/common/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,21 +592,6 @@ QString Utility::sanitizeForFileName(const QString &name)
return result;
}

QDebug &operator<<(QDebug &debug, nanoseconds in)
{
QDebugStateSaver save(debug);
debug.nospace();
const auto h = duration_cast<hours>(in);
const auto min = duration_cast<minutes>(in -= h);
const auto s = duration_cast<seconds>(in -= min);
const auto ms = duration_cast<milliseconds>(in -= s);
return debug << "std::chrono::duration("
<< h.count() << "h, "
<< min.count() << "min, "
<< s.count() << "s, "
<< ms.count() << "ms)";
}

QString Utility::renderTemplate(QString templ, const QMap<QString, QString> &values)
{
static const QRegularExpression pattern(QStringLiteral("@{([^{}]+)}"));
Expand Down Expand Up @@ -636,3 +621,18 @@ QString Utility::renderTemplate(QString templ, const QMap<QString, QString> &val
}

} // namespace OCC

QDebug &operator<<(QDebug &debug, nanoseconds in)
{
QDebugStateSaver save(debug);
debug.nospace();
const auto h = duration_cast<hours>(in);
const auto min = duration_cast<minutes>(in -= h);
const auto s = duration_cast<seconds>(in -= min);
const auto ms = duration_cast<milliseconds>(in -= s);
return debug << "std::chrono::duration("
<< h.count() << "h, "
<< min.count() << "min, "
<< s.count() << "s, "
<< ms.count() << "ms)";
}
4 changes: 2 additions & 2 deletions src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ inline bool Utility::isBSD()
return false;
#endif
}

OCSYNC_EXPORT QDebug &operator<<(QDebug &debug, std::chrono::nanoseconds in);
}
#endif // UTILITY_H

OCSYNC_EXPORT QDebug &operator<<(QDebug &debug, std::chrono::nanoseconds in);
22 changes: 16 additions & 6 deletions src/libsync/httplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@

#include "httplogger.h"

#include "common/utility.h"

#include <QRegularExpression>
#include <QLoggingCategory>
#include <QBuffer>

#include <chrono>

using namespace std::chrono;

namespace {
Q_LOGGING_CATEGORY(lcNetworkHttp, "sync.httplogger", QtWarningMsg)

Expand All @@ -33,12 +39,13 @@ bool isTextBody(const QString &s)
return regexp.match(s).hasMatch();
}

void logHttp(const QByteArray &verb, const QString &url, const QByteArray &id, const QString &contentType, const QList<QNetworkReply::RawHeaderPair> &header, QIODevice *device)
void logHttp(const QByteArray &verb, const QString &url, const QByteArray &id, const QString &contentType, const QList<QNetworkReply::RawHeaderPair> &header, QIODevice *device, const nanoseconds &duration = {})
{
const auto reply = qobject_cast<QNetworkReply *>(device);
const auto contentLength = device ? device->size() : 0;
QString msg;
QTextStream stream(&msg);
QDebug stream(&msg);
stream.nospace().noquote();
stream << id << ": ";
if (!reply) {
stream << "Request: ";
Expand All @@ -54,7 +61,7 @@ void logHttp(const QByteArray &verb, const QString &url, const QByteArray &id, c
if (reply->attribute(QNetworkRequest::HttpPipeliningWasUsedAttribute).toBool()) {
stream << "Piplined,";
}
stream << ")";
stream << duration << ")";
}
stream << " " << url << " Header: { ";
for (const auto &it : header) {
Expand Down Expand Up @@ -95,10 +102,12 @@ namespace OCC {

void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operation operation, QIODevice *device)
{
const auto request = reply->request();
if (!lcNetworkHttp().isInfoEnabled()) {
return;
}
const auto start = steady_clock::now();
const auto request = reply->request();

const auto keys = request.rawHeaderList();
QList<QNetworkReply::RawHeaderPair> header;
header.reserve(keys.size());
Expand All @@ -112,13 +121,14 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
header,
device);

QObject::connect(reply, &QNetworkReply::finished, reply, [reply] {
QObject::connect(reply, &QNetworkReply::finished, reply, [reply, start] {
logHttp(requestVerb(*reply),
reply->url().toString(),
reply->request().rawHeader(XRequestId()),
reply->header(QNetworkRequest::ContentTypeHeader).toString(),
reply->rawHeaderPairs(),
reply);
reply,
steady_clock::now() - start);
});
}

Expand Down

0 comments on commit 92846d2

Please sign in to comment.