Commit c30cfab
committed
in_kubernetes_events: buffer incomplete JSON across HTTP chunks
Fixes #11252
Problem:
The kubernetes_events plugin was failing with 'bad formed JSON' errors
when HTTP chunked transfer encoding split JSON event objects across
chunk boundaries.
Root Cause Analysis:
- Kubernetes watch API sends newline-delimited JSON over HTTP/1.1
chunked transfer encoding
- HTTP chunks are arbitrary-sized (commonly 1000-4000 bytes)
- A single JSON object can be split mid-object across chunks:
Chunk 1 (1000 bytes): {"type":"ADDED","object":{"name":"po
Chunk 2 (176 bytes): d-123","spec":{...}}}
- The HTTP client correctly decodes chunks and returns
FLB_HTTP_CHUNK_AVAILABLE after each chunk
- However, the plugin was trying to parse incomplete JSON from
each chunk, causing parse errors
Solution:
Implement application-layer buffering in the kubernetes_events plugin:
1. Added chunk_buffer field to k8s_events context to buffer incomplete
data across HTTP chunks
2. Modified process_http_chunk() to:
- Prepend any buffered data from previous chunks
- Parse only complete JSON objects (delimited by newlines)
- Buffer remaining incomplete data for the next chunk
- This follows standard network programming practice: the
application layer handles message boundaries
3. Clear buffer when stream closes or on connection errors
Why not fix in HTTP client:
- HTTP client returns FLB_HTTP_CHUNK_AVAILABLE by design after each
chunk is decoded
- HTTP layer shouldn't know about JSON or application protocols
- Similar to how TCP delivers packets but HTTP buffers for messages
- This maintains separation of concerns and doesn't break other plugins
Testing:
- Re-enabled events_v1_with_chunkedrecv test which simulates exactly
this scenario (1176-byte JSON split into 1000+176 byte chunks)
Signed-off-by: Jesse Awan <[email protected]>1 parent c88c545 commit c30cfab
File tree
4 files changed
+97
-11
lines changed- plugins/in_kubernetes_events
- tests/runtime
4 files changed
+97
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
746 | 746 | | |
747 | 747 | | |
748 | 748 | | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
749 | 771 | | |
750 | | - | |
751 | | - | |
752 | | - | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
753 | 776 | | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
754 | 786 | | |
755 | 787 | | |
756 | | - | |
757 | | - | |
| 788 | + | |
758 | 789 | | |
759 | 790 | | |
760 | | - | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
761 | 797 | | |
762 | 798 | | |
763 | 799 | | |
764 | 800 | | |
765 | | - | |
766 | | - | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
767 | 827 | | |
768 | | - | |
769 | | - | |
770 | 828 | | |
771 | 829 | | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
772 | 834 | | |
773 | 835 | | |
774 | 836 | | |
| 837 | + | |
775 | 838 | | |
776 | 839 | | |
777 | 840 | | |
| |||
889 | 952 | | |
890 | 953 | | |
891 | 954 | | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
892 | 962 | | |
893 | 963 | | |
894 | 964 | | |
| |||
938 | 1008 | | |
939 | 1009 | | |
940 | 1010 | | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
941 | 1017 | | |
942 | 1018 | | |
943 | 1019 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
88 | 91 | | |
89 | 92 | | |
90 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
161 | 164 | | |
162 | 165 | | |
163 | 166 | | |
| |||
289 | 292 | | |
290 | 293 | | |
291 | 294 | | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
292 | 299 | | |
293 | 300 | | |
294 | 301 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
447 | 447 | | |
448 | 448 | | |
449 | 449 | | |
450 | | - | |
| 450 | + | |
451 | 451 | | |
452 | 452 | | |
453 | 453 | | |
0 commit comments