metrics_registry.cpp 1.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2021/4/20.
//

#include "common/metrics/metrics_registry.h"
#include "common/log/log.h"

namespace common {

20 21
MetricsRegistry &get_metrics_registry()
{
羽飞's avatar
羽飞 已提交
22 23 24 25 26
  static MetricsRegistry instance;

  return instance;
}

27 28 29
void MetricsRegistry::register_metric(const std::string &tag, Metric *metric)
{
  std::map<std::string, Metric *>::iterator it = metrics.find(tag);
羽飞's avatar
羽飞 已提交
30 31 32 33 34
  if (it != metrics.end()) {
    LOG_WARN("%s has been registered!", tag.c_str());
    return;
  }

35
  // metrics[tag] = metric;
羽飞's avatar
羽飞 已提交
36 37 38 39
  metrics.insert(std::pair<std::string, Metric *>(tag, metric));
  LOG_INFO("Successfully register metric :%s", tag.c_str());
}

40 41
void MetricsRegistry::unregister(const std::string &tag)
{
羽飞's avatar
羽飞 已提交
42 43 44 45 46 47 48 49
  unsigned int num = metrics.erase(tag);
  if (num == 0) {
    LOG_WARN("There is no %s metric!", tag.c_str());
    return;
  }
  LOG_INFO("Successfully remove metric of %s", tag.c_str());
}

50 51 52
void MetricsRegistry::snapshot()
{
  std::map<std::string, Metric *>::iterator it = metrics.begin();
羽飞's avatar
羽飞 已提交
53 54 55 56 57
  for (; it != metrics.end(); it++) {
    it->second->snapshot();
  }
}

58 59 60 61
void MetricsRegistry::report()
{
  for (std::list<Reporter *>::iterator reporterIt = reporters.begin(); reporterIt != reporters.end(); reporterIt++) {
    for (std::map<std::string, Metric *>::iterator it = metrics.begin(); it != metrics.end(); it++) {
羽飞's avatar
羽飞 已提交
62 63 64 65 66 67

      (*reporterIt)->report(it->first, it->second);
    }
  }
}

68
}  // namespace common