@@ -234,29 +234,61 @@ MetricAPIHelper(TRITONSERVER_Metric* metric, TRITONSERVER_MetricKind kind)
234234}
235235
236236void
237- HistogramAPIHelper (TRITONSERVER_Metric* metric)
237+ HistogramAPIHelper (
238+ TRITONSERVER_Server* server, TRITONSERVER_Metric* metric,
239+ std::vector<double > buckets, std::string metric_name,
240+ std::string labels_str)
238241{
239- // Observe
242+ // Observe data
240243 std::vector<double > data{0.05 , 1.5 , 6.0 };
241- std::vector<std::uint64_t > cumulative_counts = {1 , 1 , 2 , 2 , 3 , 3 };
242244 double sum = 0.0 ;
243245 for (auto datum : data) {
244246 FAIL_TEST_IF_ERR (
245247 TRITONSERVER_MetricSet (metric, datum), " observe metric value" );
246248 sum += datum;
247249 }
250+ std::vector<std::uint64_t > cumulative_counts = {1 , 1 , 2 , 2 , 3 , 3 };
251+ ASSERT_EQ (buckets.size () + 1 , cumulative_counts.size ());
252+
253+ // Collect formatted output
254+ std::string metrics_str;
255+ GetMetrics (server, &metrics_str);
248256
249- // Collect
250- prometheus::ClientMetric value;
251- FAIL_TEST_IF_ERR (
252- TRITONSERVER_MetricCollect (metric, &value),
253- " query metric value after observe" );
254- auto hist = value.histogram ;
255- ASSERT_EQ (hist.sample_count , data.size ());
256- ASSERT_EQ (hist.sample_sum , sum);
257- ASSERT_EQ (hist.bucket .size (), cumulative_counts.size ());
258- for (uint64_t i = 0 ; i < hist.bucket .size (); ++i) {
259- ASSERT_EQ (hist.bucket [i].cumulative_count , cumulative_counts[i]);
257+ // All strings in this function are generated from ostringstream to make sure
258+ // there is no trailing zeros. For example, std::to_string(7.55) is "7.550000"
259+ // which is inconsistent with prometheus text_serializer output "7.55".
260+
261+ // custom_histogram_example_count{example1="histogram_label1",example2="histogram_label2"}
262+ // 3
263+ std::ostringstream count_ss;
264+ count_ss << metric_name << " _count{" << labels_str << " } " << data.size ();
265+ ASSERT_EQ (NumMetricMatches (server, count_ss.str ()), 1 );
266+
267+ // custom_histogram_example_sum{example1="histogram_label1",example2="histogram_label2"}
268+ // 7.55
269+ std::ostringstream sum_ss;
270+ sum_ss << metric_name << " _sum{" << labels_str << " } " << sum;
271+ ASSERT_EQ (NumMetricMatches (server, sum_ss.str ()), 1 );
272+
273+ // custom_histogram_example_bucket{example1="histogram_label1",example2="histogram_label2"
274+ std::ostringstream bucket_partial_ss;
275+ bucket_partial_ss << metric_name << " _bucket{" << labels_str;
276+ ASSERT_EQ (
277+ NumMetricMatches (server, bucket_partial_ss.str ()),
278+ cumulative_counts.size ());
279+ for (uint64_t i = 0 ; i < cumulative_counts.size (); ++i) {
280+ // custom_histogram_example_bucket{example1="histogram_label1",example2="histogram_label2",le="0.1"}
281+ // 1
282+ std::ostringstream le_ss;
283+ if (i < buckets.size ()) {
284+ le_ss << " \" " << buckets[i] << " \" " ;
285+ } else {
286+ le_ss << " \" +Inf\" " ;
287+ }
288+ std::ostringstream bucket_ss;
289+ bucket_ss << metric_name << " _bucket{" << labels_str
290+ << " ,le=" << le_ss.str () << " } " << cumulative_counts[i];
291+ ASSERT_EQ (NumMetricMatches (server, bucket_ss.str ()), 1 );
260292 }
261293}
262294
@@ -431,7 +463,9 @@ TEST_F(MetricsApiTest, TestHistogramEndToEnd)
431463 FAIL_TEST_IF_ERR (TRITONSERVER_MetricArgsDelete (args), " delete metric args" );
432464
433465 // Run through metric APIs and assert correctness
434- HistogramAPIHelper (metric);
466+ std::string labels_str =
467+ " example1=\" histogram_label1\" ,example2=\" histogram_label2\" " ;
468+ HistogramAPIHelper (server_, metric, buckets, name, labels_str);
435469
436470 // Assert custom metric is reported and found in output
437471 ASSERT_EQ (NumMetricMatches (server_, description), 1 );
0 commit comments