-
Notifications
You must be signed in to change notification settings - Fork 92
Support backend of opentsdb #36
base: master
Are you sure you want to change the base?
Changes from 6 commits
4716a0e
4155c54
d863686
8de6885
1aaa266
7e9745f
b4f1acc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #include <stddef.h> | ||
| #include <string.h> | ||
| #include "brubeck.h" | ||
|
|
||
| static inline int is_connected(struct brubeck_opentsdb *self) | ||
| { | ||
| return (self->out_sock >= 0); | ||
| } | ||
|
|
||
| static int opentsdb_connect(void *backend) | ||
| { | ||
| struct brubeck_opentsdb *self = (struct brubeck_opentsdb *)backend; | ||
|
|
||
| if (is_connected(self)) | ||
| return 0; | ||
|
|
||
| self->out_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
|
|
||
| if (self->out_sock >= 0) { | ||
| int rc = connect(self->out_sock, | ||
| (struct sockaddr *)&self->out_sockaddr, | ||
| sizeof(self->out_sockaddr)); | ||
|
|
||
| if (rc == 0) { | ||
| log_splunk("backend=opentsdb event=connected"); | ||
| sock_enlarge_out(self->out_sock); | ||
| return 0; | ||
| } | ||
|
|
||
| close(self->out_sock); | ||
| self->out_sock = -1; | ||
| } | ||
|
|
||
| log_splunk_errno("backend=opentsdb event=failed_to_connect"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strictly speaking the close() system call may overwrite errno, so you should probably log this just after connect() failed before calling close() and as an "else" block when socket() fails. That has the added benefit of indicating exactly which system call failed.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't really care about errno here, the correct logic has been returned. |
||
| return -1; | ||
| } | ||
|
|
||
| static void opentsdb_disconnect(struct brubeck_opentsdb *self) | ||
| { | ||
| log_splunk_errno("backend=opentsdb event=disconnected"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks a bit odd to me. Why would you log errno before doing any system calls?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The connection is closed by remote here. |
||
|
|
||
| close(self->out_sock); | ||
| self->out_sock = -1; | ||
| } | ||
|
|
||
| static void opentsdb_write( | ||
| const char *key, | ||
| value_t value, | ||
| void *backend) | ||
| { | ||
| struct brubeck_opentsdb *opentsdb = (struct brubeck_opentsdb *)backend; | ||
| char buffer[1024]; | ||
| char *ptr = buffer; | ||
| size_t key_len = strlen(key); | ||
| ssize_t wr; | ||
|
|
||
| if (!is_connected(opentsdb)) | ||
| return; | ||
|
|
||
| strcpy(ptr, "put"); | ||
| ptr += strlen("put"); | ||
| *ptr++ = ' '; | ||
|
|
||
| memcpy(ptr, key, key_len); | ||
| ptr += key_len; | ||
| *ptr++ = ' '; | ||
|
|
||
| ptr += brubeck_itoa(ptr, opentsdb->backend.tick_time); | ||
| *ptr++ = ' '; | ||
|
|
||
| ptr += brubeck_ftoa(ptr, value); | ||
| *ptr++ = ' '; | ||
|
|
||
| strcpy(ptr, opentsdb->tags); | ||
| ptr += strlen(opentsdb->tags); | ||
| *ptr++ = '\n'; | ||
|
|
||
| wr = write_in_full(opentsdb->out_sock, buffer, ptr - buffer); | ||
| if (wr < 0) { | ||
| opentsdb_disconnect(opentsdb); | ||
| return; | ||
| } | ||
|
|
||
| opentsdb->sent += wr; | ||
| } | ||
|
|
||
| struct brubeck_backend * | ||
| brubeck_opentsdb_new(struct brubeck_server *server, json_t *settings, int shard_n) | ||
| { | ||
| struct brubeck_opentsdb *opentsdb = xcalloc(1, sizeof(struct brubeck_opentsdb)); | ||
| char *address; | ||
| int port, frequency = 0; | ||
|
|
||
| json_unpack_or_die(settings, | ||
| "{s:s, s:i, s:i, s:s}", | ||
| "address", &address, | ||
| "port", &port, | ||
| "frequency", &frequency, | ||
| "tags", &(opentsdb->tags)); | ||
|
|
||
| opentsdb->backend.type = BRUBECK_BACKEND_OPENTSDB; | ||
| opentsdb->backend.shard_n = shard_n; | ||
| opentsdb->backend.connect = &opentsdb_connect; | ||
|
|
||
| opentsdb->backend.sample = &opentsdb_write; | ||
| opentsdb->backend.flush = NULL; | ||
|
|
||
| opentsdb->backend.sample_freq = frequency; | ||
| opentsdb->backend.server = server; | ||
| opentsdb->out_sock = -1; | ||
| url_to_inaddr2(&opentsdb->out_sockaddr, address, port); | ||
|
|
||
| brubeck_backend_run_threaded((struct brubeck_backend *)opentsdb); | ||
| log_splunk("backend=opentsdb event=started"); | ||
|
|
||
| return (struct brubeck_backend *)opentsdb; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #ifndef __BRUBECK_OPENTSDB_H__ | ||
| #define __BRUBECK_OPENTSDB_H__ | ||
|
|
||
| struct brubeck_opentsdb { | ||
| struct brubeck_backend backend; | ||
|
|
||
| int out_sock; | ||
| struct sockaddr_in out_sockaddr; | ||
|
|
||
| const char* tags; | ||
| size_t sent; | ||
| }; | ||
|
|
||
| struct brubeck_backend *brubeck_opentsdb_new( | ||
| struct brubeck_server *server, json_t *settings, int shard_n); | ||
|
|
||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if github is deceiving me, but seems to be like you have a tabs vs spaces issue here and below in the enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.^^