Source release 14.0.0
This commit is contained in:
@@ -11,9 +11,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "attribute_handler.h"
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
@@ -22,23 +22,22 @@ class CounterMetricTest;
|
||||
|
||||
// This base class provides the common defintion used by all templated
|
||||
// instances of CounterMetric.
|
||||
class BaseCounterMetric : public MetricSerializable {
|
||||
class BaseCounterMetric {
|
||||
public:
|
||||
// Send metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer);
|
||||
const std::map<std::string, int64_t>* GetValues() const {
|
||||
return &value_map_;
|
||||
};
|
||||
|
||||
protected:
|
||||
// Instantiates a BaseCounterMetric.
|
||||
BaseCounterMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name) {}
|
||||
BaseCounterMetric() {}
|
||||
|
||||
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);
|
||||
void Increment(const std::string &counter_key, int64_t value);
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
@@ -48,8 +47,9 @@ class BaseCounterMetric : public MetricSerializable {
|
||||
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.
|
||||
* 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_;
|
||||
};
|
||||
@@ -67,136 +67,84 @@ class BaseCounterMetric : public MetricSerializable {
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// CounterMetric<int, int> my_metric("multiple/fields/metric",
|
||||
// "request_type", // Field name.
|
||||
// "error_code"); // Field name.
|
||||
// CounterMetric<
|
||||
// ::drm_metrics::Attributes::kErrorCodeFieldNumber,
|
||||
// CdmResponseType,
|
||||
// ::drm_metrics::Attributes::kCdmSecurityLevelFieldNumber,
|
||||
// CdmSecurityLevel> my_metric;
|
||||
//
|
||||
// my_metric.Increment(1, 7, 23); // (counter value, request type, error code).
|
||||
// // (counter value, error code, security level)
|
||||
// my_metric.Increment(1, CdmResponseType::NO_ERROR, SecurityLevel::kLevel3);
|
||||
//
|
||||
// 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>
|
||||
// The CounterMetric class serializes its values to a repeated field of
|
||||
// drm_metrics::CounterMetric 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 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);
|
||||
explicit CounterMetric() : BaseCounterMetric() {}
|
||||
|
||||
// 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());
|
||||
F3 field3 = util::Unused(), F4 field4 = util::Unused()) {
|
||||
Increment(1, field1, field2, field3, field4);
|
||||
}
|
||||
|
||||
// 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());
|
||||
void Increment(int64_t 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);
|
||||
BaseCounterMetric::Increment(key, value);
|
||||
}
|
||||
|
||||
void ToProto(::google::protobuf::RepeatedPtrField<drm_metrics::CounterMetric>
|
||||
*counters);
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
AttributeHandler<I1, F1, I2, F2, I3, F3, I4, F4> attribute_handler_;
|
||||
};
|
||||
|
||||
// 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);
|
||||
// Partial specializations for CounterMetric template.
|
||||
|
||||
// Specialization for the CounterMetric with no attributes.
|
||||
// For this, we don't deserialize and populate the attributes message.
|
||||
template <>
|
||||
inline void CounterMetric<0, util::Unused, 0, util::Unused, 0, util::Unused, 0,
|
||||
util::Unused>::
|
||||
ToProto(::google::protobuf::RepeatedPtrField<drm_metrics::CounterMetric>
|
||||
*counters) {
|
||||
|
||||
const std::map<std::string, int64_t>* values = GetValues();
|
||||
for (std::map<std::string, int64_t>::const_iterator it = values->begin();
|
||||
it != values->end(); it++) {
|
||||
drm_metrics::CounterMetric *new_counter = counters->Add();
|
||||
new_counter->set_count(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
template <int I1, typename F1, int I2, typename F2, int I3, typename F3, int I4,
|
||||
typename F4>
|
||||
inline void CounterMetric<I1, F1, I2, F2, I3, F3, I4, F4>::ToProto(
|
||||
::google::protobuf::RepeatedPtrField<drm_metrics::CounterMetric>
|
||||
*counters) {
|
||||
const std::map<std::string, int64_t>* values = GetValues();
|
||||
for (std::map<std::string, int64_t>::const_iterator it = values->begin();
|
||||
it != values->end(); it++) {
|
||||
drm_metrics::CounterMetric *new_counter = counters->Add();
|
||||
if (!new_counter->mutable_attributes()->ParseFromString(it->first)) {
|
||||
LOGE("Failed to parse the attributes from a string.");
|
||||
}
|
||||
new_counter->set_count(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
|
||||
Reference in New Issue
Block a user