summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2024-08-01 14:55:40 +0800
committerchenzizhan <[email protected]>2024-08-01 14:55:40 +0800
commit6d98df95300d5ddae1111c559cae96e52e38d900 (patch)
tree1a3907fabc92ca4f577c5a847ffd6e3b1a9312f2
parentae4c5b46775a0b730169c2d3b13190163d50f262 (diff)
name of exporter is also a global tag
-rw-r--r--include/fieldstat/fieldstat_exporter.h5
-rw-r--r--src/exporter/cjson_exporter.c40
-rw-r--r--src/exporter/fieldstat_exporter.py13
-rw-r--r--src/fieldstat_easy.c20
-rw-r--r--test/test_exporter_json.cpp9
5 files changed, 30 insertions, 57 deletions
diff --git a/include/fieldstat/fieldstat_exporter.h b/include/fieldstat/fieldstat_exporter.h
index dfa5782..4f1a123 100644
--- a/include/fieldstat/fieldstat_exporter.h
+++ b/include/fieldstat/fieldstat_exporter.h
@@ -15,8 +15,7 @@ struct fieldstat_json_exporter;
struct fieldstat_json_exporter *fieldstat_json_exporter_new();
void fieldstat_json_exporter_set_global_dimension(struct fieldstat_json_exporter *exporter, const struct field tag_list[], size_t n_field);
-void fieldstat_json_exporter_set_name(struct fieldstat_json_exporter *exporter, const char *name);
-// todo: 这个删除,对于easy,额外记录下,其实就是单独给奇怪的global tag 开个后门,python 和 .c 之间达成约定
+
void fieldstat_json_exporter_free(struct fieldstat_json_exporter *exporter);
/*
@@ -76,7 +75,7 @@ char *fieldstat_json_exporter_export_flat(const struct fieldstat_json_exporter *
let json exporter output delta value by the side of the original value(accumulated). If a cell / metric is new, the delta value is the same as the original value.
Outputting delta value is disabled by default.
When the exporter name or exporter global tags are changed, or the fieldstat instance is reset, or one of the cube is deleted, the delta value will be reset. (next output will be the original value)
- it is recommended to call this function after fieldstat_json_exporter_set_name and fieldstat_json_exporter_set_global_dimension.
+ it is recommended to call this function after fieldstat_json_exporter_set_global_dimension.
Only affects the metrics of counter type.
Outputting delta value is time-consuming.
*/
diff --git a/src/exporter/cjson_exporter.c b/src/exporter/cjson_exporter.c
index 81d015d..31727c2 100644
--- a/src/exporter/cjson_exporter.c
+++ b/src/exporter/cjson_exporter.c
@@ -19,7 +19,6 @@
An example:
[
{
- "name":"exporter_name",
"tags":[
{"device ID":123},
{"CLIENT IP":"123.1.1.1"}
@@ -32,7 +31,6 @@
"timestamp_ms":123456789
}.
{
- "name":"exporter_name",
"tags":[
{"device ID":123},
{"CLIENT IP":"1.2.3.4"}
@@ -45,7 +43,6 @@
}
]
*/
-#define DEFAULT_EXPORTER_NAME "-"
#define NIL_TAG_JSON "\anil"
struct counter_history;
@@ -60,7 +57,6 @@ struct export_kv_pair {
};
struct fieldstat_json_exporter {
- char *name;
struct field_list *global_tag_list;
struct counter_history *history;
long long last_ts;
@@ -101,7 +97,6 @@ struct counter_history {
struct tag_metric_map *rec;
long long ts;
- char *exporter_name;
struct field_list *global_tag_list;
};
@@ -156,7 +151,6 @@ long long cal_ms_time(const struct timeval *ts)
struct counter_history *counter_history_new()
{
struct counter_history *history = calloc(1, sizeof(struct counter_history));
- history->exporter_name = strdup(DEFAULT_EXPORTER_NAME);
return history;
}
@@ -206,8 +200,6 @@ void counter_history_free(struct counter_history *history)
free(tag_node);
}
- free(history->exporter_name);
-
if (history->global_tag_list != NULL) {
fieldstat_field_list_arr_free(history->global_tag_list, 1);
}
@@ -286,17 +278,9 @@ bool counter_history_check_if_need_to_update(const struct fieldstat_json_exporte
if (history == NULL) {
return false; // delta export disabled
}
- if (history->exporter_name == NULL) { // first time
- return true;
- }
- const char *cur_exporter_name = exporter->name ? exporter->name : DEFAULT_EXPORTER_NAME;
const struct field_list *cur_global_tag_list = exporter->global_tag_list;
- if (strcmp(history->exporter_name, cur_exporter_name) != 0) {
- return true;
- }
-
if (history->global_tag_list == NULL && cur_global_tag_list != NULL) {
return true;
}
@@ -313,11 +297,8 @@ bool counter_history_check_if_need_to_update(const struct fieldstat_json_exporte
void counter_history_fill_version_info(struct counter_history *history, struct fieldstat_json_exporter *exporter, const struct fieldstat *instance)
{
- const char *cur_exporter_name = exporter->name ? exporter->name : DEFAULT_EXPORTER_NAME;
const struct field_list *cur_global_tag_list = exporter->global_tag_list;
- free(history->exporter_name);
- history->exporter_name = strdup(cur_exporter_name);
if (cur_global_tag_list != NULL) {
history->global_tag_list = field_list_dup(cur_global_tag_list);
}
@@ -770,7 +751,6 @@ int add_object_to_json_array(char **buf, int *buf_len, int start, const char *st
}
/*
{
- "name":<exporter name>
"tags": {
"device_id":<>
"IP":<>
@@ -826,10 +806,7 @@ void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *
write_delta_to_json((struct fieldstat_json_exporter *)exporter, &tag_field_pair[i], field_json_delta);
}
- const char *tmp_name = exporter->name ? exporter->name : DEFAULT_EXPORTER_NAME;
-
json_writer_start_map(root);
- json_writer_str_field(root, "name", tmp_name, strlen(tmp_name));
json_writer_object_item(root, "tags", current->tag_json_obj);
json_writer_object_item(root, "fields", field_json);
if (exporter->history != NULL) {
@@ -909,8 +886,6 @@ char *fieldstat_json_exporter_export_with_delta(const struct fieldstat_json_expo
for (int i = 0; i < n_pair_acc; i++) {
struct cellwise_rec_for_export *current = &expair_acc[i];
- const char *tmp_name = exporter->name ? exporter->name : DEFAULT_EXPORTER_NAME;
-
struct json_writer *field_json = json_writer_init();
for (int j = 0; j < expair_acc[i].n_metric; j++) {
kv_pair_write_to_json(expair_acc[i].metric_pairs[j], field_json);
@@ -924,7 +899,6 @@ char *fieldstat_json_exporter_export_with_delta(const struct fieldstat_json_expo
}
json_writer_start_map(root);
- json_writer_str_field(root, "name", tmp_name, strlen(tmp_name));
json_writer_object_item(root, "tags", current->tag_json_obj);
json_writer_object_item(root, "fields", field_json);
json_writer_object_item(root, "fields_delta", field_json_delta);
@@ -967,7 +941,6 @@ char *fieldstat_json_exporter_export_with_delta(const struct fieldstat_json_expo
struct fieldstat_json_exporter *fieldstat_json_exporter_new()
{
struct fieldstat_json_exporter *exporter = (struct fieldstat_json_exporter *)malloc(sizeof(struct fieldstat_json_exporter));
- exporter->name = NULL;
exporter->global_tag_list = NULL;
exporter->history = NULL;
exporter->last_ts = 0;
@@ -1006,19 +979,8 @@ void fieldstat_json_exporter_set_global_dimension(struct fieldstat_json_exporter
}
}
-void fieldstat_json_exporter_set_name(struct fieldstat_json_exporter *exporter, const char *name)
-{
- if (exporter->name != NULL) {
- free(exporter->name);
- }
- exporter->name = strdup(name);
-}
-
void fieldstat_json_exporter_free(struct fieldstat_json_exporter *exporter)
{
- if (exporter->name != NULL) {
- free(exporter->name);
- }
if (exporter->global_tag_list != NULL) {
fieldstat_field_list_arr_free(exporter->global_tag_list, 1);
}
@@ -1060,8 +1022,6 @@ void fieldstat_json_exporter_export_flat_array(const struct fieldstat_json_expor
kv_pair_write_to_json(tag_field_pair[i].metric_pairs[j], root);
}
- const char *tmp_name = exporter->name ? exporter->name : DEFAULT_EXPORTER_NAME;
- json_writer_str_field(root, "name", tmp_name, strlen(tmp_name));
json_writer_longlong_field(root, "timestamp_ms", timestamp_ms);
char *cjson_str;
diff --git a/src/exporter/fieldstat_exporter.py b/src/exporter/fieldstat_exporter.py
index d4e5b2c..7b21451 100644
--- a/src/exporter/fieldstat_exporter.py
+++ b/src/exporter/fieldstat_exporter.py
@@ -209,6 +209,12 @@ class PrometheusExporter:
print("Invalid JSON:", e)
fcntl.flock(file, fcntl.LOCK_UN)
for item in json_data:
+ # the item has __name__ in its global tag, move it to the name
+ if "__name__" in item["tags"]:
+ item["name"] = item["tags"]["__name__"]
+ item["tags"].pop("__name__", None)
+ else:
+ item["name"] = "-"
payload += self.__build_metrics(item)
return payload
@@ -645,6 +651,13 @@ class LocalExporter:
fcntl.flock(fd, fcntl.LOCK_EX)
try:
objects = json.load(fd)
+ for obj in objects:
+ if "__name__" in obj["tags"]:
+ obj["name"] = obj["tags"]["__name__"]
+ obj["tags"].pop("__name__", None)
+ else:
+ obj["name"] = "-"
+
except json.JSONDecodeError as e:
print("Invalid JSON:", e)
fcntl.flock(fd, fcntl.LOCK_UN)
diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c
index 3adc5da..19092ea 100644
--- a/src/fieldstat_easy.c
+++ b/src/fieldstat_easy.c
@@ -127,13 +127,23 @@ struct fieldstat_easy *fieldstat_easy_new(int max_thread_num, const char *name,
fse->accumulate = fieldstat_fork(fse->delta);
fse->exporter = fieldstat_json_exporter_new();
- if (global_dimensions != NULL && n_dimension > 0) {
- fieldstat_json_exporter_set_global_dimension(fse->exporter, global_dimensions, n_dimension);
- }
+
if (name != NULL) {
- fieldstat_json_exporter_set_name(fse->exporter, name);
+ struct field *global_dimension_more = malloc(sizeof(struct field) * (n_dimension + 1));
+ global_dimension_more[0].key = "__name__";
+ global_dimension_more[0].value_str = name;
+ global_dimension_more[0].type = FIELD_VALUE_CSTRING;
+ for (size_t i = 0; i < n_dimension; i++) {
+ global_dimension_more[i + 1] = global_dimensions[i];
+ }
+ fieldstat_json_exporter_set_global_dimension(fse->exporter, global_dimension_more, n_dimension + 1);
+ free(global_dimension_more);
+ } else {
+ if (global_dimensions != NULL && n_dimension > 0) {
+ fieldstat_json_exporter_set_global_dimension(fse->exporter, global_dimensions, n_dimension);
+ }
}
-
+
pthread_spin_init(&fse->outputting_lock, PTHREAD_PROCESS_PRIVATE);
for (int i = 0; i < max_thread_num; i++) {
diff --git a/test/test_exporter_json.cpp b/test/test_exporter_json.cpp
index ff38970..7794bb4 100644
--- a/test/test_exporter_json.cpp
+++ b/test/test_exporter_json.cpp
@@ -745,7 +745,6 @@ TEST(export_test, enable_delta_and_export_instance_with_many_cells_with_global_t
// export test
struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new();
fieldstat_json_exporter_set_global_dimension(fieldstat_json_exporter, TEST_TAG_GLOBAL, 3);
- fieldstat_json_exporter_set_name(fieldstat_json_exporter, "test_instance");
fieldstat_json_exporter_enable_delta(fieldstat_json_exporter);
char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL);
cJSON *root_arr = cJSON_Parse(json_string);
@@ -864,14 +863,6 @@ void test_reset_one_round(std::function<void(struct fieldstat *, struct fieldsta
fieldstat_free(instance);
}
-TEST(export_test, enable_delta_and_reset_on_change_exporter_name) {
- auto trigger = [](struct fieldstat *instance, struct fieldstat_json_exporter *fieldstat_json_exporter) {
- fieldstat_json_exporter_set_name(fieldstat_json_exporter, "test_instance");
- };
-
- test_reset_one_round(trigger);
-}
-
TEST(export_test, enable_delta_and_reset_on_change_exporter_tag) {
auto trigger = [](struct fieldstat *instance, struct fieldstat_json_exporter *fieldstat_json_exporter) {
fieldstat_json_exporter_set_global_dimension(fieldstat_json_exporter, TEST_TAG_GLOBAL, 3);