@@ -28,7 +28,7 @@ namespace framework
2828// [METRIC] <name>,<type> <value> <timestamp> [<tags>]
2929bool DeviceMetricsHelper::parseMetric (const std::string& s, std::smatch& match)
3030{
31- const static std::regex metricsRE (R"regex( \[METRIC\] ([a-zA-Z0-9/_-]+),(0|1|2|4) ([0-9.]+) ([0-9]+))regex" , std::regex::optimize);
31+ static std::regex metricsRE (R"regex( \[METRIC\] ([a-zA-Z0-9/_-]+),(0|1|2|4) ([0-9.a-zA-Z_/ ]+) ([0-9]+))regex" , std::regex::optimize);
3232 return std::regex_search (s, match, metricsRE);
3333}
3434
@@ -43,27 +43,26 @@ bool DeviceMetricsHelper::processMetric(const std::smatch& match,
4343 if (ep == nullptr || *ep != ' \0 ' ) {
4444 return false ;
4545 }
46- auto stringValue = match[3 ];
46+ auto valueMatch = match[3 ];
4747 size_t metricIndex = -1 ;
4848
49- auto metricType = MetricType::Unknown;
50- if (type.str () == " 0" ) {
51- metricType = MetricType::Int;
52- } else if (type.str () == " 2" ) {
53- metricType = MetricType::Float;
54- }
49+ auto metricType = static_cast <MetricType>(std::stoi (type.str (), 0 , 10 ));
5550
5651 int intValue = 0 ;
52+ StringMetric stringValue;
5753 float floatValue = 0 ;
5854 switch (metricType) {
5955 case MetricType::Int:
60- intValue = strtol (stringValue .str ().c_str (), &ep, 10 );
56+ intValue = strtol (valueMatch .str ().c_str (), &ep, 10 );
6157 if (!ep || *ep != ' \0 ' ) {
6258 return false ;
6359 }
6460 break ;
61+ case MetricType::String:
62+ strncpy (stringValue.data , valueMatch.str ().c_str (), sizeof (stringValue.data ) - 1 );
63+ break ;
6564 case MetricType::Float:
66- floatValue = strtof (stringValue .str ().c_str (), &ep);
65+ floatValue = strtof (valueMatch .str ().c_str (), &ep);
6766 if (!ep || *ep != ' \0 ' ) {
6867 return false ;
6968 }
@@ -97,6 +96,10 @@ bool DeviceMetricsHelper::processMetric(const std::smatch& match,
9796 metricInfo.storeIdx = info.intMetrics .size ();
9897 info.intMetrics .emplace_back (std::array<int , 1024 >{});
9998 break ;
99+ case MetricType::String:
100+ metricInfo.storeIdx = info.stringMetrics .size ();
101+ info.stringMetrics .emplace_back (std::array<StringMetric, 32 >{});
102+ break ;
100103 case MetricType::Float:
101104 metricInfo.storeIdx = info.floatMetrics .size ();
102105 info.floatMetrics .emplace_back (std::array<float , 1024 >{});
@@ -132,39 +135,50 @@ bool DeviceMetricsHelper::processMetric(const std::smatch& match,
132135 // We are now guaranteed our metric is present at metricIndex.
133136 MetricInfo &metricInfo = info.metrics [metricIndex];
134137
135- auto mod = info.timestamps [metricIndex].size ();
138+ // auto mod = info.timestamps[metricIndex].size();
136139 info.minDomain [metricIndex] = std::min (info.minDomain [metricIndex], (size_t )timestamp);
137140 info.maxDomain [metricIndex] = std::max (info.maxDomain [metricIndex], (size_t )timestamp);
138141
139142 switch (metricInfo.type ) {
140143 case MetricType::Int: {
141- intValue = strtol (stringValue .str ().c_str (), &ep, 10 );
144+ intValue = strtol (valueMatch .str ().c_str (), &ep, 10 );
142145 if (!ep || *ep != ' \0 ' ) {
143146 return false ;
144147 }
145148 info.intMetrics [metricInfo.storeIdx ][metricInfo.pos ] = intValue;
146149 info.max [metricIndex] = std::max (info.max [metricIndex], (float )intValue);
147150 info.min [metricIndex] = std::min (info.min [metricIndex], (float )intValue);
151+ // Save the timestamp for the current metric we do it here
152+ // so that we do not update timestamps for broken metrics
153+ info.timestamps [metricIndex][metricInfo.pos ] = timestamp;
154+ // Update the position where to write the next metric
155+ metricInfo.pos = (metricInfo.pos + 1 ) % info.intMetrics [metricInfo.storeIdx ].size ();
156+ } break ;
157+ case MetricType::String: {
158+ info.stringMetrics [metricInfo.storeIdx ][metricInfo.pos ] = stringValue;
159+ // Save the timestamp for the current metric we do it here
160+ // so that we do not update timestamps for broken metrics
161+ info.timestamps [metricIndex][metricInfo.pos ] = timestamp;
162+ metricInfo.pos = (metricInfo.pos + 1 ) % info.stringMetrics [metricInfo.storeIdx ].size ();
148163 } break ;
149164 case MetricType::Float: {
150- floatValue = strtof (stringValue .str ().c_str (), &ep);
165+ floatValue = strtof (valueMatch .str ().c_str (), &ep);
151166 if (!ep || *ep != ' \0 ' ) {
152167 return false ;
153168 }
154169 info.floatMetrics [metricInfo.storeIdx ][metricInfo.pos ] = floatValue;
155170 info.max [metricIndex] = std::max (info.max [metricIndex], floatValue);
156171 info.min [metricIndex] = std::min (info.min [metricIndex], floatValue);
172+ // Save the timestamp for the current metric we do it here
173+ // so that we do not update timestamps for broken metrics
174+ info.timestamps [metricIndex][metricInfo.pos ] = timestamp;
175+ metricInfo.pos = (metricInfo.pos + 1 ) % info.floatMetrics [metricInfo.storeIdx ].size ();
157176 } break ;
158177 default :
159178 return false ;
160179 break ;
161180 };
162181
163- // Save the timestamp for the current metric we do it here
164- // so that we do not update timestamps for broken metrics
165- info.timestamps [metricIndex][metricInfo.pos ] = timestamp;
166- // Update the position where to write the next metric
167- metricInfo.pos = (metricInfo.pos + 1 ) % mod;
168182 return true ;
169183}
170184
@@ -189,6 +203,9 @@ std::ostream& operator<<(std::ostream& oss, MetricType const& val)
189203 case MetricType::Float:
190204 oss << " float" ;
191205 break ;
206+ case MetricType::String:
207+ oss << " string" ;
208+ break ;
192209 case MetricType::Int:
193210 oss << " float" ;
194211 break ;
0 commit comments