diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2017-11-27 07:46:52 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-11-27 07:50:51 -0800 |
commit | a7c11adad8fa445be1083467ceb76b2d7c98b005 (patch) | |
tree | 6be5a49f8b360d4a730627d2772e4eeda9086671 /tensorflow/core/lib/monitoring | |
parent | 191825e63f341a4e7777b85254f616e541000d5c (diff) |
Update metric library to allow non-literal strings.
PiperOrigin-RevId: 177015295
Diffstat (limited to 'tensorflow/core/lib/monitoring')
-rw-r--r-- | tensorflow/core/lib/monitoring/collection_registry.h | 4 | ||||
-rw-r--r-- | tensorflow/core/lib/monitoring/metric_def.h | 55 | ||||
-rw-r--r-- | tensorflow/core/lib/monitoring/metric_def_test.cc | 18 |
3 files changed, 39 insertions, 38 deletions
diff --git a/tensorflow/core/lib/monitoring/collection_registry.h b/tensorflow/core/lib/monitoring/collection_registry.h index 030f8e360a..113d37e07d 100644 --- a/tensorflow/core/lib/monitoring/collection_registry.h +++ b/tensorflow/core/lib/monitoring/collection_registry.h @@ -321,13 +321,13 @@ void MetricCollector<metric_kind, Value, NumLabels>::CollectValue( const std::array<string, NumLabels>& labels, const Value& value) { point_set_->points.emplace_back(new Point()); auto* const point = point_set_->points.back().get(); - const std::vector<StringPiece> label_descriptions = + const std::vector<string> label_descriptions = metric_def_->label_descriptions(); point->labels.reserve(NumLabels); for (int i = 0; i < NumLabels; ++i) { point->labels.push_back({}); auto* const label = &point->labels.back(); - label->name = label_descriptions[i].ToString(); + label->name = label_descriptions[i]; label->value = labels[i]; } internal::CollectValue(value, point); diff --git a/tensorflow/core/lib/monitoring/metric_def.h b/tensorflow/core/lib/monitoring/metric_def.h index 3459c2ab82..a7f14f9c94 100644 --- a/tensorflow/core/lib/monitoring/metric_def.h +++ b/tensorflow/core/lib/monitoring/metric_def.h @@ -43,24 +43,6 @@ enum class ValueType : int { kInt64 = 0, kHistogram, kString }; // on this. namespace internal { -// Ensures that the string is a compile-time string literal. -class StringLiteral { - public: - // We allow implicit conversions here on purpose. - template <int N> - StringLiteral(const char (&data)[N]) : literal_(data, N - 1) {} - - // This ctor will be called for non-literals, causing compile-time failure. - template <typename NotStringLiteral> - StringLiteral(const NotStringLiteral& not_string_literal) = delete; - - // Implicit conversion to StringPiece. - operator StringPiece() const { return literal_; } - - private: - const StringPiece literal_; -}; - template <typename Value> ValueType GetValueType(); @@ -98,7 +80,7 @@ class AbstractMetricDef { StringPiece description() const { return description_; } - const std::vector<StringPiece> label_descriptions() const { + const std::vector<string>& label_descriptions() const { return label_descriptions_; } @@ -106,23 +88,21 @@ class AbstractMetricDef { template <MetricKind kind, typename Value, int NumLabels> friend class MetricDef; - AbstractMetricDef( - const MetricKind kind, const ValueType value_type, - const internal::StringLiteral name, - const internal::StringLiteral description, - const std::vector<internal::StringLiteral>& label_descriptions) + AbstractMetricDef(const MetricKind kind, const ValueType value_type, + const StringPiece name, const StringPiece description, + const std::vector<string>& label_descriptions) : kind_(kind), value_type_(value_type), - name_(name), - description_(description), - label_descriptions_(std::vector<StringPiece>( - label_descriptions.begin(), label_descriptions.end())) {} + name_(name.ToString()), + description_(description.ToString()), + label_descriptions_(std::vector<string>(label_descriptions.begin(), + label_descriptions.end())) {} const MetricKind kind_; const ValueType value_type_; - const StringPiece name_; - const StringPiece description_; - const std::vector<StringPiece> label_descriptions_; + const string name_; + const string description_; + const std::vector<string> label_descriptions_; }; // Metric definition. @@ -130,15 +110,18 @@ class AbstractMetricDef { // A metric is defined by its kind, value-type, name, description and the // description of its labels. // -// NOTE: We allow only string literals for the name, description and label -// descriptions because these should be fixed at compile-time and shouldn't be -// dynamic. +// NOTE: Name, description, and label descriptions should be logically static, +// but do not have to live for the lifetime of the MetricDef. +// +// By "logically static", we mean that they should never contain dynamic +// information, but is static for the lifetime of the MetricDef, and +// in-turn the metric; they do not need to be compile-time constants. +// This allows for e.g. prefixed metrics in a CLIF wrapped environment. template <MetricKind metric_kind, typename Value, int NumLabels> class MetricDef : public AbstractMetricDef { public: template <typename... LabelDesc> - MetricDef(const internal::StringLiteral name, - const internal::StringLiteral description, + MetricDef(const StringPiece name, const StringPiece description, const LabelDesc&... label_descriptions) : AbstractMetricDef(metric_kind, internal::GetValueType<Value>(), name, description, {label_descriptions...}) { diff --git a/tensorflow/core/lib/monitoring/metric_def_test.cc b/tensorflow/core/lib/monitoring/metric_def_test.cc index dc07a08e4f..66973b6b5f 100644 --- a/tensorflow/core/lib/monitoring/metric_def_test.cc +++ b/tensorflow/core/lib/monitoring/metric_def_test.cc @@ -41,6 +41,24 @@ TEST(MetricDefTest, Simple) { EXPECT_EQ("LabelName", metric_def1.label_descriptions()[0]); } +TEST(MetricDefTest, StringsPersist) { + // Ensure string attributes of the metric are copied into the metric + string name = "/tensorflow/metric0"; + string description = "test description"; + string label_description = "test label description"; + const MetricDef<MetricKind::kCumulative, int64, 1> metric_def( + name, description, label_description); + + // Mutate the strings + name[4] = 'A'; + description[4] = 'B'; + label_description[4] = 'C'; + + EXPECT_NE(name, metric_def.name()); + EXPECT_NE(description, metric_def.description()); + EXPECT_NE(label_description, metric_def.label_descriptions()[0]); +} + } // namespace } // namespace monitoring } // namespace tensorflow |