def test_send_metric_with_time_as_string(self, influx_write):

        timestamp = datetime(2017, 10, 19, 18, 12, 51).strftime('%Y-%m-%dT%H:%M:%S.%fZ')
        timestamp = '{}'.format(timestamp)
        data = [
                    {
                        'fields': {
                            'value': 10,
                            'add_field_1': 'a',
                            'add_field_2': 2
                        },
                        'time': '2017-10-19T18:12:51.000000Z',
                        'tags': {
                            'environment': 'dev',
                            'product': 'consignado'
                        },
                        'measurement': 'test'
                    }
                ]

        send_metric_influx(
            'localhost', 'root', 'root',  8086, 'dev', 'test', 10,
            fields=[{'add_field_1': 'a'}, {'add_field_2': 2}], tags=[{'product': 'consignado'}], timestamp=timestamp
        )

        influx_write.assert_called_with(data)
    def test_send_metric_with_no_envvar(self, client_mock):
        timestamp = datetime(2017, 10, 19, 18, 12, 51)
        fields = [
            {'value': 10},
            {'add_field_1': 'a'},
            {'add_field_2': 2},
        ]
        tags = [
            {'environment': 'dev'},
            {'product': 'consignado'},
        ]

        send_metric_influx('localhost', 'root', 'root', 8086, 'dev', 'test', value=1, fields=fields, tags=tags, timestamp=timestamp)

        client_mock.assert_called_with('localhost', 8086, 'root', 'root', 'metrics')
    def test_send_metric_with_no_fields_or_tags(self, influx_write):

        timestamp = datetime(2017, 10, 19, 18, 12, 51)
        data = [
                    {
                        'fields': {
                            'value': 10
                        },
                        'time': '2017-10-19T18:12:51.000000Z',
                        'tags': {
                            'environment': 'dev'
                        },
                        'measurement': 'test'
                    }
                ]

        send_metric_influx('localhost', 'root', 'root', 8086, 'dev', 'test', 10, timestamp=timestamp)

        influx_write.assert_called_with(data)
def send_metric(server,
                port,
                metric,
                value,
                fields=None,
                tags=None,
                timestamp=None,
                environment=None,
                client_type='influxdb',
                username='******',
                password='******'):
    '''
    Send metric generic metric
    :param server: server domain name
    :param port: metric server port
    :param environment: current environment (dev, stage, production)
    :param metric: metric name
    :param value: metric value
    :param fields: list of additional fields wanted to influx metrics (ONLY influx, this data will not be taken into account
                   on graphite!), in the form [{'key1': value1},...,{'keyN': valueN}]
    :param tags: list of tags in the form [{'key1': value1},...,{'keyN': valueN}]
    :param timestamp: datetime with metric time
    :param client_type: Type of metric collection egine (eg. 'graphite' or 'influxdb')
    :param username: Metric DB username if applicable
    :param password: Metric DB password if applicable
    :return:
    '''
    if not timestamp:
        timestamp = datetime.utcnow()

    if client_type == 'influxdb':
        logger.debug('send_metric: using influxdb')
        send_metric_influx(server, username, password, port, environment,
                           metric, value, fields, tags, timestamp)
    elif client_type == 'graphite':
        send_metric_graphite(server, port, environment, metric, value, tags,
                             timestamp)