16 ComponentHeightTracer::ComponentHeightTracer()
20 const char* envComponents = std::getenv(
"ODBDESIGN_TRACE_COMPONENTS");
21 if (envComponents !=
nullptr && envComponents[0] !=
'\0')
23 std::istringstream stream(envComponents);
25 while (std::getline(stream, name,
','))
31 m_componentsToTrace.insert(name);
36 const char* envMaxFailures = std::getenv(
"ODBDESIGN_TRACE_MAX_FAILURES");
37 if (envMaxFailures !=
nullptr)
41 m_maxFailuresToTrace = std::stoi(envMaxFailures);
43 if (m_maxFailuresToTrace <= 0)
45 logwarn(
"[ComponentHeightTracer] ODBDESIGN_TRACE_MAX_FAILURES must be positive, using default value 20");
46 m_maxFailuresToTrace = 20;
51 logwarn(
"[ComponentHeightTracer] Failed to parse ODBDESIGN_TRACE_MAX_FAILURES env var, using default value 20");
52 m_maxFailuresToTrace = 20;
60 if (m_componentsToTrace.find(compName) != m_componentsToTrace.end())
70 return m_failureCount.load() < m_maxFailuresToTrace;
75 m_failureCount.fetch_add(1);
78 std::string ComponentHeightTracer::getCompKey(
const std::string &compName,
unsigned int pkgRef)
const
81 ss << compName <<
"(Package=" << pkgRef <<
")";
85 std::string ComponentHeightTracer::formatLookupTable(
const std::map<std::string, std::string> &lookupTable)
const
90 for (
const auto &kv : lookupTable)
94 ss <<
"\"" << kv.first <<
"\":\"" << kv.second <<
"\"";
101 std::string ComponentHeightTracer::formatAttributeNames(
const std::vector<std::string> &attributeNames)
const
103 std::stringstream ss;
105 for (
size_t i = 0; i < attributeNames.size(); ++i)
109 ss << i <<
":\"" << attributeNames[i] <<
"\"";
120 std::stringstream ss;
121 ss <<
"[CompHeightTrace][PARSE_START] Component=" << compName
122 <<
", Package=" << pkgRef
123 <<
", attrIdString=\"" << attrIdString <<
"\"";
128 const std::map<std::string, std::string> &lookupTable,
129 const std::vector<std::string> &attributeNames)
const
134 std::stringstream ss;
135 ss <<
"[CompHeightTrace][PARSE_RESULT] Component=" << compName
136 <<
", Package=" << pkgRef
137 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
138 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
141 bool hasCompHeightAttr = !attributeNames.empty() && attributeNames[0] ==
".comp_height";
142 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
144 ss <<
", .comp_height@index0=" << (hasCompHeightAttr ?
"YES" :
"NO")
145 <<
", key\"0\"_exists=" << (hasKey0 ?
"YES" :
"NO");
147 if (hasCompHeightAttr && !hasKey0)
149 ss <<
" ⚠️ MISSING_KEY_0";
154 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
161 const std::map<std::string, std::string> &lookupTable,
162 const std::vector<std::string> &attributeNames)
const
167 std::stringstream ss;
168 ss <<
"[CompHeightTrace][SERIALIZE] Component=" << compName
169 <<
", Package=" << pkgRef
170 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
171 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
173 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
176 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
180 ss <<
", key\"0\"_missing";
187 const std::map<std::string, std::string> &lookupTable,
188 const std::vector<std::string> &attributeNames)
const
193 std::stringstream ss;
194 ss <<
"[CompHeightTrace][GRPC_RESPONSE] Component=" << compName
195 <<
", Package=" << pkgRef
196 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
197 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
199 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
202 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
206 ss <<
", key\"0\"_missing";
213 const std::map<std::string, std::string> &lookupTable,
214 const std::vector<std::string> &attributeNames)
const
219 std::stringstream ss;
220 ss <<
"[CompHeightTrace][REST_RESPONSE] Component=" << compName
221 <<
", Package=" << pkgRef
222 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
223 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
225 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
228 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
232 ss <<
", key\"0\"_missing";
240 m_componentsToTrace.insert(compName);
245 m_maxFailuresToTrace = maxFailures;
static ComponentHeightTracer & instance()
void addComponentToTrace(const std::string &compName)
bool shouldTrace(const std::string &compName, unsigned int pkgRef) const
void setMaxFailuresToTrace(int maxFailures)
void logParseStart(const std::string &compName, unsigned int pkgRef, const std::string &attrIdString) const
bool shouldTraceFailure() const
void logParseResult(const std::string &compName, unsigned int pkgRef, const std::map< std::string, std::string > &lookupTable, const std::vector< std::string > &attributeNames) const
void logGrpcResponse(const std::string &compName, unsigned int pkgRef, const std::map< std::string, std::string > &lookupTable, const std::vector< std::string > &attributeNames) const
void logRestResponse(const std::string &compName, unsigned int pkgRef, const std::map< std::string, std::string > &lookupTable, const std::vector< std::string > &attributeNames) const
void logSerialization(const std::string &compName, unsigned int pkgRef, const std::map< std::string, std::string > &lookupTable, const std::vector< std::string > &attributeNames) const
std::string & str_trim(std::string &s)