From 40c0e139d4a74122fe4c3d65fa2018e81b98defc Mon Sep 17 00:00:00 2001 From: Unbit Date: Tue, 22 Oct 2013 16:25:56 +0200 Subject: [PATCH] added metric api to python plugin --- plugins/python/uwsgi_pymodule.c | 111 ++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index f2d2d5aa..3cb43696 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -3169,6 +3169,111 @@ static PyMethodDef uwsgi_queue_methods[] = { {NULL, NULL}, }; +PyObject *py_uwsgi_metric_inc(PyObject * self, PyObject * args) { + char *key; + int64_t value = 1; + if (!PyArg_ParseTuple(args, "s|l:metric_inc", &key, &value)) return NULL; + + UWSGI_RELEASE_GIL + if (uwsgi_metric_inc(key, NULL, value)) { + UWSGI_GET_GIL + Py_INCREF(Py_None); + return Py_None; + } + UWSGI_GET_GIL + Py_INCREF(Py_True); + return Py_True; + +} + +PyObject *py_uwsgi_metric_dec(PyObject * self, PyObject * args) { + char *key; + int64_t value = 1; + if (!PyArg_ParseTuple(args, "s|l:metric_dec", &key, &value)) return NULL; + + UWSGI_RELEASE_GIL + if (uwsgi_metric_dec(key, NULL, value)) { + UWSGI_GET_GIL + Py_INCREF(Py_None); + return Py_None; + } + UWSGI_GET_GIL + Py_INCREF(Py_True); + return Py_True; + +} + +PyObject *py_uwsgi_metric_mul(PyObject * self, PyObject * args) { + char *key; + int64_t value = 1; + if (!PyArg_ParseTuple(args, "s|l:metric_mul", &key, &value)) return NULL; + + UWSGI_RELEASE_GIL + if (uwsgi_metric_mul(key, NULL, value)) { + UWSGI_GET_GIL + Py_INCREF(Py_None); + return Py_None; + } + UWSGI_GET_GIL + Py_INCREF(Py_True); + return Py_True; + +} + +PyObject *py_uwsgi_metric_div(PyObject * self, PyObject * args) { + char *key; + int64_t value = 1; + if (!PyArg_ParseTuple(args, "s|l:metric_div", &key, &value)) return NULL; + + UWSGI_RELEASE_GIL + if (uwsgi_metric_div(key, NULL, value)) { + UWSGI_GET_GIL + Py_INCREF(Py_None); + return Py_None; + } + UWSGI_GET_GIL + Py_INCREF(Py_True); + return Py_True; + +} + +PyObject *py_uwsgi_metric_set(PyObject * self, PyObject * args) { + char *key; + int64_t value = 1; + if (!PyArg_ParseTuple(args, "s|l:metric_set", &key, &value)) return NULL; + + UWSGI_RELEASE_GIL + if (uwsgi_metric_set(key, NULL, value)) { + UWSGI_GET_GIL + Py_INCREF(Py_None); + return Py_None; + } + UWSGI_GET_GIL + Py_INCREF(Py_True); + return Py_True; + +} + +PyObject *py_uwsgi_metric_get(PyObject * self, PyObject * args) { + char *key; + if (!PyArg_ParseTuple(args, "s:metric_get", &key)) return NULL; + + UWSGI_RELEASE_GIL + int64_t value = uwsgi_metric_get(key, NULL); + UWSGI_GET_GIL + return PyLong_FromLongLong(value); +} + + +static PyMethodDef uwsgi_metrics_methods[] = { + {"metric_inc", py_uwsgi_metric_inc, METH_VARARGS, ""}, + {"metric_dec", py_uwsgi_metric_dec, METH_VARARGS, ""}, + {"metric_mul", py_uwsgi_metric_mul, METH_VARARGS, ""}, + {"metric_div", py_uwsgi_metric_div, METH_VARARGS, ""}, + {"metric_get", py_uwsgi_metric_get, METH_VARARGS, ""}, + {"metric_set", py_uwsgi_metric_set, METH_VARARGS, ""}, + {NULL, NULL}, +}; void init_uwsgi_module_spooler(PyObject * current_uwsgi_module) { @@ -3204,6 +3309,12 @@ void init_uwsgi_module_advanced(PyObject * current_uwsgi_module) { Py_DECREF(func); } + for (uwsgi_function = uwsgi_metrics_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { + PyObject *func = PyCFunction_New(uwsgi_function, NULL); + PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); + Py_DECREF(func); + } + } void init_uwsgi_module_cache(PyObject * current_uwsgi_module) {