|
3 | 3 |
|
4 | 4 | #pragma once
|
5 | 5 |
|
| 6 | +#include <algorithm> |
| 7 | +#include <cctype> |
6 | 8 | #include <functional>
|
7 | 9 | #include <string>
|
8 | 10 |
|
@@ -65,6 +67,152 @@ struct InstrumentDescriptor
|
65 | 67 | InstrumentValueType value_type_;
|
66 | 68 | };
|
67 | 69 |
|
| 70 | +struct InstrumentDescriptorUtil |
| 71 | +{ |
| 72 | + // Case-insensitive comparison of two ASCII strings used to evaluate equality of instrument names |
| 73 | + static bool CaseInsensitiveAsciiEquals(const std::string &lhs, const std::string &rhs) noexcept |
| 74 | + { |
| 75 | + return lhs.size() == rhs.size() && |
| 76 | + std::equal(lhs.begin(), lhs.end(), rhs.begin(), [](char a, char b) { |
| 77 | + return std::tolower(static_cast<unsigned char>(a)) == |
| 78 | + std::tolower(static_cast<unsigned char>(b)); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + // Implementation of the specification requirements on duplicate instruments |
| 83 | + // An instrument is a duplicate if it has the same name (case-insensitive) as another instrument, |
| 84 | + // but different instrument kind, unit, or description. |
| 85 | + // https://github.com/open-telemetry/opentelemetry-specification/blob/9c8c30631b0e288de93df7452f91ed47f6fba330/specification/metrics/sdk.md?plain=1#L869 |
| 86 | + static bool IsDuplicate(const InstrumentDescriptor &lhs, const InstrumentDescriptor &rhs) noexcept |
| 87 | + { |
| 88 | + // Not a duplicate if case-insensitive names are not equal |
| 89 | + if (!InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals(lhs.name_, rhs.name_)) |
| 90 | + { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + // Duplicate if names equal and kinds (Type and ValueType) are not equal |
| 95 | + if (lhs.type_ != rhs.type_ || lhs.value_type_ != rhs.value_type_) |
| 96 | + { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + // Duplicate if names equal and units (case-sensitive) are not equal |
| 101 | + if (lhs.unit_ != rhs.unit_) |
| 102 | + { |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + // Duplicate if names equal and descriptions (case-sensitive) are not equal |
| 107 | + if (lhs.description_ != rhs.description_) |
| 108 | + { |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + // All identifying fields are equal |
| 113 | + // These are identical instruments or only have a name case conflict |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + static opentelemetry::nostd::string_view GetInstrumentTypeString(InstrumentType type) noexcept |
| 118 | + { |
| 119 | + switch (type) |
| 120 | + { |
| 121 | + case InstrumentType::kCounter: |
| 122 | + return "Counter"; |
| 123 | + case InstrumentType::kUpDownCounter: |
| 124 | + return "UpDownCounter"; |
| 125 | + case InstrumentType::kHistogram: |
| 126 | + return "Histogram"; |
| 127 | + case InstrumentType::kObservableCounter: |
| 128 | + return "ObservableCounter"; |
| 129 | + case InstrumentType::kObservableUpDownCounter: |
| 130 | + return "ObservableUpDownCounter"; |
| 131 | + case InstrumentType::kObservableGauge: |
| 132 | + return "ObservableGauge"; |
| 133 | + case InstrumentType::kGauge: |
| 134 | + return "Gauge"; |
| 135 | + default: |
| 136 | + return "Unknown"; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + static opentelemetry::nostd::string_view GetInstrumentValueTypeString( |
| 141 | + InstrumentValueType value_type) noexcept |
| 142 | + { |
| 143 | + switch (value_type) |
| 144 | + { |
| 145 | + case InstrumentValueType::kInt: |
| 146 | + return "Int"; |
| 147 | + case InstrumentValueType::kLong: |
| 148 | + return "Long"; |
| 149 | + case InstrumentValueType::kFloat: |
| 150 | + return "Float"; |
| 151 | + case InstrumentValueType::kDouble: |
| 152 | + return "Double"; |
| 153 | + default: |
| 154 | + return "Unknown"; |
| 155 | + } |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +struct InstrumentEqualNameCaseInsensitive |
| 160 | +{ |
| 161 | + bool operator()(const InstrumentDescriptor &lhs, const InstrumentDescriptor &rhs) const noexcept |
| 162 | + { |
| 163 | + // Names (case-insensitive) |
| 164 | + if (!InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals(lhs.name_, rhs.name_)) |
| 165 | + { |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + // Kinds (Type and ValueType) |
| 170 | + if (lhs.type_ != rhs.type_ || lhs.value_type_ != rhs.value_type_) |
| 171 | + { |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + // Units (case-sensitive) |
| 176 | + if (lhs.unit_ != rhs.unit_) |
| 177 | + { |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
| 181 | + // Descriptions (case-sensitive) |
| 182 | + if (lhs.description_ != rhs.description_) |
| 183 | + { |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + // All identifying fields are equal |
| 188 | + return true; |
| 189 | + } |
| 190 | +}; |
| 191 | + |
| 192 | +// Hash for InstrumentDescriptor |
| 193 | +// Identical instruments must have the same hash value |
| 194 | +// Two instruments are identical when all identifying fields (case-insensitive name , kind, |
| 195 | +// description, unit) are equal. |
| 196 | +struct InstrumentDescriptorHash |
| 197 | +{ |
| 198 | + std::size_t operator()(const InstrumentDescriptor &instrument_descriptor) const noexcept |
| 199 | + { |
| 200 | + std::size_t hashcode{}; |
| 201 | + |
| 202 | + for (char c : instrument_descriptor.name_) |
| 203 | + { |
| 204 | + sdk::common::GetHash(hashcode, |
| 205 | + static_cast<char>(std::tolower(static_cast<unsigned char>(c)))); |
| 206 | + } |
| 207 | + |
| 208 | + sdk::common::GetHash(hashcode, instrument_descriptor.description_); |
| 209 | + sdk::common::GetHash(hashcode, instrument_descriptor.unit_); |
| 210 | + sdk::common::GetHash(hashcode, static_cast<uint32_t>(instrument_descriptor.type_)); |
| 211 | + sdk::common::GetHash(hashcode, static_cast<uint32_t>(instrument_descriptor.value_type_)); |
| 212 | + return hashcode; |
| 213 | + } |
| 214 | +}; |
| 215 | + |
68 | 216 | using MetricAttributes = opentelemetry::sdk::metrics::FilteredOrderedAttributeMap;
|
69 | 217 | using MetricAttributesHash = opentelemetry::sdk::metrics::FilteredOrderedAttributeMapHash;
|
70 | 218 | using AggregationTemporalitySelector = std::function<AggregationTemporality(InstrumentType)>;
|
|
0 commit comments