Fix empty service.instance.id by deferring node identity injection

The Telemetry object is constructed in ApplicationImp's member initializer
list where nodeIdentity_ is not yet available, resulting in an empty
service.instance.id resource attribute. Add setServiceInstanceId() virtual
method that Application::setup() calls after nodeIdentity_ is known but
before telemetry_->start() creates the OTel resource.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Pratik Mankawde
2026-03-06 19:04:06 +00:00
parent 48a3cf56b8
commit 99a0b4094e
3 changed files with 35 additions and 3 deletions

View File

@@ -108,6 +108,23 @@ public:
virtual ~Telemetry() = default;
/** Update the service instance ID (OTel resource attribute
`service.instance.id`).
Must be called before start(). The node public key is not available
when Telemetry is constructed (during the ApplicationImp member
initializer list), so this setter allows Application::setup() to
inject the identity once nodeIdentity_ is known.
@param id The node's base58-encoded public key or custom identifier.
*/
virtual void
setServiceInstanceId(std::string const& id)
{
// Default no-op for NullTelemetry implementations.
(void)id;
}
/** Initialize the tracing pipeline (exporter, processor, provider).
Call after construction.
*/

View File

@@ -122,8 +122,11 @@ public:
*/
class TelemetryImpl : public Telemetry
{
/** Configuration from the [telemetry] config section. */
Setup const setup_;
/** Configuration from the [telemetry] config section.
Non-const so setServiceInstanceId() can update the instance ID
before start() creates the OTel resource.
*/
Setup setup_;
/** Journal used for log output during start/stop. */
beast::Journal const journal_;
@@ -140,6 +143,12 @@ public:
{
}
void
setServiceInstanceId(std::string const& id) override
{
setup_.serviceInstanceId = id;
}
void
start() override
{

View File

@@ -263,7 +263,7 @@ public:
telemetry::make_Telemetry(
telemetry::setup_Telemetry(
config_->section("telemetry"),
"", // nodePublicKey not yet available at this point
"", // Updated later via setServiceInstanceId()
BuildInfo::getVersionString()),
logs_->journal("Telemetry")))
@@ -1274,6 +1274,12 @@ ApplicationImp::setup(boost::program_options::variables_map const& cmdline)
nodeIdentity_ = getNodeIdentity(*this, cmdline);
// Now that the node identity is known, inject it into the telemetry
// resource attributes. The Telemetry object was constructed with an
// empty serviceInstanceId because nodeIdentity_ is not available in
// the ApplicationImp member initializer list.
telemetry_->setServiceInstanceId(toBase58(TokenType::NodePublic, nodeIdentity_->first));
if (!cluster_->load(config().section(SECTION_CLUSTER_NODES)))
{
JLOG(m_journal.fatal()) << "Invalid entry in cluster configuration.";