/src/fluent-bit/plugins/processor_sampling/sampling.h
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2026 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 | | #ifndef FLB_PROCESSOR_SAMPLING_H |
21 | | #define FLB_PROCESSOR_SAMPLING_H |
22 | | |
23 | | #include <fluent-bit/flb_processor_plugin.h> |
24 | | #include <ctraces/ctraces.h> |
25 | | |
26 | | enum { |
27 | | SAMPLING_TYPE_PROBABILISTIC = 0, |
28 | | SAMPLING_TYPE_TAIL, |
29 | | |
30 | | /* unused: for dev/test purposes only */ |
31 | | SAMPLING_TYPE_TEST, |
32 | | }; |
33 | | |
34 | | struct trace_span { |
35 | | struct ctrace_span *span; |
36 | | |
37 | | /* link to struct trace_entry->span_list */ |
38 | | struct cfl_list _head; |
39 | | }; |
40 | | |
41 | | struct trace_entry { |
42 | | /* binary trace_id (this wraps a cfl_sds_t) */ |
43 | | struct ctrace_id *trace_id; |
44 | | |
45 | | /* trace_id in hex format */ |
46 | | cfl_sds_t hex_trace_id; |
47 | | |
48 | | /* describe if the root span has been received */ |
49 | | int is_trace_complete; |
50 | | |
51 | | /* Linked list of spans */ |
52 | | struct cfl_list span_list; |
53 | | |
54 | | uint64_t ts_created; |
55 | | uint64_t ts_last_updated; |
56 | | |
57 | | /* link to struct sampling->trace_list */ |
58 | | struct cfl_list _head; |
59 | | |
60 | | /* link to struct sampling->trace_list_complete or trace_list_incomplete */ |
61 | | struct cfl_list _head_complete; |
62 | | }; |
63 | | |
64 | | enum { |
65 | | SAMPLING_COND_STATUS_CODE = 0, |
66 | | SAMPLING_COND_LATENCY, |
67 | | SAMPLING_COND_STRING_ATTRIBUTE, |
68 | | SAMPLING_COND_NUMERIC_ATTRIBUTE, |
69 | | SAMPLING_COND_BOOLEAN_ATTRIBUTE, |
70 | | SAMPLING_COND_SPAN_COUNT, |
71 | | SAMPLING_COND_TRACE_STATE, |
72 | | }; |
73 | | |
74 | | struct sampling_condition { |
75 | | int type; |
76 | | void *type_context; |
77 | | struct cfl_list _head; |
78 | | }; |
79 | | |
80 | | struct sampling_conditions { |
81 | | struct cfl_list list; |
82 | | }; |
83 | | |
84 | | struct sampling { |
85 | | /* config map properties */ |
86 | | flb_sds_t type_str; |
87 | | bool debug_mode; |
88 | | struct cfl_variant *sampling_settings; |
89 | | struct cfl_variant *conditions; |
90 | | |
91 | | /* |
92 | | * Internal |
93 | | * -------- |
94 | | */ |
95 | | int type; /* sampling type */ |
96 | | |
97 | | struct cfl_list plugins; |
98 | | |
99 | | struct sampling_conditions *sampling_conditions; |
100 | | |
101 | | /* plugin registration structure */ |
102 | | struct sampling_plugin *plugin; |
103 | | |
104 | | /* Lists for config map and rule properties: this list is created dinamically */ |
105 | | void *plugin_context; |
106 | | struct mk_list plugin_settings_properties; |
107 | | struct mk_list *plugin_config_map; |
108 | | |
109 | | /* Processor instance */ |
110 | | struct flb_processor_instance *ins; |
111 | | |
112 | | /* Parent input plugin instance */ |
113 | | struct flb_input_instance *input_ins; |
114 | | }; |
115 | | |
116 | | /* Common structure for all sampling mechanisms */ |
117 | | struct sampling_plugin { |
118 | | char *name; |
119 | | int type; |
120 | | struct flb_config_map *config_map; |
121 | | int (*cb_init) (struct flb_config *config, struct sampling *ctx); |
122 | | int (*cb_do_sampling) (struct sampling *ctx, void *context, |
123 | | struct ctrace *in_trace, struct ctrace **out_trace); |
124 | | int (*cb_exit) (struct flb_config *config, void *context); |
125 | | struct cfl_list _head; |
126 | | }; |
127 | | |
128 | | /* Plugins registration */ |
129 | | extern struct sampling_plugin sampling_test_plugin; |
130 | | extern struct sampling_plugin sampling_probabilistic_plugin; |
131 | | extern struct sampling_plugin sampling_tail_plugin; |
132 | | |
133 | | static inline void sampling_set_context(struct sampling *ctx, void *plugin_context) |
134 | 0 | { |
135 | 0 | ctx->plugin_context = plugin_context; |
136 | 0 | } Unexecuted instantiation: sampling.c:sampling_set_context Unexecuted instantiation: sampling_conf.c:sampling_set_context Unexecuted instantiation: sampling_span_registry.c:sampling_set_context Unexecuted instantiation: sampling_conditions.c:sampling_set_context Unexecuted instantiation: sampling_cond_status_codes.c:sampling_set_context Unexecuted instantiation: sampling_cond_latency.c:sampling_set_context Unexecuted instantiation: sampling_cond_attribute.c:sampling_set_context Unexecuted instantiation: sampling_cond_string_attribute.c:sampling_set_context Unexecuted instantiation: sampling_cond_numeric_attribute.c:sampling_set_context Unexecuted instantiation: sampling_cond_boolean_attribute.c:sampling_set_context Unexecuted instantiation: sampling_cond_span_count.c:sampling_set_context Unexecuted instantiation: sampling_cond_trace_state.c:sampling_set_context Unexecuted instantiation: sampling_tail.c:sampling_set_context Unexecuted instantiation: sampling_probabilistic.c:sampling_set_context |
137 | | |
138 | | /* sampling_conf */ |
139 | | int sampling_config_process_rules(struct flb_config *config, struct sampling *ctx); |
140 | | |
141 | | int sampling_config_map_set(struct flb_config *config, struct sampling *ctx, void *plugin_ctx, struct flb_config_map *map); |
142 | | |
143 | | struct sampling *sampling_config_create(struct flb_processor_instance *processor_instance, |
144 | | struct flb_config *config); |
145 | | void sampling_config_destroy(struct flb_config *config, struct sampling *ctx); |
146 | | |
147 | | /* conditions */ |
148 | | struct sampling_conditions *sampling_conditions_create(struct sampling *ctx, struct cfl_variant *conditions); |
149 | | int sampling_conditions_check(struct sampling *ctx, struct sampling_conditions *sampling_conditions, |
150 | | struct trace_entry *trace_entry, struct ctrace_span *span); |
151 | | |
152 | | void sampling_conditions_destroy(struct sampling_conditions *sampling_conditions); |
153 | | |
154 | | /* |
155 | | * conditions types |
156 | | * ---------------- |
157 | | */ |
158 | | |
159 | | /* condition: status_codes_check */ |
160 | | struct sampling_condition *cond_status_codes_create(struct sampling *ctx, |
161 | | struct sampling_conditions *sampling_conditions, |
162 | | struct cfl_variant *settings); |
163 | | int cond_status_codes_check(struct sampling_condition *sampling_condition, struct ctrace_span *span); |
164 | | void cond_status_codes_destroy(struct sampling_condition *sampling_condition); |
165 | | |
166 | | /* condition: latency */ |
167 | | struct sampling_condition *cond_latency_create(struct sampling *ctx, |
168 | | struct sampling_conditions *sampling_conditions, |
169 | | struct cfl_variant *settings); |
170 | | int cond_latency_check(struct sampling_condition *sampling_condition, struct ctrace_span *span); |
171 | | void cond_latency_destroy(struct sampling_condition *sampling_condition); |
172 | | |
173 | | /* condition: string_attribute */ |
174 | | struct sampling_condition *cond_string_attr_create(struct sampling *ctx, |
175 | | struct sampling_conditions *sampling_conditions, |
176 | | struct cfl_variant *settings); |
177 | | int cond_string_attr_check(struct sampling_condition *sampling_condition, struct ctrace_span *span); |
178 | | void cond_string_attr_destroy(struct sampling_condition *sampling_condition); |
179 | | |
180 | | /* condition: numeric_attribute */ |
181 | | struct sampling_condition *cond_numeric_attr_create(struct sampling *ctx, |
182 | | struct sampling_conditions *sampling_conditions, |
183 | | struct cfl_variant *settings); |
184 | | void cond_numeric_attr_destroy(struct sampling_condition *sampling_condition); |
185 | | |
186 | | |
187 | | /* condition: boolean_attribute */ |
188 | | struct sampling_condition *cond_boolean_attr_create(struct sampling *ctx, |
189 | | struct sampling_conditions *sampling_conditions, |
190 | | struct cfl_variant *settings); |
191 | | void cond_boolean_attr_destroy(struct sampling_condition *sampling_condition); |
192 | | |
193 | | /* condition: span_count */ |
194 | | int cond_span_count_check(struct sampling_condition *sampling_condition, struct trace_entry *trace_entry, struct ctrace_span *span); |
195 | | |
196 | | struct sampling_condition *cond_span_count_create(struct sampling *ctx, |
197 | | struct sampling_conditions *sampling_conditions, |
198 | | struct cfl_variant *settings); |
199 | | void cond_span_count_destroy(struct sampling_condition *sampling_condition); |
200 | | |
201 | | /* condition: trace_state */ |
202 | | int cond_trace_state_check(struct sampling_condition *sampling_condition, struct ctrace_span *span); |
203 | | struct sampling_condition *cond_trace_state_create(struct sampling *ctx, |
204 | | struct sampling_conditions *sampling_conditions, |
205 | | struct cfl_variant *settings); |
206 | | void cond_trace_state_destroy(struct sampling_condition *sampling_condition); |
207 | | |
208 | | #endif |