Ejemplo n.º 1
0
    def __init__(self):
        self.enabled = False
        if 'api_monitor' in os.environ and int(os.environ['api_monitor']) == 1:
            self.enabled = True

        self.use_prometheus = False
        if 'api_monitor_prometheus' in os.environ and int(
                os.environ['api_monitor_prometheus']) == 1:
            self.use_prometheus = True

        if self.enabled:
            self.monitor_target = os.environ['db_name'].replace("_", "-") \
                                    if 'db_name' in os.environ else 'trews;'

            if self.use_prometheus:
                self.prometheus = PrometheusMonitor()
            else:
                self._push_period_secs = int(os.environ['api_monitor_cw_push_period']) \
                                            if 'api_monitor_cw_push_period' in os.environ else 60

                # k8s pods have their pod name set as the hostname.
                stream_id = os.environ[
                    'HOSTNAME'] if 'HOSTNAME' in os.environ else 'api-testing'
                self.cw_metrics = FluentMetric().with_namespace(
                    'OpsDX').with_stream_id(stream_id)

                # Latencies and request counters.
                self._counters = {}
                self._latencies = {}

                # General-purpose metrics.
                self._metrics = {}
                self._metric_specs = {}
def test_adding_timer_starts_timer():
    name = 'test_timer'
    m = FluentMetric()
    m.with_timer(name)
    time.sleep(1)
    t = m.get_timer(name)
    assert t.start < arrow.utcnow()
    assert t.elapsed_in_ms() > 1000 and t.elapsed_in_ms() < 2000
def test_removing_dimension_removes_dimension():
    test_name = 'test_name'
    test_value = 'test_value'
    m = FluentMetric()
    m.with_dimension(test_name, test_value)
    assert m.does_dimension_exist(test_name)
    m.without_dimension(test_name)
    assert not m.does_dimension_exist(test_name)
Ejemplo n.º 4
0
 def __init__(self, source: str):
     self.INTERVAL = 60
     self.hits_counter = AtomicCounter()
     self.data_counter = AtomicCounter()
     self._exit_event = threading.Event()
     self.metrics = (FluentMetric().with_storage_resolution(
         60).with_namespace("SentinelConnectors").with_dimension(
             "Source", source))
def test_dimension_does_not_duplicate():
    test_name = 'test_name'
    test_value1 = 'test_value1'
    test_value2 = 'test_value2'
    m = FluentMetric()
    m.with_dimension(test_name, test_value1)
    assert m.get_dimension_value(test_name) == test_value1
    m.with_dimension(test_name, test_value2)
    assert m.get_dimension_value(test_name) == test_value2
def test_can_push_dimensions():
    test_name = 'test_name'
    test_value = 'test_value'
    m = FluentMetric()
    m.with_dimension(test_name, test_value)
    assert m.does_dimension_exist(test_name)
    m.push_dimensions()
    assert len(m.dimensions) == 0
    m.pop_dimensions()
    assert len(m.dimensions) == 1
Ejemplo n.º 7
0
 def with_metric(self, **kwargs):
     ns = kwargs.get('Namespace')
     m = kwargs.get('Metric')
     clean = kwargs.get('Clean', False)
     if self.metric_namespace == ns and self.metric_name == m:
         return self
     self.metric_name = m
     self.metric_namespace = ns
     self.metric = FluentMetric().with_namespace(self.metric_namespace)
     if not clean:
         self.dirty = True
     return self
def test_can_add_multiple_timers():
    name1 = 'test_timer_1'
    name2 = 'test_timer_2'
    m = FluentMetric()
    m.with_timer(name1)
    time.sleep(1)
    t = m.get_timer(name1)
    assert t.start < arrow.utcnow()
    assert t.elapsed_in_ms() > 1000 and t.elapsed_in_ms() < 2000

    m.with_timer(name2)
    time.sleep(1)
    u = m.get_timer(name2)
    assert u.start < arrow.utcnow()
    assert u.elapsed_in_ms() > 1000 and u.elapsed_in_ms() < 2000
    assert t.elapsed_in_ms() > 2000
def test_adding_dimension_adds_dimension():
    test_name = 'test_name'
    test_value = 'test_value'
    m = FluentMetric()
    m.with_dimension(test_name, test_value)
    assert m.does_dimension_exist(test_name)
Ejemplo n.º 10
0
def test_setting_namespace_sets_namespace():
    test_value = 'test_namespace'
    m = FluentMetric()
    m.with_namespace(test_value)
    assert m.namespace == test_value
Ejemplo n.º 11
0
def test_can_set_resolution():
    m = FluentMetric().with_namespace('Performance').with_storage_resolution(1)
    assert m.storage_resolution == 1
Ejemplo n.º 12
0
def test_can_log_count(fm_log):
    m = FluentMetric().with_namespace('Performance')
    m.count(MetricName='test', Count=2)
    fm_log.assert_called()
Ejemplo n.º 13
0
def test_can_disable_stream_id():
    m = FluentMetric(UseStreamId=False).with_namespace('Performance')
    assert len(m.dimensions) == 0