Skip to content

Commit 667858e

Browse files
committed
Remove TRITONSERVER_MetricCollect API
1 parent edb0533 commit 667858e

File tree

5 files changed

+57
-79
lines changed

5 files changed

+57
-79
lines changed

include/triton/core/tritonserver.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,7 +2693,6 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricArgsDelete(
26932693
/// \param family The metric family to add this new metric to.
26942694
/// \param labels The array of labels to associate with this new metric.
26952695
/// \param label_count The number of labels.
2696-
/// \param buckets Monotonically increasing values representing the
26972696
/// bucket boundaries. For histogram only.
26982697
/// \return a TRITONSERVER_Error indicating success or failure.
26992698
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricNew(
@@ -2769,18 +2768,6 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricIncrement(
27692768
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricSet(
27702769
struct TRITONSERVER_Metric* metric, double value);
27712770

2772-
/// Collect metrics.
2773-
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_COUNTER,
2774-
/// TRITONSERVER_METRIC_KIND_GAUGE, TRITONSERVER_METRIC_KIND_HISTOGRAM and
2775-
/// returns TRITONSERVER_ERROR_UNSUPPORTED for unsupported
2776-
/// TRITONSERVER_MetricKind.
2777-
///
2778-
/// \param metric The metric object to collect.
2779-
/// \param value Returns the current value of the metric object.
2780-
/// \return a TRITONSERVER_Error indicating success or failure.
2781-
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricCollect(
2782-
struct TRITONSERVER_Metric* metric, void* value);
2783-
27842771
/// Get the TRITONSERVER_MetricKind of metric of its corresponding family.
27852772
///
27862773
/// \param metric The metric object to query.

src/metric_family.cc

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ MetricFamily::Add(
101101
case TRITONSERVER_METRIC_KIND_HISTOGRAM: {
102102
if (args == nullptr) {
103103
throw std::invalid_argument(
104-
"Bucket boundaries not found in Metric constructor args.");
104+
"Bucket boundaries not found in Metric args.");
105+
}
106+
if (args->kind() != TRITONSERVER_METRIC_KIND_HISTOGRAM) {
107+
throw std::invalid_argument(
108+
"Incorrect Metric args kind in histogram Metric constructor.");
105109
}
106110
auto histogram_family_ptr =
107111
reinterpret_cast<prometheus::Family<prometheus::Histogram>*>(family_);
@@ -370,40 +374,6 @@ Metric::Set(double value)
370374
return nullptr; // Success
371375
}
372376

373-
TRITONSERVER_Error*
374-
Metric::Collect(prometheus::ClientMetric* value)
375-
{
376-
if (metric_ == nullptr) {
377-
return TRITONSERVER_ErrorNew(
378-
TRITONSERVER_ERROR_INTERNAL,
379-
"Could not collect metric value. Metric has been invalidated.");
380-
}
381-
382-
switch (kind_) {
383-
case TRITONSERVER_METRIC_KIND_COUNTER: {
384-
auto counter_ptr = reinterpret_cast<prometheus::Counter*>(metric_);
385-
*value = counter_ptr->Collect();
386-
break;
387-
}
388-
case TRITONSERVER_METRIC_KIND_GAUGE: {
389-
auto gauge_ptr = reinterpret_cast<prometheus::Gauge*>(metric_);
390-
*value = gauge_ptr->Collect();
391-
break;
392-
}
393-
case TRITONSERVER_METRIC_KIND_HISTOGRAM: {
394-
auto histogram_ptr = reinterpret_cast<prometheus::Histogram*>(metric_);
395-
*value = histogram_ptr->Collect();
396-
break;
397-
}
398-
default:
399-
return TRITONSERVER_ErrorNew(
400-
TRITONSERVER_ERROR_UNSUPPORTED,
401-
"Unsupported TRITONSERVER_MetricKind");
402-
}
403-
404-
return nullptr; // Success
405-
}
406-
407377
}} // namespace triton::core
408378

409379
#endif // TRITON_ENABLE_METRICS

src/metric_family.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ class TritonServerMetricArgs {
5050

5151
void* SetHistogramArgs(const double* buckets, uint64_t bucket_count)
5252
{
53-
kind_ = TRITONSERVER_MetricKind::TRITONSERVER_METRIC_KIND_HISTOGRAM;
53+
kind_ = TRITONSERVER_METRIC_KIND_HISTOGRAM;
5454
buckets_.resize(bucket_count);
5555
std::memcpy(buckets_.data(), buckets, sizeof(double) * bucket_count);
5656
return nullptr;
5757
}
58-
58+
TRITONSERVER_MetricKind kind() const { return kind_; }
5959
const std::vector<double>& buckets() const { return buckets_; }
6060

6161
private:
@@ -78,7 +78,7 @@ class MetricFamily {
7878

7979
void* Add(
8080
std::map<std::string, std::string> label_map, Metric* metric,
81-
const TritonServerMetricArgs* buckets);
81+
const TritonServerMetricArgs* args);
8282
void Remove(void* prom_metric, Metric* metric);
8383

8484
int NumMetrics()
@@ -125,7 +125,6 @@ class Metric {
125125
TRITONSERVER_Error* Increment(double value);
126126
TRITONSERVER_Error* Set(double value);
127127
TRITONSERVER_Error* Observe(double value);
128-
TRITONSERVER_Error* Collect(prometheus::ClientMetric* value);
129128

130129
// If a MetricFamily is deleted before its dependent Metric, we want to
131130
// invalidate the references so we don't access invalid memory.

src/test/metrics_api_test.cc

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,29 +234,61 @@ MetricAPIHelper(TRITONSERVER_Metric* metric, TRITONSERVER_MetricKind kind)
234234
}
235235

236236
void
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);

src/tritonserver.cc

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,18 +3525,6 @@ TRITONSERVER_MetricSet(TRITONSERVER_Metric* metric, double value)
35253525
#endif // TRITON_ENABLE_METRICS
35263526
}
35273527

3528-
TRITONSERVER_Error*
3529-
TRITONSERVER_MetricCollect(TRITONSERVER_Metric* metric, void* value)
3530-
{
3531-
#ifdef TRITON_ENABLE_METRICS
3532-
return reinterpret_cast<tc::Metric*>(metric)->Collect(
3533-
reinterpret_cast<prometheus::ClientMetric*>(value));
3534-
#else
3535-
return TRITONSERVER_ErrorNew(
3536-
TRITONSERVER_ERROR_UNSUPPORTED, "metrics not supported");
3537-
#endif // TRITON_ENABLE_METRICS
3538-
}
3539-
35403528
TRITONSERVER_Error*
35413529
TRITONSERVER_GetMetricKind(
35423530
TRITONSERVER_Metric* metric, TRITONSERVER_MetricKind* kind)

0 commit comments

Comments
 (0)