Refactor exporter files: reduce noise and improve structure#54
Refactor exporter files: reduce noise and improve structure#54iskakaushik merged 5 commits intomainfrom
Conversation
…Tel init stats_exporter.cc: - Remove 17 DEBUG2/DEBUG3 elog calls (bringup scaffolding) - Add ClampFieldLen<T> template to replace 3 inline clamping blocks - Remove redundant section-header comments and event_idx counter otel_exporter.cc: - Split EstablishNewConnection into CreateResource(), InitMetricsPipeline(), and InitLogPipeline() private helpers - Trim 13-line CommitBatch comment to 2-line summary event.h: - Add PSCH_MAX_APP_NAME_LEN and PSCH_MAX_CLIENT_ADDR_LEN constants Net -61 lines, no behavioral changes.
There was a problem hiding this comment.
Pull request overview
Refactors the exporter implementation to reduce debug-log noise, improve code structure, and replace magic numbers with named constants used when exporting queue events.
Changes:
- Introduces a reusable
ClampFieldLen<T>helper and uses it for query/error/app/client length clamping instats_exporter.cc. - Splits OTel connection setup into
CreateResource(),InitMetricsPipeline(), andInitLogPipeline()helpers inotel_exporter.cc. - Adds named constants for application name and client address max lengths in
event.h.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/queue/event.h | Adds max-length constants intended to replace magic numbers for fixed-size string fields. |
| src/export/stats_exporter.cc | Removes noisy DEBUG2/DEBUG3 logging and refactors repeated length-clamping logic into a helper. |
| src/export/otel_exporter.cc | Refactors EstablishNewConnection() into smaller helpers and trims an overly long comment. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/queue/event.h
Outdated
| // Maximum application name length (NAMEDATALEN - 1) | ||
| #define PSCH_MAX_APP_NAME_LEN 63 | ||
|
|
||
| // Maximum client address length (INET6_ADDRSTRLEN - 1) | ||
| #define PSCH_MAX_CLIENT_ADDR_LEN 45 |
There was a problem hiding this comment.
These constants are described as derived from NAMEDATALEN/INET6_ADDRSTRLEN, but the values are hard-coded (63/45). This can silently drift if those upstream macros change (or if the struct field sizes change). Prefer defining them in terms of the actual macros (e.g., NAMEDATALEN - 1 and INET6_ADDRSTRLEN - 1) so the code and comment can’t diverge.
| LenT ClampFieldLen(LenT len, LenT max, const char* field_name) { | ||
| if (len <= max) | ||
| return len; | ||
| elog(WARNING, "pg_stat_ch: invalid %s %u, clamping", field_name, static_cast<unsigned>(len)); |
There was a problem hiding this comment.
ClampFieldLen() warning message is missing key context: it doesn’t report the maximum allowed value, and it no longer includes any event identifier (e.g., pid/query_id). That makes diagnosing corrupted/invalid events much harder. Consider logging both the observed len and the max (and optionally pid/query_id from the caller) in the WARNING.
| LenT ClampFieldLen(LenT len, LenT max, const char* field_name) { | |
| if (len <= max) | |
| return len; | |
| elog(WARNING, "pg_stat_ch: invalid %s %u, clamping", field_name, static_cast<unsigned>(len)); | |
| LenT ClampFieldLen(LenT len, LenT max, const char* field_name, const char* event_context = nullptr) { | |
| if (len <= max) | |
| return len; | |
| if (event_context != nullptr && event_context[0] != '\0') { | |
| elog(WARNING, | |
| "pg_stat_ch: invalid %s len=%llu max=%llu, clamping (%s)", | |
| field_name, | |
| static_cast<unsigned long long>(len), | |
| static_cast<unsigned long long>(max), | |
| event_context); | |
| } else { | |
| elog(WARNING, | |
| "pg_stat_ch: invalid %s len=%llu max=%llu, clamping", | |
| field_name, | |
| static_cast<unsigned long long>(len), | |
| static_cast<unsigned long long>(max)); | |
| } |
| col_db->Append(std::string(ev.datname, ev.datname_len)); | ||
| col_username->Append(std::string(ev.username, ev.username_len)); |
There was a problem hiding this comment.
datname_len and username_len are used directly to size std::string, but they are not validated against the fixed buffers (datname[64], username[64]). If an event ever contains an out-of-range length, this becomes an out-of-bounds read (and potentially data exfiltration/crash). Since you’re already clamping other variable-length fields, please clamp these too (e.g., to sizeof(ev.datname)-1 / sizeof(ev.username)-1) before constructing strings.
| col_db->Append(std::string(ev.datname, ev.datname_len)); | |
| col_username->Append(std::string(ev.username, ev.username_len)); | |
| auto dlen = | |
| ClampFieldLen(ev.datname_len, static_cast<uint8>(sizeof(ev.datname) - 1), "datname_len"); | |
| auto ulen = ClampFieldLen(ev.username_len, static_cast<uint8>(sizeof(ev.username) - 1), | |
| "username_len"); | |
| col_db->Append(std::string(ev.datname, dlen)); | |
| col_username->Append(std::string(ev.username, ulen)); |
| logs_sdk::BatchLogRecordProcessorOptions batch_opts; | ||
| batch_opts.max_queue_size = psch_otel_log_queue_size; | ||
| batch_opts.max_export_batch_size = | ||
| std::min(static_cast<size_t>(psch_otel_log_batch_size), batch_size_by_bytes); | ||
| batch_opts.schedule_delay_millis = std::chrono::milliseconds(psch_otel_log_delay_ms); |
There was a problem hiding this comment.
InitLogPipeline() uses std::min but this file doesn’t include . It may compile today via transitive includes, but that’s not guaranteed; please add an explicit #include to make the dependency clear and robust.
e5b2015 to
aadbe0b
Compare
Add LogExporterWarning() bridge in exporter_interface.h — otel_exporter.cc cannot include postgres.h (macro conflicts with libintl.h via gRPC headers), so this bridges to ereport(WARNING) defined in stats_exporter.cc.
aadbe0b to
019f8ca
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Philip Dubé <159546+serprex@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- event.h: define INET6_ADDRSTRLEN (46) if not already provided, avoiding dependency on <arpa/inet.h> which is missing in some build environments - hostname_test.cc: stub LogExporterWarning to fix linker error
Summary
ClampFieldLen<T>template to replace 3 inline clamping blocks, remove redundant comments andevent_idxcounterEstablishNewConnectionintoCreateResource(),InitMetricsPipeline(),InitLogPipeline()private helpers; trim verboseCommitBatchcommentPSCH_MAX_APP_NAME_LENandPSCH_MAX_CLIENT_ADDR_LENconstants to replace magic numbersNet -61 lines, no behavioral changes.
Test plan
ninjacompiles both .cc files cleanly)mise run test:regress— SQL regression testsmise run test:tap— TAP tests against install_tap build🤖 Generated with Claude Code