Coverage Report

Created: 2024-09-19 07:08

/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 struct flb_config_map config_map[] = {
102
    {
103
        FLB_CONFIG_MAP_STR, "context", NULL,
104
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, context_str),
105
        "Context where the action will be applied."
106
    },
107
108
    {
109
        FLB_CONFIG_MAP_STR, "action", NULL,
110
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, action_str),
111
        "Action to perform over the content: insert, upsert, delete, rename or hash."
112
    },
113
114
    {
115
        FLB_CONFIG_MAP_STR, "key", NULL,
116
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, key),
117
        "Key to apply the action."
118
    },
119
120
    {
121
        FLB_CONFIG_MAP_STR, "value", NULL,
122
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, value),
123
        "Value to apply the action."
124
    },
125
126
    {
127
        FLB_CONFIG_MAP_STR, "pattern", NULL,
128
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, pattern),
129
        "Pattern used to create a regular expression."
130
    },
131
132
    {
133
        FLB_CONFIG_MAP_STR, "converted_type", NULL,
134
        0, FLB_TRUE, offsetof(struct content_modifier_ctx, converted_type_str),
135
        "Specify the data type to convert to, allowed values are: int, double or string."
136
    },
137
138
    /* EOF */
139
    {0}
140
};
141
142
struct flb_processor_plugin processor_content_modifier_plugin = {
143
    .name               = "content_modifier",
144
    .description        = "Modify the content of Logs, Metrics and Traces",
145
    .cb_init            = cb_init,
146
    .cb_process_logs    = cb_process_logs,
147
    .cb_process_metrics = NULL,
148
    .cb_process_traces  = cb_process_traces,
149
    .cb_exit            = cb_exit,
150
    .config_map         = config_map,
151
    .flags              = 0
152
};