Skip to content

Commit

Permalink
new cmake option to write or write not a timestamp to influxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilKoe committed Feb 2, 2024
1 parent d5e8771 commit d8d1987
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
4 changes: 4 additions & 0 deletions libiotrace/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ set(ENABLE_OUTPUT_VALUES "LOGFILE_AND_INFLUXDB;LOGFILE;INFLUXDB")
set_property(CACHE ENABLE_OUTPUT PROPERTY STRINGS ${ENABLE_OUTPUT_VALUES})
list(APPEND LIBIOTRACE_COMPILE_OPTIONS -DENABLE_OUTPUT=${ENABLE_OUTPUT})

option (WRITE_INFLUX_TIMESTAMP "send function end time to influxdb" ON)
if(WRITE_INFLUX_TIMESTAMP)
list(APPEND LIBIOTRACE_COMPILE_OPTIONS -DWRITE_INFLUX_TIMESTAMP=${WRITE_INFLUX_TIMESTAMP})
endif()

# Let the user chose the gettime functionality
# XXX Instead of hard-coding these settings, they should be chosen dependent on availability...
Expand Down
49 changes: 36 additions & 13 deletions libiotrace/src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ void send_data(const char *message, SOCKET socket) {
} else {
LOG_ERROR_AND_DIE("send() returned %d, errno: %d, socket: %d", bytes_sent,
errno, socket);

}
} else {
if ((size_t)bytes_sent < bytes_to_send) {
Expand Down Expand Up @@ -2215,9 +2216,9 @@ void init_process() {
#endif

#ifdef IOTRACE_ENABLE_INFLUXDB
if (-1 == socket_peer) {
prepare_socket();
}
if (-1 == socket_peer) {
prepare_socket();
}
#endif

/* at this point all preparations necessary for a wrapper call
Expand Down Expand Up @@ -2392,8 +2393,9 @@ void write_into_influxdb(struct basic *data) {
}

if (-1 == socket_peer) {
prepare_socket();
}
LOG_WARN("write_into_influxdb called before thread was initialized (new socket: %d).", socket_peer);
prepare_socket();
}

//buffer for body
int body_length = libiotrace_struct_push_max_size_basic(0) + 1; /* +1 for trailing null character (function build by macros; gives length of body to send) */
Expand Down Expand Up @@ -2465,18 +2467,24 @@ void write_into_influxdb(struct basic *data) {
#endif
body_labels_length = strlen(body_labels);

#ifdef WRITE_INFLUX_TIMESTAMP
int timestamp_length = COUNT_DEC_AS_CHAR(data->time_end);
char timestamp[timestamp_length];
#ifdef REALTIME
# ifdef REALTIME
snprintf(timestamp, sizeof(timestamp), "%" PRIu64, data->time_end);
#else
# else
snprintf(timestamp, sizeof(timestamp), "%" PRIu64,
system_start_time + data->time_end);
#endif
# endif
timestamp_length = strlen(timestamp);
#endif

const int content_length = body_labels_length + 1 /*space*/ + body_length + 1 /*space*/
+ timestamp_length;
const int content_length = body_labels_length + 1 /*space*/ + body_length
#ifdef WRITE_INFLUX_TIMESTAMP
+ 1 /*space*/ + timestamp_length;
#else
;
#endif

const char header[] =
"POST /api/v2/write?bucket=%s&precision=ns&org=%s HTTP/1.1" LINE_BREAK
Expand All @@ -2486,17 +2494,32 @@ void write_into_influxdb(struct basic *data) {
"Content-Length: %d" LINE_BREAK
"Content-Type: application/x-www-form-urlencoded" LINE_BREAK
LINE_BREAK
"%s %s %s";
"%s %s"
#ifdef WRITE_INFLUX_TIMESTAMP
" %s";
#else
;
#endif
const int message_length = strlen(header) + influx_bucket_len
+ influx_organization_len + database_ip_len + database_port_len
+ influx_token_len + COUNT_DEC_AS_CHAR(content_length) /* Content-Length */
+ body_labels_length + body_length + timestamp_length;
+ body_labels_length + body_length
#ifdef WRITE_INFLUX_TIMESTAMP
+ timestamp_length;
#else
;
#endif

//buffer all (header + body)
char message[message_length + 1];
snprintf(message, sizeof(message), header, influx_bucket,
influx_organization, database_ip, database_port, influx_token,
content_length, body_labels, body, timestamp);
content_length, body_labels, body
#ifdef WRITE_INFLUX_TIMESTAMP
, timestamp);
#else
);
#endif

send_data(message, socket_peer);
}
Expand Down

0 comments on commit d8d1987

Please sign in to comment.