Skip to content

Commit

Permalink
in_process_exporter_metrics: Imeplement process level of metrics expo…
Browse files Browse the repository at this point in the history
…rter
  • Loading branch information
cosmo0920 committed Sep 19, 2023
1 parent e5f12f9 commit 06d887c
Show file tree
Hide file tree
Showing 11 changed files with 1,757 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ option(FLB_IN_OPENTELEMETRY "Enable OpenTelemetry input plugin"
option(FLB_IN_ELASTICSEARCH "Enable Elasticsearch (Bulk API) input plugin" Yes)
option(FLB_IN_CALYPTIA_FLEET "Enable Calyptia Fleet input plugin" Yes)
option(FLB_IN_SPLUNK "Enable Splunk HTTP HEC input plugin" Yes)
option(FLB_IN_PROCESS_EXPORTER_METRICS "Enable process exporter metrics input plugin" Yes)
option(FLB_OUT_AZURE "Enable Azure output plugin" Yes)
option(FLB_OUT_AZURE_BLOB "Enable Azure output plugin" Yes)
option(FLB_OUT_AZURE_LOGS_INGESTION "Enable Azure Logs Ingestion output plugin" Yes)
Expand Down
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
REGISTER_IN_PLUGIN("in_docker_events")
REGISTER_IN_PLUGIN("in_node_exporter_metrics")
REGISTER_IN_PLUGIN("in_podman_metrics")
REGISTER_IN_PLUGIN("in_process_exporter_metrics")
endif()

REGISTER_IN_PLUGIN("in_kubernetes_events")
Expand Down
8 changes: 8 additions & 0 deletions plugins/in_process_exporter_metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(src
pe.c
pe_config.c
pe_process.c
pe_utils.c
)

FLB_PLUGIN(in_process_exporter_metrics "${src}" "")
166 changes: 166 additions & 0 deletions plugins/in_process_exporter_metrics/pe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2023 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_pack.h>

#include "pe.h"
#include "pe_config.h"

#include "pe_process.h"

static void update_metrics(struct flb_input_instance *ins, struct flb_pe *ctx)
{
pe_process_update(ctx);
}

/*
* Update the metrics, this function is invoked every time 'scrape_interval'
* expires.
*/
static int cb_pe_collect(struct flb_input_instance *ins,
struct flb_config *config, void *in_context)
{
int ret;
struct flb_pe *ctx = in_context;

update_metrics(ins, ctx);

/* Append the updated metrics */
ret = flb_input_metrics_append(ins, NULL, 0, ctx->cmt);
if (ret != 0) {
flb_plg_error(ins, "could not append metrics");
}

return 0;
}

static int in_pe_init(struct flb_input_instance *in,
struct flb_config *config, void *data)
{
int ret;
struct flb_pe *ctx;

/* Create plugin context */
ctx = flb_pe_config_create(in, config);
if (!ctx) {
flb_errno();
return -1;
}

/* Initialize fds */
ctx->coll_fd = -1;

/* Associate context with the instance */
flb_input_set_context(in, ctx);

/* Create the collector */
ret = flb_input_set_collector_time(in,
cb_pe_collect,
ctx->scrape_interval, 0,
config);
if (ret == -1) {
flb_plg_error(ctx->ins,
"could not set collector for Node Exporter Metrics plugin");
return -1;
}
ctx->coll_fd = ret;

/* Initialize process metric collectors */
pe_process_init(ctx);

update_metrics(in, ctx);

return 0;
}

static int in_pe_exit(void *data, struct flb_config *config)
{
struct flb_pe *ctx = data;

if (!ctx) {
return 0;
}

pe_process_exit(ctx);

flb_pe_config_destroy(ctx);

return 0;
}

static void in_pe_pause(void *data, struct flb_config *config)
{
struct flb_pe *ctx = data;

flb_input_collector_pause(ctx->coll_fd, ctx->ins);
}

static void in_pe_resume(void *data, struct flb_config *config)
{
struct flb_pe *ctx = data;

flb_input_collector_resume(ctx->coll_fd, ctx->ins);
}

/* Configuration properties map */
static struct flb_config_map config_map[] = {
{
FLB_CONFIG_MAP_TIME, "scrape_interval", "5",
0, FLB_TRUE, offsetof(struct flb_pe, scrape_interval),
"scrape interval to collect metrics from the node."
},

{
FLB_CONFIG_MAP_STR, "path.procfs", "/proc",
0, FLB_TRUE, offsetof(struct flb_pe, path_procfs),
"procfs mount point"
},

{
FLB_CONFIG_MAP_STR, "process_include_pattern", ".+",
0, FLB_TRUE, offsetof(struct flb_pe, process_regex_include_list_text),
"include list regular expression"
},

{
FLB_CONFIG_MAP_STR, "process_exclude_pattern", NULL,
0, FLB_TRUE, offsetof(struct flb_pe, process_regex_exclude_list_text),
"exclude list regular expression"
},
/* EOF */
{0}
};

struct flb_input_plugin in_process_exporter_metrics_plugin = {
.name = "process_exporter_metrics",
.description = "Process Exporter Metrics (Prometheus Compatible)",
.cb_init = in_pe_init,
.cb_pre_run = NULL,
.cb_collect = cb_pe_collect,
.cb_flush_buf = NULL,
.config_map = config_map,
.cb_pause = in_pe_pause,
.cb_resume = in_pe_resume,
.cb_exit = in_pe_exit,
.flags = FLB_INPUT_THREADED
};
75 changes: 75 additions & 0 deletions plugins/in_process_exporter_metrics/pe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2023 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FLB_PROCESS_EXPORTER_H
#define FLB_PROCESS_EXPORTER_H

/* utils: scan content type expected */
#define NE_SCAN_FILE 1
#define NE_SCAN_DIR 2

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_regex.h>
#include <fluent-bit/flb_hash_table.h>
#include <fluent-bit/flb_metrics.h>

struct flb_pe {
/* configuration */
flb_sds_t path_procfs;
int scrape_interval;

int coll_fd; /* collector fd */
struct cmt *cmt; /* cmetrics context */
struct flb_input_instance *ins; /* input instance */

/*
* Metrics Contexts
* ----------------
*/

/* process */
struct cmt_gauge *memory_bytes;
struct cmt_gauge *start_time;
struct cmt_gauge *open_fds;
struct cmt_gauge *fd_ratio;
struct cmt_counter *cpu_seconds;
struct cmt_counter *read_bytes;
struct cmt_counter *write_bytes;
struct cmt_counter *major_page_faults;
struct cmt_counter *minor_page_faults;
struct cmt_counter *context_switches;
struct cmt_gauge *num_threads;
struct cmt_gauge *states;

/* thread */
struct cmt_gauge *thread_wchan;
struct cmt_counter *thread_cpu_seconds;
struct cmt_counter *thread_io_bytes;
struct cmt_counter *thread_major_page_faults;
struct cmt_counter *thread_minor_page_faults;
struct cmt_counter *thread_context_switches;

flb_sds_t process_regex_include_list_text;
flb_sds_t process_regex_exclude_list_text;
struct flb_regex *process_regex_include_list;
struct flb_regex *process_regex_exclude_list;
};

#endif
70 changes: 70 additions & 0 deletions plugins/in_process_exporter_metrics/pe_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2015-2022 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <fluent-bit/flb_input_plugin.h>
#include "pe.h"

struct flb_pe *flb_pe_config_create(struct flb_input_instance *ins,
struct flb_config *config)
{
int ret;
struct flb_pe *ctx;

ctx = flb_calloc(1, sizeof(struct flb_pe));
if (!ctx) {
flb_errno();
return NULL;
}
ctx->ins = ins;
ctx->process_regex_include_list = NULL;
ctx->process_regex_exclude_list = NULL;

/* Load the config map */
ret = flb_input_config_map_set(ins, (void *) ctx);
if (ret == -1) {
flb_free(ctx);
return NULL;
}

/* mount points */
flb_plg_info(ins, "path.procfs = %s", ctx->path_procfs);

ctx->cmt = cmt_create();
if (!ctx->cmt) {
flb_plg_error(ins, "could not initialize CMetrics");
flb_free(ctx);
return NULL;
}


return ctx;
}

void flb_pe_config_destroy(struct flb_pe *ctx)
{
if (!ctx) {
return;
}

if (ctx->cmt) {
cmt_destroy(ctx->cmt);
}

flb_free(ctx);
}
31 changes: 31 additions & 0 deletions plugins/in_process_exporter_metrics/pe_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2023 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FLB_PE_CONFIG_H
#define FLB_PE_CONFIG_H

#include <fluent-bit/flb_input_plugin.h>
#include "pe.h"

struct flb_pe *flb_pe_config_create(struct flb_input_instance *ins,
struct flb_config *config);

void flb_pe_config_destroy(struct flb_pe *ctx);

#endif
Loading

0 comments on commit 06d887c

Please sign in to comment.