github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/metrics/metricbase.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  
    18  """
    19  The classes in this file are interfaces for metrics. They are not intended
    20  to be subclassed or created directly by users. To work with and access metrics,
    21  users should use the classes and methods exposed in metric.py.
    22  
    23  Available classes:
    24  
    25  - Metric - Base interface of a metrics object.
    26  - Counter - Counter metric interface. Allows a count to be incremented or
    27      decremented during pipeline execution.
    28  - Distribution - Distribution Metric interface. Allows statistics about the
    29      distribution of a variable to be collected during pipeline execution.
    30  - Gauge - Gauge Metric interface. Allows to track the latest value of a
    31      variable during pipeline execution.
    32  - MetricName - Namespace and name used to refer to a Metric.
    33  """
    34  
    35  # pytype: skip-file
    36  
    37  from typing import Dict
    38  from typing import Optional
    39  
    40  __all__ = [
    41      'Metric', 'Counter', 'Distribution', 'Gauge', 'Histogram', 'MetricName'
    42  ]
    43  
    44  
    45  class MetricName(object):
    46    """The name of a metric.
    47  
    48    The name of a metric consists of a namespace and a name. The namespace
    49    allows grouping related metrics together and also prevents collisions
    50    between multiple metrics of the same name.
    51    """
    52    def __init__(self, namespace, name, urn=None, labels=None):
    53      # type: (Optional[str], Optional[str], Optional[str], Optional[Dict[str, str]]) -> None
    54  
    55      """Initializes ``MetricName``.
    56  
    57      Note: namespace and name should be set for user metrics,
    58      urn and labels should be set for an arbitrary metric to package into a
    59      MonitoringInfo.
    60  
    61      Args:
    62        namespace: A string with the namespace of a metric.
    63        name: A string with the name of a metric.
    64        urn: URN to populate on a MonitoringInfo, when sending to RunnerHarness.
    65        labels: Labels to populate on a MonitoringInfo
    66      """
    67      if not urn:
    68        if not namespace:
    69          raise ValueError('Metric namespace must be non-empty')
    70        if not name:
    71          raise ValueError('Metric name must be non-empty')
    72      self.namespace = namespace
    73      self.name = name
    74      self.urn = urn
    75      self.labels = labels if labels else {}
    76  
    77    def __eq__(self, other):
    78      return (
    79          self.namespace == other.namespace and self.name == other.name and
    80          self.urn == other.urn and self.labels == other.labels)
    81  
    82    def __str__(self):
    83      if self.urn:
    84        return 'MetricName(namespace={}, name={}, urn={}, labels={})'.format(
    85            self.namespace, self.name, self.urn, self.labels)
    86      else:  # User counter case.
    87        return 'MetricName(namespace={}, name={})'.format(
    88            self.namespace, self.name)
    89  
    90    def __hash__(self):
    91      return hash((self.namespace, self.name, self.urn) +
    92                  tuple(self.labels.items()))
    93  
    94    def fast_name(self):
    95      name = self.name or ''
    96      namespace = self.namespace or ''
    97      urn = self.urn or ''
    98      labels = ''
    99      if self.labels:
   100        labels = '_'.join(['%s=%s' % (k, v) for (k, v) in self.labels.items()])
   101      return '%d_%s%s%s%s' % (len(name), name, namespace, urn, labels)
   102  
   103  
   104  class Metric(object):
   105    """Base interface of a metric object."""
   106    def __init__(self, metric_name):
   107      # type: (MetricName) -> None
   108      self.metric_name = metric_name
   109  
   110  
   111  class Counter(Metric):
   112    """Counter metric interface. Allows a count to be incremented/decremented
   113    during pipeline execution."""
   114    def inc(self, n=1):
   115      raise NotImplementedError
   116  
   117    def dec(self, n=1):
   118      self.inc(-n)
   119  
   120  
   121  class Distribution(Metric):
   122    """Distribution Metric interface.
   123  
   124    Allows statistics about the distribution of a variable to be collected during
   125    pipeline execution."""
   126    def update(self, value):
   127      raise NotImplementedError
   128  
   129  
   130  class Gauge(Metric):
   131    """Gauge Metric interface.
   132  
   133    Allows tracking of the latest value of a variable during pipeline
   134    execution."""
   135    def set(self, value):
   136      raise NotImplementedError
   137  
   138  
   139  class Histogram(Metric):
   140    """Histogram Metric interface.
   141  
   142    Allows statistics about the percentile of a variable to be collected during
   143    pipeline execution."""
   144    def update(self, value):
   145      raise NotImplementedError