pabloem commented on a change in pull request #13217:
URL: https://github.com/apache/beam/pull/13217#discussion_r525570371



##########
File path: sdks/python/apache_beam/metrics/metricbase.py
##########
@@ -50,34 +52,56 @@ class MetricName(object):
   allows grouping related metrics together and also prevents collisions
   between multiple metrics of the same name.
   """
-  def __init__(self, namespace, name):
-    # type: (str, str) -> None
+  def __init__(self, namespace, name, urn=None, labels=None):
+    # type: (Optional[str], Optional[str], Optional[str], Optional[Dict[str, 
str]]) -> None
 
     """Initializes ``MetricName``.
 
+    Note: namespace and name should be set for user metrics,
+    urn and labels should be set for an arbitrary metric to package into a
+    MonitoringInfo.
+
     Args:
       namespace: A string with the namespace of a metric.
       name: A string with the name of a metric.
+      urn: URN to populate on a MonitoringInfo, when sending to RunnerHarness.
+      labels: Labels to populate on a MonitoringInfo
     """
-    if not namespace:
-      raise ValueError('Metric namespace must be non-empty')
-    if not name:
-      raise ValueError('Metric name must be non-empty')
+    if not urn:
+      if not namespace:
+        raise ValueError('Metric namespace must be non-empty')
+      if not name:
+        raise ValueError('Metric name must be non-empty')
     self.namespace = namespace
     self.name = name
+    self.urn = urn
+    self.labels = labels if labels else {}
 
   def __eq__(self, other):
-    return self.namespace == other.namespace and self.name == other.name
+    return (
+        self.namespace == other.namespace and self.name == other.name and
+        self.urn == other.urn and self.labels == other.labels)
 
   def __ne__(self, other):
     # TODO(BEAM-5949): Needed for Python 2 compatibility.
     return not self == other
 
   def __str__(self):
-    return 'MetricName(namespace={}, name={})'.format(self.namespace, 
self.name)
+    return 'MetricName(namespace={}, name={}, urn={}, labels={})'.format(
+        self.namespace, self.name, self.urn, self.labels)
 
   def __hash__(self):
-    return hash((self.namespace, self.name))
+    return hash((self.namespace, self.name, self.urn) +
+                tuple(self.labels.items()))
+
+  def fast_name(self):
+    name = self.name or ''
+    namespace = self.namespace or ''
+    urn = self.urn or ''
+    labels = ''
+    if self.labels:
+      labels = '_'.join(['%s=%s' % (k, v) for (k, v) in self.labels.items()])
+    return '%d_%s%s%s%s' % (len(name), name, namespace, urn, labels)

Review comment:
       oh seems like the answer is no from 3.6: 
https://softwaremaniacs.org/blog/2020/02/05/dicts-ordered/
   
   Since we don't support 3.5 anymore, I think you can ignore this.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to