def test_not_kill_switch_504(self, mock_logging, mock_post): mock_post.return_value = MockResponse(code=504) a = netuitive.Client(api_key='apikey') mock_post.side_effect = urllib2.HTTPError(a.url, 504, '', {}, None) e = netuitive.Element() e.add_sample('nonsparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname') resp = a.post(e) resp2 = a.post(e) self.assertNotEqual(resp, True) self.assertFalse(resp2) self.assertFalse(a.disabled) self.assertEqual( mock_logging.exception.call_args_list[0][0][0], 'error posting payload to api ingest endpoint (%s): %s')
def test_kill_switch_410(self, mock_logging, mock_post): mock_post.return_value = MockResponse(code=410) a = netuitive.Client(api_key='apikey') mock_post.side_effect = urllib2.HTTPError(a.url, 410, '', {}, None) e = netuitive.Element() e.add_sample('nonsparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname') resp = a.post(e) resp2 = a.post(e) self.assertNotEqual(resp, True) self.assertFalse(resp2) self.assertTrue(a.disabled) self.assertEqual( mock_logging.exception.call_args_list[0][0][0], 'Posting has been disabled.See previous errors for details.')
def test_repeat_failure_general_http(self, mock_logging, mock_post): a = netuitive.Client(api_key='apikey') e = netuitive.Element() e.add_sample('nonsparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname') errs = [403, 429, 503, 404, 503, 204, 307, 302, 405, 413] for i in range(a.max_post_errors): mock_post.return_value = MockResponse(code=errs[i]) mock_post.side_effect = urllib2.HTTPError(a.url, errs[i], '', {}, None) resp = a.post(e) resp = a.post(e) self.assertNotEqual(resp, True) self.assertFalse(resp) self.assertFalse(a.disabled) self.assertEqual(len(e.samples), 0) self.assertEqual( mock_logging.exception.call_args_list[0][0][0], 'error posting payload to api ingest endpoint (%s): %s')
def test_clear_samples(self): a = netuitive.Element() a.add_sample('metricId', 1434110794, 1, 'COUNTER', host='hostname') # test clear_samples self.assertEqual(len(a._metrics), 1) a.clear_samples() self.assertEqual(len(a.metrics), 0) self.assertEqual(len(a._metrics), 0) self.assertEqual(len(a.samples), 0)
def test_post_format(self): a = netuitive.Element('SERVER', 'here') a.merge_metrics() ajson = json.dumps([a], default=lambda o: o.__dict__, sort_keys=True) self.assertEqual( ajson, getFixture('TestElementInit.test_post_format').getvalue())
def test_add_sample(self): a = netuitive.Element() a.add_sample('metricId', 1434110794, 1, 'COUNTER', host='hostname') self.assertEqual(a.id, 'hostname') self.assertEqual(a.name, 'hostname') self.assertEqual(a._metrics['metricId'].id, 'metricId') self.assertEqual(a._metrics['metricId'].type, 'COUNTER')
def test_add_sample_ms(self): a = netuitive.Element() a.add_sample('metricId', 1475158966202, 1, 'COUNTER', host='hostname', ts_is_ms=True) self.assertEqual(a.samples[0].timestamp, 1475158966202)
def __init__(self, config=None): """ initialize Netuitive api and populate agent host metadata """ if not netuitive: self.log.error('netuitive import failed. Handler disabled') self.enabled = False return try: Handler.__init__(self, config) logging.debug("initialize Netuitive handler") self.version = self._get_version() self.api = netuitive.Client(self.config['url'], self.config['api_key'], self.version) self.element = netuitive.Element( location=self.config.get('location')) self.batch_size = int(self.config['batch']) self.max_backlog_multiplier = int( self.config['max_backlog_multiplier']) self.trim_backlog_multiplier = int( self.config['trim_backlog_multiplier']) self._add_sys_meta() self._add_aws_meta() self._add_docker_meta() self._add_azure_meta() self._add_config_tags() self._add_config_relations() self._add_collectors() self.flush_time = 0 try: self.config['write_metric_fqns'] = str_to_bool( self.config['write_metric_fqns']) except KeyError, e: self.log.warning('write_metric_fqns missing from the config') self.config['write_metric_fqns'] = False if self.config['write_metric_fqns']: self.metric_fqns_path = self.config['metric_fqns_path'] truncate_fqn_file = open(self.metric_fqns_path, "w") truncate_fqn_file.close() logging.debug(self.config)
def __init__(self, elementId, ElementType=None): logger.debug('__init__ for Element') self.element = netuitive.Element(ElementType) self.elementId = elementId self.metrics = {} self.metric_types = {'c': 'COUNTER', 'g': 'GAUGE', 'ms': 'TIMER', 's': 'SET', 'h': 'HISTOGRAM'}
def test_null_element_id(self, mock_logging, mock_post): mock_post.return_value = MockResponse(code=202) # test infrastructure endpoint url creation a = netuitive.Client(api_key='apikey') e = netuitive.Element() resp = a.post(e) self.assertEqual(None, resp) self.assertEqual(str(mock_logging.exception.call_args_list[0][0][2]), 'element id is not set')
def test_add_sanitize(self): a = netuitive.Element() a.add_sample( 'mongo.wiredTiger.cache.eviction$server populating queue,:but not evicting pages', 1434110794, 1, 'COUNTER', host='hostname') self.assertEqual( a._metrics[ 'mongo.wiredTiger.cache.eviction_server_populating_queue__but_not_evicting_pages'] .id, 'mongo.wiredTiger.cache.eviction_server_populating_queue__but_not_evicting_pages' )
def test_with_unit(self): a = netuitive.Element() # test unit a.add_sample('unit', 1434110794, 1, 'COUNTER', host='hostname', unit='Bytes') a.add_sample('nonunit', 1434110794, 1, 'COUNTER', host='hostname') self.assertEqual(a._metrics['unit'].unit, 'Bytes') self.assertEqual(a._metrics['nonunit'].unit, '')
def test_add_sample_no_timestamp(self): a = netuitive.Element() c = datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1) d = c.microseconds + (c.seconds + c.days * 86400) * 10**3 a.add_sample('metricId', None, 1, 'COUNTER', host='hostname') e = a.samples[0].timestamp - d shouldbetrue = False # minimum.timstamp has to be within the 10 second if 10000 > e: shouldbetrue = True self.assertTrue(shouldbetrue)
def test_duplicate_metrics(self): a = netuitive.Element() a.add_sample('metricId', 1434110794, 1, 'COUNTER', host='hostname') a.add_sample('metricId', 1434110795, 2, 'COUNTER', host='hostname') # don't allow duplicate metrics self.assertEqual(len(a._metrics), 1) self.assertEqual(a._metrics['metricId'].id, 'metricId') self.assertEqual(a._metrics['metricId'].type, 'COUNTER') self.assertEqual(a.samples[0].metricId, 'metricId') self.assertEqual(a.samples[0].timestamp, 1434110794000) self.assertEqual(a.samples[0].val, 1) self.assertEqual(a.samples[1].metricId, 'metricId') self.assertEqual(a.samples[1].timestamp, 1434110795000) self.assertEqual(a.samples[1].val, 2)
def test_failure_general(self, mock_logging, mock_post): mock_post.return_value = MockResponse(code=500) # test infrastructure endpoint url creation a = netuitive.Client(api_key='apikey') mock_post.side_effect = urllib2.URLError('something') e = netuitive.Element() e.add_sample( 'nonsparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname') resp = a.post(e) self.assertNotEqual(resp, True) self.assertEqual(mock_logging.exception.call_args_list[0][0][ 0], 'error posting payload to api ingest endpoint (%s): %s')
def test_success(self, mock_logging, mock_post): mock_post.return_value = MockResponse(code=202) # test infrastructure endpoint url creation a = netuitive.Client(api_key='apikey') e = netuitive.Element() e.add_sample( 'nonsparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname') resp = a.post(e) self.assertTrue(resp) self.assertEqual(mock_logging.exception.call_args_list, [])
def test_add_sample_with_tags(self): a = netuitive.Element() a.add_sample('tagged', 1434110794, 1, 'COUNTER', host='hostname', tags=[{ 'utilization': 'true' }]) self.assertEqual(a.id, 'hostname') self.assertEqual(a.name, 'hostname') self.assertEqual(a._metrics['tagged'].id, 'tagged') self.assertEqual(a._metrics['tagged'].type, 'COUNTER') self.assertEqual(a._metrics['tagged'].tags[0].name, 'utilization') self.assertEqual(a._metrics['tagged'].tags[0].value, 'true')
def test_post_format(self): a = netuitive.Element() a.add_sample('min.max.avg.sum.cnt', 1434110794, 1, 'COUNTER', host='hostname', min=0, max=100, avg=50, sum=2, cnt=3) a.merge_metrics() ajson = json.dumps([a], default=lambda o: o.__dict__, sort_keys=True) self.assertEqual( ajson, getFixture('TestElementSamples.test_post_format').getvalue())
def __init__(self, config=None): """ initialize Netuitive api and populate agent host metadata """ if not netuitive: self.log.error('netuitive import failed. Handler disabled') self.enabled = False return try: Handler.__init__(self, config) logging.debug("initialize Netuitive handler") self.version = self._get_version() self.api = netuitive.Client(self.config['url'], self.config['api_key'], self.version) self.element = netuitive.Element( location=self.config.get('location')) self.batch_size = int(self.config['batch']) self.max_backlog_multiplier = int( self.config['max_backlog_multiplier']) self.trim_backlog_multiplier = int( self.config['trim_backlog_multiplier']) self._add_sys_meta() self._add_aws_meta() self._add_docker_meta() self._add_config_tags() self._add_config_relations() logging.debug(self.config) except Exception as e: logging.exception('NetuitiveHandler: init - %s', str(e))
def test_with_sparseDataStrategy(self): a = netuitive.Element() # test sparseDataStrategy a.add_sample('nonsparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname') a.add_sample('sparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname', sparseDataStrategy='ReplaceWithZero') self.assertEqual( a._metrics['nonsparseDataStrategy'].sparseDataStrategy, 'None') self.assertEqual(a._metrics['sparseDataStrategy'].sparseDataStrategy, 'ReplaceWithZero') a.clear_samples()
def test_element_type(self): a = netuitive.Element('NOT_SERVER') self.assertEqual(a.type, 'NOT_SERVER')
def test_element_localtion(self): a = netuitive.Element('SERVER', 'here') self.assertEqual(a.location, 'here') b = netuitive.Element('SERVER', location='here too') self.assertEqual(b.location, 'here too')
def setUp(self): self.a = netuitive.Element() self.a.add_attribute('Test', 'TestValue') self.a.add_attribute('Test2', 'TestValue2')
def test_with_cnt(self): a = netuitive.Element() a.add_sample('cnt', 1434110794, 1, 'COUNTER', host='hostname', cnt=3) self.assertEqual(a.samples[0].cnt, 3)
def test_with_sum(self): a = netuitive.Element() a.add_sample('sum', 1434110794, 1, 'COUNTER', host='hostname', sum=2) self.assertEqual(a.samples[0].sum, 2)
def test_with_avg(self): a = netuitive.Element() a.add_sample('avg', 1434110794, 1, 'COUNTER', host='hostname', avg=50) self.assertEqual(a.samples[0].avg, 50)
def test_with_max(self): a = netuitive.Element() a.add_sample('max', 1434110794, 1, 'COUNTER', host='hostname', max=100) self.assertEqual(a.samples[0].max, 100)
def test_with_min(self): a = netuitive.Element() a.add_sample('min', 1434110794, 1, 'COUNTER', host='hostname', min=0) self.assertEqual(a.samples[0].min, 0)
def setUp(self): self.a = netuitive.Element() self.a.add_relation('Test') self.a.add_relation('Test2')
def test(self): a = netuitive.Element() a.add_tag('Test', 'TestValue') self.assertEqual(a.tags[0].name, 'Test') self.assertEqual(a.tags[0].value, 'TestValue')