Skip to content

Commit

Permalink
First implementation of Cobalt Telemetry core logic (#551)
Browse files Browse the repository at this point in the history
* First implementation of Cobalt Telemetry core logic, see
go/cobalt-telemetry.

This PR implements Cobalt versions of all the core metrics control
classes supported by //components/metrics.

A H5vcc interface is added to facilitate a callback mechanism in
Javascript to intercept metric's payloads.

For now, only UMA is supported. UKM is explicitly disabled everywhere as
is anything related to Chromium experiments (i.e., Field Trials).

b/280094891

Change-Id: I1f6da00b9bb269f5597c4b6b329262fc45dd0908
  • Loading branch information
joeltine authored Jun 15, 2023
1 parent 5259ad1 commit 73f2621
Show file tree
Hide file tree
Showing 37 changed files with 1,781 additions and 56 deletions.
2 changes: 2 additions & 0 deletions cobalt/browser/idl_files.gni
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ source_idl_files = [
"//cobalt/h5vcc/h5vcc_audio_config_array.idl",
"//cobalt/h5vcc/h5vcc_crash_log.idl",
"//cobalt/h5vcc/h5vcc_deep_link_event_target.idl",
"//cobalt/h5vcc/h5vcc_metrics.idl",
"//cobalt/h5vcc/h5vcc_platform_service.idl",
"//cobalt/h5vcc/h5vcc_runtime.idl",
"//cobalt/h5vcc/h5vcc_runtime_event_target.idl",
Expand Down Expand Up @@ -312,6 +313,7 @@ generated_header_idl_files = [
"//cobalt/encoding/text_decoder_options.idl",
"//cobalt/encoding/text_encoder_encode_into_result.idl",
"//cobalt/h5vcc/h5vcc_crash_type.idl",
"//cobalt/h5vcc/h5vcc_metric_type.idl",
"//cobalt/h5vcc/h5vcc_storage_resource_type_quota_bytes_dictionary.idl",
"//cobalt/h5vcc/h5vcc_storage_set_quota_response.idl",
"//cobalt/h5vcc/h5vcc_storage_write_test_response.idl",
Expand Down
65 changes: 65 additions & 0 deletions cobalt/browser/metrics/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2023 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.

static_library("metrics") {
sources = [
"cobalt_enabled_state_provider.cc",
"cobalt_enabled_state_provider.h",
"cobalt_h5vcc_metrics_uploader_callback.cc",
"cobalt_h5vcc_metrics_uploader_callback.h",
"cobalt_metrics_log_uploader.cc",
"cobalt_metrics_log_uploader.h",
"cobalt_metrics_service_client.cc",
"cobalt_metrics_service_client.h",
"cobalt_metrics_services_manager_client.cc",
"cobalt_metrics_services_manager_client.h",
"cobalt_metrics_uploader_callback.h",
]

deps = [
"//base",
"//cobalt/browser:generated_types",
"//cobalt/h5vcc:metric_event_handler_wrapper",
"//components/metrics",
"//components/metrics_services_manager",
"//components/prefs",
"//third_party/metrics_proto",
]
}

target(gtest_target_type, "metrics_test") {
testonly = true
has_pedantic_warnings = true

sources = [
"cobalt_metrics_log_uploader_test.cc",
"cobalt_metrics_service_client_test.cc",
"cobalt_metrics_services_manager_client_test.cc",
]

deps = [
":metrics",
"//base",
"//cobalt/browser:generated_types",
"//cobalt/h5vcc",
"//cobalt/h5vcc:metric_event_handler_wrapper",
"//cobalt/test:run_all_unittests",
"//components/metrics",
"//components/prefs:test_support",
"//testing/gmock",
"//testing/gtest",
"//third_party/metrics_proto",
"//third_party/zlib/google:compression_utils",
]
}
41 changes: 41 additions & 0 deletions cobalt/browser/metrics/cobalt_enabled_state_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 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/browser/metrics/cobalt_enabled_state_provider.h"

#include "components/metrics/enabled_state_provider.h"

namespace cobalt {
namespace browser {
namespace metrics {

bool CobaltEnabledStateProvider::IsConsentGiven() const {
return is_consent_given_;
}

bool CobaltEnabledStateProvider::IsReportingEnabled() const {
return is_reporting_enabled_;
}

void CobaltEnabledStateProvider::SetConsentGiven(bool is_consent_given) {
is_consent_given_ = is_consent_given;
}
void CobaltEnabledStateProvider::SetReportingEnabled(
bool is_reporting_enabled) {
is_reporting_enabled_ = is_reporting_enabled;
}

} // namespace metrics
} // namespace browser
} // namespace cobalt
62 changes: 62 additions & 0 deletions cobalt/browser/metrics/cobalt_enabled_state_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 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_BROWSER_METRICS_COBALT_ENABLED_STATE_PROVIDER_H_
#define COBALT_BROWSER_METRICS_COBALT_ENABLED_STATE_PROVIDER_H_

#include "components/metrics/enabled_state_provider.h"
#include "components/prefs/pref_service.h"

namespace cobalt {
namespace browser {
namespace metrics {

// A Cobalt implementation of EnabledStateProvider. This class is the primary
// entry point into enabling/disabling metrics collection and uploading.
class CobaltEnabledStateProvider : public ::metrics::EnabledStateProvider {
public:
explicit CobaltEnabledStateProvider(bool is_consent_given,
bool is_reporting_enabled)
: is_consent_given_(is_consent_given),
is_reporting_enabled_(is_reporting_enabled) {}

CobaltEnabledStateProvider(const CobaltEnabledStateProvider&) = delete;
CobaltEnabledStateProvider& operator=(const CobaltEnabledStateProvider&) =
delete;

~CobaltEnabledStateProvider() override{};

// Indicates user consent to collect and report metrics. In Cobalt, consent
// is inherited through the web application, so this is usually true.
bool IsConsentGiven() const override;

// Whether metric collection is enabled. This is what controls
// recording/aggregation of metrics.
bool IsReportingEnabled() const override;

// Setters for consent and reporting controls.
void SetConsentGiven(bool is_consent_given);
void SetReportingEnabled(bool is_reporting_enabled);

private:
bool is_consent_given_ = false;
bool is_reporting_enabled_ = false;
};


} // namespace metrics
} // namespace browser
} // namespace cobalt

#endif // COBALT_BROWSER_METRICS_COBALT_ENABLED_STATE_PROVIDER_H_
28 changes: 28 additions & 0 deletions cobalt/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 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/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.h"

namespace cobalt {
namespace browser {
namespace metrics {

void CobaltH5vccMetricsUploaderCallback::Run(
const cobalt::h5vcc::H5vccMetricType& type, const std::string& payload) {
event_handler_->callback.value().Run(type, payload);
}

} // namespace metrics
} // namespace browser
} // namespace cobalt
48 changes: 48 additions & 0 deletions cobalt/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 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_BROWSER_METRICS_COBALT_H5VCC_METRICS_UPLOADER_CALLBACK_H_
#define COBALT_BROWSER_METRICS_COBALT_H5VCC_METRICS_UPLOADER_CALLBACK_H_

#include <string>

#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h"
#include "cobalt/h5vcc/h5vcc_metric_type.h"
#include "cobalt/h5vcc/metric_event_handler_wrapper.h"

namespace cobalt {
namespace browser {
namespace metrics {

// An implementation of CobaltMetricsUploaderCallback to support binding an
// H5vcc (JS) callback to be called with a metric payload.
class CobaltH5vccMetricsUploaderCallback
: public CobaltMetricsUploaderCallback {
public:
CobaltH5vccMetricsUploaderCallback(
h5vcc::MetricEventHandlerWrapper* event_handler)
: event_handler_(event_handler) {}

// Runs the h5vcc event_handler callback for the given type and payload.
void Run(const cobalt::h5vcc::H5vccMetricType& type,
const std::string& payload) override;

private:
h5vcc::MetricEventHandlerWrapper* event_handler_;
};
} // namespace metrics
} // namespace browser
} // namespace cobalt

#endif // COBALT_BROWSER_METRICS_COBALT_H5VCC_METRICS_UPLOADER_CALLBACK_H_
60 changes: 60 additions & 0 deletions cobalt/browser/metrics/cobalt_metrics_log_uploader.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 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/browser/metrics/cobalt_metrics_log_uploader.h"

#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h"
#include "cobalt/h5vcc/h5vcc_metric_type.h"
#include "components/metrics/log_decoder.h"
#include "components/metrics/metrics_log_uploader.h"
#include "third_party/metrics_proto/reporting_info.pb.h"


namespace cobalt {
namespace browser {
namespace metrics {

CobaltMetricsLogUploader::CobaltMetricsLogUploader(
::metrics::MetricsLogUploader::MetricServiceType service_type,
const ::metrics::MetricsLogUploader::UploadCallback& on_upload_complete)
: service_type_(service_type), on_upload_complete_(on_upload_complete) {}

void CobaltMetricsLogUploader::UploadLog(
const std::string& compressed_log_data, const std::string& log_hash,
const ::metrics::ReportingInfo& reporting_info) {
if (service_type_ == ::metrics::MetricsLogUploader::UMA) {
std::string uncompressed_serialized_proto;
::metrics::DecodeLogData(compressed_log_data,
&uncompressed_serialized_proto);
if (upload_handler_ != nullptr) {
upload_handler_->Run(h5vcc::H5vccMetricType::kH5vccMetricTypeUma,
uncompressed_serialized_proto);
}
}

// Arguments to callback don't matter much here as we're not really doing
// anything but forwarding to the H5vcc API. Run(http response code, net
// code, and was https).
on_upload_complete_.Run(/*status*/ 200, /* error_code */ 0,
/*was_https*/ true);
}

void CobaltMetricsLogUploader::SetOnUploadHandler(
CobaltMetricsUploaderCallback* upload_handler) {
upload_handler_ = upload_handler;
}

} // namespace metrics
} // namespace browser
} // namespace cobalt
67 changes: 67 additions & 0 deletions cobalt/browser/metrics/cobalt_metrics_log_uploader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 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_BROWSER_METRICS_COBALT_METRICS_LOG_UPLOADER_H_
#define COBALT_BROWSER_METRICS_COBALT_METRICS_LOG_UPLOADER_H_

#include <string>

#include "base/callback.h"
#include "base/macros.h"
#include "base/strings/string_piece.h"
#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h"
#include "cobalt/h5vcc/metric_event_handler_wrapper.h"
#include "components/metrics/metrics_log_uploader.h"
#include "third_party/metrics_proto/reporting_info.pb.h"


namespace cobalt {
namespace browser {
namespace metrics {

class ReportingInfo;

// A Cobalt implementation of MetricsLogUploader that intercepts metric's logs
// when they're ready to be sent to the server and forwards them to the web
// client via the H5vcc API.
class CobaltMetricsLogUploader : public ::metrics::MetricsLogUploader {
public:
CobaltMetricsLogUploader(
::metrics::MetricsLogUploader::MetricServiceType service_type,
const ::metrics::MetricsLogUploader::UploadCallback& on_upload_complete);

virtual ~CobaltMetricsLogUploader() {}

// Uploads a log with the specified |compressed_log_data| and |log_hash|.
// |log_hash| is expected to be the hex-encoded SHA1 hash of the log data
// before compression.
void UploadLog(const std::string& compressed_log_data,
const std::string& log_hash,
const ::metrics::ReportingInfo& reporting_info);

// Sets the event handler wrapper to be called when metrics are ready for
// upload. This should be the JavaScript H5vcc callback implementation.
void SetOnUploadHandler(CobaltMetricsUploaderCallback* metric_event_handler);

private:
const ::metrics::MetricsLogUploader::MetricServiceType service_type_;
const ::metrics::MetricsLogUploader::UploadCallback on_upload_complete_;
CobaltMetricsUploaderCallback* upload_handler_ = nullptr;
};

} // namespace metrics
} // namespace browser
} // namespace cobalt

#endif // COBALT_BROWSER_METRICS_COBALT_METRICS_LOG_UPLOADER_H_
Loading

0 comments on commit 73f2621

Please sign in to comment.