Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 788d3b63 authored by Alex Vakulenko's avatar Alex Vakulenko Committed by chrome-internal-fetch
Browse files

metrics: fork metrics sources from Chromium source base

In preparation to libchrome uprev, fork off non-protobuf code
from Chromium's metrics component into platform2/metrics.

The current version of Chromium's metrics component has been
significantly refactored and pieces of functionality that
Chromium OS was dependant on (e.g. MetricsLogBase class) has
been removed completely.

So, taking the r293518 version we have been using for a while
and putting it as part of platform2/metrics now.

BUG=None
TEST=FEATURES=test emerge-link metrics

Change-Id: Ib46ac1dff2e2b9cc881e4787b3c6b6250f0bf9c4
Reviewed-on: https://chromium-review.googlesource.com/234635


Tested-by: default avatarAlex Vakulenko <avakulenko@chromium.org>
Reviewed-by: default avatarBertrand Simonnet <bsimonnet@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
parent 7845a571
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -23,9 +23,9 @@
      'sources': [
        'c_metrics_library.cc',
        'metrics_library.cc',
        'serialization/metric_sample.cc',
        'serialization/serialization_utils.cc',
        'timer.cc',
        'components/metrics/chromeos/metric_sample.cc',
        'components/metrics/chromeos/serialization_utils.cc',
      ],
      'include_dirs': ['.'],
    },
+5 −3
Original line number Diff line number Diff line
@@ -75,12 +75,11 @@
      },
      'sources': [
        'uploader/upload_service.cc',
        'uploader/metrics_hashes.cc',
        'uploader/metrics_log.cc',
        'uploader/metrics_log_base.cc',
        'uploader/system_profile_cache.cc',
        'uploader/sender_http.cc',
        'components/metrics/metrics_log_base.cc',
        'components/metrics/metrics_log_manager.cc',
        'components/metrics/metrics_hashes.cc',
      ],
      'include_dirs': ['.']
    },
