elastic / elastic/ecs-logging-java

MDC values for event.type and event.category are not properly serialized as JSON arrays

Đang mở
#301 3 bình luận 0 reaction 0 người được giao Xem trên GitHub
agent-java community enhancement
Ngôn ngữ chính
Java
Star
148
Fork
82
Merge trung bình
2 ngày 17 giờ
Pull request đã merge (30 ngày)
3

Mô tả

## Description
When using logback-ecs-encoder with SLF4J's MDC to set array-type fields (like event.type and event.category), the values are serialized as string literals instead of proper JSON arrays. While MDC only supports String values by design, the ECS encoder could detect and properly format string values that represent arrays for fields that are defined as arrays in the ECS specification.

## Current Behavior
When setting an array value in MDC (which only accepts strings):
```java
MDC.put("event.type", Arrays.asList("connection", "allowed").toString());
```

The current output in logs:

```json
{
"@timestamp": "2025-01-08T13:00:53.318Z",
"event.type": "[connection, allowed]",
// other fields...
}
```

## Expected Behaviour
The log output should contain a proper JSON array according to ECS specification:
```json
{
"@timestamp": "2025-01-08T13:00:53.318Z",
"event.type": ["connection", "allowed"],
// other fields...
}
```

## Technical Details
The issue is in `EcsJsonSerializer.serializeMDC()` where all MDC values are treated as string literals:
```java
builder.append("\":\"");
JsonUtils.quoteAsString(toNullSafeString(String.valueOf(entry.getValue())), builder);
builder.append("\",");
```

While we understand that MDC only supports string values, the ECS encoder could detect and properly format these string values for fields that are defined as arrays in the ECS specification.

## Impact
This limitation affects any field that should be an array according to ECS specification, particularly:

- event.type
- event.category
- tags
- labels

This makes it difficult to use the library with standard Java collections for fields that should be arrays according to the ECS specification.

## Suggested Solution
The serializer could:

- Check if the field name matches known array fields from ECS specification
- Check if the string value represents a list (e.g., starts with '[' and ends with ']')
- Parse and format such values as proper JSON arrays

Example implementation approach:

```java
private static final Set ARRAY_FIELDS = Set.of(
"event.type",
"event.category",
"tags",
"labels"
);

public static void serializeMDC(StringBuilder builder, Map properties) {
if (properties != null && !properties.isEmpty()) {
for (Map.Entry entry : properties.entrySet()) {
builder.append('\"');
String key = entry.getKey();
JsonUtils.quoteAsString(key, builder);

String value = toNullSafeString(String.valueOf(entry.getValue()));
if (value.startsWith("[") && value.endsWith("]")) {
List items = Arrays.stream(
value.substring(1, value.length() - 1)
.split(","))
.map(String::trim)
.collect(Collectors.toList());

builder.append("\":");
builder.append(formatAsJsonArray(items));
builder.append(",");
} else {
builder.append("\":\"");
JsonUtils.quoteAsString(toNullSafeString(String.valueOf(entry.getValue())), builder);
builder.append("\",");
}
}
}
}
```

## Environment

- logback-ecs-encoder version: 1.6.0
- slf4j-api version: 2.0.9
- Java version: 11
- Logback version: 1.4.12

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu tại EcsJsonSerializer.serializeMDC(), nơi các giá trị MDC hiện đang được đặt trong dấu ngoặc kép dưới dạng chuỗi, và xem xét các trường ECS được nêu trong issue: event.type, event.category, tags và labels. Xác minh hành vi tuần tự hóa JSON hiện có và xác định việc hoàn thành là phát ra các mảng JSON hợp lệ cho các trường MDC được hỗ trợ có giá trị dạng mảng, đồng thời giữ nguyên cách xử lý dưới dạng chuỗi cho các trường khác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
java
Lĩnh vực
observability-sre
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.