Skip to content

Commit 9a374c4

Browse files
authored
Merge pull request syslog-ng#4954 from MrAnno/otel-source-hostname
otel: hostname handling
2 parents b49b271 + 8c72617 commit 9a374c4

7 files changed

+56
-22
lines changed

modules/grpc/otel/otel-grammar.ym

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ GrpcClientCredentialsBuilderW *last_grpc_client_credentials_builder;
7474
%token KW_BATCH_BYTES
7575
%token KW_CONCURRENT_REQUESTS
7676
%token KW_CHANNEL_ARGS
77+
%token KW_SET_HOSTNAME
7778

7879
%type <ptr> source_otel
7980
%type <ptr> parser_otel
@@ -143,6 +144,7 @@ parser_otel_options
143144

144145
parser_otel_option
145146
: parser_opt
147+
| KW_SET_HOSTNAME '(' yesno ')' { otel_protobuf_parser_set_hostname(last_parser, $3); }
146148
;
147149

148150
destination_otel
@@ -294,4 +296,3 @@ grpc_client_credentials_builder_alts_target_service_accounts
294296
/* INCLUDE_RULES */
295297

296298
%%
297-

modules/grpc/otel/otel-parser.c

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static CfgLexerKeyword otel_keywords[] =
5252
{ "batch_bytes", KW_BATCH_BYTES },
5353
{ "concurrent_requests", KW_CONCURRENT_REQUESTS },
5454
{ "channel_args", KW_CHANNEL_ARGS },
55+
{ "set_hostname", KW_SET_HOSTNAME },
5556
{ NULL }
5657
};
5758

modules/grpc/otel/otel-protobuf-parser.cpp

+33-21
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,23 @@ _add_repeated_KeyValue_fields(LogMessage *msg, const char *key, const RepeatedPt
218218
_add_repeated_KeyValue_fields_with_prefix(msg, key_buffer, 0, key, key_values);
219219
}
220220

221-
static std::string
222-
_extract_hostname(const grpc::string &peer)
221+
static void
222+
_set_hostname_from_attributes(LogMessage *msg, const RepeatedPtrField<KeyValue> &key_values)
223223
{
224-
size_t first = peer.find_first_of(':');
225-
size_t last = peer.find_last_of(':');
224+
for (const KeyValue &kv : key_values)
225+
{
226+
if (kv.key() == "host.name")
227+
{
228+
if (kv.value().value_case() != AnyValue::kStringValue)
229+
return;
226230

227-
if (first != grpc::string::npos && last != grpc::string::npos)
228-
return peer.substr(first + 1, last - first - 1);
231+
std::string hostname = kv.value().string_value();
232+
if (!hostname.empty())
233+
log_msg_set_value(msg, LM_V_HOST, hostname.c_str(), hostname.length());
229234

230-
return "";
235+
return;
236+
}
237+
}
231238
}
232239

233240
static GSockAddr *
@@ -262,7 +269,7 @@ _extract_saddr(const grpc::string &peer)
262269
}
263270

