This repository was archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Support backend of opentsdb #36
Open
ccl0326
wants to merge
7
commits into
github:master
Choose a base branch
from
ccl0326:opentsdb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4716a0e
use a longer default expire period. fixes #33
4155c54
Merge pull request #34 from znull/default-config
d863686
add backend of opentsdb
8de6885
fix typo
1aaa266
fix bad point param
7e9745f
remove fake comment
b4f1acc
fix tab style in makefile
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| 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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
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.
I think we don't really care about errno here, the correct logic has been returned.