Exemplo n.º 1
0
 def test_extra_fields(self):
   self.metric.field_spec = [metrics.StringField('custom'),
                             metrics.StringField('type')]
   with helpers.ScopedMeasureTime(self.metric, 'custom', 'ok', 'fail',
                                  extra_fields_values={'type': 'normal'},
                                  time_fn=self.time_fn):
     pass
   self.metric.add.assert_called_once_with(0.25, {'custom': 'ok',
                                                  'type': 'normal'})
Exemplo n.º 2
0
 def test_bad_target_type(self):
     with self.assertRaises(errors.MetricDefinitionError):
         field_spec = [metrics.StringField('string')]
         metrics.Metric('/foo',
                        'foo',
                        field_spec,
                        units='us',
                        target_type=my_target_pb2.MyTarget())
     with self.assertRaises(errors.MetricDefinitionError):
         field_spec = [metrics.StringField('string')]
         metrics.Metric('/foo',
                        'foo',
                        field_spec,
                        units='us',
                        target_type=metrics.StringField)
Exemplo n.º 3
0
    def setUp(self):
        super(TSMonJSHandlerTest, self).setUp()

        config.reset_for_unittest()
        target = targets.TaskTarget('test_service', 'test_job', 'test_region',
                                    'test_host')
        self.mock_state = interface.State(target=target)
        mock.patch('infra_libs.ts_mon.common.interface.state',
                   new=self.mock_state).start()
        self.request = webapp2.Request.blank('/_/ts_mon_js')
        self.response = webapp2.Response()
        self.ts_mon_handler = handlers.TSMonJSHandler(request=self.request,
                                                      response=self.response)
        self.ts_mon_handler.register_metrics([
            metrics.BooleanMetric(
                'frontend/boolean_test',
                'Boolean metric test',
                field_spec=[metrics.StringField('client_id')]),
        ])
        self.ts_mon_handler.xsrf_is_valid = mock.Mock(return_value=True)
        self.mock_timestamp = 1537821859

        def time_fn():
            return self.mock_timestamp

        self.ts_mon_handler.time_fn = time_fn
Exemplo n.º 4
0
 def setUp(self):
   # To avoid floating point nightmare, use values which are exact in IEEE754.
   self.time_fn = mock.Mock(time.time, autospec=True, side_effect=[0.25, 0.50])
   self.metric = mock.create_autospec(metrics.CumulativeDistributionMetric,
                                      spec_set=True)
   self.metric.field_spec = [metrics.StringField('status')]
   self.metric.units = metrics.MetricsDataUnits.SECONDS
Exemplo n.º 5
0
 def test_custom_success(self):
   self.metric.field_spec = [metrics.StringField('label')]
   self.metric.units = metrics.MetricsDataUnits.MILLISECONDS
   with helpers.ScopedMeasureTime(self.metric, 'label', 'ok', 'fail',
                                  time_fn=self.time_fn):
     pass
   self.metric.add.assert_called_once_with(250.0, {'label': 'ok'})
Exemplo n.º 6
0
    def test_post_distribution_metrics_not_a_dict(self):
        """Test case when a distribution metric value is not a dict."""
        self.request.body = json.dumps({
            'metrics': [
                {
                    'MetricInfo': {
                        'Name': 'frontend/cumulative_test',
                        'ValueType': 2,
                    },
                    'Cells': [{
                        'value': 'rutabaga',
                        'fields': {
                            'client_id': '789',
                        },
                        'start_time': self.mock_timestamp - 60,
                    }],
                },
            ],
        })
        self.ts_mon_handler.register_metrics([
            metrics.CumulativeDistributionMetric(
                'frontend/cumulative_test',
                'Cumulative metric test',
                field_spec=[metrics.StringField('client_id')]),
        ])

        self.ts_mon_handler.post()
        self.assertEqual(self.response.status_int, 400)
        self.assertIn('Distribution metric values must be a dict',
                      self.response.body)
