Browse code

statsd: clang-format

Victor Seva authored on 29/10/2021 12:23:55
Showing 1 changed files
... ...
@@ -11,124 +11,129 @@
11 11
 #include "../../core/sr_module.h"
12 12
 #include "lib_statsd.h"
13 13
 
14
-static StatsConnection statsd_connection = {
15
-    "127.0.0.1",
16
-    "8125",
17
-    -1
18
-};
19
-
20
-bool statsd_connect(void){
21
-
22
-    struct addrinfo *serverAddr = NULL;
23
-    int rc;
24
-
25
-    if (statsd_connection.sock > 0){
26
-        return true;
27
-    }
28
-
29
-    rc = getaddrinfo(
30
-        statsd_connection.ip, statsd_connection.port,
31
-        NULL, &serverAddr);
32
-    if (rc != 0 || serverAddr == NULL)
33
-    {
34
-        LM_ERR("Statsd: could not initiate server information (%s)\n",
35
-            gai_strerror(rc));
36
-        if(serverAddr) freeaddrinfo(serverAddr);
37
-        return false;
38
-    }
39
-
40
-    statsd_connection.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP);
41
-    if (statsd_connection.sock < 0 ){
42
-        LM_ERR("Statsd: could not create a socket for statsd connection\n");
43
-        freeaddrinfo(serverAddr);
44
-        return false;
45
-    }
46
-
47
-    rc = connect(statsd_connection.sock, serverAddr->ai_addr,
48
-            serverAddr->ai_addrlen);
49
-    freeaddrinfo(serverAddr);
50
-    if (rc < 0){
51
-        LM_ERR("Statsd: could not initiate a connect to statsd\n");
52
-        return false;
53
-    }
54
-    return true;
14
+static StatsConnection statsd_connection = {"127.0.0.1", "8125", -1};
15
+
16
+bool statsd_connect(void)
17
+{
18
+
19
+	struct addrinfo *serverAddr = NULL;
20
+	int rc;
21
+
22
+	if(statsd_connection.sock > 0) {
23
+		return true;
24
+	}
25
+
26
+	rc = getaddrinfo(
27
+			statsd_connection.ip, statsd_connection.port, NULL, &serverAddr);
28
+	if(rc != 0 || serverAddr == NULL) {
29
+		LM_ERR("Statsd: could not initiate server information (%s)\n",
30
+				gai_strerror(rc));
31
+		if(serverAddr)
32
+			freeaddrinfo(serverAddr);
33
+		return false;
34
+	}
35
+
36
+	statsd_connection.sock =
37
+			socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP);
38
+	if(statsd_connection.sock < 0) {
39
+		LM_ERR("Statsd: could not create a socket for statsd connection\n");
40
+		freeaddrinfo(serverAddr);
41
+		return false;
42
+	}
43
+
44
+	rc = connect(statsd_connection.sock, serverAddr->ai_addr,
45
+			serverAddr->ai_addrlen);
46
+	freeaddrinfo(serverAddr);
47
+	if(rc < 0) {
48
+		LM_ERR("Statsd: could not initiate a connect to statsd\n");
49
+		return false;
50
+	}
51
+	return true;
55 52
 }
56 53
 
