Skip to content

Commit

Permalink
Restore TRITONSERVER_MetricObserve
Browse files Browse the repository at this point in the history
  • Loading branch information
yinggeh committed Aug 16, 2024
1 parent 60f1e63 commit 349daa6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
30 changes: 20 additions & 10 deletions include/triton/core/tritonserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2648,7 +2648,7 @@ TRITONSERVER_MetricFamilyDelete(struct TRITONSERVER_MetricFamily* family);

/// Get the TRITONSERVER_MetricKind of the metric family.
///
/// \param metric The metric family object to query.
/// \param family The metric family object to query.
/// \param kind Returns the TRITONSERVER_MetricKind of metric.
/// \return a TRITONSERVER_Error indicating success or failure.
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
Expand All @@ -2664,10 +2664,12 @@ TRITONSERVER_GetMetricFamilyKind(
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricArgsNew(
struct TRITONSERVER_MetricArgs** args);

/// Set metric args with prometheus histogram metric parameter.
/// Set metric args with histogram metric parameter.
///
/// \param args The metric args object to set.
/// \param buckets The array of bucket boundaries.
/// \param buckets The array of bucket boundaries for the expected range of
/// observed values.
///
/// \param buckets_count The number of bucket boundaries.
/// \return a TRITONSERVER_Error indicating success or failure.
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
Expand Down Expand Up @@ -2732,10 +2734,9 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricDelete(
struct TRITONSERVER_Metric* metric);

/// Get the current value of a metric object.
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_COUNTER,
/// TRITONSERVER_METRIC_KIND_GAUGE, TRITONSERVER_METRIC_KIND_HISTOGRAM, and
/// returns TRITONSERVER_ERROR_UNSUPPORTED for unsupported
/// TRITONSERVER_MetricKind.
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_COUNTER
/// and TRITONSERVER_METRIC_KIND_GAUGE, and returns
/// TRITONSERVER_ERROR_UNSUPPORTED for unsupported TRITONSERVER_MetricKind.
///
/// \param metric The metric object to query.
/// \param value Returns the current value of the metric object.
Expand All @@ -2756,9 +2757,8 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricValue(
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricIncrement(
struct TRITONSERVER_Metric* metric, double value);

/// Set the current value of metric to value or observe the value to metric.
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_GAUGE and
/// TRITONSERVER_METRIC_KIND_HISTOGRAM. Returns
/// Set the current value of metric to value.
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_GAUGE and returns
/// TRITONSERVER_ERROR_UNSUPPORTED for unsupported TRITONSERVER_MetricKind.
///
/// \param metric The metric object to update.
Expand All @@ -2767,6 +2767,16 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricIncrement(
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricSet(
struct TRITONSERVER_Metric* metric, double value);

/// Sample an observation and count it to the appropriate bucket of a metric.
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_HISTOGRAM and returns
/// TRITONSERVER_ERROR_UNSUPPORTED for unsupported TRITONSERVER_MetricKind.
///
/// \param metric The metric object to update.
/// \param value The amount for metric to sample observation.
/// \return a TRITONSERVER_Error indicating success or failure.
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricObserve(
struct TRITONSERVER_Metric* metric, double value);

/// Get the TRITONSERVER_MetricKind of metric of its corresponding family.
///
/// \param metric The metric object to query.
Expand Down
34 changes: 34 additions & 0 deletions src/metric_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,40 @@ Metric::Set(double value)
gauge_ptr->Set(value);
break;
}
case TRITONSERVER_METRIC_KIND_HISTOGRAM: {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED,
"TRITONSERVER_METRIC_KIND_HISTOGRAM does not support Set");
}
default:
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED,
"Unsupported TRITONSERVER_MetricKind");
}

return nullptr; // Success
}

TRITONSERVER_Error*
Metric::Observe(double value)
{
if (metric_ == nullptr) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INTERNAL,
"Could not set metric value. Metric has been invalidated.");
}

switch (kind_) {
case TRITONSERVER_METRIC_KIND_COUNTER: {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED,
"TRITONSERVER_METRIC_KIND_COUNTER does not support Observe");
}
case TRITONSERVER_METRIC_KIND_GAUGE: {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED,
"TRITONSERVER_METRIC_KIND_GAUGE does not support Observe");
}
case TRITONSERVER_METRIC_KIND_HISTOGRAM: {
auto histogram_ptr = reinterpret_cast<prometheus::Histogram*>(metric_);
histogram_ptr->Observe(value);
Expand Down
2 changes: 1 addition & 1 deletion src/test/metrics_api_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ HistogramAPIHelper(
double sum = 0.0;
for (auto datum : data) {
FAIL_TEST_IF_ERR(
TRITONSERVER_MetricSet(metric, datum), "observe metric value");
TRITONSERVER_MetricObserve(metric, datum), "observe metric value");
sum += datum;
}
std::vector<std::uint64_t> cumulative_counts = {1, 1, 2, 2, 3, 3};
Expand Down
11 changes: 11 additions & 0 deletions src/tritonserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3525,6 +3525,17 @@ TRITONSERVER_MetricSet(TRITONSERVER_Metric* metric, double value)
#endif // TRITON_ENABLE_METRICS
}

TRITONSERVER_Error*
TRITONSERVER_MetricObserve(TRITONSERVER_Metric* metric, double value)
{
#ifdef TRITON_ENABLE_METRICS
return reinterpret_cast<tc::Metric*>(metric)->Observe(value);
#else
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED, "metrics not supported");
#endif // TRITON_ENABLE_METRICS
}

TRITONSERVER_Error*
TRITONSERVER_GetMetricKind(
TRITONSERVER_Metric* metric, TRITONSERVER_MetricKind* kind)
Expand Down
5 changes: 5 additions & 0 deletions src/tritonserver_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,11 @@ TRITONSERVER_MetricSet()
{
}

TRITONAPI_DECLSPEC void
TRITONSERVER_MetricObserve()
{
}

TRITONAPI_DECLSPEC void
TRITONSERVER_GetMetricKind()
{
Expand Down

0 comments on commit 349daa6

Please sign in to comment.