Skip to content

Commit

Permalink
otel: set the $SOURCEIP to the IP of the gRPC peer
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Feb 4, 2024
1 parent a9c807f commit fa3ff2e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
32 changes: 24 additions & 8 deletions modules/grpc/otel/otel-protobuf-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,35 @@ _add_repeated_KeyValue_fields(LogMessage *msg, const char *key, const RepeatedPt
_add_repeated_KeyValue_fields_with_prefix(msg, key_buffer, 0, key, key_values);
}

static std::string
_extract_hostname(const grpc::string &peer)
static GSockAddr *
_extract_saddr(const grpc::string &peer)
{
size_t first = peer.find_first_of(':');
size_t last = peer.find_last_of(':');

/* expected format: ipv6:[::1]:32768 or ipv4:1.2.3.4:32768 */
if (first != grpc::string::npos && last != grpc::string::npos)
return peer.substr(first + 1, last - first - 1);
{
const std::string ip_version = peer.substr(0, first);
std::string host;
int port = std::stoi(peer.substr(last + 1, grpc::string::npos), nullptr, 10);

if (peer.at(first + 1) == '[')
host = peer.substr(first + 2, last - first - 3);
else
host = peer.substr(first + 1, last - first - 1);

if (ip_version.compare("ipv6") == 0)
{
return g_sockaddr_inet6_new(host.c_str(), port);
}
else if (ip_version.compare("ipv4") == 0)
{
return g_sockaddr_inet_new(host.c_str(), port);
}
}

return "";
return NULL;
}

static bool
Expand Down Expand Up @@ -1091,10 +1110,7 @@ syslogng::grpc::otel::ProtobufParser::store_raw_metadata(LogMessage *msg, const
{
std::string serialized;

/* HOST */
std::string hostname = _extract_hostname(peer);
if (hostname.length())
log_msg_set_value(msg, LM_V_HOST, hostname.c_str(), hostname.length());
msg->saddr = _extract_saddr(peer);

/* .otel_raw.resource */
resource.SerializePartialToString(&serialized);
Expand Down
9 changes: 7 additions & 2 deletions modules/grpc/otel/tests/test-otel-protobuf-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ _create_dummy_log_msg()
{
LogMessage *msg = log_msg_new_empty();

grpc::string peer = "ipv6:[::1]:36372";
grpc::string peer = "ipv4:127.0.0.5:36372";
Resource resource;
std::string resource_schema_url = "dummy_resource_schema_url";
InstrumentationScope scope;
Expand Down Expand Up @@ -149,7 +149,8 @@ Test(otel_protobuf_parser, metadata)
ProtobufParser::store_raw(msg, LogRecord());
ProtobufParser().process(msg);

_assert_log_msg_value(msg, "HOST", "[::1]", -1, LM_VT_STRING);
cr_assert(msg->saddr != NULL);
_assert_log_msg_value(msg, "SOURCEIP", "::1", -1, LM_VT_STRING);

_assert_log_msg_value(msg, ".otel.resource.attributes.null_key", "", -1, LM_VT_NULL);
_assert_log_msg_value(msg, ".otel.resource.attributes.string_key", "string_attribute", -1, LM_VT_STRING);
Expand Down Expand Up @@ -206,6 +207,10 @@ Test(otel_protobuf_parser, log_record)
ProtobufParser::store_raw(msg, log_record);
cr_assert(ProtobufParser().process(msg));

cr_assert(msg->saddr != NULL);

_assert_log_msg_value(msg, "SOURCEIP", "127.0.0.5", -1, LM_VT_STRING);

_assert_log_msg_value(msg, ".otel.type", "log", -1, LM_VT_STRING);
_assert_log_msg_value(msg, ".otel.log.time_unix_nano", "111000222000", -1, LM_VT_INTEGER);
_assert_log_msg_value(msg, ".otel.log.observed_time_unix_nano", "333000444000", -1, LM_VT_INTEGER);
Expand Down

0 comments on commit fa3ff2e

Please sign in to comment.