Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions plugins/in_opentelemetry/opentelemetry_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_log_event_encoder.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_opentelemetry.h>
#include <fluent-otel-proto/fluent-otel.h>

Expand Down Expand Up @@ -217,10 +218,12 @@ static int otel_pack_v1_metadata(struct flb_opentelemetry *ctx,

flb_mp_map_header_init(&mh, mp_pck);

flb_mp_map_header_append(&mh);
msgpack_pack_str(mp_pck, 18);
msgpack_pack_str_body(mp_pck, "observed_timestamp", 18);
msgpack_pack_uint64(mp_pck, log_record->observed_time_unix_nano);
if (log_record->observed_time_unix_nano != 0) {
flb_mp_map_header_append(&mh);
msgpack_pack_str(mp_pck, 18);
msgpack_pack_str_body(mp_pck, "observed_timestamp", 18);
msgpack_pack_uint64(mp_pck, log_record->observed_time_unix_nano);
}

/* Value of 0 indicates unknown or missing timestamp. */
if (log_record->time_unix_nano != 0) {
Expand Down Expand Up @@ -514,8 +517,13 @@ static int binary_payload_to_msgpack(struct flb_opentelemetry *ctx,
flb_time_from_uint64(&tm, log_records[log_record_index]->time_unix_nano);
ret = flb_log_event_encoder_set_timestamp(encoder, &tm);
}
else if (log_records[log_record_index]->observed_time_unix_nano > 0) {
flb_time_from_uint64(&tm, log_records[log_record_index]->observed_time_unix_nano);
ret = flb_log_event_encoder_set_timestamp(encoder, &tm);
}
else {
ret = flb_log_event_encoder_set_current_timestamp(encoder);
flb_time_get(&tm);
ret = flb_log_event_encoder_set_timestamp(encoder, &tm);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/opentelemetry/flb_opentelemetry_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_log_event_encoder.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_opentelemetry.h>
#include <ctype.h>

Expand Down Expand Up @@ -73,13 +74,9 @@ static int process_json_payload_log_records_entry(
result = flb_otel_utils_find_map_entry_by_key(log_records_entry, "observedTimeUnixNano", 0, FLB_TRUE);
}

/* we need a timestamp... */
/* fallback to current time if both timestamp fields are missing */
if (result == -1) {
if (error_status) {
*error_status = FLB_OTEL_LOGS_ERR_MISSING_TIMESTAMP;
}
return -FLB_OTEL_LOGS_ERR_MISSING_TIMESTAMP;

flb_time_get(&timestamp);
}
else {
timestamp_object = &log_records_entry->ptr[result].val;
Expand Down
2 changes: 1 addition & 1 deletion tests/internal/data/opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ For error cases, use the `expected_error` field:
"test_case_name": {
"input": { ... },
"expected_error": {
"code": "FLB_OTEL_LOGS_ERR_MISSING_TIMESTAMP"
"code": "FLB_OTEL_LOGS_ERR_UNEXPECTED_LOGRECORDS_ENTRY_TYPE"
}
}
}
Expand Down
52 changes: 6 additions & 46 deletions tests/internal/data/opentelemetry/test_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
},

"missing_timestamp": {
"input": {"resourceLogs": [{"scopeLogs": [{"logRecords": [{}]}]}]},
"expected_error": {
"code": "FLB_OTEL_LOGS_ERR_MISSING_TIMESTAMP"
"input": {"resourceLogs": [{"scopeLogs": [{"logRecords": [{"body": {"stringValue": "test"}}]}]}]},
"expected": {
"group_metadata": {"schema":"otlp","resource_id":0,"scope_id":0},
"group_body": {"resource":{}},
"log_metadata": {"otlp":{}},
"log_body": {"log": "test"}
}
},

Expand Down Expand Up @@ -988,49 +991,6 @@
}
},

"multiple_records_no_timestamp_after_valid_ones": {
"input": {
"resourceLogs": [{
"resource": {
"attributes": [
{"key": "service.name", "value": {"stringValue": "test-service"}}
]
},
"scopeLogs": [{
"scope": {
"name": "test-scope"
},
"logRecords": [
{
"timeUnixNano": "1640995200000000000",
"severityNumber": 9,
"severityText": "INFO",
"attributes": [
{"key": "user.id", "value": {"stringValue": "user123"}},
{"key": "action", "value": {"stringValue": "login"}}
],
"body": {"stringValue": "User login successful"}
},
{
"timeUnixNano": "1640995201000000000",
"severityNumber": 5,
"severityText": "DEBUG",
"body": {"stringValue": "Processing user session"}
},
{
"severityNumber": 13,
"severityText": "ERROR",
"body": {"stringValue": "This log is missing timestamp"}
}
]
}]
}]
},
"expected_error": {
"code": "FLB_OTEL_LOGS_ERR_MISSING_TIMESTAMP"
}
},

"large_structure_fails_at_resource_attributes": {
"input": {
"resourceLogs": [
Expand Down
Loading