1 /************************************************************************************* 2 Copyright (C) 2012, 2013 James Slocum 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy of this 5 software and associated documentation files (the "Software"), to deal in the Software 6 without restriction, including without limitation the rights to use, copy, modify, 7 merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 8 permit persons to whom the Software is furnished to do so, subject to the following 9 conditions: 10 11 The above copyright notice and this permission notice shall be included in all copies 12 or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 16 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 17 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 18 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 19 OR OTHER DEALINGS IN THE SOFTWARE. 20 **************************************************************************************/ 21 22 23 #ifndef LIB_STATS_D_H 24 #define LIB_STATS_D_H 25 26 #define ADDAPI 27 #define ADDCALL 28 29 #if defined (_WIN32) 30 //Windows Specific includes 31 #include <winsock2.h> 32 #include <ws2tcpip.h> 33 34 //Undefine the macros, and redefine them to perform 35 //windows specific tasks 36 #undef ADDAPI 37 #undef ADDCALL 38 39 #ifdef ADD_EXPORTS 40 #define ADDAPI __declspec(dllexport) 41 #else 42 #define ADDAPI __declspec(dllimport) 43 #endif 44 45 #define ADDCALL __cdecl 46 47 #else 48 //Unix specific includes 49 #include <sys/types.h> 50 #include <sys/socket.h> 51 #include <arpa/inet.h> 52 #include <netinet/in.h> 53 54 #endif //end of windows/unix/linux OS stuff 55 56 #define STATSD_PORT 8125 57 #define NO_SAMPLE_RATE 0 58 #ifndef BATCH_MAX_SIZE 59 #define BATCH_MAX_SIZE 512 60 #endif 61 62 typedef struct _statsd_t { 63 const char* serverAddress; 64 char ipAddress[128]; 65 const char* nameSpace; 66 const char* bucket; 67 int port; 68 int socketFd; 69 struct sockaddr_in destination; 70 71 int (*random)(void); 72 73 char batch[BATCH_MAX_SIZE]; 74 int batchIndex; 75 } Statsd; 76 77 typedef enum { 78 STATSD_NONE = 0, 79 STATSD_COUNT, 80 STATSD_GAUGE, 81 STATSD_SET, 82 STATSD_TIMING, 83 STATSD_BATCH 84 } StatsType; 85 86 typedef enum { 87 STATSD_SUCCESS = 0, 88 STATSD_BAD_BUCKET, 89 STATSD_SOCKET, 90 STATSD_SOCKET_INIT, 91 STATSD_NTOP, 92 STATSD_MALLOC, 93 STATSD_BAD_SERVER_ADDRESS, 94 STATSD_UDP_SEND, 95 STATSD_BATCH_IN_PROGRESS, 96 STATSD_NO_BATCH, 97 STATSD_BATCH_FULL, 98 STATSD_BAD_STATS_TYPE 99 } StatsError; 100 101 #ifdef __cplusplus 102 extern "C" { 103 #endif 104 105 ADDAPI int ADDCALL statsd_new(Statsd **stats, const char* serverAddress, int port, const char* nameSpace, const char* bucket); 106 ADDAPI void ADDCALL statsd_free(Statsd* statsd); 107 ADDAPI void ADDCALL statsd_release(Statsd* statsd); 108 ADDAPI int ADDCALL statsd_init(Statsd* statsd, const char* server, int port, const char* nameSpace, const char* bucket); 109 ADDAPI int ADDCALL statsd_increment(Statsd* statsd, const char* bucket); 110 ADDAPI int ADDCALL statsd_decrement(Statsd* statsd, const char* bucket); 111 ADDAPI int ADDCALL statsd_count(Statsd* statsd, const char* bucket, int64_t count, double sampleRate); 112 ADDAPI int ADDCALL statsd_gauge(Statsd* statsd, const char* bucket, int64_t value, double sampleRate); 113 ADDAPI int ADDCALL statsd_set(Statsd* statsd, const char* bucket, int64_t value, double sampleRate); 114 ADDAPI int ADDCALL statsd_timing(Statsd* statsd, const char* bucket, int timing, double sampleRate); 115 ADDAPI int ADDCALL statsd_resetBatch(Statsd* statsd); 116 ADDAPI int ADDCALL statsd_addToBatch(Statsd* statsd, StatsType type, const char* bucket, int64_t value, double sampleRate); 117 ADDAPI int ADDCALL statsd_addToBatch_dbl(Statsd* statsd, StatsType type, const char* bucket, double value, double sampleRate); 118 ADDAPI int ADDCALL statsd_sendBatch(Statsd* statsd); 119 120 #ifdef __cplusplus 121 } 122 #endif 123 124 #endif //LIB_STATS_D_H 125