Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use different layouts with HCHttp client , other than JacksonJsonLayout. #53

Open
J-Man13 opened this issue Oct 6, 2020 · 2 comments

Comments

@J-Man13
Copy link

J-Man13 commented Oct 6, 2020

Good day, sir I am having a small issue here , could really use your advices or recommendations.

Currently I'm trying to append logs into elastic using Elasticsearch appender configuration with Jest client:

<Elasticsearch name="SERVICE_LOGS_ELASTICSEARCH"> <IndexName indexName="java-service-${bundle:application:info.build.archiveBaseName}"/> <PatternLayout pattern="%m%n"/> <AsyncBatchDelivery batchSize="1000" deliveryInterval="5000"> <JestHttp serverUris="${bundle:application:spring.elasticsearch.jest.uris}" mappingType="_doc"/> </AsyncBatchDelivery> </Elasticsearch>

You might be wondering , why would i use PatternLayout in the elastic appender , but the main reason for that is because I am using custom messages whenever I'm logging to escape log4j2 inserted metadata in the log message. Everything is working just fine with such configuration for elastic appender. A little bit later I decided to use HCHttp client instead of Jest client because it was mentioned in documentation , that it is more optimized. Here is configuration of appender:

<Elasticsearch name="SERVICE_LOGS_ELASTICSEARCH"> <IndexName indexName="java-service-${bundle:application:info.build.archiveBaseName}"/> <PatternLayout pattern="%m%n"/> <AsyncBatchDelivery batchSize="1000" deliveryInterval="5000"> <HCHttp serverUris="${bundle:application:spring.elasticsearch.jest.uris}"> <PooledItemSourceFactory poolName="batchPool" itemSizeInBytes="1024000" initialPoolSize="3"/> </HCHttp> </AsyncBatchDelivery> </Elasticsearch>
So , this configuration would not wrok , throwing java.lang.UnsupportedOperationException: Use ItemSource based API instead.
Here is the my app link , if you wonder about what I am trying to achieve : https://github.com/jafarjafarov/actuatorSwaggerCRUDSample. The main idea is to allow the exposure of different java objects in elastic in json format during some flow execution.
Thanks in advance

@rfoltyns
Copy link
Owner

rfoltyns commented Oct 6, 2020

If you'd like to use HCHttp to get the optimizations, JacksonJsonLayout is the only out-of-the-box layout that you can use. Otherwise, you have to implement your own ItemSourceLayout.

ItemSource API is a part of the optimization here - in this case, it provides a bunch of wrapped, resizable buffers for serialized data (see Object Pooling). Whole HCHttp is based on it, so PatternLayout will not work here, because it serializes to String, not to ItemSource.

If your logs are already in JSON format, you can use messageOnly flag to send the log as-is with following configuration:

<Elasticsearch messageOnly="true" >
    <JacksonJsonLayout>
        <PooledItemSourceFactory itemSizeInBytes="1024" initialPoolSize="20000" />
    </JacksonJsonLayout>
    <AsyncBatchDelivery batchSize="5000" deliveryInterval="1000" >
        ...
        <HCHttp serverUris="...">
            ...
            <PooledItemSourceFactory itemSizeInBytes="5120000" initialPoolSize="4" />
            ...
        </HCHttp>
    </AsyncBatchDelivery>
</Elasticsearch>

Otherwise, you can override LogEvent and Message serializers with JacksonMixIn (see docs) to get desired effect.

With HCHttp, PooledItemSourceFactory config is crucial here. It tells JacksonJsonLayout to use specified factory to serialize logs. Factory in the layout defines buffers for logs (1024 bytes in this case, adjust for your logs accordingly). Factory in HCHttp defines buffers for batch requests.

batch itemSizeInBytes = layout itemSizeInBytes * batchSize

layout layoutPoolSize = batch initialPoolSize * batchSize

@rfoltyns
Copy link
Owner

rfoltyns commented Dec 17, 2020

@jafarjafarov Did the suggestion above help you solve your issue? Is there anything else you'd like me to address in this scope?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants