Developer guide for beginners on contributing to Fluent Bit

Assuming you have some basic knowledge of C, this guide should help you understand how to make code changes to Fluent Bit.

Libraries

Most external libraries are embedded in the project in the /lib folder. To keep its footprint low and make cross-platform builds simple, Fluent Bit attempts keep its dependency graph small.

The external library you are mostly likely to interact with is msgpack.

For crypto, Fluent Bit uses mbedtls.

Memory Management

When you write Fluent Bit code, you will use Fluent Bit’s versions of the standard C functions for working with memory:

Note that many types have a specialized create and destroy function. For example, flb_sds_create() and flb_sds_destroy() (more about this in the next section).

Strings

Fluent Bit has a stripped down version of the popular SDS string library. See flb_sds.h for the API.

In general, you should use SDS strings in any string processing code. SDS strings are fully compatible with any C function that accepts a null-terminated sequence of characters; to understand how they work, see the explanation on Github.

HTTP Client

Fluent Bit has its own network connection library. The key types and functions are defined in the following header files:

The following code demonstrates making an HTTP request in Fluent Bit:

  1. #include <fluent-bit/flb_upstream.h>
  2. #include <fluent-bit/flb_io.h>
  3. #include <fluent-bit/flb_http_client.h>
  4. #include <fluent-bit/flb_info.h>
  5. #include <fluent-bit/flb_config.h>
  6. #define HOST "127.0.0.1"
  7. #define PORT 80
  8. static flb_sds_t make_request(struct flb_config *config)
  9. {
  10. struct flb_upstream *upstream;
  11. struct flb_http_client *client;
  12. size_t b_sent;
  13. int ret;
  14. struct flb_upstream_conn *u_conn;
  15. flb_sds_t resp;
  16. /* Create an 'upstream' context */
  17. upstream = flb_upstream_create(config, HOST, PORT, FLB_IO_TCP, NULL);
  18. if (!upstream) {
  19. flb_error("[example] connection initialization error");
  20. return -1;
  21. }
  22. /* Retrieve a TCP connection from the 'upstream' context */
  23. u_conn = flb_upstream_conn_get(upstream);
  24. if (!u_conn) {
  25. flb_error("[example] connection initialization error");
  26. flb_upstream_destroy(upstream);
  27. return -1;
  28. }
  29. /* Create HTTP Client request/context */
  30. client = flb_http_client(u_conn,
  31. FLB_HTTP_GET, metadata_path,
  32. NULL, 0,
  33. FLB_FILTER_AWS_IMDS_V2_HOST, 80,
  34. NULL, 0);
  35. if (!client) {
  36. flb_error("[example] count not create http client");
  37. flb_upstream_conn_release(u_conn);
  38. flb_upstream_destroy(upstream);
  39. return -1;
  40. }
  41. /* Perform the HTTP request */
  42. ret = flb_http_do(client, &b_sent)
  43. /* Validate return status and HTTP status if set */
  44. if (ret != 0 || client->resp.status != 200) {
  45. if (client->resp.payload_size > 0) {
  46. flb_debug("[example] Request failed and returned: \n%s",
  47. client->resp.payload);
  48. }
  49. flb_http_client_destroy(client);
  50. flb_upstream_conn_release(u_conn);
  51. flb_upstream_destroy(upstream);
  52. return -1;
  53. }
  54. /* Copy payload response to an output SDS buffer */
  55. data = flb_sds_create_len(client->resp.payload,
  56. client->resp.payload_size);
  57. flb_http_client_destroy(client);
  58. flb_upstream_conn_release(u_conn);
  59. flb_upstream_destroy(upstream);
  60. return resp;
  61. }

An flb_upstream structure represents a host/endpoint that you want to call. Normally, you’d store this structure somewhere so that it can be re-used. An flb_upstream_conn represents a connection to that host for a single HTTP request. The connection structure should not be used for more than one request.

Linked Lists

Fluent Bit contains a library for constructing linked lists- mk_list. The type stores data as a circular linked list.

The mk_list.h header file contains several macros and functions for use with the lists. The example below shows how to create a list, iterate through it, and delete an element.

  1. #include <monkey/mk_core/mk_list.h>
  2. #include <fluent-bit/flb_info.h>
  3. struct item {
  4. char some_data;
  5. struct mk_list _head;
  6. };
  7. static int example()
  8. {
  9. struct mk_list *tmp;
  10. struct mk_list *head;
  11. struct mk_list items;
  12. int i;
  13. int len;
  14. char characters[] = "abcdefghijk";
  15. struct item *an_item;
  16. len = strlen(characters);
  17. /* construct a list */
  18. mk_list_init(&items);
  19. for (i = 0; i < len; i++) {
  20. an_item = flb_malloc(sizeof(struct item));
  21. if (!an_item) {
  22. flb_errno();
  23. return -1;
  24. }
  25. an_item->some_data = characters[i];
  26. mk_list_add(&an_item->_head, &items);
  27. }
  28. /* iterate through the list */
  29. flb_info("Iterating through list");
  30. mk_list_foreach_safe(head, tmp, &items) {
  31. an_item = mk_list_entry(head, struct item, _head);
  32. flb_info("list item data value: %c", an_item->some_data);
  33. }
  34. /* remove an item */
  35. mk_list_foreach_safe(head, tmp, &items) {
  36. an_item = mk_list_entry(head, struct item, _head);
  37. if (an_item->some_data == 'b') {
  38. mk_list_del(&an_item->_head);
  39. flb_free(an_item);
  40. }
  41. }
  42. }

Message Pack

Fluent Bit uses msgpack to internally store data. If you write code for Fluent Bit, it is almost certain that you will interact with msgpack.

