레코드를 수동으로 수집하기 (Ingest records manually)
호출하는 애플리케이션에서 Fluent Bit 라이브러리를 사용해 레코드를 어떤 목적지로 보내야 하는 경우가 있어요. 이 과정을 수동 데이터 수집(manual data ingestion)이라고 해요.
이를 위해 lib라는 특정 입력 플러그인이 있으며, flb_lib_push() API 함수와 함께 사용할 수 있어요.
출처: 문서
본문
데이터 형식
lib 입력 플러그인은 다음 고정 JSON 형식의 데이터를 기대해요.
[UNIX_TIMESTAMP, MAP]
모든 레코드는 최소 두 개의 항목을 포함하는 JSON 배열이어야 해요. 첫 번째는 이벤트 생성과 관련된 시간(에포크 시간)을 나타내는 숫자인 UNIX_TIMESTAMP이고, 두 번째는 키와 값 목록을 가진 JSON 맵이에요. 유효한 항목은 다음과 같을 수 있어요.
[1449505010, {"key1": "some value", "key2": false}]
사용법
다음 C 코드 스니펫은 실행 중인 Fluent Bit 엔진에 몇 개의 JSON 레코드를 넣는 방법을 보여 줘요.
#include <fluent-bit.h>
#define JSON_1 "[1449505010, {\"key1\": \"some value\"}]"
#define JSON_2 "[1449505620, {\"key1\": \"some new value\"}]"
int main()
{
int ret;
int in_ffd;
int out_ffd;
flb_ctx_t *ctx;
/* Create library context */
ctx = flb_create();
if (!ctx) {
return -1;
}
/* Enable the input plugin for manual data ingestion */
in_ffd = flb_input(ctx, "lib", NULL);
if (in_ffd == -1) {
flb_destroy(ctx);
return -1;
}
/* Enable output plugin 'stdout' (print records to the standard output) */
out_ffd = flb_output(ctx, "stdout", NULL);
if (out_ffd == -1) {
flb_destroy(ctx);
return -1;
}
/* Start the engine */
ret = flb_start(ctx);
if (ret == -1) {
flb_destroy(ctx);
return -1;
}
/* Ingest data manually */
flb_lib_push(ctx, in_ffd, JSON_1, sizeof(JSON_1) - 1);
flb_lib_push(ctx, in_ffd, JSON_2, sizeof(JSON_2) - 1);
/* Stop the engine (5 seconds to flush remaining data) */
flb_stop(ctx);
/* Destroy library context, release all resources */
flb_destroy(ctx);
return 0;
}