def test__build_similar_elements__histogram_buckets(self, aggregator): check = AgentCheck() check.submit_histogram_bucket('histogram.bucket3', 1, 0.0, 10.0, True, "hostname", ["tag2"]) check.submit_histogram_bucket('histogram.bucket2', 1, 125.0, 312.0, True, "hostname", ["tag1"]) check.submit_histogram_bucket('histogram.bucket1', 1, 0.0, 10.0, True, "hostname", ["tag1"]) check.submit_histogram_bucket('histogram.bucket4', 1, 125.0, 312.0, True, "hostname2", ["tag1"]) check.submit_histogram_bucket('histogram.bucket5', 1, 125.0, 312.0, True, "hostname2", ["tag2"]) check.submit_histogram_bucket('histogram.bucket0', 2, 125.0, 312.0, False, "hostname2", ["tag2"]) expected_histogram_bucket = HistogramBucketStub( 'histogram.bucket', 1, 0.0, 10.0, True, "hostname", ["tag1"]) similar_histogram_bucket = similar._build_similar_elements( expected_histogram_bucket, aggregator._histogram_buckets) # expect buckets in closest similarity order assert similar_histogram_bucket[0][ 1].name == 'histogram.bucket1' # exact match (except name) assert similar_histogram_bucket[1][ 1].name == 'histogram.bucket3' # value/upper/lower/monotonic/host match assert similar_histogram_bucket[2][ 1].name == 'histogram.bucket2' # value/monotonic/host/tag match assert similar_histogram_bucket[3][ 1].name == 'histogram.bucket4' # value/monotonic/tag match assert similar_histogram_bucket[4][ 1].name == 'histogram.bucket5' # value/monotonic match assert similar_histogram_bucket[5][ 1].name == 'histogram.bucket0' # no match
def assert_histogram_bucket( self, name, value, lower_bound, upper_bound, monotonic, hostname, tags, count=None, at_least=1 ): candidates = [] for bucket in self.histogram_bucket(name): if value is not None and value != bucket.value: continue if tags and tags != sorted(bucket.tags): continue if hostname and hostname != bucket.hostname: continue candidates.append(bucket) expected_bucket = HistogramBucketStub(name, value, lower_bound, upper_bound, monotonic, hostname, tags) if 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=condition, msg=msg, expected_stub=expected_bucket, submitted_elements=self._histogram_buckets )
def histogram_bucket(self, name): """ Return the histogram buckets received under the given name """ return [ HistogramBucketStub( ensure_unicode(stub.name), stub.value, stub.lower_bound, stub.upper_bound, stub.monotonic, ensure_unicode(stub.hostname), normalize_tags(stub.tags), ) for stub in self._histogram_buckets.get(to_string(name), []) ]
def submit_histogram_bucket(self, check, check_id, name, value, lower_bound, upper_bound, monotonic, hostname, tags): self._histogram_buckets[name].append( HistogramBucketStub(name, value, lower_bound, upper_bound, monotonic, hostname, tags))