瀏覽代碼

[desktop] Fix MDL generation

Erick Tryzelaar 10 年之前
父節點
當前提交
9ed8727040

+ 130 - 10
desktop/core/src/desktop/lib/metrics/registry.py

@@ -37,27 +37,27 @@ class MetricsRegistry(object):
     return list(self._schemas)
 
   def counter(self, name, **kwargs):
-    self._schemas.append(MetricDefinition('counter', name, is_counter=True, **kwargs))
+    self._schemas.append(CounterDefinition(name, **kwargs))
     return self._registry.counter(name)
 
   def histogram(self, name, **kwargs):
-    self._schemas.append(MetricDefinition('histogram', name, is_counter=True, **kwargs))
+    self._schemas.append(HistogramDefinition(name, **kwargs))
     return self._registry.histogram(name)
 
   def gauge(self, name, gauge=None, default=float('nan'), **kwargs):
-    self._schemas.append(MetricDefinition('gauge', name, **kwargs))
+    self._schemas.append(GaugeDefinition(name, **kwargs))
     return self._registry.gauge(name, gauge, default)
 
   def gauge_callback(self, name, callback, default=float('nan'), **kwargs):
-    self._schemas.append(MetricDefinition('gauge', name, **kwargs))
+    self._schemas.append(GaugeDefinition(name, **kwargs))
     return self._registry.gauge(name, pyformance.meters.CallbackGauge(callback), default)
 
   def meter(self, name, **kwargs):
-    self._schemas.append(MetricDefinition('meter', name, is_counter=True, **kwargs))
+    self._schemas.append(MeterDefinition(name, **kwargs))
     return self._registry.meter(name)
 
   def timer(self, name, **kwargs):
-    self._schemas.append(MetricDefinition('timer', name, is_counter=True, **kwargs))
+    self._schemas.append(TimerDefinition(name, **kwargs))
     return Timer(self._registry.timer(name))
 
   def dump_metrics(self):
@@ -76,21 +76,141 @@ class MetricsRegistry(object):
 
 
 class MetricDefinition(object):
