summaryrefslogtreecommitdiff
path: root/chromium/components/metrics/structured/event_base.h
blob: d28c0052ce60f134afc125160fa537d0d08f972b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2019 The Chromium 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 COMPONENTS_METRICS_STRUCTURED_EVENT_BASE_H_
#define COMPONENTS_METRICS_STRUCTURED_EVENT_BASE_H_

#include <string>
#include <utility>
#include <vector>

namespace metrics {
namespace structured {

// A base class for generated structured metrics event objects. This class
// should not be used directly.
class EventBase {
 public:
  EventBase(const EventBase& other);
  virtual ~EventBase();

  // Specifies which value type a Metric object holds.
  enum class MetricType {
    kString = 0,
    kInt = 1,
  };

  // Stores all information about a single metric: name hash, value, and a
  // specifier of the value type.
  struct Metric {
    Metric(uint64_t name_hash, MetricType type);
    ~Metric();

    // First 8 bytes of the MD5 hash of the metric name, as defined in
    // structured.xml. This is calculated by
    // tools/metrics/structured/codegen.py.
    uint64_t name_hash;
    MetricType type;

    // TODO(crbug.com/10116655): Replace this with a base::Value.
    // All possible value types a metric can take. Exactly one of these should
    // be set. If |string_value| is set (with |type| as MetricType::STRING),
    // only the HMAC digest will be reported, so it is safe to put any value
    // here.
    std::string string_value;
    int int_value;
  };

  // Finalizes the event and sends it for recording. After this call, the event
  // is left in an invalid state and should not be used further.
  void Record();

  std::vector<Metric> metrics() const { return metrics_; }

  uint64_t name_hash() const { return event_name_hash_; }

  uint64_t project_name_hash() const { return project_name_hash_; }

 protected:
  explicit EventBase(uint64_t event_name_hash, uint64_t project_name_hash);

  void AddStringMetric(uint64_t name_hash, const std::string& value);

  void AddIntMetric(uint64_t name_hash, int value);

 private:
  // First 8 bytes of the MD5 hash of the event name, as defined in
  // structured.xml. This is calculated by tools/metrics/structured/codegen.py.
  uint64_t event_name_hash_;

  // The project name hash is used to to determine which key to use for hashing
  // events. The project name comes from this event's definition in
  // structured.xml, and is decided by the rules:
  //
  //  - if this event references a project, eg. <event name="..."
  //    project="...">, use that project's name.
  //
  //  - otherwise, use the event's name.
  //
  // |project_name_hash_| is the first 8 bytes of the MD5 hash of the project
  // name.
  uint64_t project_name_hash_;

  std::vector<Metric> metrics_;
};

}  // namespace structured
}  // namespace metrics

#endif  // COMPONENTS_METRICS_STRUCTURED_EVENT_BASE_H_