@@ -136,6 +135,7 @@
          'includes': ['../common-mk/common_test.gypi'],
          'sources': [
            'metrics_library_test.cc',
            'serialization/serialization_utils_unittest.cc',
          ],
          'link_settings': {
            'libraries': [
@@ -157,6 +157,8 @@
          'type': 'executable',
          'sources': [
            'persistent_integer.cc',
            'uploader/metrics_hashes_unittest.cc',
            'uploader/metrics_log_base_unittest.cc',
            'uploader/mock/sender_mock.cc',
            'uploader/upload_service_test.cc',
          ],
+3 −5
Original line number Diff line number Diff line
@@ -13,13 +13,11 @@
#include <cstdio>
#include <cstring>

#include "components/metrics/chromeos/metric_sample.h"
#include "components/metrics/chromeos/serialization_utils.h"
#include "metrics/serialization/metric_sample.h"
#include "metrics/serialization/serialization_utils.h"

#include "policy/device_policy.h"

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
static const char kUMAEventsPath[] = "/var/run/metrics/uma-events";
static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
@@ -206,7 +204,7 @@ void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
}

bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
  for (size_t i = 0; i < ARRAY_SIZE(kCrosEventNames); i++) {
  for (size_t i = 0; i < arraysize(kCrosEventNames); i++) {
    if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
      return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
    }
+197 −0
Original line number Diff line number Diff line
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "metrics/serialization/metric_sample.h"

#include <string>
#include <vector>

#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"

namespace metrics {

MetricSample::MetricSample(MetricSample::SampleType sample_type,
                           const std::string& metric_name,
                           int sample,
                           int min,
                           int max,
                           int bucket_count)
    : type_(sample_type),
      name_(metric_name),
      sample_(sample),
      min_(min),
      max_(max),
      bucket_count_(bucket_count) {
}

MetricSample::~MetricSample() {
}

bool MetricSample::IsValid() const {
  return name().find(' ') == std::string::npos &&
         name().find('\0') == std::string::npos && !name().empty();
}

std::string MetricSample::ToString() const {
  if (type_ == CRASH) {
    return base::StringPrintf("crash%c%s%c",
                              '\0',
                              name().c_str(),
                              '\0');
  } else if (type_ == SPARSE_HISTOGRAM) {
    return base::StringPrintf("sparsehistogram%c%s %d%c",
                              '\0',
                              name().c_str(),
                              sample_,
                              '\0');
  } else if (type_ == LINEAR_HISTOGRAM) {
    return base::StringPrintf("linearhistogram%c%s %d %d%c",
                              '\0',
                              name().c_str(),
                              sample_,
                              max_,
                              '\0');
  } else if (type_ == HISTOGRAM) {
    return base::StringPrintf("histogram%c%s %d %d %d %d%c",
                              '\0',
                              name().c_str(),
                              sample_,
                              min_,
                              max_,
                              bucket_count_,
                              '\0');
  } else {
    // The type can only be USER_ACTION.
    CHECK_EQ(type_, USER_ACTION);
    return base::StringPrintf("useraction%c%s%c",
                              '\0',
                              name().c_str(),
                              '\0');
  }
}

int MetricSample::sample() const {
  CHECK_NE(type_, USER_ACTION);
  CHECK_NE(type_, CRASH);
  return sample_;
}

int MetricSample::min() const {
  CHECK_EQ(type_, HISTOGRAM);
  return min_;
}

int MetricSample::max() const {
  CHECK_NE(type_, CRASH);
  CHECK_NE(type_, USER_ACTION);
  CHECK_NE(type_, SPARSE_HISTOGRAM);
  return max_;
}

int MetricSample::bucket_count() const {
  CHECK_EQ(type_, HISTOGRAM);
  return bucket_count_;
}

// static
scoped_ptr<MetricSample> MetricSample::CrashSample(
    const std::string& crash_name) {
  return scoped_ptr<MetricSample>(
      new MetricSample(CRASH, crash_name, 0, 0, 0, 0));
}

// static
scoped_ptr<MetricSample> MetricSample::HistogramSample(
    const std::string& histogram_name,
    int sample,
    int min,
    int max,
    int bucket_count) {
  return scoped_ptr<MetricSample>(new MetricSample(
      HISTOGRAM, histogram_name, sample, min, max, bucket_count));
}

// static
scoped_ptr<MetricSample> MetricSample::ParseHistogram(
    const std::string& serialized_histogram) {
  std::vector<std::string> parts;
  base::SplitString(serialized_histogram, ' ', &parts);

  if (parts.size() != 5)
    return scoped_ptr<MetricSample>();
  int sample, min, max, bucket_count;
  if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
      !base::StringToInt(parts[2], &min) ||
      !base::StringToInt(parts[3], &max) ||
      !base::StringToInt(parts[4], &bucket_count)) {
    return scoped_ptr<MetricSample>();
  }

  return HistogramSample(parts[0], sample, min, max, bucket_count);
}

// static
scoped_ptr<MetricSample> MetricSample::SparseHistogramSample(
    const std::string& histogram_name,
    int sample) {
  return scoped_ptr<MetricSample>(
      new MetricSample(SPARSE_HISTOGRAM, histogram_name, sample, 0, 0, 0));
}

// static
scoped_ptr<MetricSample> MetricSample::ParseSparseHistogram(
    const std::string& serialized_histogram) {
  std::vector<std::string> parts;
  base::SplitString(serialized_histogram, ' ', &parts);
  if (parts.size() != 2)
    return scoped_ptr<MetricSample>();
  int sample;
  if (parts[0].empty() || !base::StringToInt(parts[1], &sample))
    return scoped_ptr<MetricSample>();

  return SparseHistogramSample(parts[0], sample);
}

// static
scoped_ptr<MetricSample> MetricSample::LinearHistogramSample(
    const std::string& histogram_name,
    int sample,
    int max) {
  return scoped_ptr<MetricSample>(
      new MetricSample(LINEAR_HISTOGRAM, histogram_name, sample, 0, max, 0));
}

// static
scoped_ptr<MetricSample> MetricSample::ParseLinearHistogram(
    const std::string& serialized_histogram) {
  std::vector<std::string> parts;
  int sample, max;
  base::SplitString(serialized_histogram, ' ', &parts);
  if (parts.size() != 3)
    return scoped_ptr<MetricSample>();
  if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
      !base::StringToInt(parts[2], &max)) {
    return scoped_ptr<MetricSample>();
  }

  return LinearHistogramSample(parts[0], sample, max);
}

// static
scoped_ptr<MetricSample> MetricSample::UserActionSample(
    const std::string& action_name) {
  return scoped_ptr<MetricSample>(
      new MetricSample(USER_ACTION, action_name, 0, 0, 0, 0));
}

bool MetricSample::IsEqual(const MetricSample& metric) {
  return type_ == metric.type_ && name_ == metric.name_ &&
         sample_ == metric.sample_ && min_ == metric.min_ &&
         max_ == metric.max_ && bucket_count_ == metric.bucket_count_;
}

}  // namespace metrics
+119 −0
Original line number Diff line number Diff line
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef METRICS_SERIALIZATION_METRIC_SAMPLE_H_
#define METRICS_SERIALIZATION_METRIC_SAMPLE_H_

