Skip to content

Commit

Permalink
Disallow histogram.set, counter.observe, gauge.observe
Browse files Browse the repository at this point in the history
  • Loading branch information
yinggeh committed Aug 14, 2024
1 parent 58eed78 commit 5265d47
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/metric.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,36 @@ Metric::Increment(const double& value)
void
Metric::SetValue(const double& value)
{
// SetValue and Observe share the same C API TRITONSERVER_MetricSet.
// Throws if SetValue is called by a histogram metric.
TRITONSERVER_MetricKind kind;
THROW_IF_TRITON_ERROR(TRITONSERVER_GetMetricKind(
reinterpret_cast<TRITONSERVER_Metric*>(metric_address_), &kind));
if (kind == TRITONSERVER_METRIC_KIND_HISTOGRAM) {
throw PythonBackendException(
"TRITONSERVER_METRIC_KIND_HISTOGRAM does not support SetValue");
}

auto triton_metric = reinterpret_cast<TRITONSERVER_Metric*>(metric_address_);
THROW_IF_TRITON_ERROR(TRITONSERVER_MetricSet(triton_metric, value));
}

void
Metric::Observe(const double& value)
{
// SetValue and Observe share the same C API TRITONSERVER_MetricSet.
// Throws if Observe is called by a non-histogram metric.
TRITONSERVER_MetricKind kind;
THROW_IF_TRITON_ERROR(TRITONSERVER_GetMetricKind(
reinterpret_cast<TRITONSERVER_Metric*>(metric_address_), &kind));
if (kind == TRITONSERVER_METRIC_KIND_COUNTER) {
throw PythonBackendException(
"TRITONSERVER_METRIC_KIND_COUNTER does not support Observe");
} else if (kind == TRITONSERVER_METRIC_KIND_GAUGE) {
throw PythonBackendException(
"TRITONSERVER_METRIC_KIND_GAUGE does not support Observe");
}

auto triton_metric = reinterpret_cast<TRITONSERVER_Metric*>(metric_address_);
THROW_IF_TRITON_ERROR(TRITONSERVER_MetricSet(triton_metric, value));
}
Expand Down
4 changes: 2 additions & 2 deletions src/metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ class Metric {

// The labels of the metric, which is the identifier of the metric.
std::string labels_;
// Monotonically increasing values representing the
// bucket boundaries. For histogram only.
// Monotonically increasing values representing bucket boundaries for creating
// histogram metric.
std::optional<std::vector<double>> buckets_;
// The value used for incrementing or setting the metric.
double operation_value_;
Expand Down
4 changes: 2 additions & 2 deletions src/metric_family.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class MetricFamily {

/// Create a metric from the metric family and store it in the metric map.
/// \param labels The labels of the metric.
/// \param buckets Monotonically increasing values representing the
/// bucket boundaries. For histogram only.
/// \param buckets Monotonically increasing values representing bucket
/// boundaries for creating histogram metric.
/// \return Returns the shared pointer to the created metric.
std::shared_ptr<Metric> CreateMetric(
const py::object& labels, const py::object& buckets);
Expand Down

0 comments on commit 5265d47

Please sign in to comment.