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 XHR Request Timing Telemetry #2711

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions cobalt/xhr/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ static_library("xhr") {
"xml_http_request.h",
"xml_http_request_event_target.cc",
"xml_http_request_event_target.h",
"xml_http_request_metrics.cc",
"xml_http_request_metrics.h",
]

public_deps = [ ":xhr_settings" ]
Expand Down Expand Up @@ -67,6 +69,7 @@ target(gtest_target_type, "xhr_test") {
sources = [
"fetch_buffer_pool_test.cc",
"xhr_settings_test.cc",
"xml_http_request_metrics_test.cc",
"xml_http_request_test.cc",
]
deps = [
Expand Down
1 change: 1 addition & 0 deletions cobalt/xhr/xml_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,7 @@
void XMLHttpRequestImpl::ReportLoadTimingInfo(
const net::LoadTimingInfo& timing_info) {
load_timing_info_ = timing_info;
metrics_.ReportResourceTimingMetrics(timing_info);

Check warning on line 1220 in cobalt/xhr/xml_http_request.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/xhr/xml_http_request.cc#L1220

Added line #L1220 was not covered by tests
}

void XMLHttpRequestImpl::GetLoadTimingInfoAndCreateResourceTiming() {
Expand Down
3 changes: 3 additions & 0 deletions cobalt/xhr/xml_http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "cobalt/web/environment_settings.h"
#include "cobalt/xhr/url_fetcher_buffer_writer.h"
#include "cobalt/xhr/xml_http_request_event_target.h"
#include "cobalt/xhr/xml_http_request_metrics.h"
#include "cobalt/xhr/xml_http_request_upload.h"
#include "net/base/load_timing_info.h"
#include "net/http/http_request_headers.h"
Expand Down Expand Up @@ -455,6 +456,8 @@ class XMLHttpRequestImpl
prevent_gc_until_send_complete_;

std::string request_body_text_;

XMLHttpRequestMetrics metrics_;
};

class DOMXMLHttpRequestImpl : public XMLHttpRequestImpl {
Expand Down
76 changes: 76 additions & 0 deletions cobalt/xhr/xml_http_request_metrics.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/xhr/xml_http_request_metrics.h"

#include "base/metrics/histogram_macros.h"
#include "net/base/load_timing_info.h"

namespace cobalt {
namespace xhr {

void XMLHttpRequestMetrics::ReportResourceTimingMetrics(
const net::LoadTimingInfo& timing_info) {
if (!(timing_info.connect_timing.connect_start.is_null() ||
timing_info.connect_timing.connect_end.is_null())) {
base::TimeDelta tcp_handshake_time =
timing_info.connect_timing.connect_end -
timing_info.connect_timing.connect_start;
UMA_HISTOGRAM_TIMES("Cobalt.Media.XHR.ResourceTiming.TcpHandshakeTime",
tcp_handshake_time);
}

if (!(timing_info.connect_timing.dns_start.is_null() ||
timing_info.connect_timing.dns_end.is_null())) {
base::TimeDelta dns_lookup_time = timing_info.connect_timing.dns_end -
timing_info.connect_timing.dns_start;
UMA_HISTOGRAM_TIMES("Cobalt.Media.XHR.ResourceTiming.DnsLookupTime",
dns_lookup_time);
}

if (!(timing_info.send_start.is_null() ||
timing_info.receive_headers_end.is_null())) {
base::TimeDelta request_time =
timing_info.receive_headers_end - timing_info.send_start;
UMA_HISTOGRAM_TIMES("Cobalt.Media.XHR.ResourceTiming.RequestTime",
request_time);
}

if (!(timing_info.connect_timing.ssl_start.is_null() ||
timing_info.send_start.is_null())) {
base::TimeDelta tls_negotiation_time =
timing_info.send_start - timing_info.connect_timing.ssl_start;
UMA_HISTOGRAM_TIMES("Cobalt.Media.XHR.ResourceTiming.TlsNegotiationTime",
tls_negotiation_time);
}

if (!(timing_info.request_start.is_null())) {
base::TimeDelta time_to_fetch =
clock_->NowTicks() - timing_info.request_start;
UMA_HISTOGRAM_TIMES("Cobalt.Media.XHR.ResourceTiming.TimeToFetch",
time_to_fetch);
}

if (!(timing_info.service_worker_start_time.is_null() ||
timing_info.request_start.is_null())) {
base::TimeDelta service_worker_processing_time =
timing_info.request_start - timing_info.service_worker_start_time;
UMA_HISTOGRAM_TIMES(
"Cobalt.Media.XHR.ResourceTiming.ServiceWorkerProcessingTime",
service_worker_processing_time);
}
}

} // namespace xhr
} // namespace cobalt
44 changes: 44 additions & 0 deletions cobalt/xhr/xml_http_request_metrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COBALT_XHR_XML_HTTP_REQUEST_METRICS_H_
#define COBALT_XHR_XML_HTTP_REQUEST_METRICS_H_

#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "net/base/load_timing_info.h"

namespace cobalt {
namespace xhr {

class XMLHttpRequestMetrics {
public:
XMLHttpRequestMetrics(
const base::TickClock* clock = base::DefaultTickClock::GetInstance())
: clock_(clock) {}
~XMLHttpRequestMetrics() = default;

void ReportResourceTimingMetrics(const net::LoadTimingInfo& timing_info);

private:
XMLHttpRequestMetrics(const XMLHttpRequestMetrics&) = delete;
XMLHttpRequestMetrics& operator=(const XMLHttpRequestMetrics&) = delete;

const base::TickClock* clock_;
};

} // namespace xhr
} // namespace cobalt

#endif // COBALT_XHR_XML_HTTP_REQUEST_METRICS_H_
Loading
Loading