Source release v3.5.0
This commit is contained in:
205
metrics/include/counter_metric.h
Normal file
205
metrics/include/counter_metric.h
Normal file
@@ -0,0 +1,205 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
#define WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class CounterMetricTest;
|
||||
|
||||
// This base class provides the common defintion used by all templated
|
||||
// instances of CounterMetric.
|
||||
class BaseCounterMetric : public MetricSerializable {
|
||||
public:
|
||||
// Send metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer);
|
||||
|
||||
protected:
|
||||
// Instantiates a BaseCounterMetric.
|
||||
BaseCounterMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name) {}
|
||||
|
||||
virtual ~BaseCounterMetric() {}
|
||||
|
||||
// Increment will look for an existing instance of the field names and
|
||||
// add the new value to the existing value. If the instance does not exist,
|
||||
// this method will create it.
|
||||
void Increment(const std::string& field_names_values, int64_t value);
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
const std::string metric_name_;
|
||||
// value_map_ contains a mapping from the field name/value pairs to the
|
||||
// counter(int64_t) instance.
|
||||
std::map<std::string, int64_t> value_map_;
|
||||
|
||||
/*
|
||||
* This locks the internal state of the counter metric to ensure safety across
|
||||
* multiple threads preventing the caller from worrying about locking.
|
||||
*/
|
||||
Lock internal_lock_;
|
||||
};
|
||||
|
||||
// The CounterMetric class is used to track a counter such as the number of
|
||||
// times a method is called. It can also track a delta that could be positive
|
||||
// or negative.
|
||||
//
|
||||
// The CounterMetric class supports the ability to keep track of multiple
|
||||
// variations of the count based on certain "field" values. E.g. keep track of
|
||||
// the counts of success and failure counts for a method call. Or keep track of
|
||||
// number of times the method was called with a particular parameter.
|
||||
// Fields define what variations to track. Each Field is a separate dimension.
|
||||
// The count for each combination of field values is tracked independently.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// CounterMetric<int, int> my_metric("multiple/fields/metric",
|
||||
// "request_type", // Field name.
|
||||
// "error_code"); // Field name.
|
||||
//
|
||||
// my_metric.Increment(1, 7, 23); // (counter value, request type, error code).
|
||||
//
|
||||
// The CounterMetric supports serialization. A call to Serialize will serialize
|
||||
// all values to the provided MetricsSerializer instance.
|
||||
//
|
||||
// example:
|
||||
//
|
||||
// class MyMetricSerializer : public MetricSerializer {
|
||||
// // Add implementation here.
|
||||
// }
|
||||
//
|
||||
// MyMetricSerializer serializer;
|
||||
// my_metric.Serialize(&serializer);
|
||||
template<typename F1=util::Unused,
|
||||
typename F2=util::Unused,
|
||||
typename F3=util::Unused,
|
||||
typename F4=util::Unused>
|
||||
class CounterMetric : public BaseCounterMetric {
|
||||
public:
|
||||
// Create a CounterMetric instance with the name |metric_name|.
|
||||
CounterMetric(const std::string& metric_name);
|
||||
|
||||
// Overloaded constructors with variable field name arguments. The number
|
||||
// of |field_name| arguments must match the number of used Field type
|
||||
// arguments.
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4);
|
||||
|
||||
// Increment will update the counter value associated with the provided
|
||||
// field values.
|
||||
void Increment(F1 field1 = util::Unused(), F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(), F4 field4 = util::Unused());
|
||||
|
||||
// Increment will add the value to the counter associated with the provided
|
||||
// field values.
|
||||
void Increment(int64_t value,
|
||||
F1 field1 = util::Unused(), F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(), F4 field4 = util::Unused());
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
};
|
||||
|
||||
// Overloaded template constructor implementations for CounterMetric.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(2);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(3);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(4);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2, field_name3);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(5);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2,
|
||||
field_name3, field_name4);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void CounterMetric<F1, F2, F3, F4>::Increment(
|
||||
F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseCounterMetric::Increment(field_name_values, 1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void CounterMetric<F1, F2, F3, F4>::Increment(
|
||||
int64_t value, F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseCounterMetric::Increment(field_name_values, value);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
53
metrics/include/distribution.h
Normal file
53
metrics/include/distribution.h
Normal file
@@ -0,0 +1,53 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the definition of a Distribution class which computes
|
||||
// the distribution values of a series of samples.
|
||||
|
||||
#ifndef WVCDM_METRICS_DISTRIBUTION_H_
|
||||
#define WVCDM_METRICS_DISTRIBUTION_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// The Distribution class holds statistics about a series of values that the
|
||||
// client provides via the Record method. A caller will call Record once for
|
||||
// each of the values in a series. The Distribution instance will calculate the
|
||||
// mean, count, min, max and variance of the distribution.
|
||||
//
|
||||
// Example usage:
|
||||
// Distribution dist;
|
||||
// dist.Record(1);
|
||||
// dist.Record(2);
|
||||
// dist.Mean(); // Returns 1.5.
|
||||
// dist.Count(); // Returns 2.
|
||||
class Distribution {
|
||||
public:
|
||||
Distribution();
|
||||
|
||||
// Uses the provided sample value to update the computed statistics.
|
||||
void Record(double value);
|
||||
|
||||
// Return the value for each of the stats computed about the series of
|
||||
// values (min, max, count, etc.).
|
||||
double Min() const { return min_; }
|
||||
double Max() const { return max_; }
|
||||
double Mean() const { return mean_; }
|
||||
int64_t Count() const { return count_; }
|
||||
double Variance() const {
|
||||
return count_ == 0 ? 0.0 : sum_squared_deviation_ / count_;
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t count_;
|
||||
double min_;
|
||||
double max_;
|
||||
double mean_;
|
||||
double sum_squared_deviation_;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_DISTRIBUTION_H_
|
||||
232
metrics/include/event_metric.h
Normal file
232
metrics/include/event_metric.h
Normal file
@@ -0,0 +1,232 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the EventMetric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_EVENT_METRIC_H_
|
||||
#define WVCDM_METRICS_EVENT_METRIC_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "distribution.h"
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class EventMetricTest;
|
||||
|
||||
// This base class provides the common defintion used by all templated
|
||||
// instances of EventMetric.
|
||||
class BaseEventMetric : public MetricSerializable {
|
||||
public:
|
||||
// Send metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer);
|
||||
|
||||
protected:
|
||||
// Instantiates a BaseEventMetric.
|
||||
BaseEventMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name) {}
|
||||
|
||||
virtual ~BaseEventMetric();
|
||||
|
||||
// Record will look for an existing instance of the Distribution identified
|
||||
// by the field_names_values string and update it. If the instance does
|
||||
// not exist, this will create it.
|
||||
void Record(const std::string& field_names_values, double value);
|
||||
|
||||
private:
|
||||
friend class EventMetricTest;
|
||||
const std::string metric_name_;
|
||||
// value_map_ contains a mapping from the field name/value pairs to the
|
||||
// distribution instance which holds the metric information (min, max, sum,
|
||||
// etc.).
|
||||
std::map<std::string, Distribution*> value_map_;
|
||||
|
||||
/*
|
||||
* This locks the internal state of the event metric to ensure safety across
|
||||
* multiple threads preventing the caller from worrying about locking.
|
||||
*/
|
||||
Lock internal_lock_;
|
||||
};
|
||||
|
||||
// This class converts the size_t value into the highest power of two
|
||||
// below the value. E.g. for 7, the value is 4. For 11, the value is 8.
|
||||
// This class is intended to simplify the use of EventMetric Fields that may
|
||||
// have many possible values, but we want to bucket them into a small set of
|
||||
// numbers (32 or 64).
|
||||
class Pow2Bucket {
|
||||
public:
|
||||
explicit Pow2Bucket(size_t value) : value_(GetLowerBucket(value)) {}
|
||||
|
||||
Pow2Bucket(const Pow2Bucket& value) : value_(value.value_) {}
|
||||
|
||||
// Support for converting to string.
|
||||
friend std::ostream& operator<<(std::ostream& os, const Pow2Bucket& log)
|
||||
{ return os << log.value_; }
|
||||
|
||||
private:
|
||||
inline size_t GetLowerBucket(size_t value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t log = 0;
|
||||
while (value) {
|
||||
log++;
|
||||
value >>= 1;
|
||||
}
|
||||
|
||||
return 1u << (log - 1);
|
||||
}
|
||||
|
||||
size_t value_;
|
||||
};
|
||||
|
||||
// The EventMetric class is used to capture statistics about an event such as
|
||||
// a method call. EventMetric keeps track of the count, mean, min, max and
|
||||
// variance for the value being measured. For example, we may want to track the
|
||||
// latency of a particular operation. Each time the operation is run, the time
|
||||
// is calculated and provided to the EventMetric by using the
|
||||
// EventMetric::Record method to capture and maintain statistics about the
|
||||
// latency (max, min, count, mean, variance).
|
||||
//
|
||||
// The EventMetric class supports the ability to breakdown the statistics based
|
||||
// on certain "field" values. For example, if a particular operation can run in
|
||||
// one of two modes, it's useful to track the latency of the operation in each
|
||||
// mode separately. You can use Fields to define how to breakdown the
|
||||
// statistics. Each Field is a separate dimension. The statistics for each
|
||||
// combination of field values are tracked independently.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// EventMetric<int, int> my_metric("multiple/fields/metric",
|
||||
// "request_type", "error_code", // Field names);
|
||||
//
|
||||
// my_metric.Record(1, 7, 23); // (latency value, request type, error code).
|
||||
//
|
||||
// The EventMetric supports serialization. A call to Serialize will
|
||||
// serialize all values to the provided MetricsSerializer instance.
|
||||
//
|
||||
// example:
|
||||
//
|
||||
// class MyMetricSerializer : public MetricSerializer {
|
||||
// // Add implementation here.
|
||||
// }
|
||||
//
|
||||
// MyMetricSerializer serializer;
|
||||
// my_metric.Serialize(&serializer);
|
||||
//
|
||||
template<typename F1=util::Unused,
|
||||
typename F2=util::Unused,
|
||||
typename F3=util::Unused,
|
||||
typename F4=util::Unused>
|
||||
class EventMetric : public BaseEventMetric {
|
||||
public:
|
||||
// Create an EventMetric instance with the name |metric_name|.
|
||||
EventMetric(const std::string& metric_name);
|
||||
|
||||
// Overloaded constructors with variable field name arguments. The number
|
||||
// of |field_name| arguments must match the number of used Field type
|
||||
// arguments.
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name);
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2);
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3);
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4);
|
||||
|
||||
// Record will update the statistics of the EventMetric broken down by the
|
||||
// given field values.
|
||||
void Record(double value,
|
||||
F1 field1 = util::Unused(),
|
||||
F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(),
|
||||
F4 field4 = util::Unused());
|
||||
|
||||
private:
|
||||
friend class EventMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
};
|
||||
|
||||
// Overloaded template constructor implementations for EventMetric.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(2);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(3);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(4);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2, field_name3);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(5);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2,
|
||||
field_name3, field_name4);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void EventMetric<F1, F2, F3, F4>::Record(
|
||||
double value, F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseEventMetric::Record(field_name_values, value);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_EVENT_METRIC_H_
|
||||
128
metrics/include/field_tuples.h
Normal file
128
metrics/include/field_tuples.h
Normal file
@@ -0,0 +1,128 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the helper classes and methods for using field tuples
|
||||
// used by metrics classes to record variations of a single metric.
|
||||
#ifndef WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
#define WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
namespace util {
|
||||
|
||||
// TODO(blueeyes): Change to use C++ 11 support for variadic template args.
|
||||
// The C++ 03 pattern is no longer needed since we require C++11. b/68766426.
|
||||
|
||||
// This is a placeholder type for unused type parameters. It aids in supporting
|
||||
// templated classes with "variable" type arguments.
|
||||
struct Unused {
|
||||
// Required for compilation. Should never be used.
|
||||
inline friend std::ostream& operator<< (std::ostream& out, const Unused&)
|
||||
{ return out; }
|
||||
};
|
||||
|
||||
// This utility method formats the collection of field name/value pairs.
|
||||
// The format of the string is:
|
||||
//
|
||||
// [{field:value[&field:value]*}]
|
||||
//
|
||||
// If there are no pairs, returns a blank string.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
std::string MakeFieldNameString(const std::vector<std::string>& field_names,
|
||||
const F1 field1, const F2 field2,
|
||||
const F3 field3, const F4 field4) {
|
||||
std::stringstream field_name_and_values;
|
||||
std::vector<std::string>::const_iterator field_name_iterator =
|
||||
field_names.begin();
|
||||
if (field_name_iterator == field_names.end()) {
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
// There is at least one name/value pair. Prepend open brace.
|
||||
field_name_and_values << "{";
|
||||
field_name_and_values << *field_name_iterator << ':' << field1;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field2;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field3;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field4;
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
|
||||
// This specialization of the helper method is a shortcut for class
|
||||
// instances with no fields.
|
||||
template<>
|
||||
inline std::string MakeFieldNameString<Unused, Unused, Unused, Unused>(
|
||||
const std::vector<std::string>& /* field_names */,
|
||||
const Unused /* unused1 */, const Unused /* unused2 */,
|
||||
const Unused /* unused3 */, const Unused /* unused4 */) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// This helper function appends the field names to a vector of strings.
|
||||
inline void AppendFieldNames(std::vector<std::string>* field_name_vector,
|
||||
int field_count, ...) {
|
||||
va_list field_names;
|
||||
|
||||
va_start(field_names, field_count);
|
||||
for (int x = 0; x < field_count; x++) {
|
||||
field_name_vector->push_back(va_arg(field_names, const char*));
|
||||
}
|
||||
va_end(field_names);
|
||||
}
|
||||
|
||||
// These helper methods and FirstUnusedType assure that there is no mismatch
|
||||
// between the specified types for metrics type parameters and the constructors
|
||||
// and methods used for the specializations.
|
||||
template <bool>
|
||||
struct CompileAssert {};
|
||||
#define COMPILE_ASSERT(expr, msg) \
|
||||
typedef util::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
|
||||
|
||||
template <typename T> struct is_unused { static const bool value = false; };
|
||||
template <> struct is_unused<Unused> { static const bool value = true; };
|
||||
|
||||
template <typename F1, typename F2, typename F3, typename F4>
|
||||
class FirstUnusedType {
|
||||
static const bool a = is_unused<F1>::value;
|
||||
static const bool b = is_unused<F2>::value;
|
||||
static const bool c = is_unused<F3>::value;
|
||||
static const bool d = is_unused<F4>::value;
|
||||
// Check that all types after the first Unused are also Unused.
|
||||
COMPILE_ASSERT(a <= b, Invalid_Unused_At_Position_2);
|
||||
COMPILE_ASSERT(b <= c, Invalid_Unused_At_Position_3);
|
||||
COMPILE_ASSERT(c <= d, Invalid_Unused_At_Position_4);
|
||||
|
||||
public:
|
||||
static const int value = 5 - (a + b + c + d);
|
||||
};
|
||||
|
||||
// Asserts that no Unused types exist before N; after N, are all Unused types.
|
||||
#define ASSERT_METRIC_UNUSED_START_FROM(N) \
|
||||
COMPILE_ASSERT((\
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value) == N, \
|
||||
Unused_Start_From_##N)
|
||||
|
||||
} // namespace util
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
|
||||
46
metrics/include/metric_serialization.h
Normal file
46
metrics/include/metric_serialization.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_METRIC_SERIALIZATION_H_
|
||||
#define WVCDM_METRICS_METRIC_SERIALIZATION_H_
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// The MetricSerializer is implemented by the code that instantiates
|
||||
// MetricSerializable instances. The caller provides this MetricSerializer
|
||||
// instance to MetricSerializable::Serialize. In turn, Serialize will make a
|
||||
// call to Set* for each value to be Serialized. The MetricSerialiable
|
||||
// implementation may set zero or more values on the MetricSerializer.
|
||||
class MetricSerializer {
|
||||
public:
|
||||
virtual ~MetricSerializer() {};
|
||||
|
||||
// The metric_id is the metric name, plus the metric part name, and the
|
||||
// metric field name/value string. E.g.
|
||||
// name: "drm/cdm/decrypt/duration"
|
||||
// part: "mean"
|
||||
// field name/value string: "{error_code:0&buffer_size:1024}"
|
||||
// becomes: "drm/cdm/decrypt/duration/mean/{error_code:0&buffer_size:1024}".
|
||||
virtual void SetString(const std::string& metric_id,
|
||||
const std::string& value) = 0;
|
||||
virtual void SetInt32(const std::string& metric_id, int32_t value) = 0;
|
||||
virtual void SetInt64(const std::string& metric_id, int64_t value) = 0;
|
||||
virtual void SetDouble(const std::string& metric_id, double value) = 0;
|
||||
};
|
||||
|
||||
// This abstract class merely provides the definition for serializing the value
|
||||
// of the metric.
|
||||
class MetricSerializable {
|
||||
public:
|
||||
virtual ~MetricSerializable() { }
|
||||
// Serialize metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer) = 0;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_METRIC_SERIALIZATION_H_
|
||||
301
metrics/include/metrics_collections.h
Normal file
301
metrics/include/metrics_collections.h
Normal file
@@ -0,0 +1,301 @@
|
||||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains definitions for metrics being collected throughout the
|
||||
// CDM.
|
||||
|
||||
#ifndef WVCDM_METRICS_METRICS_GROUP_H_
|
||||
#define WVCDM_METRICS_METRICS_GROUP_H_
|
||||
|
||||
#include <ostream>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "counter_metric.h"
|
||||
#include "event_metric.h"
|
||||
#include "metrics.pb.h"
|
||||
#include "OEMCryptoCENC.h"
|
||||
#include "value_metric.h"
|
||||
#include "wv_cdm_types.h"
|
||||
|
||||
// This definition indicates that a given metric does not need timing
|
||||
// stats. Example:
|
||||
//
|
||||
// M_RECORD(my_metrics, my_metric_name, NO_TIME);
|
||||
#define NO_TIME 0
|
||||
|
||||
// Used to record metric timing and additional information about a specific
|
||||
// event. Assumes that a microsecond timing has been provided. Example:
|
||||
//
|
||||
// long total_time = 0;
|
||||
// long error_code = TimeSomeOperation(&total_time);
|
||||
// M_RECORD(my_metrics, my_metric_name, total_time, error_code);
|
||||
#define M_RECORD(GROUP, METRIC, TIME, ...) \
|
||||
if ( GROUP ) { \
|
||||
( GROUP ) -> METRIC . Record( TIME, ##__VA_ARGS__ ); \
|
||||
}
|
||||
|
||||
// This definition automatically times an operation and records the time and
|
||||
// additional information such as error code to the provided metric.
|
||||
// Example:
|
||||
//
|
||||
// OEMCryptoResult sts;
|
||||
// M_TIME(
|
||||
// sts = OEMCrypto_Initialize(),
|
||||
// my_metrics_collection,
|
||||
// oemcrypto_initialize_,
|
||||
// sts);
|
||||
#define M_TIME(CALL, GROUP, METRIC, ...) \
|
||||
if ( GROUP ) { \
|
||||
wvcdm::metrics::TimerMetric timer; \
|
||||
timer.Start(); \
|
||||
CALL; \
|
||||
( GROUP ) -> METRIC . Record(timer.AsUs(), ##__VA_ARGS__ ); \
|
||||
} else { \
|
||||
CALL; \
|
||||
}
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// This enum defines the conditions encountered during OEMCrypto Initialization
|
||||
// in oemcrypto_adapter_dynamic.
|
||||
typedef enum OEMCryptoInitializationMode {
|
||||
OEMCrypto_INITIALIZED_USING_IN_APP = 0,
|
||||
OEMCrypto_INITIALIZED_FORCING_L3 = 1,
|
||||
OEMCrypto_INITIALIZED_USING_L3_NO_L1_LIBRARY_PATH = 2,
|
||||
OEMCrypto_INITIALIZED_USING_L3_L1_OPEN_FAILED = 3,
|
||||
OEMCrypto_INITIALIZED_USING_L3_L1_LOAD_FAILED = 4,
|
||||
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INITIALIZE_L1 = 5,
|
||||
OEMCrypto_INITIALIZED_USING_L3_WRONG_L1_VERSION = 6,
|
||||
OEMCrypto_INITIALIZED_USING_L1_WITH_KEYBOX = 7,
|
||||
OEMCrypto_INITIALIZED_USING_L1_WITH_CERTIFICATE = 8,
|
||||
OEMCrypto_INITIALIZED_USING_L1_CERTIFICATE_MIX = 9,
|
||||
OEMCrypto_INITIALIZED_USING_L3_BAD_KEYBOX = 10,
|
||||
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_OPEN_FACTORY_KEYBOX = 11,
|
||||
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INSTALL_KEYBOX = 12,
|
||||
OEMCrypto_INITIALIZED_USING_L1_INSTALLED_KEYBOX = 13,
|
||||
OEMCrypto_INITIALIZED_USING_L3_INVALID_L1 = 14,
|
||||
OEMCrypto_INITIALIZED_USING_L1_WITH_PROVISIONING_3_0 = 15,
|
||||
OEMCrypto_INITIALIZED_L3_INITIALIZATION_FAILED = 16
|
||||
} OEMCryptoInitializationMode;
|
||||
|
||||
|
||||
// This class contains metrics for Crypto Session and OEM Crypto.
|
||||
class CryptoMetrics {
|
||||
public:
|
||||
CryptoMetrics();
|
||||
|
||||
void Serialize(drm_metrics::MetricsGroup* metrics);
|
||||
|
||||
/* CRYPTO SESSION */
|
||||
// TODO(blueeyes): Convert this to crypto_session_default_security_level_.
|
||||
ValueMetric<CdmSecurityLevel> crypto_session_security_level_;
|
||||
CounterMetric<CdmResponseType> crypto_session_delete_all_usage_reports_;
|
||||
CounterMetric<CdmResponseType> crypto_session_delete_multiple_usage_information_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmEncryptionAlgorithm> crypto_session_generic_decrypt_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmEncryptionAlgorithm> crypto_session_generic_encrypt_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmSigningAlgorithm> crypto_session_generic_sign_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmSigningAlgorithm> crypto_session_generic_verify_;
|
||||
CounterMetric<bool> crypto_session_get_device_unique_id_;
|
||||
CounterMetric<bool> crypto_session_get_token_;
|
||||
ValueMetric<double> crypto_session_life_span_;
|
||||
EventMetric<bool> crypto_session_load_certificate_private_key_;
|
||||
EventMetric<CdmResponseType, SecurityLevel> crypto_session_open_; // This is the requested security level.
|
||||
ValueMetric<uint32_t> crypto_session_system_id_;
|
||||
EventMetric<CdmResponseType> crypto_session_update_usage_information_;
|
||||
ValueMetric<bool> crypto_session_usage_information_support_;
|
||||
/* OEMCRYPTO */
|
||||
ValueMetric<uint32_t> oemcrypto_api_version_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_close_session_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_copy_buffer_;
|
||||
ValueMetric<OEMCrypto_HDCP_Capability> oemcrypto_current_hdcp_capability_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_deactivate_usage_entry_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_decrypt_cenc_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_delete_usage_entry_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_delete_usage_table_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_derive_keys_from_session_key_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_force_delete_usage_entry_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_generate_derived_keys_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_generate_nonce_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generate_rsa_signature_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generate_signature_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_decrypt_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_encrypt_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_sign_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_verify_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_get_device_id_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_get_key_data_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_get_oem_public_certificate_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_get_random_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_initialize_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_install_keybox_;
|
||||
ValueMetric<bool> oemcrypto_is_anti_rollback_hw_present_;
|
||||
ValueMetric<bool> oemcrypto_is_keybox_valid_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_load_device_rsa_key_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_load_keys_;
|
||||
ValueMetric<OEMCrypto_HDCP_Capability> oemcrypto_max_hdcp_capability_;
|
||||
ValueMetric<size_t> oemcrypto_max_number_of_sessions_;
|
||||
ValueMetric<size_t> oemcrypto_number_of_open_sessions_;
|
||||
ValueMetric<OEMCrypto_ProvisioningMethod> oemcrypto_provisioning_method_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_refresh_keys_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_report_usage_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_rewrap_device_rsa_key_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_rewrap_device_rsa_key_30_;
|
||||
ValueMetric<uint16_t> oemcrypto_security_patch_level_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_select_key_;
|
||||
ValueMetric<bool> oemcrypto_supports_usage_table_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_update_usage_table_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_wrap_keybox_;
|
||||
};
|
||||
|
||||
// This class contains session-scoped metrics. All properties and
|
||||
// statistics related to operations within a single session are
|
||||
// recorded here.
|
||||
class SessionMetrics {
|
||||
public:
|
||||
SessionMetrics();
|
||||
|
||||
// Sets the session id of the metrics group. This allows the session
|
||||
// id to be captured and reported as part of the collection of metrics.
|
||||
void SetSessionId(const CdmSessionId& session_id) {
|
||||
session_id_ = session_id; }
|
||||
|
||||
// Returns the session id or an empty session id if it has not been set.
|
||||
const CdmSessionId& GetSessionId() const { return session_id_; }
|
||||
|
||||
// Marks the metrics object as completed and ready for serialization.
|
||||
void SetCompleted() { completed_ = true; }
|
||||
|
||||
// Returns true if the object is completed. This is used to determine
|
||||
// when the stats are ready to be published.
|
||||
bool IsCompleted() const { return completed_; }
|
||||
|
||||
// Returns a pointer to the crypto metrics belonging to the engine instance.
|
||||
// This instance retains ownership of the object.
|
||||
CryptoMetrics* GetCryptoMetrics() { return &crypto_metrics_; }
|
||||
|
||||
// Metrics collected at the session level.
|
||||
ValueMetric<double> cdm_session_life_span_; // Milliseconds.
|
||||
EventMetric<CdmResponseType> cdm_session_renew_key_;
|
||||
CounterMetric<CdmResponseType> cdm_session_restore_offline_session_;
|
||||
CounterMetric<CdmResponseType> cdm_session_restore_usage_session_;
|
||||
|
||||
// Serialize the session metrics to the provided |metric_group|.
|
||||
// |metric_group| is owned by the caller and must not be null.
|
||||
void Serialize(drm_metrics::MetricsGroup* metric_group);
|
||||
|
||||
private:
|
||||
void SerializeSessionMetrics(drm_metrics::MetricsGroup* metric_group);
|
||||
CdmSessionId session_id_;
|
||||
bool completed_;
|
||||
CryptoMetrics crypto_metrics_;
|
||||
};
|
||||
|
||||
// This class contains metrics for the OEMCrypto Dynamic Adapter. They are
|
||||
// separated from other metrics because they need to be encapsulated in a
|
||||
// singleton object. This is because the dynamic adapter uses the OEMCrypto
|
||||
// function signatures and contract and cannot be extended to inject
|
||||
// dependencies.
|
||||
//
|
||||
// Operations for this metrics class are serialized since these particular
|
||||
// metrics may be accessed by a separate thread during intialize even as
|
||||
// the metric may be serialized.
|
||||
class OemCryptoDynamicAdapterMetrics {
|
||||
public:
|
||||
explicit OemCryptoDynamicAdapterMetrics();
|
||||
|
||||
// Set methods for OEMCrypto metrics.
|
||||
void SetInitializationMode(OEMCryptoInitializationMode mode);
|
||||
void SetL1ApiVersion(uint32_t version);
|
||||
void SetL1MinApiVersion(uint32_t version);
|
||||
|
||||
// Serialize the session metrics to the provided |metric_group|.
|
||||
// |metric_group| is owned by the caller and must not be null.
|
||||
void Serialize(drm_metrics::MetricsGroup* metric_group);
|
||||
|
||||
// Clears the existing metric values.
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
Lock adapter_lock_;
|
||||
ValueMetric<OEMCryptoInitializationMode> oemcrypto_initialization_mode_;
|
||||
ValueMetric<uint32_t> oemcrypto_l1_api_version_;
|
||||
ValueMetric<uint32_t> oemcrypto_l1_min_api_version_;
|
||||
};
|
||||
|
||||
// This will fetch the singleton instance for dynamic adapter metrics.
|
||||
// This method is safe only if we use C++ 11. In C++ 11, static function-local
|
||||
// initialization is guaranteed to be threadsafe. We return the reference to
|
||||
// avoid non-guaranteed destructor order problems. Effectively, the destructor
|
||||
// is never run for the created instance.
|
||||
OemCryptoDynamicAdapterMetrics& GetDynamicAdapterMetricsInstance();
|
||||
|
||||
// This class contains engine-scoped metrics. All properties and
|
||||
// statistics related to operations within the engine, but outside
|
||||
// the scope of a session are recorded here.
|
||||
class EngineMetrics {
|
||||
public:
|
||||
EngineMetrics();
|
||||
~EngineMetrics();
|
||||
|
||||
// Add a new SessionMetrics instance and return a pointer to the caller.
|
||||
// The new SessionMetrics instance is owned by this EngineMetrics instance
|
||||
// and will exist until RemoveSession is called or this object is deleted.
|
||||
SessionMetrics* AddSession();
|
||||
|
||||
// Removes the metrics object for the given session id. This should only
|
||||
// be called when the SessionMetrics instance is no longer in use.
|
||||
void RemoveSession(CdmSessionId session_id);
|
||||
|
||||
// Returns a pointer to the crypto metrics belonging to the engine instance.
|
||||
// The CryptoMetrics instance is still owned by this object and will exist
|
||||
// until this object is deleted.
|
||||
CryptoMetrics* GetCryptoMetrics() { return &crypto_metrics_; }
|
||||
|
||||
// Serialize engine and session metrics into a serialized MetricsGroup
|
||||
// instance and output that instance to the provided |metric_group|.
|
||||
// |metric_group| is owned by the caller and must NOT be null.
|
||||
// |completed_only| indicates that this call should only publish
|
||||
// SessionMetrics instances that are marked as completed.
|
||||
// |clear_sessions| indicates that this call should clear sessions metrics
|
||||
// for those sessions that were serialized. This allows atomic
|
||||
// serialization and closing of session-level metrics.
|
||||
void Serialize(drm_metrics::MetricsGroup* metric_group, bool completed_only,
|
||||
bool clear_serialized_sessions);
|
||||
|
||||
void SetAppPackageName(const std::string& app_package_name);
|
||||
|
||||
// Metrics recorded at the engine level.
|
||||
EventMetric<CdmResponseType> cdm_engine_add_key_;
|
||||
ValueMetric<std::string> cdm_engine_cdm_version_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_close_session_;
|
||||
ValueMetric<int64_t> cdm_engine_creation_time_millis_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket> cdm_engine_decrypt_;
|
||||
CounterMetric<bool> cdm_engine_find_session_for_key_;
|
||||
EventMetric<CdmResponseType> cdm_engine_generate_key_request_;
|
||||
EventMetric<CdmResponseType> cdm_engine_get_provisioning_request_;
|
||||
EventMetric<CdmResponseType> cdm_engine_get_usage_info_;
|
||||
EventMetric<CdmResponseType> cdm_engine_handle_provisioning_response_;
|
||||
ValueMetric<double> cdm_engine_life_span_; // Milliseconds
|
||||
CounterMetric<CdmResponseType> cdm_engine_open_key_set_session_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_open_session_;
|
||||
EventMetric<CdmResponseType> cdm_engine_query_key_status_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_release_all_usage_info_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_release_usage_info_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_remove_keys_;
|
||||
EventMetric<CdmResponseType> cdm_engine_restore_key_;
|
||||
CounterMetric<CdmResponseType, CdmSecurityLevel> cdm_engine_unprovision_;
|
||||
|
||||
private:
|
||||
Lock session_metrics_lock_;
|
||||
std::vector<metrics::SessionMetrics*> session_metrics_list_;
|
||||
CryptoMetrics crypto_metrics_;
|
||||
std::string app_package_name_;
|
||||
|
||||
void SerializeEngineMetrics(drm_metrics::MetricsGroup* out);
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_METRICS_GROUP_H_
|
||||
24
metrics/include/timer_metric.h
Normal file
24
metrics/include/timer_metric.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef WVCDM_METRICS_TIMER_METRIC_H_
|
||||
#define WVCDM_METRICS_TIMER_METRIC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class TimerMetric {
|
||||
|
||||
public:
|
||||
void Start();
|
||||
double AsMs() const;
|
||||
double AsUs() const;
|
||||
|
||||
private:
|
||||
double sec_;
|
||||
double usec_;
|
||||
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
#endif
|
||||
106
metrics/include/value_metric.h
Normal file
106
metrics/include/value_metric.h
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_VALUE_METRIC_H_
|
||||
#define WVCDM_METRICS_VALUE_METRIC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// Private namespace for some helper implementation functions.
|
||||
namespace impl {
|
||||
|
||||
// These helper functions map the templated ValueMetric class
|
||||
// Serialize call to the MetricSerializer explicit calls.
|
||||
template<typename T>
|
||||
void Serialize(MetricSerializer* serializer,
|
||||
const std::string& metric_name, const T& t);
|
||||
|
||||
inline void SerializeError(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const int& error_code) {
|
||||
serializer->SetInt32(metric_name + "/error", error_code);
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
|
||||
// The Metric class supports storing a single value which can be overwritten.
|
||||
// the Metric class also supports the MetricSerializer interface through
|
||||
// which the value can be serialized. If the value was never given a value
|
||||
// or an error code, then the metric will not serialize anything.
|
||||
//
|
||||
// Example Usage:
|
||||
// Metric<string> cdm_version("drm/cdm/version")
|
||||
// .Record("a.b.c.d");
|
||||
//
|
||||
// MyMetricSerializerImpl serialzer;
|
||||
// cdm_version.Serialize(&serializer);
|
||||
//
|
||||
// Example Error Usage:
|
||||
//
|
||||
// Metric<string> cdm_version("drm/cdm/version")
|
||||
// .SetError(error_code);
|
||||
//
|
||||
// Note that serialization is the same. But the ValueMetric will serialize
|
||||
// the error code to <metric_name>/error instead of just <metric_name>.
|
||||
template<typename T>
|
||||
class ValueMetric : public MetricSerializable {
|
||||
public:
|
||||
// Constructs a metric with the given metric name.
|
||||
explicit ValueMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name), error_code_(0),
|
||||
has_error_(false), has_value_(false) {}
|
||||
|
||||
// Serialize the metric name and value using the given serializer.
|
||||
// Caller owns |serializer| which cannot be null.
|
||||
virtual void Serialize(MetricSerializer* serializer) {
|
||||
if (has_value_) {
|
||||
impl::Serialize(serializer, metric_name_, value_);
|
||||
} else if (has_error_) {
|
||||
impl::SerializeError(serializer, metric_name_, error_code_);
|
||||
} else {
|
||||
// Do nothing if there is no value and no error.
|
||||
}
|
||||
}
|
||||
|
||||
// Record the value of the metric.
|
||||
void Record(const T& value) {
|
||||
value_ = value;
|
||||
has_value_ = true;
|
||||
has_error_ = false;
|
||||
}
|
||||
|
||||
// Set the error code if an error was encountered.
|
||||
void SetError(int error_code) {
|
||||
error_code_ = error_code;
|
||||
has_value_ = false;
|
||||
has_error_ = true;
|
||||
}
|
||||
|
||||
// Get the current value of the metric.
|
||||
const T& GetValue() { return value_; }
|
||||
|
||||
// Clears the indicators that the metric or error was set.
|
||||
void Clear() {
|
||||
has_value_ = false;
|
||||
has_error_ = false;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string metric_name_;
|
||||
T value_;
|
||||
int error_code_;
|
||||
bool has_error_;
|
||||
bool has_value_;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_VALUE_METRIC_H_
|
||||
Reference in New Issue
Block a user