def onSuccess(metric_list): registry = CollectorRegistry() registry.register(ListCollector(metric_list)) output = generate_latest(registry) request.setHeader("Content-Type", "text/plain; charset=UTF-8") request.setResponseCode(200) request.write(output) request.finish()
class AppConfig(DjangoAppConfig): name = 'mtp_common.metrics' verbose_name = _('Prisoner money metrics') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.metric_registry = CollectorRegistry() def ready(self): super().ready() autodiscover_modules('metrics') def register_collector(self, collector): self.metric_registry.register(collector)
self.info = InfoMetricFamily('mtp_app', 'Details of a money-to-prisoners app', value=dict( app=settings.APP, environment=settings.ENVIRONMENT, git_commit=settings.APP_GIT_COMMIT, build_tag=settings.APP_BUILD_TAG, build_date=settings.APP_BUILD_DATE, )) def collect(self): return [self.info] metric_registry = CollectorRegistry() metric_registry.register(AppMetricCollector()) app = Flask(__name__) app.config.update( SESSION_COOKIE_HTTPONLY=True, SESSION_COOKIE_SECURE=True, SESSION_COOKIE_SAMESITE='Lax', ) @app.route('/') def index(): if settings.ENVIRONMENT == 'prod': return redirect('https://www.gov.uk/send-prisoner-money') return render_template('index.html', **settings.TEMPLATE_CONTEXT)
return c def __E_Year(self, site, timestamp, prefix): # TODO check the accumulator E_Year = site.get('E_Year', 0.0) E_Year = E_Year / 1000 if E_Year else 0.0 c = GaugeMetricFamily(f'{prefix}fronius_year_kWh_average', '', labels=['chart', 'family', 'dimension']) c.add_metric(value=E_Year, labels=[ 'fronius_GetPowerFlowRealtimeData.energy.year', 'energy', 'year' ], timestamp=timestamp) return c registry.register(CustomCollector()) from flask import Flask from werkzeug.middleware.dispatcher import DispatcherMiddleware from prometheus_client import make_wsgi_app # Create my app app = Flask(__name__) # Add prometheus wsgi middleware to route /metrics requests app.wsgi_app = DispatcherMiddleware( app.wsgi_app, {'/api/v1/allmetrics': make_wsgi_app(registry)}) app.run(host="0.0.0.0", port=19999)