forked from axoflow/axosyslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr-variable.c
248 lines (210 loc) · 7.34 KB
/
expr-variable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (c) 2023 Balazs Scheidler <balazs.scheidler@axoflow.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "filterx/expr-variable.h"
#include "filterx/object-message-value.h"
#include "filterx/object-string.h"
#include "filterx/filterx-scope.h"
#include "filterx/filterx-eval.h"
#include "filterx/filterx-variable.h"
#include "logmsg/logmsg.h"
#include "stats/stats-registry.h"
#include "stats/stats-cluster-single.h"
typedef struct _FilterXVariableExpr
{
FilterXExpr super;
FilterXObject *variable_name;
NVHandle handle;
guint32 declared:1, handle_is_macro:1;
} FilterXVariableExpr;
static FilterXObject *
_pull_variable_from_message(FilterXVariableExpr *self, FilterXEvalContext *context, LogMessage *msg)
{
gssize value_len;
LogMessageValueType t;
const gchar *value = log_msg_get_value_if_set_with_type(msg, self->handle, &value_len, &t);
if (!value)
{
filterx_eval_push_error("No such name-value pair in the log message", &self->super, self->variable_name);
return NULL;
}
if (self->handle_is_macro)
return filterx_message_value_new(value, value_len, t);
else
return filterx_message_value_new_borrowed(value, value_len, t);
}
/* NOTE: unset on a variable that only exists in the LogMessage, without making the message writable */
static void
_whiteout_variable(FilterXVariableExpr *self, FilterXEvalContext *context)
{
filterx_scope_register_variable(context->scope, self->handle, NULL);
}
static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
FilterXEvalContext *context = filterx_eval_get_context();
FilterXVariable *variable;
variable = filterx_scope_lookup_variable(context->scope, self->handle);
if (variable)
{
FilterXObject *value = filterx_variable_get_value(variable);
if (!value)
{
filterx_eval_push_error("Variable is unset", &self->super, self->variable_name);
}
return value;
}
if (!filterx_variable_handle_is_floating(self->handle))
{
FilterXObject *msg_ref = _pull_variable_from_message(self, context, context->msgs[0]);
if(!msg_ref)
return NULL;
filterx_scope_register_variable(context->scope, self->handle, msg_ref);
return msg_ref;
}
filterx_eval_push_error("No such variable", s, self->variable_name);
return NULL;
}
static void
_update_repr(FilterXExpr *s, FilterXObject *new_repr)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
FilterXScope *scope = filterx_eval_get_scope();
FilterXVariable *variable = filterx_scope_lookup_variable(scope, self->handle);
g_assert(variable != NULL);
filterx_variable_set_value(variable, new_repr);
}
static gboolean
_assign(FilterXExpr *s, FilterXObject *new_value)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
FilterXScope *scope = filterx_eval_get_scope();
FilterXVariable *variable = filterx_scope_lookup_variable(scope, self->handle);
if (!variable)
{
/* NOTE: we pass NULL as initial_value to make sure the new variable
* is considered changed due to the assignment */
if (self->declared)
variable = filterx_scope_register_declared_variable(scope, self->handle, NULL);
else
variable = filterx_scope_register_variable(scope, self->handle, NULL);
}
/* this only clones mutable objects */
new_value = filterx_object_clone(new_value);
filterx_variable_set_value(variable, new_value);
filterx_object_unref(new_value);
return TRUE;
}
static gboolean
_isset(FilterXExpr *s)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
FilterXScope *scope = filterx_eval_get_scope();
FilterXVariable *variable = filterx_scope_lookup_variable(scope, self->handle);
if (variable)
return filterx_variable_is_set(variable);
FilterXEvalContext *context = filterx_eval_get_context();
LogMessage *msg = context->msgs[0];
return log_msg_is_value_set(msg, self->handle);
}
static gboolean
_unset(FilterXExpr *s)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
FilterXEvalContext *context = filterx_eval_get_context();
FilterXVariable *variable = filterx_scope_lookup_variable(context->scope, self->handle);
if (variable)
{
filterx_variable_unset_value(variable);
return TRUE;
}
LogMessage *msg = context->msgs[0];
if (log_msg_is_value_set(msg, self->handle))
_whiteout_variable(self, context);
return TRUE;
}
static void
_free(FilterXExpr *s)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
filterx_object_unref(self->variable_name);
filterx_expr_free_method(s);
}
static gboolean
_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
stats_lock();
StatsClusterKey sc_key;
stats_cluster_single_key_set(&sc_key, "fx_variable_evals_total", NULL, 0);
stats_register_counter(STATS_LEVEL3, &sc_key, SC_TYPE_SINGLE_VALUE, &self->super.eval_count);
stats_unlock();
return filterx_expr_init_method(s, cfg);
}
static void
_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
stats_lock();
StatsClusterKey sc_key;
stats_cluster_single_key_set(&sc_key, "fx_variable_evals_total", NULL, 0);
stats_unregister_counter(&sc_key, SC_TYPE_SINGLE_VALUE, &self->super.eval_count);
stats_unlock();
return filterx_expr_deinit_method(s, cfg);
}
static FilterXExpr *
filterx_variable_expr_new(FilterXString *name, FilterXVariableType type)
{
FilterXVariableExpr *self = g_new0(FilterXVariableExpr, 1);
filterx_expr_init_instance(&self->super, "variable");
self->super.free_fn = _free;
self->super.init = _init;
self->super.deinit = _deinit;
self->super.eval = _eval;
self->super._update_repr = _update_repr;
self->super.assign = _assign;
self->super.is_set = _isset;
self->super.unset = _unset;
self->variable_name = (FilterXObject *) name;
self->handle = filterx_map_varname_to_handle(filterx_string_get_value_ref(self->variable_name, NULL), type);
if (type == FX_VAR_MESSAGE)
self->handle_is_macro = log_msg_is_handle_macro(filterx_variable_handle_to_nv_handle(self->handle));
return &self->super;
}
FilterXExpr *
filterx_msg_variable_expr_new(FilterXString *name)
{
return filterx_variable_expr_new(name, FX_VAR_MESSAGE);
}
FilterXExpr *
filterx_floating_variable_expr_new(FilterXString *name)
{
return filterx_variable_expr_new(name, FX_VAR_FLOATING);
}
void
filterx_variable_expr_declare(FilterXExpr *s)
{
FilterXVariableExpr *self = (FilterXVariableExpr *) s;
g_assert(s->eval == _eval);
self->declared = TRUE;
}