import os
import logging
import importlib

from performanceplatform.collector import arguments
from performanceplatform.collector.logging_setup import set_up_logging


def logging_for_entrypoint(entrypoint):
    logfile_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)), '..', '..', 'log')
    set_up_logging(entrypoint, logging.INFO, logfile_path)


def merge_performanceplatform_config(
        performanceplatform, data_set, token, dry_run=False):
    return {
        'url': '{0}/{1}/{2}'.format(
            performanceplatform['url'],
            data_set['data-group'],
            data_set['data-type']
        ),
        'token': token['token'],
        'data-group': data_set['data-group'],
        'data-type': data_set['data-type'],
        'dry_run': dry_run
    }


def main():
    args = arguments.parse_args('Performance Platform Collector')

    entrypoint = args.query['entrypoint']

    if args.console_logging:
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)
    else:
        logging_for_entrypoint(entrypoint)

    entrypoint_module = importlib.import_module(entrypoint)
    entrypoint_module.main(
        args.credentials,
        merge_performanceplatform_config(
            args.performanceplatform,
            args.query['data-set'],
            args.token,
            args.dry_run
        ),
        args.query['query'],
        args.query['options'],
        args.start_at,
        args.end_at
    )


if __name__ == '__main__':
    main()

@@ -36,16 +36,53 @@
 ortlib%0A%0A
+from collections import OrderedDict%0A%0A
 from per
@@ -125,16 +125,16 @@
 guments%0A
-
 from per
@@ -235,16 +235,29 @@
 trypoint
+, json_fields
 ):%0A    l
@@ -413,16 +413,29 @@
 ile_path
+, json_fields
 )%0A%0A%0Adef 
@@ -859,16 +859,993 @@
     %7D%0A%0A%0A
+def make_extra_json_fields(args):%0A    %22%22%22%0A    From the parsed command-line arguments, generate a dictionary of additional%0A    fields to be inserted into JSON logs (logstash_formatter module)%0A    %22%22%22%0A    return %7B%0A        '@data_group': _get_data_group(args.query),%0A        '@data_type': _get_data_type(args.query),%0A        '@data_group_data_type': _get_data_group_data_type(args.query),%0A        '@query': _get_query_params(args.query),%0A    %7D%0A%0A%0Adef _get_data_group(query):%0A    return query%5B'data-set'%5D%5B'data-group'%5D%0A%0A%0Adef _get_data_type(query):%0A    return query%5B'data-set'%5D%5B'data-type'%5D%0A%0A%0Adef _get_data_group_data_type(query):%0A    return '%7B%7D/%7B%7D'.format(_get_data_group(query), _get_data_type(query))%0A%0A%0Adef _get_query_params(query):%0A    %22%22%22%0A    %3E%3E%3E _get_data_group_data_type(%7B'query': %7B'a': 1, 'c': 3, 'b': 5%7D%7D)%0A    'a=1 b=5 c=3'%0A    %22%22%22%0A    query_params = OrderedDict(sorted(query%5B'query'%5D.items()))%0A    return ' '.join(%5B'%7B%7D=%7B%7D'.format(k, v) for k, v in query_params.items()%5D)%0A%0A%0A
 def main
@@ -2072,16 +2072,16 @@
   else:%0A
-
         
@@ -2113,16 +2113,46 @@
 trypoint
+, make_extra_json_fields(args)
 )%0A%0A    e

Add extra fields to JSON log