#include <string>

#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"

namespace metrics {

// This class is used by libmetrics (ChromeOS) to serialize
// and deserialize measurements to send them to a metrics sending service.
// It is meant to be a simple container with serialization functions.
class MetricSample {
 public:
  // Types of metric sample used.
  enum SampleType {
    CRASH,
    HISTOGRAM,
    LINEAR_HISTOGRAM,
    SPARSE_HISTOGRAM,
    USER_ACTION
  };

  ~MetricSample();

  // Returns true if the sample is valid (can be serialized without ambiguity).
  //
  // This function should be used to filter bad samples before serializing them.
  bool IsValid() const;

  // Getters for type and name. All types of metrics have these so we do not
  // need to check the type.
  SampleType type() const { return type_; }
  const std::string& name() const { return name_; }

  // Getters for sample, min, max, bucket_count.
  // Check the metric type to make sure the request make sense. (ex: a crash
  // sample does not have a bucket_count so we crash if we call bucket_count()
  // on it.)
  int sample() const;
  int min() const;
  int max() const;
  int bucket_count() const;

  // Returns a serialized version of the sample.
  //
  // The serialized message for each type is:
  // crash: crash\0|name_|\0
  // user action: useraction\0|name_|\0
  // histogram: histogram\0|name_| |sample_| |min_| |max_| |bucket_count_|\0
  // sparsehistogram: sparsehistogram\0|name_| |sample_|\0
  // linearhistogram: linearhistogram\0|name_| |sample_| |max_|\0
  std::string ToString() const;

  // Builds a crash sample.
  static scoped_ptr<MetricSample> CrashSample(const std::string& crash_name);

  // Builds a histogram sample.
  static scoped_ptr<MetricSample> HistogramSample(
      const std::string& histogram_name,
      int sample,
      int min,
      int max,
      int bucket_count);
  // Deserializes a histogram sample.
  static scoped_ptr<MetricSample> ParseHistogram(const std::string& serialized);

  // Builds a sparse histogram sample.
  static scoped_ptr<MetricSample> SparseHistogramSample(
      const std::string& histogram_name,
      int sample);
  // Deserializes a sparse histogram sample.
  static scoped_ptr<MetricSample> ParseSparseHistogram(
      const std::string& serialized);

  // Builds a linear histogram sample.
  static scoped_ptr<MetricSample> LinearHistogramSample(
      const std::string& histogram_name,
      int sample,
      int max);
  // Deserializes a linear histogram sample.
  static scoped_ptr<MetricSample> ParseLinearHistogram(
      const std::string& serialized);

  // Builds a user action sample.
  static scoped_ptr<MetricSample> UserActionSample(
      const std::string& action_name);

  // Returns true if sample and this object represent the same sample (type,
  // name, sample, min, max, bucket_count match).
  bool IsEqual(const MetricSample& sample);

 private:
  MetricSample(SampleType sample_type,
               const std::string& metric_name,
               const int sample,
               const int min,
               const int max,
               const int bucket_count);

  const SampleType type_;
  const std::string name_;
  const int sample_;
  const int min_;
  const int max_;
  const int bucket_count_;

  DISALLOW_COPY_AND_ASSIGN(MetricSample);
};

}  // namespace metrics

#endif  // METRICS_SERIALIZATION_METRIC_SAMPLE_H_
Loading