summaryrefslogtreecommitdiffstats
path: root/ceee/ie/common/metrics_util.h
blob: c38fba50f9795a61f35235dcd61bb636fca861de (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
// Copyright (c) 2010 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.
//
// CEEE module-wide metrics utilities.

#ifndef CEEE_IE_COMMON_METRICS_UTIL_H__
#define CEEE_IE_COMMON_METRICS_UTIL_H__

#include <atlsafe.h>
#include <string>

#include "base/logging.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "base/win/scoped_bstr.h"
#include "base/win/windows_version.h"
#include "ceee/ie/broker/broker_rpc_client.h"

namespace metrics_util {

// This is a class that measures the time taken from its construction to its
// destruction. A good way to use it is to instantiate it on the stack.
// TODO(hansl): Make sure the BrokerRpcClient calls from this class are non-
//              blocking.
class ScopedTimer {
 public:
  // Callers must ensure broker_rpc exceeds the life of this object.
  ScopedTimer(const std::string name, BrokerRpcClient* broker_rpc)
      : name_(name), broker_rpc_(broker_rpc), start_(base::TimeTicks::Now()) {
    if (name.length() == 0) {
      NOTREACHED() << "Histogram name shouldn't be empty.";
      broker_rpc_ = NULL;  // Ensure we don't call the broker_rpc.
    }
  }

  ~ScopedTimer() {
    if (broker_rpc_) {
      base::TimeDelta delta = base::TimeTicks::Now() - start_;
      if (FAILED(broker_rpc_->SendUmaHistogramTimes(
              name_.c_str(), static_cast<int>(delta.InMilliseconds())))) {
        NOTREACHED() << "An error happened during RPC.";
      }
    }
  }

  void Drop() {
    // Something happened and we should not log.
    broker_rpc_ = NULL;
  }

 protected:
  base::TimeTicks start_;

  // The RPC Client to use when sending events.
  BrokerRpcClient* broker_rpc_;

  // The name of the event when sending it.
  std::string name_;
};

}

#endif  // CEEE_IE_COMMON_METRICS_UTIL_H__