Mercurial > code > home > repos > patchablegraph
changeset 3:703adc4f78b1
scales -> promethewus
author | drewp@bigasterisk.com |
---|---|
date | Wed, 24 Nov 2021 19:47:06 -0800 |
parents | a8a001175948 |
children | dc4f852d0d70 |
files | patchablegraph.py setup.py |
diffstat | 2 files changed, 22 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/patchablegraph.py Wed Nov 24 10:21:06 2021 -0800 +++ b/patchablegraph.py Wed Nov 24 19:47:06 2021 -0800 @@ -22,7 +22,7 @@ """ import json, logging, itertools, html -from greplin import scales +from prometheus_client import Counter, Gauge, Summary from rdfdb.grapheditapi import GraphEditApi from rdflib import ConjunctiveGraph from rdflib.namespace import NamespaceManager @@ -35,6 +35,13 @@ log = logging.getLogger('patchablegraph') +SERIALIZE_CALLS = Summary('serialize_calls', 'PatchableGraph.serialize calls') +PATCH_CALLS = Summary('patch_calls', 'PatchableGraph.patch calls') +STATEMENT_COUNT = Gauge('statement_count', 'current PatchableGraph graph size') +OBSERVERS_CURRENT = Gauge('observers_current', 'current observer count') +OBSERVERS_ADDED = Counter('observers_added', 'observers added') + + # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py def _graphFromQuads2(q): g = ConjunctiveGraph() @@ -81,10 +88,8 @@ with self._serialize.time(): return self._graph.serialize(*arg, **kw) - _patch = scales.PmfStat('patch') - _len = scales.IntStat('statementCount') def patch(self, p): - with self._patch.time(): + with PATCH_CALLS.labels(graph=self.label).time(): # assuming no stmt is both in p.addQuads and p.delQuads. dels = set([q for q in p.delQuads if inGraph(q, self._graph)]) adds = set([q for q in p.addQuads if not inGraph(q, self._graph)]) @@ -97,17 +102,15 @@ perfect=False) # true? for ob in self._observers: ob(patchAsJson(p)) - self._len = len(self._graph) + STATEMENT_COUNT.labels(graph=self.label).set(len(self._graph)) def asJsonLd(self): return graphAsJson(self._graph) - _currentObservers = scales.IntStat('observers/current') - _observersAdded = scales.IntStat('observers/added') def addObserver(self, onPatch): self._observers.append(onPatch) - self._currentObservers = len(self._observers) - self._observersAdded += 1 + OBSERVERS_CURRENT.labels(graph=self.label).set(len(self._observers)) + OBSERVERS_ADDED.labels(graph=self.label).inc() def removeObserver(self, onPatch): try: @@ -119,9 +122,8 @@ def setToGraph(self, newGraph): self.patch(Patch.fromDiff(self._graph, newGraph)) - _sendSimpleGraph = scales.PmfStat('serve/simpleGraph') - _sendFullGraph = scales.PmfStat('serve/events/sendFull') - _sendPatch = scales.PmfStat('serve/events/sendPatch') + +SEND_SIMPLE_GRAPH = Summary('send_simple_graph', 'calls to _writeGraphResponse') class CycloneGraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler): @@ -129,7 +131,7 @@ self.masterGraph = masterGraph def get(self): - with self.masterGraph._sendSimpleGraph.time(): + with SEND_SIMPLE_GRAPH.time(): self._writeGraphResponse() def _writeGraphResponse(self): @@ -210,6 +212,10 @@ ''') +SEND_FULL_GRAPH = Summary('send_full_graph', 'fullGraph SSE events') +SEND_PATCH = Summary('send_patch', 'patch SSE events') + + class CycloneGraphEventsHandler(cyclone.sse.SSEHandler): """ One session with one client. @@ -226,14 +232,14 @@ self.masterGraph = masterGraph def bind(self): - with self.masterGraph._sendFullGraph.time(): + with SEND_FULL_GRAPH.time(): graphJson = self.masterGraph.asJsonLd() log.debug("send fullGraph event: %s", graphJson) self.sendEvent(message=graphJson, event=b'fullGraph') self.masterGraph.addObserver(self.onPatch) def onPatch(self, patchJson): - with self.masterGraph._sendPatch.time(): + with SEND_PATCH.time(): # throttle and combine patches here- ideally we could see how # long the latency to the client is to make a better rate choice self.sendEvent(message=patchJson, event=b'patch')
--- a/setup.py Wed Nov 24 10:21:06 2021 -0800 +++ b/setup.py Wed Nov 24 19:47:06 2021 -0800 @@ -10,7 +10,7 @@ 'twisted', 'rdflib >= 6.0.1', 'rdfdb >= 0.8.0', - 'scales @ git+http://github.com/drewp/scales.git@448d59fb491b7631877528e7695a93553bfaaa93', + 'prometheus_client', 'cycloneerr', 'twisted_sse >= 0.3.0', ],