Exemplo n.º 7
0
    def test_post_rejects_start_time_in_past(self):
        """Test rejects when start_time is >1 month in the past."""
        one_month_seconds = 60 * 60 * 24 * 30
        self.request.body = json.dumps({
            'metrics': [
                {
                    'MetricInfo': {
                        'Name': 'frontend/cumulative_test',
                        'ValueType': 2,
                    },
                    'Cells': [{
                        'value':
                        'rutabaga',
                        'fields': {
                            'client_id': '789',
                        },
                        'start_time':
                        self.mock_timestamp - one_month_seconds * 2,
                    }],
                },
            ],
        })
        self.ts_mon_handler.register_metrics([
            metrics.CumulativeDistributionMetric(
                'frontend/cumulative_test',
                'Cumulative metric test',
                field_spec=[metrics.StringField('client_id')]),
        ])

        self.ts_mon_handler.post()

        self.assertEqual(self.response.status_int, 400)
        self.assertIn('Invalid start_time', self.response.body)
Exemplo n.º 8
0
    def test_post_rejects_cumulative_without_start_time(self):
        """Test case where start_time is not supplied for CumulativeDistribution."""
        self.request.body = json.dumps({
            'metrics': [
                {
                    'MetricInfo': {
                        'Name': 'frontend/cumulative_test',
                        'ValueType': 2,
                    },
                    'Cells': [{
                        'value': 'rutabaga',
                        'fields': {
                            'client_id': '789',
                        },
                    }],
                },
            ],
        })
        self.ts_mon_handler.register_metrics([
            metrics.CumulativeDistributionMetric(
                'frontend/cumulative_test',
                'Cumulative metric test',
                field_spec=[metrics.StringField('client_id')]),
        ])
        self.ts_mon_handler.post()

        self.assertEqual(self.response.status_int, 400)
        self.assertIn('Cumulative metrics', self.response.body)
Exemplo n.º 9
0
    def test_generate_every_type_of_field(self):
        counter = metrics.CounterMetric('counter', 'desc', [
            metrics.IntegerField('a'),
            metrics.BooleanField('b'),
            metrics.StringField('c'),
        ])
        interface.register(counter)
        counter.increment({'a': 1, 'b': True, 'c': 'test'})

        proto = list(interface._generate_proto())[0]
        data_set = proto.metrics_collection[0].metrics_data_set[0]

        field_type = metrics_pb2.MetricsDataSet.MetricFieldDescriptor
        self.assertEqual('a', data_set.field_descriptor[0].name)
        self.assertEqual(field_type.INT64,
                         data_set.field_descriptor[0].field_type)

        self.assertEqual('b', data_set.field_descriptor[1].name)
        self.assertEqual(field_type.BOOL,
                         data_set.field_descriptor[1].field_type)

        self.assertEqual('c', data_set.field_descriptor[2].name)
        self.assertEqual(field_type.STRING,
                         data_set.field_descriptor[2].field_type)

        self.assertEqual(1, data_set.data[0].int64_value)

        self.assertEqual('a', data_set.data[0].field[0].name)
        self.assertEqual(1, data_set.data[0].field[0].int64_value)

        self.assertEqual('b', data_set.data[0].field[1].name)
        self.assertTrue(data_set.data[0].field[1].bool_value)

        self.assertEqual('c', data_set.data[0].field[2].name)
        self.assertEqual('test', data_set.data[0].field[2].string_value)
Exemplo n.º 10
0
 def test_invalid_field_name(self):
   with self.assertRaises(errors.MetricDefinitionError):
     metrics.StringField('foo', 'desc', [metrics.StringField(' ')])
   with self.assertRaises(errors.MetricDefinitionError):
     metrics.StringField('foo', 'desc', [metrics.StringField('123')])
   with self.assertRaises(errors.MetricDefinitionError):
     metrics.StringField('foo', 'desc', [metrics.StringField('')])
   with self.assertRaises(errors.MetricDefinitionError):
     metrics.StringField('foo', 'desc', [metrics.StringField(u'\U0001F4A9')])
Exemplo n.º 11
0
 def test_custom_exception(self):
   self.metric.field_spec = [metrics.StringField('label')]
   self.metric.units = metrics.MetricsDataUnits.MICROSECONDS
   with self.assertRaises(_CustomException):
     with helpers.ScopedMeasureTime(self.metric, 'label', 'ok', 'fail',
                                    time_fn=self.time_fn):
       raise _CustomException()
   self.metric.add.assert_called_once_with(250000.0, {'label': 'fail'})
