Coverage Report

Created: 2025-01-28 07:34

/src/fluent-bit/plugins/processor_content_modifier/cm.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2024 The Fluent Bit Authors
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License");
8
 *  you may not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
21
#include <fluent-bit/flb_processor_plugin.h>
22
#include <fluent-bit/flb_utils.h>
23
#include <fluent-bit/flb_time.h>
24
#include <fluent-bit/flb_hash.h>
25
#include <fluent-bit/flb_pack.h>
26
#include <fluent-bit/flb_processor.h>
27
#include <fluent-bit/flb_log_event_decoder.h>
28
#include <fluent-bit/flb_log_event_encoder.h>
29
30
#include "cm.h"
31
#include "cm_config.h"
32
33
static int cb_init(struct flb_processor_instance *ins, void *source_plugin_instance,
34
                   int source_plugin_type, struct flb_config *config)
35
0
{
36
0
    struct content_modifier_ctx *ctx;
37
38
0
    ctx = cm_config_create(ins, config);
39
0
    if (!ctx) {
40
0
        return -1;
41
0
    }
42
43
0
    flb_processor_instance_set_context(ins, ctx);
44
45
0
    return FLB_PROCESSOR_SUCCESS;
46
0
}
47
48
static int cb_exit(struct flb_processor_instance *ins, void *data)
49
0
{
50
0
    struct content_modifier_ctx *ctx;
51
52
0
    if (!ins) {
53
0
        return FLB_PROCESSOR_SUCCESS;
54
0
    }
55
56
0
    ctx = data;
57
0
    if (ctx) {
58
0
        cm_config_destroy(ctx);
59
0
    }
60
61
0
    return FLB_PROCESSOR_SUCCESS;
62
0
}
63
64
static int cb_process_logs(struct flb_processor_instance *ins,
65
                           void *chunk_data,
66
                           const char *tag,
67
                           int tag_len)
68
0
{
69
0
    int ret;
70
0
    struct content_modifier_ctx *ctx;
71
0
    struct flb_mp_chunk_cobj *chunk_cobj = (struct flb_mp_chunk_cobj *) chunk_data;
72
73
0
    if (!ins->context) {
74
0
        return FLB_PROCESSOR_FAILURE;
75
0
    }
76
0
    ctx = ins->context;
77
78
0
    ret = cm_logs_process(ins, ctx, chunk_cobj, tag, tag_len);
79
0
    return ret;
80
81
0
}
82
83
static int cb_process_traces(struct flb_processor_instance *ins,
84
                             struct ctrace *traces_context,
85
                             const char *tag,
86
                             int tag_len)
87
0
{
88
0
    int ret;
89
0
    struct content_modifier_ctx *ctx;
90
91
0
    if (!ins->context) {
92
0
        return FLB_PROCESSOR_FAILURE;
93
0
    }
94
0
    ctx = ins->context;
95
96
0
    ret = cm_traces_process(ins, ctx, traces_context, tag, tag_len);
97
0
    return ret;
98
99
0
}
100
101
static int cb_process_metrics(struct flb_processor_instance *ins,
102
                              struct cmt *in_cmt,
103
                              struct cmt **out_cmt,
104
                              const char *tag,
105
                              int tag_len)
106
0
{
107
0
    int ret;
108
0
    struct content_modifier_ctx *ctx;
109
110
0
    if (!ins->context) {
111
0
        return FLB_PROCESSOR_FAILURE;
112
0
    }
113
0
    ctx = ins->context;
114
115
0
    ret = cm_metrics_process(ins, ctx, in_cmt, out_cmt, tag, tag_len);
116
0
    return ret;
117
0
}
118
119
static struct flb_config_map config_map[] = {
120
    {
121
        FLB_CONFIG_MAP_STR, "context", NULL,
122
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, context_str),
123
        "Context where the action will be applied."
124
    },
125
126
    {
127
        FLB_CONFIG_MAP_STR, "action", NULL,
128
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, action_str),
129
        "Action to perform over the content: insert, upsert, delete, rename or hash."
130
    },
131
132
    {
133
        FLB_CONFIG_MAP_STR, "key", NULL,
134
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, key),
135
        "Key to apply the action."
136
    },
137
138
    {
139
        FLB_CONFIG_MAP_STR, "value", NULL,
140
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, value),
141
        "Value to apply the action."
142
    },
143
144
    {
145
        FLB_CONFIG_MAP_STR, "pattern", NULL,
146
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, pattern),
147
        "Pattern used to create a regular expression."
148
    },
149
150
    {
151
        FLB_CONFIG_MAP_STR, "converted_type", NULL,
152
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, converted_type_str),
153
        "Specify the data type to convert to, allowed values are: int, double or string."
154
    },
155
156
    /* EOF */
157
    {0}
158
};
159
160
struct flb_processor_plugin processor_content_modifier_plugin = {
161
    .name               = "content_modifier",
162
    .description        = "Modify the content of Logs, Metrics and Traces",
163
    .cb_init            = cb_init,
164
    .cb_process_logs    = cb_process_logs,
165
    .cb_process_metrics = cb_process_metrics,
166
    .cb_process_traces  = cb_process_traces,
167
    .cb_exit            = cb_exit,
168
    .config_map         = config_map,
169
    .flags              = 0
170
};