1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_LIB_MONITORING_METRIC_DEF_H_ 17 #define TENSORFLOW_CORE_LIB_MONITORING_METRIC_DEF_H_ 18 19 #include <array> 20 #include <vector> 21 22 #include "tensorflow/core/framework/summary.pb.h" 23 #include "tensorflow/core/lib/core/stringpiece.h" 24 #include "tensorflow/core/platform/types.h" 25 26 namespace tensorflow { 27 namespace monitoring { 28 29 // The different metric kinds available. 30 // 31 // Gauge indicates that the metric's values are instantaneous measurements of a 32 // (typically) continuously varying value. Examples: a process's current heap 33 // size, a queue's current length, the name of the binary used by a process, 34 // whether a task is complete. 35 // 36 // Cumulative indicates that the metric's values represent non-negative changes 37 // over specified time periods. Example: the number of rpc calls to a service. 38 enum class MetricKind : int { kGauge = 0, kCumulative }; 39 40 // The type of the metric values. 41 enum class ValueType : int { kInt64 = 0, kHistogram, kString, kBool }; 42 43 // Everything in the internal namespace is implementation details. Do not depend 44 // on this. 45 namespace internal { 46 47 template <typename Value> 48 ValueType GetValueType(); 49 50 template <> 51 inline ValueType GetValueType<int64>() { 52 return ValueType::kInt64; 53 } 54 55 template <> 56 inline ValueType GetValueType<HistogramProto>() { 57 return ValueType::kHistogram; 58 } 59 60 template <> 61 inline ValueType GetValueType<string>() { 62 return ValueType::kString; 63 } 64 65 template <> 66 inline ValueType GetValueType<bool>() { 67 return ValueType::kBool; 68 } 69 70 } // namespace internal 71 72 // Abstract base class for a metric definition. 73 // 74 // Unlike MetricDef, this class is non-templatized and allows storing and 75 // accessing metric definitions without the full type information. 76 // 77 // Everything except the value type of a metric is stored here. Please read 78 // MetricDef class comments for more details. 79 class AbstractMetricDef { 80 public: kind()81 MetricKind kind() const { return kind_; } 82 value_type()83 ValueType value_type() const { return value_type_; } 84 name()85 StringPiece name() const { return name_; } 86 description()87 StringPiece description() const { return description_; } 88 label_descriptions()89 const std::vector<string>& label_descriptions() const { 90 return label_descriptions_; 91 } 92 93 private: 94 template <MetricKind kind, typename Value, int NumLabels> 95 friend class MetricDef; 96 AbstractMetricDef(const MetricKind kind,const ValueType value_type,const StringPiece name,const StringPiece description,const std::vector<string> & label_descriptions)97 AbstractMetricDef(const MetricKind kind, const ValueType value_type, 98 const StringPiece name, const StringPiece description, 99 const std::vector<string>& label_descriptions) 100 : kind_(kind), 101 value_type_(value_type), 102 name_(name), 103 description_(description), 104 label_descriptions_(std::vector<string>(label_descriptions.begin(), 105 label_descriptions.end())) {} 106 107 const MetricKind kind_; 108 const ValueType value_type_; 109 const string name_; 110 const string description_; 111 const std::vector<string> label_descriptions_; 112 }; 113 114 // Metric definition. 115 // 116 // A metric is defined by its kind, value-type, name, description and the 117 // description of its labels. 118 // 119 // NOTE: Name, description, and label descriptions should be logically static, 120 // but do not have to live for the lifetime of the MetricDef. 121 // 122 // By "logically static", we mean that they should never contain dynamic 123 // information, but is static for the lifetime of the MetricDef, and 124 // in-turn the metric; they do not need to be compile-time constants. 125 // This allows for e.g. prefixed metrics in a CLIF wrapped environment. 126 template <MetricKind metric_kind, typename Value, int NumLabels> 127 class MetricDef : public AbstractMetricDef { 128 public: 129 template <typename... LabelDesc> MetricDef(const StringPiece name,const StringPiece description,const LabelDesc &...label_descriptions)130 MetricDef(const StringPiece name, const StringPiece description, 131 const LabelDesc&... label_descriptions) 132 : AbstractMetricDef(metric_kind, internal::GetValueType<Value>(), name, 133 description, {label_descriptions...}) { 134 static_assert(sizeof...(LabelDesc) == NumLabels, 135 "Mismatch between Counter<NumLabels> and number of label " 136 "descriptions."); 137 } 138 }; 139 140 } // namespace monitoring 141 } // namespace tensorflow 142 143 #endif // TENSORFLOW_CORE_LIB_MONITORING_METRIC_DEF_H_ 144