Fluent Bit embeds the msgpack-c library. The example below shows manipulating message pack to add a new key-value pair to a record. In Fluent Bit, the filter_record_modifier plugin adds or deletes keys from records. See its code for more.

  1. #define A_NEW_KEY "key"
  2. #define A_NEW_KEY_LEN 3
  3. #define A_NEW_VALUE "value"
  4. #define A_NEW_VALUE_LEN 5
  5. static int cb_filter(const void *data, size_t bytes,
  6. const char *tag, int tag_len,
  7. void **out_buf, size_t *out_size,
  8. struct flb_filter_instance *f_ins,
  9. void *context,
  10. struct flb_config *config)
  11. {
  12. (void) f_ins;
  13. (void) config;
  14. size_t off = 0;
  15. int i = 0;
  16. int ret;
  17. struct flb_time tm;
  18. int total_records;
  19. int new_keys = 1;
  20. msgpack_sbuffer tmp_sbuf;
  21. msgpack_packer tmp_pck;
  22. msgpack_unpacked result;
  23. msgpack_object *obj;
  24. msgpack_object_kv *kv;
  25. /* Create temporary msgpack buffer */
  26. msgpack_sbuffer_init(&tmp_sbuf);
  27. msgpack_packer_init(&tmp_pck, &tmp_sbuf, msgpack_sbuffer_write);
  28. /* Iterate over each item */
  29. msgpack_unpacked_init(&result);
  30. while (msgpack_unpack_next(&result, data, bytes, &off) == MSGPACK_UNPACK_SUCCESS) {
  31. /*
  32. * Each record is a msgpack array [timestamp, map] of the
  33. * timestamp and record map. We 'unpack' each record, and then re-pack
  34. * it with the new fields added.
  35. */
  36. if (result.data.type != MSGPACK_OBJECT_ARRAY) {
  37. continue;
  38. }
  39. /* unpack the array of [timestamp, map] */
  40. flb_time_pop_from_msgpack(&tm, &result, &obj);
  41. /* obj should now be the record map */
  42. if (obj->type != MSGPACK_OBJECT_MAP) {
  43. continue;
  44. }
  45. /* re-pack the array into a new buffer */
  46. msgpack_pack_array(&tmp_pck, 2);
  47. flb_time_append_to_msgpack(&tm, &tmp_pck, 0);
  48. /* new record map size is old size + the new keys we will add */
  49. total_records = obj->via.map.size + new_keys;
  50. msgpack_pack_map(&tmp_pck, total_records);
  51. /* iterate through the old record map and add it to the new buffer */
  52. kv = obj->via.map.ptr;
  53. for(i=0; i < obj->via.map.size; i++) {
  54. msgpack_pack_object(&tmp_pck, (kv+i)->key);
  55. msgpack_pack_object(&tmp_pck, (kv+i)->val);
  56. }
  57. /* append new keys */
  58. msgpack_pack_str(&tmp_pck, A_NEW_KEY_LEN);
  59. msgpack_pack_str_body(&tmp_pck, A_NEW_KEY, A_NEW_KEY_LEN);
  60. msgpack_pack_str(&tmp_pck, A_NEW_VALUE_LEN);
  61. msgpack_pack_str_body(&tmp_pck, A_NEW_VALUE, A_NEW_VALUE_LEN);
  62. }
  63. msgpack_unpacked_destroy(&result);
  64. /* link new buffers */
  65. *out_buf = tmp_sbuf.data;
  66. *out_size = tmp_sbuf.size;
  67. return FLB_FILTER_MODIFIED;

Please also check out the message pack examples on the msgpack-c GitHub repo.

Plugin API

Each plugin is a shared object which is loaded into Fluent Bit using dlopen and dlsym.

Input

The input plugin structure is defined in flb_input.h. There are a number of functions which a plugin can implement, most only implement cb_init, cb_collect, and cb_exit.

The "dummy" input plugin very simple and is an excellent example to review to understand more.

Filter

The structure for filter plugins is defined in flb_filter.h. Each plugin must implement cb_init, cb_filter, and cb_exit.

The filter_record_modifier is a good example of a filter plugin.

Note that filter plugins can not asynchronously make HTTP requests. If your plugin needs to make a request, add the following code when you initialize your flb_upstream:

  1. /* Remove async flag from upstream */
  2. upstream->flags &= ~(FLB_IO_ASYNC);

Output

Output plugins are defined in flb_output.h. Each plugin must implement cb_init, cb_flush, and cb_exit.

The stdout plugin is very simple; review its code to understand how output plugins work.

Testing

During development, you can build Fluent Bit as follows:

  1. cd build
  2. cmake -DFLB_DEV=On ../
  3. make

Note that Fluent Bit uses Cmake 3 and on some systems you may need to invoke it as cmake3.

To enable the unit tests run:

  1. cmake -DFLB_DEV=On -DFLB_TESTS_RUNTIME=On -DFLB_TESTS_INTERNAL=On ../
  2. make

Internal tests are for the internal libraries of Fluent Bit. Runtime tests are for the plugins.

You can run the unit tests with make test, however, this is inconvenient in practice. Each test file will create an executable in the build/bin directory which you can run directly. For example, if you want to run the SDS tests, you can invoke them as follows:

  1. $ ./bin/flb-it-sds
  2. Test sds_usage... [ OK ]
  3. Test sds_printf... [ OK ]
  4. SUCCESS: All unit tests have passed.

Need more help?

The best way to learn how Fluent Bit code works is to read it. If you need help understanding the code, reach out to the community, or open a PR with changes that are a work in progress.