Skip to content

Commit

Permalink
Merge pull request redpanda-data#24023 from michael-redpanda/telemetr…
Browse files Browse the repository at this point in the history
…y/core-8141/add-host-name

[CORE-8141] Add host information to metrics report
  • Loading branch information
michael-redpanda authored Nov 7, 2024
2 parents 7d76108 + 99df629 commit 9da76a2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/v/cluster/metrics_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
#include <seastar/core/coroutine.hh>
#include <seastar/core/lowres_clock.hh>
#include <seastar/core/shared_ptr.hh>
#include <seastar/net/dns.hh>
#include <seastar/net/tls.hh>
#include <seastar/util/defer.hh>

#include <absl/algorithm/container.h>
#include <absl/container/node_hash_map.h>
Expand All @@ -54,9 +56,38 @@
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <fmt/core.h>
#include <sys/socket.h>

#include <climits>
#include <netdb.h>
#include <stdexcept>

namespace {
ss::sstring get_hostname() {
std::array<char, HOST_NAME_MAX> hostname{};
if (::gethostname(hostname.data(), hostname.size()) != 0) {
return {};
}

return hostname.data();
}

ss::sstring get_domainname() {
std::array<char, HOST_NAME_MAX> domainname{};
if (::getdomainname(domainname.data(), domainname.size()) != 0) {
return {};
}

return domainname.data();
}

ss::future<std::vector<ss::sstring>> get_fqdns(std::string_view hostname) {
ss::net::dns_resolver resolver;
auto hostent = co_await resolver.get_host_by_name(hostname.data());
co_return hostent.names;
}
} // namespace

namespace cluster {

namespace details {
Expand Down Expand Up @@ -218,6 +249,14 @@ metrics_reporter::build_metrics_snapshot() {
}

metrics.uptime_ms = report->local_state.uptime / 1ms;
auto& advertised_listeners
= nm->get().broker.kafka_advertised_listeners();
metrics.advertised_listeners.reserve(advertised_listeners.size());
std::transform(
advertised_listeners.begin(),
advertised_listeners.end(),
std::back_inserter(metrics.advertised_listeners),
[](const model::broker_endpoint& ep) { return ep.address; });
}
auto& topics = _topics.local().topics_map();
snapshot.topic_count = 0;
Expand Down Expand Up @@ -277,6 +316,10 @@ metrics_reporter::build_metrics_snapshot() {

snapshot.enterprise_features.emplace(std::move(feature_report));

snapshot.host_name = get_hostname();
snapshot.domain_name = get_domainname();
snapshot.fqdns = co_await get_fqdns(snapshot.host_name);

co_return snapshot;
}

Expand Down Expand Up @@ -566,6 +609,15 @@ void rjson_serialize(
w.EndArray();
}

w.Key("hostname");
w.String(snapshot.host_name);

w.Key("domainname");
w.String(snapshot.domain_name);

w.Key("fqdns");
rjson_serialize(w, snapshot.fqdns);

w.EndObject();
}

Expand Down Expand Up @@ -602,6 +654,8 @@ void rjson_serialize(
rjson_serialize(w, d);
}
w.EndArray();
w.Key("kafka_advertised_listeners");
rjson_serialize(w, nm.advertised_listeners);

w.EndObject();
}
Expand Down
6 changes: 6 additions & 0 deletions src/v/cluster/metrics_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "model/metadata.h"
#include "security/fwd.h"
#include "utils/prefix_logger.h"
#include "utils/unresolved_address.h"

#include <seastar/core/abort_source.hh>
#include <seastar/core/gate.hh>
Expand Down Expand Up @@ -58,6 +59,7 @@ class metrics_reporter {
cluster_version logical_version{invalid_version};
std::vector<node_disk_space> disks;
uint64_t uptime_ms{0};
std::vector<net::unresolved_address> advertised_listeners;
};

struct metrics_snapshot {
Expand Down Expand Up @@ -85,6 +87,10 @@ class metrics_reporter {
bool has_valid_license{false};

std::optional<features::enterprise_feature_report> enterprise_features;

ss::sstring host_name;
ss::sstring domain_name;
std::vector<ss::sstring> fqdns;
};
static constexpr ss::shard_id shard = 0;

Expand Down
7 changes: 7 additions & 0 deletions tests/rptest/tests/metrics_reporter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def assert_fields_are_the_same(metadata, field):
assert_fields_are_the_same(metadata, 'has_valid_license')
assert_fields_are_the_same(metadata, 'has_enterprise_features')
assert_fields_are_the_same(metadata, 'enterprise_features')
assert_fields_are_the_same(metadata, 'hostname')
assert_fields_are_the_same(metadata, 'domainname')
assert_fields_are_the_same(metadata, 'fqdns')
# get the last report
last = metadata.pop()
assert last['topic_count'] == total_topics
Expand All @@ -154,6 +157,9 @@ def assert_fields_are_the_same(metadata, field):
assert 'has_enterprise_features' in last
assert 'enterprise_features' in last
assert type(last['enterprise_features']) == list
assert 'hostname' in last
assert 'domainname' in last
assert 'fqdns' in last
nodes_meta = last['nodes']

assert len(last['nodes']) == len(self.redpanda.nodes)
Expand All @@ -165,6 +171,7 @@ def assert_fields_are_the_same(metadata, field):
assert all('uptime_ms' in n for n in nodes_meta)
assert all('is_alive' in n for n in nodes_meta)
assert all('disks' in n for n in nodes_meta)
assert all('kafka_advertised_listeners' in n for n in nodes_meta)

# Check cluster UUID and creation time survive a restart
for n in self.redpanda.nodes:
Expand Down

0 comments on commit 9da76a2

Please sign in to comment.