Exemplo n.º 12
0
 def test_equality(self):
     field_spec = [metrics.StringField('string')]
     m = metrics.Metric('/foo',
                        'foo',
                        field_spec,
                        units='us',
                        target_type=my_target_pb2.MyTarget)
     self.assertEqual(m, m)
Exemplo n.º 13
0
 def test_get_all(self):
     m = metrics.CounterMetric('test', 'test', [metrics.StringField('foo')])
     m.increment({'foo': ''})
     m.increment({'foo': 'bar'})
     self.assertEqual([
         (('', ), 1),
         (('bar', ), 1),
     ], sorted(m.get_all()))
Exemplo n.º 14
0
 def test_multiple_field_values(self):
     m = metrics.CounterMetric('test', 'test', [metrics.StringField('foo')])
     m.increment({'foo': 'bar'})
     m.increment({'foo': 'baz'})
     m.increment({'foo': 'bar'})
     with self.assertRaises(errors.WrongFieldsError):
         m.get()
     self.assertIsNone(m.get({'foo': ''}))
     self.assertEqual(2, m.get({'foo': 'bar'}))
     self.assertEqual(1, m.get({'foo': 'baz'}))
Exemplo n.º 15
0
 def test_properties(self):
     field_spec = [metrics.StringField('string')]
     m = metrics.Metric('/foo',
                        'foo',
                        field_spec,
                        units='us',
                        target_type=my_target_pb2.MyTarget)
     self.assertEqual(m.name, 'foo')
     self.assertEqual(m.field_spec, field_spec)
     self.assertEqual(m.units, 'us')
     self.assertEqual(m.target_type, my_target_pb2.MyTarget)
Exemplo n.º 16
0
 def test_string_field(self):
     f = metrics.StringField('name')
     f.validate_value('', 'string')
     f.validate_value('', u'string')
     with self.assertRaises(errors.MonitoringInvalidFieldTypeError):
         f.validate_value('', 123)
     with self.assertRaises(errors.MonitoringInvalidFieldTypeError):
         f.validate_value('', long(123))
     with self.assertRaises(errors.MonitoringInvalidFieldTypeError):
         f.validate_value('', True)
     with self.assertRaises(errors.MonitoringInvalidFieldTypeError):
         f.validate_value('', None)
     with self.assertRaises(errors.MonitoringInvalidFieldTypeError):
         f.validate_value('', 12.34)
Exemplo n.º 17
0
    def test_post_metrics_normal(self):
        """Test successful POST case."""
        self.request.body = json.dumps({
            'metrics': [
                {
                    'MetricInfo': {
                        'Name': 'frontend/boolean_test',
                        'ValueType': 2,
                    },
                    'Cells': [{
                        'value': True,
                        'fields': {
                            'client_id': '789',
                        },
                    }],
                },
                {
                    'MetricInfo': {
                        'Name': 'frontend/cumulative_test',
                        'ValueType': 2,
                    },
                    'Cells': [{
                        'value': {
                            'sum': 1234,
                            'count': 4321,
                            'buckets': {
                                0: 123,
                                1: 321,
                                2: 213,
                            },
                        },
                        'fields': {
                            'client_id': '789',
                        },
                        'start_time': self.mock_timestamp - 60,
                    }],
                },
            ],
        })
        self.ts_mon_handler.register_metrics([
            metrics.CumulativeDistributionMetric(
                'frontend/cumulative_test',
                'Cumulative metric test',
                field_spec=[metrics.StringField('client_id')]),
        ])

        self.ts_mon_handler.post()
        self.assertEqual(self.response.status_int, 201)
Exemplo n.º 18
0
    def test_populate_fields(self):
        data = metrics_pb2.MetricsData()
        m = metrics.Metric('test', 'test', [
            metrics.IntegerField('a'),
            metrics.BooleanField('b'),
            metrics.StringField('c'),
        ])
        m._populate_fields(data, (1, True, 'test'))

        self.assertEqual(3, len(data.field))

        self.assertEqual('a', data.field[0].name)
        self.assertEqual(1, data.field[0].int64_value)

        self.assertEqual('b', data.field[1].name)
        self.assertTrue(data.field[1].bool_value)

        self.assertEqual('c', data.field[2].name)
        self.assertEqual('test', data.field[2].string_value)
