OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import json |
| 6 import logging |
| 7 import os |
| 8 import socket |
| 9 import sys |
| 10 import urlparse |
| 11 import re |
| 12 |
| 13 import monitors |
| 14 import interface |
| 15 from common import standard_metrics |
| 16 from common import targets |
| 17 |
| 18 |
| 19 def initialize(endpoint=None, flush='manual', job_name=None, |
| 20 service_name=None, instance=0): |
| 21 """Initialize the global monitor. |
| 22 |
| 23 Also initializes the default target if endpoint argument is supplied. |
| 24 If they aren't, all created metrics will have to supply their own target. |
| 25 This is generally a bad idea, as many libraries rely on the default target |
| 26 being set up. |
| 27 |
| 28 Args: |
| 29 endpoint: url (in format pubsub://project/topic) to post ts_mon metrics to |
| 30 flush: metric push behavior: all (send every metric individually), |
| 31 or manual (only send when flush() is called) |
| 32 job_name: name of the job instance of the task |
| 33 instance: number of instance for this task |
| 34 service_name: name of service being monitored |
| 35 |
| 36 """ |
| 37 hostname = socket.getfqdn().split('.')[0] |
| 38 try: |
| 39 region = fqdn.split('.')[1] |
| 40 except: |
| 41 region = '' |
| 42 |
| 43 url = urlparse.urlparse(endpoint) |
| 44 project = url.netloc |
| 45 topic = url.path.strip('/') |
| 46 interface.state.global_monitor = monitors.PubSubMonitor(project, topic) |
| 47 |
| 48 # Reimplement ArgumentParser.error, since we don't have access to the parser |
| 49 if not service_name: |
| 50 logging.error('service_name variable is not set for task.') |
| 51 if not job_name: # pragma: no cover |
| 52 logging.error('job_name variable is not set for task.') |
| 53 interface.state.default_target = targets.TaskTarget( |
| 54 service_name, job_name, region, hostname, instance) |
| 55 |
| 56 interface.state.flush_mode = flush |
| 57 |
| 58 standard_metrics.init() |
OLD | NEW |