264271
static bool
265-
_parse_metadata(LogMessage *msg)
272+
_parse_metadata(LogMessage *msg, bool set_hostname)
266273
{
267274
char number_buf[G_ASCII_DTOSTR_BUF_SIZE];
268275
gssize len;
@@ -282,6 +289,8 @@ _parse_metadata(LogMessage *msg)
282289

283290
/* .otel.resource.attributes */
284291
_add_repeated_KeyValue_fields(msg, ".otel.resource.attributes", resource.attributes());
292+
if (set_hostname)
293+
_set_hostname_from_attributes(msg, resource.attributes());
285294

286295
/* .otel.resource.dropped_attributes_count */
287296
std::snprintf(number_buf, G_N_ELEMENTS(number_buf), "%" PRIu32, resource.dropped_attributes_count());
@@ -1113,11 +1122,6 @@ syslogng::grpc::otel::ProtobufParser::store_raw_metadata(LogMessage *msg, const
11131122
{
11141123
std::string serialized;
11151124

1116-
/* HOST */
1117-
std::string hostname = _extract_hostname(peer);
1118-
if (hostname.length())
1119-
log_msg_set_value(msg, LM_V_HOST, hostname.c_str(), hostname.length());
1120-
11211125
msg->saddr = _extract_saddr(peer);
11221126

11231127
/* .otel_raw.resource */
@@ -1393,7 +1397,9 @@ syslogng::grpc::otel::ProtobufParser::process(LogMessage *msg)
13931397

13941398
gssize len;
13951399
LogMessageValueType log_msg_type;
1396-
const gchar *type = log_msg_get_value_with_type(msg, logmsg_handle::RAW_TYPE, &len, &log_msg_type);
1400+
1401+
/* _parse_metadata() may invalidate the returned char pointer, so a copy is made with std::string */
1402+
std::string type = log_msg_get_value_with_type(msg, logmsg_handle::RAW_TYPE, &len, &log_msg_type);
13971403

13981404
if (log_msg_type == LM_VT_NULL)
13991405
{
@@ -1409,17 +1415,20 @@ syslogng::grpc::otel::ProtobufParser::process(LogMessage *msg)
14091415
return false;
14101416
}
14111417

1412-
if (strncmp(type, "log", len) == 0)
1418+
if (!_parse_metadata(msg, this->set_host))
1419+
return false;
1420+
1421+
if (type == "log")
14131422
{
14141423
if (!_parse_log_record(msg))
14151424
return false;
14161425
}
1417-
else if (strncmp(type, "metric", len) == 0)
1426+
else if (type == "metric")
14181427
{
14191428
if (!_parse_metric(msg))
14201429
return false;
14211430
}
1422-
else if (strncmp(type, "span", len) == 0)
1431+
else if (type == "span")
14231432
{
14241433
if (!_parse_span(msg))
14251434
return false;
@@ -1428,13 +1437,10 @@ syslogng::grpc::otel::ProtobufParser::process(LogMessage *msg)
14281437
{
14291438
msg_error("OpenTelemetry: unexpected .otel_raw.type",
14301439
evt_tag_msg_reference(msg),
1431-
evt_tag_str("type", type));
1440+
evt_tag_str("type", type.c_str()));
14321441
return false;
14331442
}
14341443

1435-
if (!_parse_metadata(msg))
1436-
return false;
1437-
14381444
_unset_raw_fields(msg);
14391445

14401446
return true;
@@ -1458,6 +1464,12 @@ _clone(LogPipe *s)
14581464
return &cloned->super.super;
14591465
}
14601466

1467+
void
1468+
otel_protobuf_parser_set_hostname(LogParser *s, gboolean set_hostname)
1469+
{
1470+
get_ProtobufParser(s)->set_hostname(set_hostname);
1471+
}
1472+
14611473
static void
14621474
_free(LogPipe *s)
14631475
{

modules/grpc/otel/otel-protobuf-parser.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
typedef struct OtelProtobufParser_ OtelProtobufParser;
3333

3434
LogParser *otel_protobuf_parser_new(GlobalConfig *cfg);
35+
void otel_protobuf_parser_set_hostname(LogParser *s, gboolean set_hostname);
3536

3637
#include "compat/cpp-end.h"
3738

modules/grpc/otel/otel-protobuf-parser.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class ProtobufParser
5353
public:
5454
bool process(LogMessage *msg);
5555

56+
void set_hostname(bool s)
57+
{
58+
this->set_host = s;
59+
}
60+
5661
static void store_raw_metadata(LogMessage *msg, const ::grpc::string &peer,
5762
const Resource &resource, const std::string &resource_schema_url,
5863
const InstrumentationScope &scope, const std::string &scope_schema_url);
@@ -69,6 +74,9 @@ class ProtobufParser
6974
static void set_syslog_ng_macros(LogMessage *msg, const KeyValueList &macros);
7075
static void set_syslog_ng_address(LogMessage *msg, GSockAddr **sa, const KeyValueList &addr);
7176
static void parse_syslog_ng_tags(LogMessage *msg, const std::string &tags_as_str);
77+
78+
private:
79+
bool set_host = true;
7280
};
7381

7482
}

modules/grpc/otel/otel-source.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ syslogng::grpc::otel::SourceDriver::init()
121121
if (fetch_limit == -1)
122122
fetch_limit = super->super.worker_options.super.init_window_size;
123123

124+
/*
125+
* syslog-ng-otlp(): the original HOST is always kept
126+
* opentelemetry(): there is no parsing in this source, HOST always falls back to saddr (LogSource)
127+
*/
128+
super->super.worker_options.super.keep_hostname = TRUE;
129+
124130
return log_threaded_source_driver_init_method(&super->super.super.super.super);
125131
}
126132

modules/grpc/otel/tests/test-otel-protobuf-parser.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ Test(otel_protobuf_parser, metadata)
143143
bytes_attr->set_key("bytes_key");
144144
bytes_attr->mutable_value()->set_bytes_value({0, 1, 2, 3, 4, 5, 6, 7});
145145

146+
KeyValue *hostname = resource.add_attributes();
147+
hostname->set_key("host.name");
148+
hostname->mutable_value()->set_string_value("myhost");
149+
146150
std::string scope_schema_url = "my_scope_schema_url";
147151

148152
LogMessage *msg = log_msg_new_empty();
@@ -152,6 +156,7 @@ Test(otel_protobuf_parser, metadata)
152156

153157
cr_assert(msg->saddr != NULL);
154158
_assert_log_msg_value(msg, "SOURCEIP", "::1", -1, LM_VT_STRING);
159+
_assert_log_msg_value(msg, "HOST", "myhost", -1, LM_VT_STRING);
155160

156161
_assert_log_msg_value(msg, ".otel.resource.attributes.null_key", "", -1, LM_VT_NULL);
157162
_assert_log_msg_value(msg, ".otel.resource.attributes.string_key", "string_attribute", -1, LM_VT_STRING);

0 commit comments

Comments
 (0)