Source release 14.0.0
This commit is contained in:
@@ -10,10 +10,14 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "OEMCryptoCENC.h"
|
||||
#include "attribute_handler.h"
|
||||
#include "distribution.h"
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
#include "log.h"
|
||||
#include "metrics.pb.h"
|
||||
#include "pow2bucket.h"
|
||||
#include "shared_ptr.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
@@ -22,31 +26,26 @@ class EventMetricTest;
|
||||
|
||||
// This base class provides the common defintion used by all templated
|
||||
// instances of EventMetric.
|
||||
class BaseEventMetric : public MetricSerializable {
|
||||
class BaseEventMetric {
|
||||
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) {}
|
||||
BaseEventMetric() {}
|
||||
|
||||
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
|
||||
// by the distribution_key string and update it. If the instance does
|
||||
// not exist, this will create it.
|
||||
void Record(const std::string& field_names_values, double value);
|
||||
void Record(const std::string &distribution_key, double value);
|
||||
|
||||
// value_map_ contains a mapping from the string key (attribute name/values)
|
||||
// to the distribution instance which holds the metric information
|
||||
// (min, max, sum, etc.).
|
||||
std::map<std::string, Distribution *> value_map_;
|
||||
|
||||
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
|
||||
@@ -55,39 +54,6 @@ class BaseEventMetric : public MetricSerializable {
|
||||
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
|
||||
@@ -105,125 +71,100 @@ class Pow2Bucket {
|
||||
//
|
||||
// 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).
|
||||
// EventMetric<
|
||||
// ::drm_metrics::Attributes::kErrorCodeFieldNumber,
|
||||
// CdmResponseType,
|
||||
// ::drm_metrics::Attributes::kCdmSecurityLevelFieldNumber,
|
||||
// CdmSecurityLevel> my_metric;
|
||||
//
|
||||
// The EventMetric supports serialization. A call to Serialize will
|
||||
// serialize all values to the provided MetricsSerializer instance.
|
||||
// // (latency value, error code, security level)
|
||||
// my_metric.Increment(1, CdmResponseType::NO_ERROR, SecurityLevel::kLevel3);
|
||||
//
|
||||
// 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>
|
||||
// The EventMetric class serializes its values to a repeated field of
|
||||
// drm_metrics::DistributionMetric instances. The field numbers used for
|
||||
// recording the drm_metrics::Attributes are specified in the template
|
||||
// parameters above (e.g. kErrorCodeFieldNumber).
|
||||
template <int I1 = 0, typename F1 = util::Unused, int I2 = 0,
|
||||
typename F2 = util::Unused, int I3 = 0, typename F3 = util::Unused,
|
||||
int I4 = 0, 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);
|
||||
// Create an EventMetric instance with no attribute breakdowns.
|
||||
explicit EventMetric() : BaseEventMetric(){}
|
||||
|
||||
// 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());
|
||||
void Record(double value, F1 field1 = util::Unused(),
|
||||
F2 field2 = util::Unused(), F3 field3 = util::Unused(),
|
||||
F4 field4 = util::Unused()) {
|
||||
std::string key =
|
||||
attribute_handler_.GetSerializedAttributes(field1, field2,
|
||||
field3, field4);
|
||||
BaseEventMetric::Record(key, value);
|
||||
}
|
||||
|
||||
const std::map<std::string, Distribution *>* GetDistributions() const {
|
||||
return &value_map_;
|
||||
};
|
||||
|
||||
void ToProto(
|
||||
::google::protobuf::RepeatedPtrField<drm_metrics::DistributionMetric>
|
||||
*distributions_proto);
|
||||
|
||||
private:
|
||||
friend class EventMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
AttributeHandler<I1, F1, I2, F2, I3, F3, I4, F4> attribute_handler_;
|
||||
|
||||
inline void SetDistributionValues(
|
||||
const Distribution &distribution,
|
||||
drm_metrics::DistributionMetric *metric_proto) {
|
||||
metric_proto->set_mean(distribution.Mean());
|
||||
metric_proto->set_operation_count(distribution.Count());
|
||||
if (distribution.Count() > 1) {
|
||||
metric_proto->set_min(distribution.Min());
|
||||
metric_proto->set_max(distribution.Max());
|
||||
metric_proto->set_mean(distribution.Mean());
|
||||
metric_proto->set_variance(distribution.Variance());
|
||||
metric_proto->set_operation_count(distribution.Count());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 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);
|
||||
// Partial Specializations of Event Metrics.
|
||||
|
||||
// Specialization for the event metric with no attributes.
|
||||
// For this, we don't deserialize and populate the attributes message.
|
||||
template <>
|
||||
inline void EventMetric<0, util::Unused, 0, util::Unused, 0, util::Unused, 0,
|
||||
util::Unused>::
|
||||
ToProto(
|
||||
::google::protobuf::RepeatedPtrField<drm_metrics::DistributionMetric>
|
||||
*distributions_proto) {
|
||||
const std::map<std::string, Distribution *>* distributions
|
||||
= GetDistributions();
|
||||
for (std::map<std::string, Distribution *>::const_iterator it =
|
||||
distributions->begin(); it != distributions->end(); it++) {
|
||||
drm_metrics::DistributionMetric *new_metric = distributions_proto->Add();
|
||||
SetDistributionValues(*it->second, new_metric);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
template <int I1, typename F1, int I2, typename F2, int I3, typename F3, int I4,
|
||||
typename F4>
|
||||
inline void EventMetric<I1, F1, I2, F2, I3, F3, I4, F4>::ToProto(
|
||||
::google::protobuf::RepeatedPtrField<drm_metrics::DistributionMetric>
|
||||
*distributions_proto) {
|
||||
const std::map<std::string, Distribution *>* distributions
|
||||
= GetDistributions();
|
||||
for (std::map<std::string, Distribution *>::const_iterator it =
|
||||
distributions->begin(); it != distributions->end(); it++) {
|
||||
drm_metrics::DistributionMetric *new_metric = distributions_proto->Add();
|
||||
if (!new_metric->mutable_attributes()->ParseFromString(it->first)) {
|
||||
LOGE("Failed to parse the attributes from a string.");
|
||||
}
|
||||
SetDistributionValues(*it->second, new_metric);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
|
||||
Reference in New Issue
Block a user