fileSink.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2021 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "fileSink.hpp"
  14. #include <cstdio>
  15. #include "platform.h"
  16. using namespace hpccMetrics;
  17. extern "C" MetricSink* getSinkInstance(const char *name, const IPropertyTree *pSettingsTree)
  18. {
  19. MetricSink *pSink = new FileMetricSink(name, pSettingsTree);
  20. return pSink;
  21. }
  22. FileMetricSink::FileMetricSink(const char *name, const IPropertyTree *pSettingsTree) :
  23. PeriodicMetricSink(name, "file", pSettingsTree)
  24. {
  25. pSettingsTree->getProp("@filename", fileName);
  26. clearFileOnStartCollecting = pSettingsTree->getPropBool("@clear", false);
  27. }
  28. void FileMetricSink::prepareToStartCollecting()
  29. {
  30. fhandle = fopen(fileName.str(), clearFileOnStartCollecting ? "w" : "a");
  31. }
  32. void FileMetricSink::doCollection()
  33. {
  34. auto reportMetrics = pManager->queryMetricsForReport(name);
  35. writeReportHeaderToFile();
  36. for (auto &pMetric: reportMetrics)
  37. {
  38. writeMeasurementToFile(pMetric);
  39. }
  40. }
  41. void FileMetricSink::collectingHasStopped()
  42. {
  43. fclose(fhandle);
  44. }
  45. void FileMetricSink::writeMeasurementToFile(const std::shared_ptr<IMetric> &pMetric) const
  46. {
  47. std::string name = pMetric->queryName();
  48. const auto & metaData = pMetric->queryMetaData();
  49. for (auto &metaDataIt: metaData)
  50. {
  51. name.append(".").append(metaDataIt.value);
  52. }
  53. const char *unitsStr = pManager->queryUnitsString(pMetric->queryUnits());
  54. if (unitsStr)
  55. {
  56. name.append(".").append(unitsStr);
  57. }
  58. fprintf(fhandle, " %s -> %" I64F "d, %s\n", name.c_str(), pMetric->queryValue(), pMetric->queryDescription().c_str());
  59. fflush(fhandle);
  60. }
  61. void FileMetricSink::writeReportHeaderToFile() const
  62. {
  63. auto timenow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  64. std::string timeStr(ctime(&timenow));
  65. timeStr.pop_back();
  66. fprintf(fhandle, "------------ Metric Report [%s] ------------\n", timeStr.c_str());
  67. fflush(fhandle);
  68. }