ecs-logging-java icon indicating copy to clipboard operation
ecs-logging-java copied to clipboard

Add constants for known ecs fields for code efficiency when you log

Open Fr33Radical opened this issue 6 years ago • 7 comments
trafficstars

Hi, I'd like to have the ability to contribute to the project. It would be the first time I do it, so I am not sure if I can create a branch or anything. Would that be possible?

I'd like juste to create a Constants.java class that would have all the ECS fields so it would be accessible from any project and that would be more efficient.

Plus, it would be more easy to look at which fields already exist.

Fr33Radical avatar Oct 07 '19 18:10 Fr33Radical

Hi and welcome!

We have some ideas around auto-generating a typed object hierarchy based on the generated ECS yaml file: https://github.com/elastic/ecs/blob/master/generated/ecs/ecs_nested.yml.

That would have the benefit that the fields are not only named but also that the types correspond to those used in ECS. Combine that with a fluent API and you could do something like:

logger.info(EcsMessage
    .withMessage("Hello World")
    .withUser()
        .withId(42)
        .withName("Jon Doe")
        .and()
    .withGeo()
        .withCountry("Canada"));
ThreadContext.putAll(EcsMessage.withUser().withId(42).withName("Jon Doe").toFlatMap()):

felixbarny avatar Oct 08 '19 06:10 felixbarny

I understand the example is not implemented yet. Do you have some kind of target date or any news about this feature?

Thank you, much appreciated.

Fr33Radical avatar Dec 04 '19 19:12 Fr33Radical

It's just an idea at the moment and there's no timeline for that.

felixbarny avatar Dec 05 '19 09:12 felixbarny

I'd like to implement it.

Do you have some details about where I should start to auto-generate "a typed object hierarchy based on the generated ECS yaml"?

What would the input? How do you see it works?

I'm not sure about the specifications but I really like this feature and I would enjoy to implement it myself.

Is it to parse the yaml to create constant in a enum that we could instantiate with numbers?

Fr33Radical avatar Dec 18 '19 23:12 Fr33Radical

These are the fields from ECS: https://github.com/elastic/ecs/blob/master/generated/ecs/ecs_flat.yml.

See also https://github.com/elastic/ecs-dotnet for how a typed object hierarchy is generated for .NET.

felixbarny avatar Dec 19 '19 12:12 felixbarny

Hello, I looked at ecs-dotnet and so far I understood that they use YamlDotNet.Serialization to create the source file. In java, this article is what I found that would be similar to ecs-dotnet : https://www.freecodecamp.org/news/how-to-generate-data-classes-in-java-fead8fa354a2/

For jackson, this example shows how to create instances of an object but not the java class definition. https://www.baeldung.com/jackson-yaml

I have been recommend to use swagger and I think it would really be the way to go. The issue is that swagger works with a yaml format that does not fit expressively with ecs_nested.yml or ecs_flat.yml. See the swagger editor for an example: https://editor.swagger.io/ See this example of swagger for generating java source files: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin. You can simply download the project and run mvn clean install to see the generated-sources folder.

In the image, we can see that from the yaml file, we can describe the java classes, their members, getters and setters. Screenshot 2021-08-29 160738

Can elastic create a ecs_nested.yml file that fit the swagger format? Does the ECS yaml file can be written in that format?

Fr33Radical avatar Aug 29 '21 20:08 Fr33Radical

Not sure that swagger would be the way to go. It's about specifying HTTP-based APIs. On the plus side, it does come with a wide range of generators for different languages. The best place to propose that would be https://github.com/elastic/ecs.

felixbarny avatar Aug 30 '21 07:08 felixbarny