@@ -218,16 +218,23 @@ _add_repeated_KeyValue_fields(LogMessage *msg, const char *key, const RepeatedPt
218
218
_add_repeated_KeyValue_fields_with_prefix (msg, key_buffer, 0 , key, key_values);
219
219
}
220
220
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 )
223
223
{
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 ;
226
230
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 ());
229
234
230
- return " " ;
235
+ return ;
236
+ }
237
+ }
231
238
}
232
239
233
240
static GSockAddr *
@@ -262,7 +269,7 @@ _extract_saddr(const grpc::string &peer)
262
269
}
263
270
264
271
static bool
265
- _parse_metadata (LogMessage *msg)
272
+ _parse_metadata (LogMessage *msg, bool set_hostname )
266
273
{
267
274
char number_buf[G_ASCII_DTOSTR_BUF_SIZE];
268
275
gssize len;
@@ -282,6 +289,8 @@ _parse_metadata(LogMessage *msg)
282
289
283
290
/* .otel.resource.attributes */
284
291
_add_repeated_KeyValue_fields (msg, " .otel.resource.attributes" , resource.attributes ());
292
+ if (set_hostname)
293
+ _set_hostname_from_attributes (msg, resource.attributes ());
285
294
286
295
/* .otel.resource.dropped_attributes_count */
287
296
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
1113
1122
{
1114
1123
std::string serialized;
1115
1124
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
-
1121
1125
msg->saddr = _extract_saddr (peer);
1122
1126
1123
1127
/* .otel_raw.resource */
@@ -1393,7 +1397,9 @@ syslogng::grpc::otel::ProtobufParser::process(LogMessage *msg)
1393
1397
1394
1398
gssize len;
1395
1399
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);
1397
1403
1398
1404
if (log_msg_type == LM_VT_NULL)
1399
1405
{
@@ -1409,17 +1415,20 @@ syslogng::grpc::otel::ProtobufParser::process(LogMessage *msg)
1409
1415
return false ;
1410
1416
}
1411
1417
1412
- if (strncmp (type, " log" , len) == 0 )
1418
+ if (!_parse_metadata (msg, this ->set_host ))
1419
+ return false ;
1420
+
1421
+ if (type == " log" )
1413
1422
{
1414
1423
if (!_parse_log_record (msg))
1415
1424
return false ;
1416
1425
}
1417
- else if (strncmp ( type, " metric " , len) == 0 )
1426
+ else if (type == " metric " )
1418
1427
{
1419
1428
if (!_parse_metric (msg))
1420
1429
return false ;
1421
1430
}
1422
- else if (strncmp ( type, " span " , len) == 0 )
1431
+ else if (type == " span " )
1423
1432
{
1424
1433
if (!_parse_span (msg))
1425
1434
return false ;
@@ -1428,13 +1437,10 @@ syslogng::grpc::otel::ProtobufParser::process(LogMessage *msg)
1428
1437
{
1429
1438
msg_error (" OpenTelemetry: unexpected .otel_raw.type" ,
1430
1439
evt_tag_msg_reference (msg),
1431
- evt_tag_str (" type" , type));
1440
+ evt_tag_str (" type" , type. c_str () ));
1432
1441
return false ;
1433
1442
}
1434
1443
1435
- if (!_parse_metadata (msg))
1436
- return false ;
1437
-
1438
1444
_unset_raw_fields (msg);
1439
1445
1440
1446
return true ;
@@ -1458,6 +1464,12 @@ _clone(LogPipe *s)
1458
1464
return &cloned->super .super ;
1459
1465
}
1460
1466
1467
+ void
1468
+ otel_protobuf_parser_set_hostname (LogParser *s, gboolean set_hostname)
1469
+ {
1470
+ get_ProtobufParser (s)->set_hostname (set_hostname);
1471
+ }
1472
+
1461
1473
static void
1462
1474
_free (LogPipe *s)
1463
1475
{
0 commit comments