手动提取记录

在某些情况下,使用 Fluent Bit 库将记录从调用者应用程序发送到某个目的地,这个过程称为手动数据提取

为此,我们提供了一个名为 lib 的特殊输入插件,可以与 flb_lib_push() API 函数结合使用。

Data Format

lib 输入插件期望数据采用固定的 JSON 格式,如下所示:

  1. [UNIX_TIMESTAMP, MAP]

每个记录必须是一个至少包含两项的 JSON 数组。第一个是 Unix 时间戳,表示事件生成的时间,第二个是带有键值列表的 JSON 映射。有效的示例可以是如下内容:

  1. [1449505010, {"key1": "some value", "key2": false}]

Usage

以下 C 代码片段演示了如何在运行的 Fluent Bit 引擎中插入一些 JSON 记录:

  1. #include <fluent-bit.h>
  2. #define JSON_1 "[1449505010, {\"key1\": \"some value\"}]"
  3. #define JSON_2 "[1449505620, {\"key1\": \"some new value\"}]"
  4. int main()
  5. {
  6. int ret;
  7. int in_ffd;
  8. int out_ffd;
  9. flb_ctx_t *ctx;
  10. /* Create library context */
  11. ctx = flb_create();
  12. if (!ctx) {
  13. return -1;
  14. }
  15. /* Enable the input plugin for manual data ingestion */
  16. in_ffd = flb_input(ctx, "lib", NULL);
  17. if (in_ffd == -1) {
  18. flb_destroy(ctx);
  19. return -1;
  20. }
  21. /* Enable output plugin 'stdout' (print records to the standard output) */
  22. out_ffd = flb_output(ctx, "stdout", NULL);
  23. if (out_ffd == -1) {
  24. flb_destroy(ctx);
  25. return -1;
  26. }
  27. /* Start the engine */
  28. ret = flb_start(ctx);
  29. if (ret == -1) {
  30. flb_destroy(ctx);
  31. return -1;
  32. }
  33. /* Ingest data manually */
  34. flb_lib_push(ctx, in_ffd, JSON_1, sizeof(JSON_1) - 1);
  35. flb_lib_push(ctx, in_ffd, JSON_2, sizeof(JSON_2) - 1);
  36. /* Stop the engine (5 seconds to flush remaining data) */
  37. flb_stop(ctx);
  38. /* Destroy library context, release all resources */
  39. flb_destroy(ctx);
  40. return 0;
  41. }