You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by GitBox <gi...@apache.org> on 2022/10/19 11:11:59 UTC

[GitHub] [incubator-brpc] ldak4747 commented on issue #1956: 支持MultiDimension后,如果想获取1秒内,该metric的各个label的"求和"/"均值",应该怎么做?

ldak4747 commented on issue #1956:
URL: https://github.com/apache/incubator-brpc/issues/1956#issuecomment-1283839476

   我这两天的实践发现不太明白,我贴下代码:
   
   ////// 1、基类,定义了一个多维度的metric,::bvar::MultiDimension<Recorder>类型
   template<class Recorder>
   class MBvarBaseInstance {
   public:
       using MdRecorder = ::bvar::MultiDimension<Recorder>;
       MBvarBaseInstance(
           const std::string &metric_name, const std::list<std::string> &labels)
           : metric_name_(metric_name) {
           recorder_md_.reset(
               new ::bvar::MultiDimension<Recorder>(metric_name, labels));
       }
   
       const std::string &GetMetricName() const { return metric_name_; }
   
       void EventRecordIncr(const std::list<std::string> &label_value) const {
           if (!recorder_md_) { return; }
           Recorder *recorder = GetLabelRecorder(label_value);
           if (recorder) { (*recorder) << 1; }
       }
   
       Recorder *GetLabelRecorder(const std::list<std::string> &label_value) const {
           if (!recorder_md_) { return nullptr; }
           return recorder_md_->get_stats(label_value);
       }
   
   private:
       std::string metric_name_;
       std::unique_ptr<MdRecorder> recorder_md_;
   };
   
   
   /////// 2、用于生成多维度的window实例的类型,通过具体的labels,获取对应的adder,并根据adder生成::bvar::Window对象
   /////// 也就是,每个维度取值,对应一个本类的对象:含有该label对应的adder,还含有根据此adder生成的::bvar::Window对象
   template<class Recorder>
   class MBvarInstance {
   public:
       using WindowType = ::bvar::Window<Recorder, ::bvar::SERIES_IN_SECOND>;
       MBvarInstance() = default;
       MBvarInstance(const MBvarBaseInstance<Recorder> &base_instance,
                     const std::list<std::string> &label_values,
                     time_t window_size = 1) {
           recorder_ = base_instance.GetLabelRecorder(label_values);
           if (window_size > 0 && recorder_) {
               window_.reset(
               new WindowType(base_instance.GetMetricName(), recorder_, window_size));
           }
       }
       ~MBvarInstance() { recorder_ = nullptr; }
   
       template<class DataType>
       MBvarInstance<Recorder> &operator<<(DataType value) {
           if (recorder_) {
               (*recorder_) << value;
           }
   
           return *this;
       }
   
   private:
       Recorder *recorder_ = nullptr;
       std::unique_ptr<WindowType> window_;
   };
   
   
   
   
   ////// 3、全局变量index_req_md_stater,它只有一种类型的label
   using SumWindowRecorder = ::bvar::Adder<uint64_t>;
   MBvarBaseInstance<SumWindowRecorder> index_req_md_stater(
       "index_req_cnt", {"index_name"});
   
   //////// 4、针对"index_1",生产该label取值,对应的"含adder和::bvar::Window对象"的MBvarInstance实例
   std::list<std::string> label = {"index_1"};
   MBvarInstance<SumWindowRecorder> index_req_handle(index_req_md_stater, label);
   
   /////// 5、MBvarInstance实例,自增这个计数
   index_req_handle << 1
   
   
   
   //////// 6、curl 127.0.0.1:8080/brpc_metrics | grep -E 'index_req_cnt|index_1'
   为空,也就是多维的metric"index_req_cnt",及其label"index_1",都没有获取到
   
   
   
   
   
   是不是我的用法有问题?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org