elastic / elastic/ecs-logging-java

Support numeric types on AdditionalField

Aperta
#319 0 commenti 1 reazione 0 assegnatari Vedi su GitHub
agent-java community triage
Lingua principale
Java
Stelle
148
Fork
82
Merge medio
2g 17h
PR unite (30g)
3

Descrizione

We just moved away from `logstash` to now directly send the logs from our systems into elastic using filebeat.
As we used structured logging of logstash, we had to compensate that by using a custom `Slf4jKeyValueEncoder` which maps the key/value pairs from `slf4` to the AdditionalField supported by ECS encoder.

Something like that:

```
public final class Slf4jKeyValueEncoder extends EcsEncoder {

@Override
protected void addCustomFields(ILoggingEvent event, StringBuilder builder) {
requireNonNull(event, "A logging event is required");
var logEventAdditionalFields = convertKeyValuePairsToAdditionalFields(event.getKeyValuePairs());
EcsJsonSerializer.serializeAdditionalFields(builder, logEventAdditionalFields);
}

private List convertKeyValuePairsToAdditionalFields(List keyValuePairs) {
if (keyValuePairs == null) {
return Collections.emptyList();
}

return keyValuePairs
.stream()
.map(it -> new AdditionalField(it.key, it.value.toString()))
.toList();
}
}
```
As we can see, `AdditionalField` supports values of `String` only.
The problem is that if we log something that is an `Integer` or `Long`, this ends up in Elastic like "1" or "1321654654". That prevents having fields in Elastic as numeric values.

Perhaps we could have value as an `Object` and a switch statement, when writing to the `StringBuilder`, to check if it's `Number` or `Boolean` and write it without the double quotes?

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.