-  def __init__(self, metric_type, name, label, description, numerator,
+  def __init__(self, name, label, description, numerator,
       denominator=None,
-      is_counter=False,
       weighting_metric_name=None,
       context=None):
-    self.metric_type = metric_type
     self.name = name
     self.label = label
     self.description = description
     self.numerator = numerator
     self.denominator = denominator
-    self.is_counter = is_counter
     self.weighting_metric_name = weighting_metric_name
     self.context = context
 
+    assert self.name is not None
+    assert self.label is not None
+    assert self.description is not None
+    assert self.numerator is not None
+
+
+  def to_json(self):
+    raise NotImplementedError
+
+
+  def _make_json(self, key, **kwargs):
+    mdl = dict(
+      context='%s::%s' % (self.name, key),
+      name='hue_%s_%s' % (self.name.replace('.', '_').replace('-', '_'), key),
+      label=self.label,
+      description=self.description,
+      numeratorUnit=self.numerator,
+    )
+    mdl.update(**kwargs)
+
+    return mdl
+
+
+
+class CounterDefinition(MetricDefinition):
+  def __init__(self, *args, **kwargs):
+    super(CounterDefinition, self).__init__(*args, **kwargs)
+
+    assert self.denominator is None, "Counters should not have denominators"
+
+
+  def to_json(self):
+    return [
+        self._make_json('counter', counter=True),
+    ]
+
+
+class HistogramDefinition(MetricDefinition):
+  def __init__(self, *args, **kwargs):
+    self.counter_numerator = kwargs.pop('counter_numerator')
+
+    super(HistogramDefinition, self).__init__(*args, **kwargs)
+
+
+  def to_json(self):
+    return [
+        self._make_json('max'),
+        self._make_json('min'),
+        self._make_json('avg'),
+        self._make_json('count', counter=True, numeratorUnit=self.counter_numerator),
+        self._make_json('std_dev'),
+        self._make_json('75_percentile'),
+        self._make_json('95_percentile'),
+        self._make_json('99_percentile'),
+        self._make_json('999_percentile'),
+    ]
+
+
+class GaugeDefinition(MetricDefinition):
+  def __init__(self, *args, **kwargs):
+    self.counter = kwargs.pop('counter', False)
+
+    super(GaugeDefinition, self).__init__(*args, **kwargs)
+
+    assert not self.counter or self.denominator is None, \
+        "Gauge metrics that are marked as counters cannot have a denominator"
+
+
+  def to_json(self):
+    return [
+        self._make_json('gauge', counter=self.counter),
+    ]
+
+
+class MeterDefinition(MetricDefinition):
+  def __init__(self, *args, **kwargs):
+    self.counter_numerator = kwargs.pop('counter_numerator')
+    self.rate_denominator = kwargs.pop('rate_denominator')
+
+    assert self.counter_numerator is not None
+    assert self.rate_denominator is not None
+
+    super(MeterDefinition, self).__init__(*args, **kwargs)
+
+  def to_json(self):
+    return [
+        self._make_json('count', counter=True, numeratorUnit=self.counter_numerator),
+        self._make_json('15m_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+        self._make_json('5m_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+        self._make_json('1m_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+        self._make_json('mean_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+    ]
+
+
+class TimerDefinition(MetricDefinition):
+  def __init__(self, *args, **kwargs):
+    self.counter_numerator = kwargs.pop('counter_numerator')
+    self.rate_denominator = kwargs.pop('rate_denominator')
+
+    assert self.counter_numerator is not None
+    assert self.rate_denominator is not None
+
+    super(TimerDefinition, self).__init__(*args, **kwargs)
+
+
+  def to_json(self):
+    return [
+        self._make_json('avg'),
+        self._make_json('sum'),
+        self._make_json('count', counter=True, numeratorUnit=self.counter_numerator),
+        self._make_json('max'),
+        self._make_json('min'),
+        self._make_json('std_dev'),
+        self._make_json('15m_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+        self._make_json('5m_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+        self._make_json('1m_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+        self._make_json('mean_rate', numeratorUnit=self.counter_numerator, denominatorUnit=self.rate_denominator),
+        self._make_json('75_percentile'),
+        self._make_json('95_percentile'),
+        self._make_json('99_percentile'),
+        self._make_json('999_percentile'),
+    ]
+
 
 class Timer(object):
   """

+ 1 - 24
desktop/core/src/desktop/management/commands/generate_mdl.py

@@ -31,39 +31,16 @@ class Command(NoArgsCommand):
   def handle_noargs(self, **options):
     """Generates a Monitor Descriptor file."""
     registry = global_registry()
-    metrics = registry.dump_metrics()
     definitions = []
 
     for schema in registry.schemas:
-      metric = metrics[schema.name]
-      for key in metric.iterkeys():
-        definition = {
-          'context': '%s::%s::%s' % (schema.metric_type, schema.name, key),
-          'name': '%s_%s' % (schema.name.replace('.', '_').replace('-', '_'), key),
-          'label': schema.label,
-          'description': schema.description,
-          'numeratorUnit': schema.numerator,
-          'counter': schema.is_counter,
-        }
-
-        if schema.denominator is not None:
-          definition['denominatorUnit'] = schema.denominator
-
-        if schema.weighting_metric_name is not None:
-          definition['weightingMetricName'] = schema.weighting_metric_name
-
-        definitions.append(definition)
+      definitions.extend(schema.to_json())
 
     d = {
         'name': 'HUE',
         'nameForCrossEntityAggregateMetrics': 'hues',
         'version': 1,
         'metricDefinitions': [],
-        'compability': {
-          'cdhVersion': {
-            'min': '5.5',
-          },
-        },
         'roles': [
           {
             'name': 'HUE_SERVER',

+ 15 - 10
desktop/core/src/desktop/metrics.py

@@ -132,8 +132,9 @@ response_time = global_registry().timer(
     name='desktop.requests.aggregate-response-time',
     label='Request aggregate response time',
     description='Time taken to respond to requests',
-    numerator='requests',
-    denominator='seconds',
+    numerator='s',
+    counter_numerator='requests',
+    rate_denominator='seconds',
 )
 
 # ------------------------------------------------------------------------------
@@ -178,30 +179,34 @@ ldap_authentication_time = global_registry().timer(
     name='desktop.auth.ldap.authentication-time',
     label='LDAP Authentication time',
     description='Time taken to authenticate a user with LDAP',
-    numerator='auths',
-    denominator='seconds',
+    numerator='s',
+    counter_numerator='auths',
+    rate_denominator='seconds',
 )
 
 oauth_authentication_time = global_registry().timer(
     name='desktop.auth.oauth.authentication-time',
     label='OAUTH Authentication time',
     description='Time taken to authenticate a user with OAUTH',
-    numerator='auths',
-    denominator='seconds',
+    numerator='s',
+    counter_numerator='auths',
+    rate_denominator='seconds',
 )
 
 pam_authentication_time = global_registry().timer(
     name='desktop.auth.pam.authentication-time',
     label='PAM Authentication time',
     description='Time taken to authenticate a user with PAM',
-    numerator='auths',
-    denominator='seconds',
+    numerator='s',
+    counter_numerator='auths',
+    rate_denominator='seconds',
 )
 
 spnego_authentication_time = global_registry().timer(
     name='desktop.auth.spnego.authentication-time',
     label='SPNEGO Authentication time',
     description='Time taken to authenticate a user with SPNEGO',
-    numerator='auths',
-    denominator='seconds',
+    numerator='s',
+    counter_numerator='auths',
+    rate_denominator='seconds',
 )

+ 1 - 0
desktop/libs/libopenid/src/libopenid/__init__.py

@@ -15,3 +15,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import libopenid.metrics

+ 3 - 2
desktop/libs/libopenid/src/libopenid/metrics.py

@@ -22,6 +22,7 @@ openid_authentication_time = global_registry().timer(
     name='desktop.auth.openid.authentication-time',
     label='OpenID Authentication time',
     description='Time taken to authenticate a user with OpenID',
-    numerator='auths',
-    denominator='seconds',
+    numerator='s',
+    counter_numerator='auths',
+    rate_denominator='seconds',
 )

+ 18 - 0
desktop/libs/libsaml/src/libsaml/__init__.py

@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import libsaml.metrics

+ 3 - 2
desktop/libs/libsaml/src/libsaml/metrics.py

@@ -22,6 +22,7 @@ saml2_authentication_time = global_registry().timer(
     name='desktop.auth.saml2.authentication-time',
     label='SAML2 Authentication time',
     description='Time taken to authenticate a user with SAML2',
-    numerator='auths',
-    denominator='seconds',
+    numerator='s',
+    counter_numerator='auths',
+    rate_denominator='seconds',
 )