summaryrefslogtreecommitdiff
path: root/inc/fieldstat.h
blob: c031ba9b2c991c2884a18cf085e18c117d11f6ce (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#pragma once
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif

enum field_type
{
	FIELD_TYPE_COUNTER,
	FIELD_TYPE_GAUGE,
	FILED_TYPE_HISTOGRAM,
	FIELD_TYPE_SUMMARY
};


struct fieldstat_tag{
	const char *key;
	int value_type;  // 0 int, 1 double, 2 str
	union{
		long long value_int;
		double value_double;
		const char *value_str;
	};
};

struct fieldstat_instance;
struct fieldstat_dynamic_instance;

/**
 * Create fieldstat instance.
 * The operation should be executed in single-threaded fashion.
 * @param name The instance name.
 * @return Instance ptr. NULL failed, Not NULL successd.
 */
struct fieldstat_instance * fieldstat_instance_new(const char *name);
/**
 * Create prometheus endpoint. 
 * The operation should be executed in single-threaded fashion.
 * @param listen_port The endpoint listen port. i.e., 80,8080
 * @param url_path The endpoint url path. url path is "/metrics" when url path is NULL.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, const char *url_path);
/**
 * Enable fieldstat instance output prometheus
 * The operation should be executed in single-threaded fashion.
 * @param instance The fieldstat instance.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_enable_prometheus_output(struct fieldstat_instance *instance);
/**
 * Enable output metric to statsd server.
 * The operation should be executed in single-threaded fashion.
 * @param instance The fieldstat instance.
 * @param ip  Statsd server ip.
 * @param port Statsd server port. i.e., 80,8080
 * @return -1 is failed. 0 is success.
 */
int fieldstat_set_statsd_server(struct fieldstat_instance *instance, const char *ip, unsigned short port);
/**
 * Enable output metric to line protocol server.
 * The operation should be executed in single-threaded fashion.
 * @param instance The fieldstat instance.
 * @param ip  Line protocol server ip.
 * @param port Line protocol server port. i.e., 80,8080
 * @return -1 is failed. 0 is success.
 */
int fieldstat_set_line_protocol_server(struct fieldstat_instance *instance, const char *ip, unsigned short port);
/**
 * Enable output metric to file.
 * The operation should be executed in single-threaded fashion.
 * @param instance The fieldstat instance.
 * @param filename The output target filename.
 * @param format The output format. value in {"json","default"}
 * @return -1 is failed. 0 is success.
 */
int fieldstat_set_local_output(struct fieldstat_instance *instance, const char *filename, const char *format);
/**
 * Disable the background thread.
 * The operation should be executed in single-threaded fashion.
 * @param instance The fieldstat instance.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_disable_background_thread(struct fieldstat_instance *instance);
/**
 * Set the output interval in milliseconds
 * The operation should be executed in single-threaded fashion.
 * @param seconds  In milliseconds.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_set_output_interval(struct fieldstat_instance *instance, int milliseconds);
/**
 * Register counter and gauge type metrics
 * @param instance The fieldstat instance.
 * @param type counter, gauge.
 * @param field_name Metric field name.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @return metric id. -1 is failed. >=0 is success.
 */
int fieldstat_register(struct fieldstat_instance *instance, enum field_type type, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag);
/**
 * Register table. Max table num is 64. Max table columns is 64.
 * @param instance The fieldstat instance.
 * @param table_name  The table name.
 * @param column_name column name array.
 * @param column_type column type array. Type in [counter, gauge]
 * @param n_column size of column_type[] and column_name[]
 * @return table id.  -1 is failed. >=0 is success.
 */
int fieldstat_register_table(struct fieldstat_instance *instance, const char *table_name, const char *column_name[], enum field_type column_type[], size_t n_column);
/**
 * Register table row
 * @param instance The fieldstat instance.
 * @param table_id The table id by fieldstat_register_table create.
 * @param row_name The table row name.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param output_metric_ids Output metric ids
 * @return -1 is failed. 0 is success.
 */
int fieldstat_register_table_row(struct fieldstat_instance *instance, int table_id, const char *row_name, const struct fieldstat_tag tags[],size_t n_tag, int output_metric_ids[]);
/**
 * Increment fieldstat metric value by metric_id.
 * @param instance The fieldstat instance.
 * @param metric_id The metric id.
 * @param increment The value to increment.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_value_incrby(struct fieldstat_instance *instance, int metric_id, long long increment);
/**
 * Set fieldstat metric value by metric id.
 * @param instance The fieldstat instance.
 * @param metric_id The metric id.
 * @param value The value to set.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_value_set(struct fieldstat_instance *instance, int metric_id, long long value);
/**
 * Decrement fieldstat metric value.
 * @param instance The fieldstat instance.
 * @param metric_id The metric id.
 * @param decrement The value to decrement.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_value_decrby(struct fieldstat_instance *instance, int metric_id, long long decrment);
/**
 * Make the fieldstat instance affect.
 * @param instance The fieldstat instance.
 */
void fieldstat_instance_start(struct fieldstat_instance *instance);
/**
 * Passive output
 * @param instance The fieldstat instance.
 */
void fieldstat_passive_output(struct fieldstat_instance *instance);
/**
 * Register histogram metric
 * @param instance fieldstat instance
 * @param field_name  field name
 * @param tags        The tag array
 * @param n_tag       Size of tags
 * @param upper_inclusive_bounds The upper inclusive bound of histogram
 * @param lowest_trackable_value The smallest possible value to be put into the
 * histogram.
 * @param highest_trackable_value The largest possible value to be put into the
 * histogram.
 * @param significant_figures The level of precision for this histogram, i.e. the number
 * of figures in a decimal number that will be maintained.  E.g. a value of 3 will mean
 * the results from the histogram will be accurate up to the first three digits.  Must
 * be a value between 1 and 5 (inclusive).
 * @param output_window The of output data. 1: output the data in the time interval window, 0: output data all the time.
 * @return metric id: -1 is failed,  >= 0 is success
 */
int fieldstat_register_histogram(struct fieldstat_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag, 
		const char *upper_inclusive_bounds, const long long lowest_trackable_value, long long highest_trackable_value, int significant_figures, int output_window);

/**
 * Register summary metric
 * @param instance fieldstat instance
 * @param field_name  field name
 * @param tags		  The tag array
 * @param n_tag       Size of tags
 * @param quantiles   the quantiles of summary output
 * @param lowest_trackable_value The smallest possible value to be put into the
 * histogram.
 * @param highest_trackable_value The largest possible value to be put into the
 * histogram.
 * @param significant_figures The level of precision for this histogram, i.e. the number
 * of figures in a decimal number that will be maintained.  E.g. a value of 3 will mean
 * the results from the histogram will be accurate up to the first three digits.  Must
 * be a value between 1 and 5 (inclusive).
 * @param output_window The of output data. 1: output the data in the time interval window, 0: output data all the time.
 * @return metric id: -1 is failed,  >= 0 is success
 */
int fieldstat_register_summary(struct fieldstat_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag, 
		const char *quantiles, const long long lowest_trackable_value, long long highest_trackable_value, int significant_figures, int output_window);

/**
 * Register dynamic fieldstat instance.
 * @param instance fieldstat instance
 * @param name The instance name.
 * @param n_thread The count of thread.
 * @return  ptr dynamic_instance. NULL is failad. Not NULL is successd.
 */
struct fieldstat_dynamic_instance * fieldstat_dynamic_instance_new(const char *name, int n_thread);

/**
 * Enable output metric to line protocol server.
 * The operation should be executed in single-threaded fashion.
 * @param instance The fieldstat instance.
 * @param ip  Line protocol server ip.
 * @param port Line protocol server port. i.e., 80,8080
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_set_line_protocol_server(struct fieldstat_dynamic_instance *instance, const char *ip, unsigned short port);
/**
 * Disable the dynamic metric output background thread.
 * The operation should be executed in single-threaded fashion.
 * @param instance The fieldstat instance.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_disable_background_thread(struct fieldstat_dynamic_instance *instance);
/**
 * Set the dynamic metric output interval in milliseconds
 * The operation should be executed in single-threaded fashion.
 * @param seconds  In milliseconds.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_set_output_interval(struct fieldstat_dynamic_instance *instance, int milliseconds);
/**
 * Dynamic metrics passive output
 * @param instance The fieldstat instance.
 */
void fieldstat_dynamic_passive_output(struct fieldstat_dynamic_instance *instance);
/**
 * Make the fieldstat dynamic instance affect.
 * @param instance The fieldstat instance.
 */
void fieldstat_dynamic_instance_start(struct fieldstat_dynamic_instance *instance);
/**
 * Register table. Max table num is 64. Max table columns is 64.
 * @param instance The fieldstat dynamic instance.
 * @param table_name  The table name.
 * @param column_name column name array.
 * @param column_type column type array. Type in [counter, gauge]
 * @param n_column size of column_type[] and column_name[]
 * @param out_column_ids output metric id in table row.
 * @return table id.  -1 is failed. >=0 is success.
 */
int fieldstat_register_dynamic_table(struct fieldstat_dynamic_instance *instance, const char *table_name, const char *column_name[], enum field_type column_type[], size_t n_column, unsigned int out_column_ids[]);
/**
 * fieldstat dynamic instance metric value increment operate. metirc type in[gauge, counter]
 * @param instance The fieldstat dynamic instance.
 * @param type The metric type.
 * @param field_name The metric field name.
 * @param value The increment value.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_metric_value_incrby(struct fieldstat_dynamic_instance *instance, enum field_type type, const char *field_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat dynamic instance metric value set operate. metirc type in[gauge, counter]
 * @param instance The fieldstat dynamic instance.
 * @param type The metric type.
 * @param field_name The metric field name.
 * @param value The set value.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_metric_value_set(struct fieldstat_dynamic_instance *instance, enum field_type type, const char *field_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat dynamic instance metric value decrment operate. metirc type in[gauge, counter]
 * @param instance The fieldstat dynamic instance.
 * @param type The metric type.
 * @param field_name The metric field name.
 * @param value The decrment value.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_metric_value_decrby(struct fieldstat_dynamic_instance *instance, enum field_type type, const char *field_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat dynamic instance table metric value incrment operate.
 * @param instance The fieldstat dynamic instance.
 * @param table_id The table id.
 * @param column_id The column id.
 * @param row_name The row name of table.
 * @param value The incrment value.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_table_metric_value_incrby(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat dynamic instance table metric value set operate.
 * @param instance The fieldstat dynamic instance.
 * @param table_id The table id.
 * @param column_id The column id.
 * @param row_name The row name of table.
 * @param value The set value.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_table_metric_value_set(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat dynamic instance table metric value decrment operate.
 * @param instance The fieldstat dynamic instance.
 * @param table_id The table id.
 * @param column_id The column id.
 * @param row_name The row name of table.
 * @param value The decrment value.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_table_metric_value_decrby(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat instance free.
 * @param instance The fieldstat instance need to free.
 */
void fieldstat_instance_free(struct fieldstat_instance *instance);
/**
 * fieldstat dynamic instance free.
 * @param instance The fieldstat dynamic instance need to free.
 */
void fieldstat_dynamic_instance_free(struct fieldstat_dynamic_instance *instance);
/**
 * fieldstat dynamic instance table metric value get operate.
 * @param instance The fieldstat dynamic instance.
 * @param table_id The table id.
 * @param column_id The column id.
 * @param row_name The row name of table.
 * @param value The set value.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return  long long value.
 */
long long fieldstat_dynamic_table_metric_value_get(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat dynamic instance metric value get operate. metirc type in[gauge, counter]
 * @param instance The fieldstat dynamic instance.
 * @param field_name The metric field name.
 * @param tags The tag array.
 * @param n_tag Size of tags[]
 * @param thread_id The thread id of the call.
 * @return long long value.
 */
long long fieldstat_dynamic_metric_value_get(struct fieldstat_dynamic_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tags, int thread_id);
/**
 * fieldstat set metric ratio parameter. 
 * @param instance The fieldstat instance.
 * @param metric_id  The metric id need to set ratio.
 * @param numerator_metric_id The metric id as numerator.
 * @param denominator_metric_id The metric id as denominator.
 * @return  -1 is failed. 0 is success.
 */
int fieldstat_set_metric_ratio_para(struct fieldstat_instance *instance, int metric_id, int numerator_metric_id, int denominator_metric_id, int output_scaling);

/**
 * fieldstat set metric invisible parameter. 
 * @param instance The fieldstat instance.
 * @param metric_id  The metric id need to set invisible.
 * @return  -1 is failed. 0 is success.
 */
int fieldstat_set_metric_invisible_para(struct fieldstat_instance *instance, int metric_id);


/**
 * fieldstat dynamic instance table row metric values incrment operate.
 * @param instance The fieldstat dynamic instance.
 * @param table_id The table id.
 * @param row_name The row name of table.
 * @param values The value array.
 * @param n_values Size of value array.
 * @param tags The tag array.
 * @param n_tags Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_table_row_metric_values_incrby(
    struct fieldstat_dynamic_instance *instance,
    int table_id,
    const char *row_name,
    long long values[],
    size_t n_values,
    const struct fieldstat_tag tags[],
    size_t n_tags,
    int thread_id);

/**
 * fieldstat dynamic instance table row metric values decrment operate.
 * @param instance The fieldstat dynamic instance.
 * @param table_id The table id.
 * @param row_name The row name of table.
 * @param values The value array.
 * @param n_values Size of value array.
 * @param tags The tag array.
 * @param n_tags Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_table_row_metric_values_decrby(
    struct fieldstat_dynamic_instance *instance,
    int table_id,
    const char *row_name,
    long long values[],
    size_t n_values,
    const struct fieldstat_tag tags[],
    size_t n_tags,
    int thread_id);

/**
 * fieldstat dynamic instance table row metric values set operate.
 * @param instance The fieldstat dynamic instance.
 * @param table_id The table id.
 * @param row_name The row name of table.
 * @param values The value array.
 * @param n_values Size of value array.
 * @param tags The tag array.
 * @param n_tags Size of tags[]
 * @param thread_id The thread id of the call.
 * @return -1 is failed. 0 is success.
 */
int fieldstat_dynamic_table_row_metric_values_set(
    struct fieldstat_dynamic_instance *instance,
    int table_id,
    const char *row_name,
    long long values[],
    size_t n_values,
    const struct fieldstat_tag tags[],
    size_t n_tags,
    int thread_id);


#ifdef __cplusplus
}
#endif