def test__build_similar_elements__metric_device(self, aggregator): metrics = { 'test.similar_metric2': [ MetricStub('test.similar_metric2', AggregatorStub.GAUGE, 10, [], None, 'less_similar_device') ], 'test.similar_metric1': [ MetricStub('test.similar_metric1', AggregatorStub.GAUGE, 10, [], None, 'similar_device') ], 'test.similar_metric3': [ MetricStub('test.similar_metric3', AggregatorStub.GAUGE, 10, [], None, 'different') ], } expected_metric = MetricStub("test.test.similar_metric", type=None, value=10, tags=None, hostname=None, device='similar_device') similar_metrics = similar._build_similar_elements( expected_metric, metrics) # expect similar metrics in a similarity order assert similar_metrics[0][1].name == 'test.similar_metric1' assert similar_metrics[1][1].name == 'test.similar_metric2' assert similar_metrics[2][1].name == 'test.similar_metric3'
def test_build_similar_elements_msg(self, aggregator): check = AgentCheck() check.gauge('test.another_similar_metric', 0) check.gauge('test.very_different_metric', 0) check.gauge('test.most_similar_metric', 0) check.gauge('test.very_very_different', 0) expected_metric = MetricStub("test.similar_metric", None, None, None, None) actual_msg = similar.build_similar_elements_msg( expected_metric, aggregator._metrics) expected_msg = ''' Expected: MetricStub(name='test.similar_metric', type=None, value=None, tags=None, hostname=None) Similar submitted: Score Most similar 0.88 MetricStub(name='test.most_similar_metric', type=0, value=0.0, tags=[], hostname='') 0.83 MetricStub(name='test.another_similar_metric', type=0, value=0.0, tags=[], hostname='') 0.62 MetricStub(name='test.very_different_metric', type=0, value=0.0, tags=[], hostname='') 0.42 MetricStub(name='test.very_very_different', type=0, value=0.0, tags=[], hostname='') ''' assert expected_msg.strip() == actual_msg.strip( ), "Actual message:\n" + actual_msg
def test_build_similar_elements_msg(self, aggregator): check = AgentCheck() check.gauge('test.another_similar_metric', 0) check.gauge('test.very_different_metric', 0) check.gauge('test.most_similar_metric', 0) check.gauge('test.very_very_different', 0) expected_metric = MetricStub("test.similar_metric", None, None, None, None, None) actual_msg = similar.build_similar_elements_msg( expected_metric, aggregator._metrics).strip() expected_msg = ''' Expected: MetricStub(name='test.similar_metric', type=None, value=None, tags=None, hostname=None, device=None, flush_first_value=None) Similar submitted: Score Most similar 0.88 MetricStub(name='test.most_similar_metric', type=0, value=0.0, tags=[], hostname='', device=None, flush_first_value=False) 0.83 MetricStub(name='test.another_similar_metric', type=0, value=0.0, tags=[], hostname='', device=None, flush_first_value=False) 0.62 MetricStub(name='test.very_different_metric', type=0, value=0.0, tags=[], hostname='', device=None, flush_first_value=False) 0.42 MetricStub(name='test.very_very_different', type=0, value=0.0, tags=[], hostname='', device=None, flush_first_value=False) '''.strip() # noqa: E501 delta = difflib.ndiff([expected_msg], [actual_msg]) assert expected_msg == actual_msg, delta
def assert_metric(self, name, value=None, tags=None, count=None, at_least=1, hostname=None, metric_type=None, device=None): """ Assert a metric was processed by this stub """ self._asserted.add(name) expected_tags = normalize_tags(tags, sort=True) candidates = [] for metric in self.metrics(name): if value is not None and not self.is_aggregate( metric.type) and value != metric.value: continue if expected_tags and expected_tags != sorted(metric.tags): continue if hostname and hostname != metric.hostname: continue if metric_type is not None and metric_type != metric.type: continue if device is not None and device != metric.device: continue candidates.append(metric) expected_metric = MetricStub(name, metric_type, value, tags, hostname, device) if value is not None and candidates and all( self.is_aggregate(m.type) for m in candidates): got = sum(m.value for m in candidates) msg = "Expected count value for '{}': {}, got {}".format( name, value, got) condition = value == got elif count is not None: msg = "Needed exactly {} candidates for '{}', got {}".format( count, name, len(candidates)) condition = len(candidates) == count else: msg = "Needed at least {} candidates for '{}', got {}".format( at_least, name, len(candidates)) condition = len(candidates) >= at_least self._assert(condition, msg=msg, expected_stub=expected_metric, submitted_elements=self._metrics)
def normalize_stub_metric(stub, tags_to_skip): tags = [t for t in stub.tags if not is_skipped_tag(t, tags_to_skip)] # Remove skipped tag return MetricStub( stub.name, stub.type, stub.value, tags, stub.hostname, stub.device, )
def normalize_stub_metric(stub): tags = [t for t in stub.tags if not t.startswith('loader:')] # Remove `loader` tag return MetricStub( stub.name, stub.type, stub.value, tags, stub.hostname, stub.device, )
def submit_metric_e2e(self, check, check_id, mtype, name, value, tags, hostname, device=None): # Device is only present in metrics read from the real agent in e2e tests. Normally it is submitted as a tag if not self.ignore_metric(name): self._metrics[name].append( MetricStub(name, mtype, value, tags, hostname, device))
def metrics(self, name): """ Return the metrics received under the given name """ return [ MetricStub( ensure_unicode(stub.name), stub.type, stub.value, normalize_tags(stub.tags), ensure_unicode(stub.hostname), ) for stub in self._metrics.get(to_string(name), []) ]
def test__build_similar_elements__metric_value(self, aggregator): check = AgentCheck() check.gauge('test.similar_metric1', 10) check.gauge('test.similar_metric2', 20) check.gauge('test.similar_metric3', 30) expected_metric = MetricStub("test.my_metric", type=None, value=20, tags=None, hostname=None, device=None) similar_metrics = similar._build_similar_elements(expected_metric, aggregator._metrics) expected_most_similar_metric = similar_metrics[0][1] print(similar_metrics) assert expected_most_similar_metric.name == 'test.similar_metric2'
def test__build_similar_elements__metric_hostname(self, aggregator): check = AgentCheck() check.gauge('test.similar_metric2', 10, hostname='less_similar_host') check.gauge('test.similar_metric1', 10, hostname='similar_host') check.gauge('test.similar_metric3', 10, hostname='different') expected_metric = MetricStub( "test.test.similar_metric", type=None, value=10, tags=None, hostname='similar_host', device=None ) similar_metrics = similar._build_similar_elements(expected_metric, aggregator._metrics) # expect similar metrics in a similarity order assert similar_metrics[0][1].name == 'test.similar_metric1' assert similar_metrics[1][1].name == 'test.similar_metric2' assert similar_metrics[2][1].name == 'test.similar_metric3'
def test__build_similar_elements__metric_name(self, aggregator): check = AgentCheck() check.gauge('test.another_similar_metric', 0) check.gauge('test.very_different_metric', 0) check.gauge('test.most_similar_metric', 0) check.gauge('test.very_very_different', 0) expected_metric = MetricStub( "test.similar_metric", type=None, value=None, tags=None, hostname=None, device=None ) similar_metrics = similar._build_similar_elements(expected_metric, aggregator._metrics) expected_most_similar_metric = similar_metrics[0][1] expected_second_most_similar_metric = similar_metrics[1][1] assert expected_most_similar_metric.name == 'test.most_similar_metric' assert expected_second_most_similar_metric.name == 'test.another_similar_metric'
def test__build_similar_elements__metric_tags(self, aggregator): check = AgentCheck() check.gauge('test.similar_metric2', 10, tags=['name:less_similar_tag']) check.gauge('test.similar_metric1', 10, tags=['name:similar_tag']) check.gauge('test.similar_metric3', 10, tags=['something:different']) expected_metric = MetricStub("test.test.similar_metric", type=None, value=10, tags=['name:similar_tag'], hostname=None) similar_metrics = similar._build_similar_elements( expected_metric, aggregator._metrics) # expect similar metrics in a similarity order assert similar_metrics[0][1].name == 'test.similar_metric1' assert similar_metrics[1][1].name == 'test.similar_metric2' assert similar_metrics[2][1].name == 'test.similar_metric3'
def submit_metric(self, check, check_id, mtype, name, value, tags, hostname): if not self.ignore_metric(name): self._metrics[name].append( MetricStub(name, mtype, value, tags, hostname, None))