File: stats_pusher_mongodb.cc

package info (click to toggle)
uwsgi 2.0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: ansic: 87,027; python: 7,001; cpp: 1,131; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (52 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <uwsgi.h>

#include "client/dbclient.h"

extern struct uwsgi_server uwsgi;

// one for each mongodb stats pusher
struct stats_pusher_mongodb_conf {
	char *address;
	char *collection;
	char *freq;
};


extern "C" void stats_pusher_mongodb(struct uwsgi_stats_pusher_instance *uspi, time_t now, char *json, size_t json_len) {

	struct stats_pusher_mongodb_conf *spmc = (struct stats_pusher_mongodb_conf *) uspi->data;
	if (!uspi->configured) {
		spmc = (struct stats_pusher_mongodb_conf *) uwsgi_calloc(sizeof(struct stats_pusher_mongodb_conf));
		if (uspi->arg) {
			if (uwsgi_kvlist_parse(uspi->arg, strlen(uspi->arg), ',', '=',
                                "addr", &spmc->address,
                                "address", &spmc->address,
                                "collection", &spmc->collection,
                                "freq", &spmc->freq, NULL)) {
                                free(spmc);
                                return;
                        }
		}
		if (!spmc->address) spmc->address = (char *) "127.0.0.1:27017";
		if (!spmc->collection) spmc->collection = (char *) "uwsgi.statistics";
		if (spmc->freq) uspi->freq = atoi(spmc->freq);
		uspi->data = spmc;
		uspi->configured = 1;
	}

	try {
		int j_len = (int) json_len;
		mongo::BSONObj b = mongo::fromjson(json, &j_len);
		// the connection object (will be automatically destroyed at each cycle)
		mongo::DBClientConnection c;
		// set the socket timeout
		c.setSoTimeout(uwsgi.socket_timeout);
		// connect
		c.connect(spmc->address);
		c.insert(spmc->collection, b);
	}	
	catch ( mongo::DBException &e ) {
		uwsgi_log("[stats-pusher-mongodb] ERROR(%s/%s): %s\n", spmc->address, spmc->collection, e.what());
	}

}