Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_process_exporter_metrics: implement process exporter metrics #7943

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -206,6 +206,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
REGISTER_IN_PLUGIN("in_docker")
REGISTER_IN_PLUGIN("in_docker_events")
REGISTER_IN_PLUGIN("in_podman_metrics")
REGISTER_IN_PLUGIN("in_process_exporter_metrics")
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
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}" "")
174 changes: 174 additions & 0 deletions plugins/in_process_exporter_metrics/pe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/* -*- 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"
},

{
FLB_CONFIG_MAP_CLIST, "metrics",
PE_DEFAULT_ENABLED_METRICS,
0, FLB_TRUE, offsetof(struct flb_pe, metrics),
"Comma separated list of keys to enable metrics."
},

/* 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
};
89 changes: 89 additions & 0 deletions plugins/in_process_exporter_metrics/pe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* -*- 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>

#define PE_DEFAULT_ENABLED_METRICS "cpu,io,memory,state,context_switches,fd,start_time,thread_wchan,thread"

#define METRIC_CPU (1 << 0)
#define METRIC_IO (1 << 1)
#define METRIC_MEMORY (1 << 2)
#define METRIC_STATE (1 << 3)
#define METRIC_CTXT (1 << 4)
#define METRIC_FD (1 << 5)
#define METRIC_START_TIME (1 << 6)
#define METRIC_THREAD_WCHAN (1 << 7)
#define METRIC_THREAD (1 << 8)

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 */
struct mk_list *metrics; /* enabled metrics */
int enabled_flag; /* indicate enabled metrics */

/*
* 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
Loading
Loading