artdaq  v3_12_02
ArtdaqMetric_mfPlugin.cc
1 #include "artdaq/DAQdata/Globals.hh"
2 
3 #include "cetlib/PluginTypeDeducer.h"
4 #include "cetlib/ProvideMakePluginMacros.h"
5 #include "fhiclcpp/ParameterSet.h"
6 #include "fhiclcpp/types/ConfigurationTable.h"
7 
8 #include "cetlib/compiler_macros.h"
9 #include "messagefacility/MessageService/ELdestination.h"
10 #include "messagefacility/Utilities/ELseverityLevel.h"
11 #include "messagefacility/Utilities/exception.h"
12 
13 #define TRACE_NAME "ArtdaqMetric"
14 
15 namespace mfplugins {
16 using mf::ELseverityLevel;
17 using mf::ErrorObj;
18 using mf::service::ELdestination;
19 
23 class ELArtdaqMetric : public ELdestination
24 {
25 public:
29  struct Config
30  {
32  fhicl::TableFragment<ELdestination::Config> elDestConfig;
34  fhicl::Atom<bool> showDebug =
35  fhicl::Atom<bool>{fhicl::Name{"showDebug"}, fhicl::Comment{"Send Metrics for Debug messages"}, false};
37  fhicl::Atom<bool> showInfo =
38  fhicl::Atom<bool>{fhicl::Name{"showInfo"}, fhicl::Comment{"Send Metrics for Info messages"}, false};
40  fhicl::Atom<bool> showWarning =
41  fhicl::Atom<bool>{fhicl::Name{"showWarning"}, fhicl::Comment{"Send Metrics for Warning messages"}, true};
43  fhicl::Atom<bool> showError =
44  fhicl::Atom<bool>{fhicl::Name{"showError"}, fhicl::Comment{"Send Metrics for Error messages"}, true};
46  fhicl::Atom<size_t> messageLength = fhicl::Atom<size_t>(fhicl::Name{"messageLength"}, fhicl::Comment{"Maximum length of metric titles (0 for unlimited)"}, 40);
48  fhicl::Atom<size_t> metricLevelOffset = fhicl::Atom<size_t>(fhicl::Name{"metricLevelOffset"}, fhicl::Comment{"Offset for Metric Levels (+0: summary rates, +1 errors, ...)"}, 10);
49  };
51  using Parameters = fhicl::WrappedTable<Config>;
52 
53 public:
58  ELArtdaqMetric(Parameters const& pset);
59 
65  void fillPrefix(std::ostringstream& o, const ErrorObj& msg) override;
66 
72  void fillUsrMsg(std::ostringstream& o, const ErrorObj& msg) override;
73 
77  void fillSuffix(std::ostringstream& /*unused*/, const ErrorObj& /*msg*/) override {}
78 
84  void routePayload(const std::ostringstream& o, const ErrorObj& msg) override;
85 
86 private:
87  std::string makeMetricName_(const ErrorObj& msg);
88  bool showDebug_{false};
89  bool showInfo_{false};
90  bool showWarning_{true};
91  bool showError_{true};
92  size_t messageLength_{20};
93  size_t metricLevelOffset_{10};
94 };
95 
96 // END DECLARATION
97 //======================================================================
98 // BEGIN IMPLEMENTATION
99 
100 //======================================================================
101 // ELArtdaqMetric c'tor
102 //======================================================================
104  : ELdestination(pset().elDestConfig())
105  , showDebug_(pset().showDebug())
106  , showInfo_(pset().showInfo())
107  , showWarning_(pset().showWarning())
108  , showError_(pset().showError())
109  , messageLength_(pset().messageLength())
110  , metricLevelOffset_(pset().metricLevelOffset())
111 {
112  TLOG(TLVL_INFO) << "ELArtdaqMetric MessageLogger destination plugin initialized.";
113 }
114 
115 //======================================================================
116 // Message prefix filler ( overriddes ELdestination::fillPrefix )
117 //======================================================================
118 void ELArtdaqMetric::fillPrefix(std::ostringstream&, const ErrorObj&)
119 {
120 }
121 
122 //======================================================================
123 // Message filler ( overriddes ELdestination::fillUsrMsg )
124 //======================================================================
125 void ELArtdaqMetric::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg)
126 {
127  std::ostringstream tmposs;
128  ELdestination::fillUsrMsg(tmposs, msg);
129 
130  // remove "\n" if present
131  std::string tmpStr = tmposs.str();
132  auto cpos = tmpStr.find(':');
133  if (cpos != std::string::npos)
134  {
135  tmpStr.erase(0, cpos + 1);
136  }
137 
138  // remove numbers
139  std::string usrMsg;
140  std::copy_if(tmpStr.begin(), tmpStr.end(),
141  std::back_inserter(usrMsg), isalpha);
142 
143  if (messageLength_ > 0 && usrMsg.size() > messageLength_)
144  {
145  usrMsg.resize(messageLength_);
146  }
147 
148  oss << usrMsg;
149 }
150 
151 //======================================================================
152 // Message router ( overriddes ELdestination::routePayload )
153 //======================================================================
154 void ELArtdaqMetric::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
155 {
156  const auto& xid = msg.xid();
157  auto message = oss.str();
158 
159  auto level = xid.severity().getLevel();
160  int lvlNum = -1;
161  bool sendMessageMetric = false;
162 
163  switch (level)
164  {
165  case mf::ELseverityLevel::ELsev_success:
166  case mf::ELseverityLevel::ELsev_zeroSeverity:
167  case mf::ELseverityLevel::ELsev_unspecified:
168  sendMessageMetric = showDebug_;
169  lvlNum = 3;
170  break;
171 
172  case mf::ELseverityLevel::ELsev_info:
173  sendMessageMetric = showInfo_;
174  lvlNum = 2;
175  break;
176 
177  case mf::ELseverityLevel::ELsev_warning:
178  sendMessageMetric = showWarning_;
179  lvlNum = 1;
180  break;
181  default:
182  sendMessageMetric = showError_;
183  lvlNum = 0;
184  break;
185  }
186 
187  if (metricMan)
188  {
189  if (sendMessageMetric)
190  {
191  metricMan->sendMetric(message, 1, "messages", lvlNum + metricLevelOffset_ + 1, artdaq::MetricMode::Rate);
192  }
193  switch (lvlNum)
194  {
195  case 0:
196  metricMan->sendMetric("Error Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
197  break;
198  case 1:
199  metricMan->sendMetric("Warning Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
200  break;
201  case 2:
202  metricMan->sendMetric("Info Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
203  break;
204  case 3:
205  metricMan->sendMetric("Debug Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
206  break;
207  }
208  }
209 }
210 } // end namespace mfplugins
211 
212 //======================================================================
213 //
214 // makePlugin function
215 //
216 //======================================================================
217 
218 #ifndef EXTERN_C_FUNC_DECLARE_START
219 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
220 #endif
221 
222 EXTERN_C_FUNC_DECLARE_START
223 auto makePlugin(const std::string& /*unused*/, const fhicl::ParameterSet& pset)
224 {
225  return std::make_unique<mfplugins::ELArtdaqMetric>(pset);
226 }
227 }
228 
229 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
Message Facility destination which logs messages to a TRACE buffer
void fillUsrMsg(std::ostringstream &o, const ErrorObj &msg) override
Fill the &quot;User Message&quot; portion of the message.
void routePayload(const std::ostringstream &o, const ErrorObj &msg) override
Serialize a MessageFacility message to the output.
fhicl::Atom< bool > showInfo
&quot;showInfo&quot; (Default: False): Send metrics for Info messages
ELArtdaqMetric(Parameters const &pset)
ELArtdaqMetric Constructor
fhicl::Atom< bool > showError
&quot;showError&quot; (Default: true): Send metrics for Error messages
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common parameters.
fhicl::Atom< size_t > messageLength
&quot;messageLength&quot; (Default: 40): Number of characters to use for metric title
void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the &quot;Suffix&quot; portion of the message (Unused)
Configuration Parameters for ELArtdaqMetric.
fhicl::Atom< bool > showDebug
&quot;showDebug&quot; (Default: False): Send metrics for Debug messages
fhicl::Atom< bool > showWarning
&quot;showWarning&quot; (Default: true): Send metrics for Warning messages
fhicl::Atom< size_t > metricLevelOffset
&quot;metricLevelOffset&quot; (Default: 10): Offset for Metric Levels (+0: summary rates, +1 errors...
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.
void fillPrefix(std::ostringstream &o, const ErrorObj &msg) override
Fill the &quot;Prefix&quot; portion of the message.