github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/reporting.py (about)

     1  """Reporting helper class for communicating with influx db."""
     2  import time
     3  import datetime
     4  try:
     5      import urlparse
     6  except ImportError:
     7      import urllib.parse as urlparse
     8  
     9  from influxdb import InfluxDBClient
    10  from influxdb.client import InfluxDBClientError
    11  
    12  __metaclass__ = type
    13  
    14  DBNAME = 'txn_metrics'
    15  POLICYNAME = 'txn_metric'
    16  
    17  class _Reporting:
    18      """_Reporting represents a class to report metrics upon"""
    19  
    20      def __init__(self, client):
    21          self.client = client
    22          self.labels = {
    23              "total_time": "txn_metric.total_time",
    24              "total_num_txns": "txn_metric.total_num_txns",
    25              "max_time": "txn_metric.max_time",
    26              "test_duration": "txn_metric.test_duration",
    27          }
    28  
    29  class InfluxClient(_Reporting):
    30      """InfluxClient represents a influx db reporting client"""
    31  
    32      def __init__(self, *args, **kwargs):
    33          super(InfluxClient, self).__init__(*args, **kwargs)
    34  
    35      def report(self, metrics, tags):
    36          """Report the metrics to the underlying reporting client
    37          """
    38  
    39          now = datetime.datetime.today()
    40          series = []
    41          for key, label in self.labels.items():
    42              if key in metrics:
    43                  pointValue = {
    44                      "measurement": label,
    45                      "tags": tags,
    46                      "time": int(now.strftime('%s')),
    47                      "fields": {
    48                          "value": metrics[key],
    49                      },
    50                  }
    51                  series.append(pointValue)
    52          self.client.write_points(series, retention_policy=POLICYNAME)
    53  
    54  def construct_metrics(total_time, total_num_txns, max_time, test_duration):
    55      """Make metrics creates a dictionary of items to pass to the 
    56         reporting client.
    57      """
    58  
    59      return {
    60          "total_time": total_time,
    61          "total_num_txns": total_num_txns,
    62          "max_time": max_time,
    63          "test_duration": test_duration,
    64      }
    65  
    66  def get_reporting_client(uri):
    67      """Reporting client returns a client for reporting metrics to.
    68         It expects that the uri can be parsed and sent to the client constructor.
    69  
    70      :param uri: URI to connect to the client.
    71      """
    72      # Extract the uri 
    73      parsed_uri = urlparse.urlsplit(uri)
    74      client = InfluxDBClient(
    75          host=parsed_uri.hostname, 
    76          port=parsed_uri.port,
    77          username=parsed_uri.username,
    78          password=parsed_uri.password,
    79          database=DBNAME,
    80      )
    81      try:
    82          client.switch_database(DBNAME)
    83      except InfluxDBClientError:
    84          client.create_database(DBNAME)
    85          client.create_retention_policy(POLICYNAME, 'INF', '1', DBNAME)
    86      return InfluxClient(client)