1- //  Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+ //  Copyright (c) 2022-2024, NVIDIA CORPORATION & AFFILIATES. All rights
2+ //  reserved.
23// 
34//  Redistribution and use in source and binary forms, with or without
45//  modification, are permitted provided that the following conditions
@@ -54,6 +55,12 @@ MetricFamily::MetricFamily(
5455                                             .Help (description)
5556                                             .Register (*registry));
5657      break ;
58+     case  TRITONSERVER_METRIC_KIND_HISTOGRAM:
59+       family_ = reinterpret_cast <void *>(&prometheus::BuildHistogram ()
60+                                              .Name (name)
61+                                              .Help (description)
62+                                              .Register (*registry));
63+       break ;
5764    default :
5865      throw  std::invalid_argument (
5966          " Unsupported kind passed to MetricFamily constructor."  );
@@ -63,24 +70,49 @@ MetricFamily::MetricFamily(
6370}
6471
6572void *
66- MetricFamily::Add (std::map<std::string, std::string> label_map, Metric* metric)
73+ MetricFamily::Add (
74+     std::map<std::string, std::string> label_map, Metric* metric,
75+     const  TritonServerMetricArgs* args)
6776{
6877  void * prom_metric = nullptr ;
6978  switch  (kind_) {
7079    case  TRITONSERVER_METRIC_KIND_COUNTER: {
80+       if  (args != nullptr ) {
81+         throw  std::invalid_argument (
82+             " Unexpected args found in counter Metric constructor."  );
83+       }
7184      auto  counter_family_ptr =
7285          reinterpret_cast <prometheus::Family<prometheus::Counter>*>(family_);
7386      auto  counter_ptr = &counter_family_ptr->Add (label_map);
7487      prom_metric = reinterpret_cast <void *>(counter_ptr);
7588      break ;
7689    }
7790    case  TRITONSERVER_METRIC_KIND_GAUGE: {
91+       if  (args != nullptr ) {
92+         throw  std::invalid_argument (
93+             " Unexpected args found in gauge Metric constructor."  );
94+       }
7895      auto  gauge_family_ptr =
7996          reinterpret_cast <prometheus::Family<prometheus::Gauge>*>(family_);
8097      auto  gauge_ptr = &gauge_family_ptr->Add (label_map);
8198      prom_metric = reinterpret_cast <void *>(gauge_ptr);
8299      break ;
83100    }
101+     case  TRITONSERVER_METRIC_KIND_HISTOGRAM: {
102+       if  (args == nullptr ) {
103+         throw  std::invalid_argument (
104+             " Bucket boundaries not found in Metric args."  );
105+       }
106+       if  (args->kind () != TRITONSERVER_METRIC_KIND_HISTOGRAM) {
107+         throw  std::invalid_argument (" Metric args not set to histogram kind."  );
108+       }
109+       auto  histogram_family_ptr =
110+           reinterpret_cast <prometheus::Family<prometheus::Histogram>*>(family_);
111+       auto  histogram_ptr =
112+           &histogram_family_ptr->Add (label_map, args->buckets ());
113+       prom_metric = reinterpret_cast <void *>(histogram_ptr);
114+       break ;
115+     }
84116    default :
85117      throw  std::invalid_argument (
86118          " Unsupported family kind passed to Metric constructor."  );
@@ -134,6 +166,14 @@ MetricFamily::Remove(void* prom_metric, Metric* metric)
134166      gauge_family_ptr->Remove (gauge_ptr);
135167      break ;
136168    }
169+     case  TRITONSERVER_METRIC_KIND_HISTOGRAM: {
170+       auto  histogram_family_ptr =
171+           reinterpret_cast <prometheus::Family<prometheus::Histogram>*>(family_);
172+       auto  histogram_ptr =
173+           reinterpret_cast <prometheus::Histogram*>(prom_metric);
174+       histogram_family_ptr->Remove (histogram_ptr);
175+       break ;
176+     }
137177    default :
138178      //  Invalid kind should be caught in constructor
139179      LOG_ERROR << " Unsupported kind in Metric destructor."  ;
@@ -169,7 +209,8 @@ MetricFamily::~MetricFamily()
169209// 
170210Metric::Metric (
171211    TRITONSERVER_MetricFamily* family,
172-     std::vector<const  InferenceParameter*> labels)
212+     std::vector<const  InferenceParameter*> labels,
213+     const  TritonServerMetricArgs* args)
173214{
174215  family_ = reinterpret_cast <MetricFamily*>(family);
175216  kind_ = family_->Kind ();
@@ -188,7 +229,7 @@ Metric::Metric(
188229        std::string (reinterpret_cast <const  char *>(param->ValuePointer ()));
189230  }
190231
191-   metric_ = family_->Add (label_map, this );
232+   metric_ = family_->Add (label_map, this , args );
192233}
193234
194235Metric::~Metric ()
@@ -235,6 +276,11 @@ Metric::Value(double* value)
235276      *value = gauge_ptr->Value ();
236277      break ;
237278    }
279+     case  TRITONSERVER_METRIC_KIND_HISTOGRAM: {
280+       return  TRITONSERVER_ErrorNew (
281+           TRITONSERVER_ERROR_UNSUPPORTED,
282+           " TRITONSERVER_METRIC_KIND_HISTOGRAM does not support Value"  );
283+     }
238284    default :
239285      return  TRITONSERVER_ErrorNew (
240286          TRITONSERVER_ERROR_UNSUPPORTED,
@@ -279,6 +325,11 @@ Metric::Increment(double value)
279325      }
280326      break ;
281327    }
328+     case  TRITONSERVER_METRIC_KIND_HISTOGRAM: {
329+       return  TRITONSERVER_ErrorNew (
330+           TRITONSERVER_ERROR_UNSUPPORTED,
331+           " TRITONSERVER_METRIC_KIND_HISTOGRAM does not support Increment"  );
332+     }
282333    default :
283334      return  TRITONSERVER_ErrorNew (
284335          TRITONSERVER_ERROR_UNSUPPORTED,
@@ -308,6 +359,45 @@ Metric::Set(double value)
308359      gauge_ptr->Set (value);
309360      break ;
310361    }
362+     case  TRITONSERVER_METRIC_KIND_HISTOGRAM: {
363+       return  TRITONSERVER_ErrorNew (
364+           TRITONSERVER_ERROR_UNSUPPORTED,
365+           " TRITONSERVER_METRIC_KIND_HISTOGRAM does not support Set"  );
366+     }
367+     default :
368+       return  TRITONSERVER_ErrorNew (
369+           TRITONSERVER_ERROR_UNSUPPORTED,
370+           " Unsupported TRITONSERVER_MetricKind"  );
371+   }
372+ 
373+   return  nullptr ;  //  Success
374+ }
375+ 
376+ TRITONSERVER_Error*
377+ Metric::Observe (double  value)
378+ {
379+   if  (metric_ == nullptr ) {
380+     return  TRITONSERVER_ErrorNew (
381+         TRITONSERVER_ERROR_INTERNAL,
382+         " Could not set metric value. Metric has been invalidated."  );
383+   }
384+ 
385+   switch  (kind_) {
386+     case  TRITONSERVER_METRIC_KIND_COUNTER: {
387+       return  TRITONSERVER_ErrorNew (
388+           TRITONSERVER_ERROR_UNSUPPORTED,
389+           " TRITONSERVER_METRIC_KIND_COUNTER does not support Observe"  );
390+     }
391+     case  TRITONSERVER_METRIC_KIND_GAUGE: {
392+       return  TRITONSERVER_ErrorNew (
393+           TRITONSERVER_ERROR_UNSUPPORTED,
394+           " TRITONSERVER_METRIC_KIND_GAUGE does not support Observe"  );
395+     }
396+     case  TRITONSERVER_METRIC_KIND_HISTOGRAM: {
397+       auto  histogram_ptr = reinterpret_cast <prometheus::Histogram*>(metric_);
398+       histogram_ptr->Observe (value);
399+       break ;
400+     }
311401    default :
312402      return  TRITONSERVER_ErrorNew (
313403          TRITONSERVER_ERROR_UNSUPPORTED,
0 commit comments