Exemplo n.º 19
0
    def test_populate_field_descriptors(self):
        data_set_pb = metrics_pb2.MetricsDataSet()
        m = metrics.Metric('test', 'test', [
            metrics.IntegerField('a'),
            metrics.BooleanField('b'),
            metrics.StringField('c'),
        ])
        m._populate_field_descriptors(data_set_pb)

        field_type = metrics_pb2.MetricsDataSet.MetricFieldDescriptor
        self.assertEqual(3, len(data_set_pb.field_descriptor))

        self.assertEqual('a', data_set_pb.field_descriptor[0].name)
        self.assertEqual(field_type.INT64,
                         data_set_pb.field_descriptor[0].field_type)

        self.assertEqual('b', data_set_pb.field_descriptor[1].name)
        self.assertEqual(field_type.BOOL,
                         data_set_pb.field_descriptor[1].field_type)

        self.assertEqual('c', data_set_pb.field_descriptor[2].name)
        self.assertEqual(field_type.STRING,
                         data_set_pb.field_descriptor[2].field_type)
Exemplo n.º 20
0
 def test_wrong_field(self):
     self.metric.field_spec = [metrics.StringField('wrong')]
     with self.assertRaises(AssertionError):
         helpers.ScopedMeasureTime(self.metric, 'status')
Exemplo n.º 21
0
 def test_init_too_many_fields(self):
     fields = [metrics.StringField('field%d' % i) for i in xrange(13)]
     with self.assertRaises(errors.MonitoringTooManyFieldsError) as e:
         metrics.Metric('test', 'test', fields)
     self.assertEqual(e.exception.metric, 'test')
     self.assertEqual(len(e.exception.fields), 13)
Exemplo n.º 22
0
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from infra_libs.ts_mon.common import metrics

# Extending HTTP status codes to client-side errors and timeouts.
STATUS_OK = 200
STATUS_ERROR = 901
STATUS_TIMEOUT = 902
STATUS_EXCEPTION = 909

request_bytes = metrics.CumulativeDistributionMetric(
    'http/request_bytes', 'Bytes sent per http request (body only).', [
        metrics.StringField('name'),
        metrics.StringField('client'),
    ])
response_bytes = metrics.CumulativeDistributionMetric(
    'http/response_bytes', 'Bytes received per http request (content only).', [
        metrics.StringField('name'),
        metrics.StringField('client'),
    ])
durations = metrics.CumulativeDistributionMetric(
    'http/durations', 'Time elapsed between sending a request and getting a'
    ' response (including parsing) in milliseconds.', [
        metrics.StringField('name'),
        metrics.StringField('client'),
    ])
response_status = metrics.CounterMetric(
    'http/response_status',
    'Number of responses received by HTTP status code.', [
Exemplo n.º 23
0
 def test_properties(self):
     field_spec = [metrics.StringField('string')]
     m1 = metrics.Metric('/foo', 'foo', field_spec, 'us')
     self.assertEquals(m1.name, 'foo')
     self.assertEquals(m1.field_spec, field_spec)
     self.assertEquals(m1.units, 'us')
Exemplo n.º 24
0
import logging.handlers
import os
import re
import socket
import sys
import tempfile
import textwrap

import pytz

from infra_libs.ts_mon.common import metrics

log_metric = metrics.CumulativeMetric(
    'proc/log_lines',
    'Number of log lines, per severity level.',
    [metrics.StringField('level')])

if sys.platform.startswith('win'):  # pragma: no cover
  DEFAULT_LOG_DIRECTORIES = os.pathsep.join([
      'E:\\chrome-infra-logs',
      'C:\\chrome-infra-logs',
  ])
else:
  DEFAULT_LOG_DIRECTORIES = '/var/log/chrome-infra'


class InfraFilter(logging.Filter):  # pragma: no cover
  """Adds fields used by the infra-specific formatter.

  Fields added:
Exemplo n.º 25
0
 def test_equality(self):
     field_spec = [metrics.StringField('string')]
     m = metrics.Metric('/foo', 'foo', field_spec, 'us')
     self.assertEquals(m, m)