57
-bool send_command(char *command){
58
-    int send_result;
59
-
60
-    if (!statsd_connect()){
61
-        return false;
62
-    }
63
-
64
-    send_result = send(statsd_connection.sock, command, strlen(command), 0);
65
-    if ( send_result < 0){
66
-        LM_ERR("could not send the correct info to statsd (%i| %s)\n",
67
-            send_result, strerror(errno));
68
-        return true;
69
-    }
70
-    LM_DBG("Sent to statsd (%s)", command);
71
-    return true;
54
+bool send_command(char *command)
55
+{
56
+	int send_result;
57
+
58
+	if(!statsd_connect()) {
59
+		return false;
60
+	}
61
+
62
+	send_result = send(statsd_connection.sock, command, strlen(command), 0);
63
+	if(send_result < 0) {
64
+		LM_ERR("could not send the correct info to statsd (%i| %s)\n",
65
+				send_result, strerror(errno));
66
+		return true;
67
+	}
68
+	LM_DBG("Sent to statsd (%s)", command);
69
+	return true;
72 70
 }
73 71
 
74
-bool statsd_set(char *key, char *value){
75
-   char* end = 0;
76
-   char command[254];
77
-   int val;
78
-   val = strtol(value, &end, 0);
79
-   if (*end){
80
-       LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
81
-       return false;
82
-   }
83
-   snprintf(command, sizeof command, "%s:%i|s\n", key, val);
84
-   return send_command(command);
72
+bool statsd_set(char *key, char *value)
73
+{
74
+	char *end = 0;
75
+	char command[254];
76
+	int val;
77
+	val = strtol(value, &end, 0);
78
+	if(*end) {
79
+		LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
80
+		return false;
81
+	}
82
+	snprintf(command, sizeof command, "%s:%i|s\n", key, val);
83
+	return send_command(command);
85 84
 }
86 85
 
87 86
 
88
-bool statsd_gauge(char *key, char *value){
89
-   char command[254];
90
-   snprintf(command, sizeof command, "%s:%s|g\n", key, value);
91
-   return send_command(command);
87
+bool statsd_gauge(char *key, char *value)
88
+{
89
+	char command[254];
90
+	snprintf(command, sizeof command, "%s:%s|g\n", key, value);
91
+	return send_command(command);
92 92
 }
93 93
 
94
-bool statsd_histogram(char *key, char *value){
95
-   char command[254];
96
-   snprintf(command, sizeof command, "%s:%s|h\n", key, value);
97
-   return send_command(command);
94
+bool statsd_histogram(char *key, char *value)
95
+{
96
+	char command[254];
97
+	snprintf(command, sizeof command, "%s:%s|h\n", key, value);
98
+	return send_command(command);
98 99
 }
99 100
 
100
-bool statsd_count(char *key, char *value){
101
-   char* end = 0;
102
-   char command[254];
103
-   int val;
104
-
105
-   val = strtol(value, &end, 0);
106
-   if (*end){
107
-       LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
108
-       return false;
109
-   }
110
-   snprintf(command, sizeof command, "%s:%i|c\n", key, val);
111
-   return send_command(command);
101
+bool statsd_count(char *key, char *value)
102
+{
103
+	char *end = 0;
104
+	char command[254];
105
+	int val;
106
+
107
+	val = strtol(value, &end, 0);
108
+	if(*end) {
109
+		LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
110
+		return false;
111
+	}
112
+	snprintf(command, sizeof command, "%s:%i|c\n", key, val);
113
+	return send_command(command);
112 114
 }
113 115
 
114
-bool statsd_timing(char *key, int value){
115
-   char command[254];
116
-   snprintf(command, sizeof command, "%s:%i|ms\n", key, value);
117
-   return send_command(command);
116
+bool statsd_timing(char *key, int value)
117
+{
118
+	char command[254];
119
+	snprintf(command, sizeof command, "%s:%i|ms\n", key, value);
120
+	return send_command(command);
118 121
 }
119 122
 
120
-bool statsd_init(char *ip, char *port){
123
+bool statsd_init(char *ip, char *port)
124
+{
121 125
 
122
-    if (ip != NULL){
123
-        statsd_connection.ip = ip;
124
-    }
125
-    if (port != NULL ){
126
-       statsd_connection.port = port;
127
-    }
128
-    return statsd_connect();
126
+	if(ip != NULL) {
127
+		statsd_connection.ip = ip;
128
+	}
129
+	if(port != NULL) {
130
+		statsd_connection.port = port;
131
+	}
132
+	return statsd_connect();
129 133
 }
130 134
 
131
-bool statsd_destroy(void){
132
-    statsd_connection.sock = 0;
133
-    return true;
135
+bool statsd_destroy(void)
136
+{
137
+	statsd_connection.sock = 0;
138
+	return true;
134 139
 }
Browse code

stastd: adding histogram (#2774)

Julien Chavanton authored on 10/06/2021 16:49:47 • GitHub committed on 10/06/2021 16:49:47
Showing 1 changed files
... ...
@@ -91,6 +91,12 @@ bool statsd_gauge(char *key, char *value){
91 91
    return send_command(command);
92 92
 }
93 93
 
94
+bool statsd_histogram(char *key, char *value){
95
+   char command[254];
96
+   snprintf(command, sizeof command, "%s:%s|h\n", key, value);
97
+   return send_command(command);
98
+}
99
+
94 100
 bool statsd_count(char *key, char *value){
95 101
    char* end = 0;
96 102
    char command[254];
Browse code

statsd: simplified error handling inside statsd_connect()

Daniel-Constantin Mierla authored on 24/07/2017 09:44:21
Showing 1 changed files
... ...
@@ -29,25 +29,24 @@ bool statsd_connect(void){
29 29
     rc = getaddrinfo(
30 30
         statsd_connection.ip, statsd_connection.port,
31 31
         NULL, &serverAddr);
32
-    if (rc != 0)
32
+    if (rc != 0 || serverAddr == NULL)
33 33
     {
34
-        LM_ERR(
35
-            "Statsd: could not initiate server information (%s)\n",
34
+        LM_ERR("Statsd: could not initiate server information (%s)\n",
36 35
             gai_strerror(rc));
37
-		if(serverAddr) freeaddrinfo(serverAddr);
36
+        if(serverAddr) freeaddrinfo(serverAddr);
38 37
         return false;
39 38
     }
40 39
 
41 40
     statsd_connection.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP);
42 41
     if (statsd_connection.sock < 0 ){
43 42
         LM_ERR("Statsd: could not create a socket for statsd connection\n");
44
-		if(serverAddr) freeaddrinfo(serverAddr);
43
+        freeaddrinfo(serverAddr);
45 44
         return false;
46 45
     }
47 46
 
48
-    rc = connect(
49
-        statsd_connection.sock, serverAddr->ai_addr, serverAddr->ai_addrlen);
50
-	freeaddrinfo(serverAddr);
47
+    rc = connect(statsd_connection.sock, serverAddr->ai_addr,
48
+            serverAddr->ai_addrlen);
49
+    freeaddrinfo(serverAddr);
51 50
     if (rc < 0){
52 51
         LM_ERR("Statsd: could not initiate a connect to statsd\n");
53 52
         return false;
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -8,7 +8,7 @@
8 8
 #include <math.h>
9 9
 #include <errno.h>
10 10
 
11
-#include "../../sr_module.h"
11
+#include "../../core/sr_module.h"
12 12
 #include "lib_statsd.h"
13 13
 
14 14
 static StatsConnection statsd_connection = {
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,129 @@
1
+#include <stdio.h>
2
+#include <sys/socket.h>
3
+#include <netinet/in.h>
4
+#include <string.h>
5
+#include <stdlib.h>
6
+#include <sys/types.h>
7
+#include <netdb.h>
8
+#include <math.h>
9
+#include <errno.h>
10
+
11
+#include "../../sr_module.h"
12
+#include "lib_statsd.h"
13
+
14
+static StatsConnection statsd_connection = {
15
+    "127.0.0.1",
16
+    "8125",
17
+    -1
18
+};
19
+
20
+bool statsd_connect(void){
21
+
22
+    struct addrinfo *serverAddr = NULL;
23
+    int rc;
24
+
25
+    if (statsd_connection.sock > 0){
26
+        return true;
27
+    }
28
+
29
+    rc = getaddrinfo(
30
+        statsd_connection.ip, statsd_connection.port,
31
+        NULL, &serverAddr);
32
+    if (rc != 0)
33
+    {
34
+        LM_ERR(
35
+            "Statsd: could not initiate server information (%s)\n",
36
+            gai_strerror(rc));
37
+		if(serverAddr) freeaddrinfo(serverAddr);
38
+        return false;
39
+    }
40
+
41
+    statsd_connection.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP);
42
+    if (statsd_connection.sock < 0 ){
43
+        LM_ERR("Statsd: could not create a socket for statsd connection\n");
44
+		if(serverAddr) freeaddrinfo(serverAddr);
45
+        return false;
46
+    }
47
+
48
+    rc = connect(
49
+        statsd_connection.sock, serverAddr->ai_addr, serverAddr->ai_addrlen);
50
+	freeaddrinfo(serverAddr);
51
+    if (rc < 0){
52
+        LM_ERR("Statsd: could not initiate a connect to statsd\n");
53
+        return false;
54
+    }
55
+    return true;
56
+}
57
+
58
+bool send_command(char *command){
59
+    int send_result;
60
+
61
+    if (!statsd_connect()){
62
+        return false;
63
+    }
64
+
65
+    send_result = send(statsd_connection.sock, command, strlen(command), 0);
66
+    if ( send_result < 0){
67
+        LM_ERR("could not send the correct info to statsd (%i| %s)\n",
68
+            send_result, strerror(errno));
69
+        return true;
70
+    }
71
+    LM_DBG("Sent to statsd (%s)", command);
72
+    return true;
73
+}
74
+
75
+bool statsd_set(char *key, char *value){
76
+   char* end = 0;
77
+   char command[254];
78
+   int val;
79
+   val = strtol(value, &end, 0);
80
+   if (*end){
81
+       LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
82
+       return false;
83
+   }
84
+   snprintf(command, sizeof command, "%s:%i|s\n", key, val);
85
+   return send_command(command);
86
+}
87
+
88
+
89
+bool statsd_gauge(char *key, char *value){
90
+   char command[254];
91
+   snprintf(command, sizeof command, "%s:%s|g\n", key, value);
92
+   return send_command(command);
93
+}
94
+
95
+bool statsd_count(char *key, char *value){
96
+   char* end = 0;
97
+   char command[254];
98
+   int val;
99
+
100
+   val = strtol(value, &end, 0);
101
+   if (*end){
102
+       LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
103
+       return false;
104
+   }
105
+   snprintf(command, sizeof command, "%s:%i|c\n", key, val);
106
+   return send_command(command);
107
+}
108
+
109
+bool statsd_timing(char *key, int value){
110
+   char command[254];
111
+   snprintf(command, sizeof command, "%s:%i|ms\n", key, value);
112
+   return send_command(command);
113
+}
114
+
115
+bool statsd_init(char *ip, char *port){
116
+
117
+    if (ip != NULL){
118
+        statsd_connection.ip = ip;
119
+    }
120
+    if (port != NULL ){
121
+       statsd_connection.port = port;
122
+    }
123
+    return statsd_connect();
124
+}
125
+
126
+bool statsd_destroy(void){
127
+    statsd_connection.sock = 0;
128
+    return true;
129
+}