Skip to content

Commit 5265d47

Browse files
committed
Disallow histogram.set, counter.observe, gauge.observe
1 parent 58eed78 commit 5265d47

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/metric.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,36 @@ Metric::Increment(const double& value)
358358
void
359359
Metric::SetValue(const double& value)
360360
{
361+
// SetValue and Observe share the same C API TRITONSERVER_MetricSet.
362+
// Throws if SetValue is called by a histogram metric.
363+
TRITONSERVER_MetricKind kind;
364+
THROW_IF_TRITON_ERROR(TRITONSERVER_GetMetricKind(
365+
reinterpret_cast<TRITONSERVER_Metric*>(metric_address_), &kind));
366+
if (kind == TRITONSERVER_METRIC_KIND_HISTOGRAM) {
367+
throw PythonBackendException(
368+
"TRITONSERVER_METRIC_KIND_HISTOGRAM does not support SetValue");
369+
}
370+
361371
auto triton_metric = reinterpret_cast<TRITONSERVER_Metric*>(metric_address_);
362372
THROW_IF_TRITON_ERROR(TRITONSERVER_MetricSet(triton_metric, value));
363373
}
364374

365375
void
366376
Metric::Observe(const double& value)
367377
{
378+
// SetValue and Observe share the same C API TRITONSERVER_MetricSet.
379+
// Throws if Observe is called by a non-histogram metric.
380+
TRITONSERVER_MetricKind kind;
381+
THROW_IF_TRITON_ERROR(TRITONSERVER_GetMetricKind(
382+
reinterpret_cast<TRITONSERVER_Metric*>(metric_address_), &kind));
383+
if (kind == TRITONSERVER_METRIC_KIND_COUNTER) {
384+
throw PythonBackendException(
385+
"TRITONSERVER_METRIC_KIND_COUNTER does not support Observe");
386+
} else if (kind == TRITONSERVER_METRIC_KIND_GAUGE) {
387+
throw PythonBackendException(
388+
"TRITONSERVER_METRIC_KIND_GAUGE does not support Observe");
389+
}
390+
368391
auto triton_metric = reinterpret_cast<TRITONSERVER_Metric*>(metric_address_);
369392
THROW_IF_TRITON_ERROR(TRITONSERVER_MetricSet(triton_metric, value));
370393
}

src/metric.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ class Metric {
166166

167167
// The labels of the metric, which is the identifier of the metric.
168168
std::string labels_;
169-
// Monotonically increasing values representing the
170-
// bucket boundaries. For histogram only.
169+
// Monotonically increasing values representing bucket boundaries for creating
170+
// histogram metric.
171171
std::optional<std::vector<double>> buckets_;
172172
// The value used for incrementing or setting the metric.
173173
double operation_value_;

src/metric_family.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ class MetricFamily {
9797

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

0 commit comments

Comments
 (0)