Skip to content

Commit

Permalink
reset the prometheus exporter registry between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KPrasch committed Oct 24, 2023
1 parent d959e10 commit ab2e73d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
6 changes: 4 additions & 2 deletions porter/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ def run(
allow_origins_list = allow_origins.split(",") # split into list of origins to allow
emitter.message(PORTER_CORS_ALLOWED_ORIGINS.format(allow_origins=allow_origins_list), color='green')

controller = PORTER.make_web_controller(crash_on_error=False,
cors_allow_origins_list=allow_origins_list)
controller = PORTER.make_web_controller(
crash_on_error=False,
cors_allow_origins_list=allow_origins_list
)
message = PORTER_RUN_MESSAGE.format(http_port=http_port)
emitter.message(message, color='green', bold=True)
return controller.start(port=http_port,
Expand Down
15 changes: 9 additions & 6 deletions porter/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from pathlib import Path
from typing import Dict, List, NamedTuple, Optional, Sequence

from constant_sorrow.constants import NO_CONTROL_PROTOCOL
from eth_typing import ChecksumAddress
from eth_utils import to_checksum_address
Expand All @@ -23,7 +20,9 @@
TreasureMap,
)
from nucypher_core.umbral import PublicKey
from pathlib import Path
from prometheus_flask_exporter import PrometheusMetrics
from typing import Dict, List, NamedTuple, Optional, Sequence

from porter.controllers import PorterCLIController, WebController
from porter.interfaces import PorterInterface
Expand Down Expand Up @@ -229,6 +228,9 @@ def make_cli_controller(self, crash_on_error: bool = False):
self.controller = controller
return controller

def _setup_prometheus(self, app):
self.controller.metrics = PrometheusMetrics(app)

def make_web_controller(self,
crash_on_error: bool = False,
htpasswd_filepath: Path = None,
Expand All @@ -240,11 +242,12 @@ def make_web_controller(self,

# Register Flask Decorator
porter_flask_control = controller.make_control_transport()
self._setup_prometheus(porter_flask_control)

# static information as metric
metrics = PrometheusMetrics(porter_flask_control)
metrics.info('app_info', 'Application info', version='1.0.3')
by_path_counter = metrics.counter(

self.controller.metrics.info('app_info', 'Application info', version='1.0.3')
by_path_counter = controller.metrics.counter(
'by_path_counter', 'Request count by request paths',
labels={'path': lambda: request.path}
)
Expand Down
22 changes: 14 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
from typing import Iterable, List, Optional, Tuple
from unittest.mock import MagicMock

import maya
import os
import prometheus_client
import pytest
from click.testing import CliRunner
from eth_typing import ChecksumAddress
Expand All @@ -27,9 +25,7 @@
from nucypher.utilities.logging import GlobalLoggerSettings
from nucypher_core import HRAC, Address, ThresholdMessageKit, TreasureMap
from nucypher_core.ferveo import DkgPublicKey, Validator

from porter.emitters import WebEmitter
from porter.main import Porter
from prometheus_flask_exporter import PrometheusMetrics
from tests.constants import (
MOCK_ETH_PROVIDER_URI,
TESTERCHAIN_CHAIN_ID,
Expand All @@ -38,6 +34,11 @@
from tests.mock.agents import MockContractAgent
from tests.mock.interfaces import MockBlockchain
from tests.utils.registry import MockRegistrySource, mock_registry_sources
from typing import Iterable, List, Optional, Tuple
from unittest.mock import MagicMock

from porter.emitters import WebEmitter
from porter.main import Porter

# Crash on server error by default
WebEmitter._crash_on_error_default = True
Expand Down Expand Up @@ -256,7 +257,12 @@ def random_treasure_map_data(alice, bob, ursulas):


@pytest.fixture(scope='module')
def porter_web_controller(porter):
def porter_web_controller(porter, monkeymodule):
def _setup_prometheus(_porter, app):
_porter.controller.metrics = PrometheusMetrics(app)
_porter.controller.metrics.registry = prometheus_client.CollectorRegistry(auto_describe=True)

Porter._setup_prometheus = _setup_prometheus
web_controller = porter.make_web_controller(crash_on_error=False)
yield web_controller.test_client()

Expand Down

0 comments on commit ab2e73d

Please sign in to comment.