Skip to content

Commit a6eb6cb

Browse files
committed
add doc type count example pg function and sql
1 parent 1fd4989 commit a6eb6cb

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

couchdb/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,126 @@ Add something like below to crontab:
351351
Adjust frequency of collection as required.
352352

353353
TODO: Add example sql and graphs of various metrics
354+
355+
356+
357+
### database doc types count
358+
359+
When the doc.type map/reduce is in place and the couch_get_dbs_stats.sh has it enabled a metric doc looks something like the below:
360+
361+
```
362+
{
363+
"_id": "3ad893cb4cf1560add7b4caffd4adfac",
364+
"_rev": "1-41d8695a6d2d0a4dec24e9f90e67c967",
365+
"name": "myname.couch_dbs_stats",
366+
"type": "couch_dbs_stats",
367+
"ts": 1445725502,
368+
"dbs": [
369+
{
370+
"db_name": "aatest",
371+
"doc_count": 2326396,
372+
"doc_del_count": 0,
373+
"update_seq": 2326398,
374+
"purge_seq": 0,
375+
"compact_running": false,
376+
"disk_size": 641671281,
377+
"data_size": 575344173,
378+
"instance_start_time": "1445258443050873",
379+
"disk_format_version": 6,
380+
"committed_update_seq": 2326398,
381+
"doc_types_count": [
382+
{
383+
"counter": 594423
384+
},
385+
{
386+
"counter_rate": 51942
387+
},
388+
{
389+
"cpu": 9813
390+
},
391+
{
392+
"gauges": 542081
393+
},
394+
{
395+
"if": 9809
396+
},
397+
{
398+
"io": 14706
399+
},
400+
{
401+
"load": 9809
402+
},
403+
{
404+
"mem": 9809
405+
},
406+
{
407+
"null": 2
408+
},
409+
{
410+
"set": 542000
411+
},
412+
{
413+
"timers": 542000
414+
}
415+
]
416+
},
417+
{
418+
"db_name": "abtest",
419+
"doc_count": 315,
420+
"doc_del_count": 4,
421+
"update_seq": 326,
422+
"purge_seq": 0,
423+
"compact_running": false,
424+
"disk_size": 163953,
425+
"data_size": 75808,
426+
"instance_start_time": "1445545165301518",
427+
"disk_format_version": 6,
428+
"committed_update_seq": 326,
429+
"doc_types_count": [
430+
{
431+
"counter": 311
432+
},
433+
{
434+
"gauge": 2
435+
},
436+
{
437+
"timers": 1
438+
}
439+
]
440+
}
441+
]
442+
}
443+
```
444+
445+
Below will return all of the doc types count series for a metric and couch db name:
446+
```
447+
CREATE OR REPLACE FUNCTION get_couch_dbs_doc_type_counts(metricname TEXT, dbname TEXT)
448+
RETURNS text AS
449+
$BODY$
450+
DECLARE
451+
doctype text;
452+
rec record;
453+
sql TEXT := '';
454+
result TEXT;
455+
BEGIN
456+
FOR doctype IN
457+
SELECT DISTINCT doc_type AS doctype FROM couch_dbs_stats WHERE metric_name=metricname AND db_name=dbname
458+
LOOP
459+
sql := sql || 'SELECT '',{ "series": [{ "name": "' || dbname || '.' || doctype || '", "columns": ["time", "count"], "values": '' || json_agg(json_build_array((time::numeric * 1000),doc_count::numeric)) || '' }] }'' AS v FROM couch_dbs_stats UNION ALL ';
460+
END LOOP;
461+
sql := 'SELECT ''' || left( right(sql,length(sql)-9), -10 ) ;
462+
463+
result := '';
464+
FOR rec IN EXECUTE sql LOOP
465+
result := result || rec.v;
466+
END LOOP;
467+
RETURN result;
468+
469+
END
470+
$BODY$
471+
LANGUAGE plpgsql;
472+
```
473+
To be called from grafana as:
474+
```
475+
SELECT '{ "results": [' || get_couch_dbs_doc_type_counts('myname.couch_dbs_stats','aatest') || ']}' AS ret
476+
```

0 commit comments

Comments
 (0)