本文整理汇总了Python中prometheus_client.Gauge.set方法的典型用法代码示例。如果您正苦于以下问题:Python Gauge.set方法的具体用法?Python Gauge.set怎么用?Python Gauge.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类prometheus_client.Gauge
的用法示例。
在下文中一共展示了Gauge.set方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: collect_snmp
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
def collect_snmp(config, host, port=161):
"""Scrape a host and return prometheus text format for it"""
start = time.time()
metrics = {}
for metric in config['metrics']:
metrics[metric['name']] = Metric(metric['name'], 'SNMP OID {0}'.format(metric['oid']), 'untyped')
values = walk_oids(host, port, config['walk'])
oids = {}
for oid, value in values:
oids[tuple(oid)] = value
for oid, value in oids.items():
for metric in config['metrics']:
prefix = oid_to_tuple(metric['oid'])
if oid[:len(prefix)] == prefix:
value = float(value)
indexes = oid[len(prefix):]
labels = parse_indexes(indexes, metric.get('indexes', {}), metric.get('lookups', {}), oids)
metrics[metric['name']].add_sample(metric['name'], value=value, labels=labels)
class Collector():
def collect(self):
return metrics.values()
registry = CollectorRegistry()
registry.register(Collector())
duration = Gauge('snmp_scrape_duration_seconds', 'Time this SNMP scrape took, in seconds', registry=registry)
duration.set(time.time() - start)
walked = Gauge('snmp_oids_walked', 'Number of oids walked in this scrape', registry=registry)
walked.set(len(oids))
return generate_latest(registry)
示例2: update_gauges
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
def update_gauges(metrics):
metric_dict = {}
for (name_list, label_dict, value) in metrics:
metric_name = format_metric_name(name_list)
if metric_name not in metric_dict:
metric_dict[metric_name] = (tuple(label_dict.keys()), {})
label_keys = metric_dict[metric_name][0]
label_values = tuple([
format_label_value(label_dict[key])
for key in label_keys
])
metric_dict[metric_name][1][label_values] = value
for metric_name, (label_keys, value_dict) in metric_dict.items():
if metric_name in gauges:
(old_label_values_set, gauge) = gauges[metric_name]
else:
old_label_values_set = set()
gauge = Gauge(metric_name, '', label_keys)
new_label_values_set = set(value_dict.keys())
for label_values in old_label_values_set - new_label_values_set:
gauge.remove(*label_values)
for label_values, value in value_dict.items():
if label_values:
gauge.labels(*label_values).set(value)
else:
gauge.set(value)
gauges[metric_name] = (new_label_values_set, gauge)
示例3: collect_snmp
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
def collect_snmp(config, host, port=161):
"""Scrape a host and return prometheus text format for it"""
start = time.time()
metrics = {}
for metric in config['metrics']:
metrics[metric['name']] = Metric(metric['name'], 'SNMP OID {0}'.format(metric['oid']), 'untyped')
do_bulkget = 'bulkget' not in config or config['bulkget']
values = walk_oids(host, port, config['walk'], config.get('community', 'public'), do_bulkget)
oids = {}
for oid, value in values:
oids[oid_to_tuple(oid)] = value
# Netsnmp doesn't tell us if an error has occured, so
# try to spot it by no results.
if not oids:
raise Exception("No OIDs returned, device not responding?")
# Build a tree from the rules based on oid for faster lookup.
metric_tree = {}
for metric in config['metrics']:
prefix = oid_to_tuple(metric['oid'])
head = metric_tree
for i in prefix:
head.setdefault('children', {})
head['children'].setdefault(i, {})
head = head['children'][i]
head['entry'] = metric
for oid, value in oids.items():
head = metric_tree
for i in oid:
head = head.get('children', {}).get(i)
if not head:
break
if 'entry' in head:
metric = head['entry']
prefix = oid_to_tuple(metric['oid'])
value = float(value)
indexes = oid[len(prefix):]
labels = parse_indexes(indexes, metric.get('indexes', {}), metric.get('lookups', {}), oids)
metrics[metric['name']].add_sample(metric['name'], value=value, labels=labels)
break
class Collector():
def collect(self):
return metrics.values()
registry = CollectorRegistry()
registry.register(Collector())
duration = Gauge('snmp_scrape_duration_seconds', 'Time this SNMP scrape took, in seconds', registry=registry)
duration.set(time.time() - start)
walked = Gauge('snmp_oids_walked', 'Number of oids walked in this scrape', registry=registry)
walked.set(len(oids))
return generate_latest(registry)
示例4: TestGauge
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
class TestGauge(unittest.TestCase):
def setUp(self):
self.registry = CollectorRegistry()
self.gauge = Gauge('g', 'help', registry=self.registry)
def test_gauge(self):
self.assertEqual(0, self.registry.get_sample_value('g'))
self.gauge.inc()
self.assertEqual(1, self.registry.get_sample_value('g'))
self.gauge.dec(3)
self.assertEqual(-2, self.registry.get_sample_value('g'))
self.gauge.set(9)
self.assertEqual(9, self.registry.get_sample_value('g'))
def test_function_decorator(self):
self.assertEqual(0, self.registry.get_sample_value('g'))
@self.gauge.track_inprogress()
def f():
self.assertEqual(1, self.registry.get_sample_value('g'))
f()
self.assertEqual(0, self.registry.get_sample_value('g'))
def test_block_decorator(self):
self.assertEqual(0, self.registry.get_sample_value('g'))
with self.gauge.track_inprogress():
self.assertEqual(1, self.registry.get_sample_value('g'))
self.assertEqual(0, self.registry.get_sample_value('g'))
def test_gauge_function(self):
x = {}
self.gauge.set_function(lambda: len(x))
self.assertEqual(0, self.registry.get_sample_value('g'))
self.gauge.inc()
self.assertEqual(0, self.registry.get_sample_value('g'))
x['a'] = None
self.assertEqual(1, self.registry.get_sample_value('g'))
def test_function_decorator(self):
self.assertEqual(0, self.registry.get_sample_value('g'))
@self.gauge.time()
def f():
time.sleep(.001)
f()
self.assertNotEqual(0, self.registry.get_sample_value('g'))
def test_block_decorator(self):
self.assertEqual(0, self.registry.get_sample_value('g'))
with self.gauge.time():
time.sleep(.001)
self.assertNotEqual(0, self.registry.get_sample_value('g'))
示例5: CounterRoller
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
class CounterRoller(RollerBase):
"""Accepts a Counter object and creates a gauge tracking its value over a given time period.
"""
def __init__(self, counter, options=None, registry=REGISTRY, roller_registry=ROLLER_REGISTRY):
self.counter = counter
if self.counter._type != 'counter':
raise ValueError('Only a Counter object should be passed to CounterRoller')
options = options or {}
self.extract_options(options)
self.past_values = deque()
full_name, _, _ = self.get_sample()
self.configure_with_full_name(full_name)
self.gauge = Gauge(
self.name,
self.documentation,
registry=registry
)
roller_registry[self.name] = self
def get_sample(self):
"""Returns (full_name, labels, value)
"""
return self.counter.collect()[0].samples[0]
def collect(self):
"""Update tracked counter values and current gauge value
"""
now = datetime.datetime.now()
# Fetch value from counter
_, _, value = self.get_sample()
# Add value
self.past_values.append((now, value))
# Drop old values
remove_old_values(self.past_values, now - self.retention_td)
# Calculate and record new rolled value
v = self.reducer(values_to_deltas(self.past_values), **self.reducer_kwargs)
self.gauge.set(v)
示例6: collect_snmp
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
def collect_snmp(config, host, port=161):
"""Scrape a host and return prometheus text format for it"""
start = time.time()
metrics = {}
for metric in config['metrics']:
prom_type = metric['metric_type'] if 'metric_type' in metric else 'gauge'
prom_help = metric['metric_help'] if 'metric_help' in metric else 'SNMP OID {0}'.format( metric['oid'] if 'oid' in metric else "NaN" )
metrics[metric['name']] = Metric(metric['name'], prom_help, prom_type)
values = walk_oids(host, port, config['walk'], config.get('community', 'public'), config.get('timeout', 5), config.get('retries', 3))
oids = {}
for oid, value in values:
if oid_to_tuple(oid) in oids:
if (((not oids[oid_to_tuple(oid)]) or oids[oid_to_tuple(oid)] == None) and value):
oids[oid_to_tuple(oid)] = value
else:
oids[oid_to_tuple(oid)] = value
for oid, value in oids.items():
for metric in config['metrics']:
prefix = oid_to_tuple(metric['oid'])
if oid[:len(prefix)] == prefix:
try:
value = float(value)
except ValueError as e:
print(e)
value = 0.0
indexes = oid[len(prefix):]
labels = parse_indexes(indexes, metric.get('indexes', {}), metric.get('lookups', {}), oids)
metrics[metric['name']].add_sample(metric['name'], value=value, labels=labels)
class Collector():
def collect(self):
return metrics.values()
registry = CollectorRegistry()
registry.register(Collector())
duration = Gauge('snmp_scrape_duration_seconds', 'Time this SNMP scrape took, in seconds', registry=registry)
duration.set(time.time() - start)
walked = Gauge('snmp_oids_walked', 'Number of oids walked in this scrape', registry=registry)
walked.set(len(oids))
return generate_latest(registry)
示例7: notify_success
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
def notify_success(self, source, hostname, filename, stats):
registry = CollectorRegistry()
s = Summary('backup_size', 'Size of backup file in bytes', registry=registry)
s.observe(stats.size)
s = Summary('backup_dumptime', 'Time taken to dump and compress/encrypt backup in seconds', registry=registry)
s.observe(stats.dumptime)
s = Summary('backup_uploadtime', 'Time taken to upload backup in seconds', registry=registry)
s.observe(stats.uploadtime)
if stats.retained_copies is not None:
g = Gauge('backup_retained_copies', 'Number of retained backups found on destination', registry=registry)
g.set(stats.retained_copies)
g = Gauge('backup_timestamp', 'Time backup completed as seconds-since-the-epoch', registry=registry)
g.set_to_current_time()
def auth_handler(url, method, timeout, headers, data):
return basic_auth_handler(url, method, timeout, headers, data, self.username, self.password)
push_to_gateway(self.url, job=source.id, registry=registry, handler=auth_handler)
logging.info("Pushed metrics for job '%s' to gateway (%s)" % (source.id, self.url))
示例8: test_gauge
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
def test_gauge(self):
g = Gauge('gg', 'A gauge', registry=self.registry)
g.set(17)
self.assertEqual(b'# HELP gg A gauge\n# TYPE gg gauge\ngg 17.0\n', generate_latest(self.registry))
示例9: Gauge
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
humidity_gauge = Gauge('home_humidity', 'Humidity at home')
target_temperature_f_gauge = Gauge('home_target_temperature_f', 'Target Temperature at home')
ambient_temperature_f_gauge = Gauge('home_ambient_temperature_f', 'Ambient Temperature at home')
client = pymongo.MongoClient("mongodb://localhost:27017")
nest_database = client.get_database("nest")
thermostat_log = nest_database.get_collection("thermostat_log")
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(HTTP_PORT)
# Generate some requests.
import time
while True:
result = thermostat_log.find().sort([('_id', -1)]).limit(1)[0]
last_connection = result.get('last_connection')
print("last_connection: %s" % last_connection)
humidity = result.get('humidity')
print("humidity: %s" % humidity)
humidity_gauge.set(humidity)
target_temperature_f = result.get('target_temperature_f')
print("target_temperature_f: %s" % target_temperature_f)
target_temperature_f_gauge.set(target_temperature_f)
ambient_temperature_f = result.get('ambient_temperature_f')
print("ambient_temperature_f: %s" % ambient_temperature_f)
ambient_temperature_f_gauge.set(ambient_temperature_f)
print("sleeping %s seconds" % SLEEP_DURATION)
time.sleep(SLEEP_DURATION)
示例10: str
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
record['Contacts_LeadType_S_Data_Status1'] = 'CAMPAIGN DETAIL ERROR'
else:
record['Contacts_LeadType_MostRecent_Offer_PrimarySol1'] = thisCampaign['Solution_Code_Family__c']
record['Contacts_LeadType_MostRecent_Offer_ProductSer1'] = thisCampaign['Solution_Code__c']
record['Contacts_LeadType_S_Data_Status1'] = 'CAMPAIGN DETAILS RETREIVED'
if (thisCampaign['Solution_Code_Family__c']==None):
nullCount += 1
logging.info("Records with no Primary Solution: " + str(nullCount))
importDefName = 'Contacts.LeadType - Get Campaign Details ' + str(datetime.now())
cdoInDef = elq.CreateDef(defType='imports', entity='customObjects', cdoID=1269, fields=myFields, defName=importDefName, identifierFieldName='Email_Address1')
logging.info("import definition created: " + cdoInDef['uri'])
postInData = elq.PostSyncData(data = dataOut, defObject=cdoInDef, maxPost=20000)
logging.info("Data successfully imported, job finished: " + str(datetime.now()))
else:
logging.info("No records, job finished")
### Logging for Prometheus
registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
h = Gauge('job_total_records_success', 'Total number of records successfully processed in last batch', registry=registry)
h.set(len(data))
push_to_gateway(os.environ['PUSHGATEWAY'], job='Contacts.LeadType_getOfferDetails', registry=registry)
示例11: Histogram
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
SERVER_SPAWN_DURATION_SECONDS = Histogram(
'server_spawn_duration_seconds',
'time taken for server spawning operation',
['status'],
# Use custom bucket sizes, since the default bucket ranges
# are meant for quick running processes. Spawns can take a while!
buckets=[0.5, 1, 2.5, 5, 10, 15, 30, 60, 120, float("inf")]
)
RUNNING_SERVERS = Gauge(
'running_servers',
'the number of user servers currently running'
)
RUNNING_SERVERS.set(0)
TOTAL_USERS = Gauge(
'total_users',
'toal number of users'
)
TOTAL_USERS.set(0)
CHECK_ROUTES_DURATION_SECONDS = Histogram(
'check_routes_duration_seconds',
'Time taken to validate all routes in proxy'
)
class ServerSpawnStatus(Enum):
"""
示例12: ESGaugeMetric
# 需要导入模块: from prometheus_client import Gauge [as 别名]
# 或者: from prometheus_client.Gauge import set [as 别名]
class ESGaugeMetric(object):
def __init__(self, name, desc, labels, value, value_converter, url, query, logger=None):
'''
name -- metric name (e.g. node_network_status)
desc -- metric description
labels -- indexes (tuple of strings) in metric_data taken as labels
value -- index in metric_data (dict) taken as value for metric
value_converter -- sometime value may came in mixed format like - 5s, 3GB.
we need to convert this value to numeric.
pass a function reference to this converter, can be lambda as well.
url -- elasticsearch url to index or GET query
query -- elasticsearch query data for POST request
logger -- instance of logging.Logger class
'''
self.gauge = Gauge(name, desc, list(labels))
self.name = name
self.labels = labels
self.value = value
self.value_converter = value_converter
self.url = url
self.query = query
self.logger = logger
def path_converter(self, path):
'''
convert path from indexA.indexB to ['indexA']['indexB']
path -- path in dot notaion
return -- path in bracket notaion
'''
elems = []
for elem in path.split('.'):
bracket = "['{0}']".format(elem)
elems.append(bracket)
return ''.join(elems)
def es_query(self, url, data):
'''
query Elasticsearch cluster and return raw requests.Response object
url -- url to elastic search e.g. - http://localhost:9200/bank/_search
data -- query in json format - more info reffer to Elasticsearch documentation
return -- raw requests.Response object
'''
headers = {'Content-Type': 'application/json'}
resp = requests.post(url, headers=headers, data=data)
return resp
def populate(self, metric_data):
'''
populate labels and value with data
metric_data -- dict object
return -- metric_labels - dict with label=value, metric_value - converted value
'''
try:
converter = getattr(self, self.value_converter)
except Exception:
converter = self.value_converter
value_path = self.path_converter(self.value)
value_var = 'metric_data{0}'.format(value_path)
metric_value = converter(eval(value_var))
metric_labels = {}
for label_name, label_path in self.labels.iteritems():
label_path = self.path_converter(label_path)
label_var = 'metric_data{0}'.format(label_path)
metric_labels[label_name] = eval(label_var)
return metric_labels, metric_value
def print_metric(self, metric_labels, metric_value):
'''
build and print metric
metric_labels -- labels to print
metric_value -- value to print
'''
if metric_labels:
label_value = []
for label, value in metric_labels.iteritems():
label_value.append('{l}={v}'.format(l=label, v=value))
# show labels in a log
text = '{n}{{{lv}}} {v}'.format(n=self.name, lv=', '.join(label_value), v=metric_value)
else:
# there are no labels to show
text = '{n} {v}'.format(n=self.name, v=metric_value)
if self.logger:
self.logger.info(text)
else:
print '[INFO]: {t}'.format(t=text)
def update(self, print_metric=False):
'''
query ES and update metric with newer value
print_metric -- print metric to stdout (good for dev stage)
'''
resp = self.es_query(self.url, data=self.query)
metric_data = json.loads(resp.text)
metric_labels, metric_value = self.populate(metric_data)
if print_metric:
self.print_metric(metric_labels, metric_value)
if self.labels:
self.gauge.labels(**metric_labels).set(metric_value)
else:
self.gauge.set(metric_value)