def CollectGAMetric(self, category, action, label=VERSION, value=0, execution_time=None, **custom_params): """Adds a GA metric with the given parameters to the metrics queue. Args: category: str, the GA Event category. action: str, the GA Event action. label: str, the GA Event label. value: int, the GA Event value. execution_time: int, the execution time to record in ms. **custom_params: A dictionary of key, value pairs containing custom metrics and dimensions to send with the GA Event. """ params = [('ec', category), ('ea', action), ('el', label), ('ev', value), (_GA_LABEL_MAP['Timestamp'], _GetTimeInMillis())] params.extend([(k, v) for k, v in custom_params.iteritems() if v is not None]) params.extend([(k, v) for k, v in self.ga_params.iteritems() if v is not None]) # Log how long after the start of the program this event happened. if execution_time is None: execution_time = _GetTimeInMillis() - self.start_time params.append((_GA_LABEL_MAP['Execution Time'], execution_time)) data = urllib.urlencode(sorted(params)) self._metrics.append(Metric(endpoint=self.endpoint, method='POST', body=data, user_agent=self.user_agent))
from gslib.util import ONE_MIB from gslib.util import START_CALLBACK_PER_BYTES import mock # A piece of the URL logged for all of the tests. GLOBAL_DIMENSIONS_URL_PARAMS = ( 'a=b&c=d&cd1=cmd1+action1&cd10=0&cd2=x%2Cy%2Cz&cd3=opta%2Coptb&' 'cd6=CommandException&cm1=0') GLOBAL_PARAMETERS = ['a=b', 'c=d', 'cd1=cmd1 action1', 'cd2=x,y,z', 'cd3=opta,optb', 'cd6=CommandException', 'cm1=0', 'ev=0', 'el={0}'.format(VERSION)] COMMAND_AND_ERROR_TEST_METRICS = set([ Metric( 'https://example.com', 'POST', '{0}&cm2=3&ea=cmd1+action1&ec={1}&el={2}&ev=0'.format( GLOBAL_DIMENSIONS_URL_PARAMS, metrics._GA_COMMANDS_CATEGORY, VERSION), 'user-agent-007'), Metric( 'https://example.com', 'POST', '{0}&cm2=2&ea=Exception&ec={1}&el={2}&ev=0'.format( GLOBAL_DIMENSIONS_URL_PARAMS, metrics._GA_ERRORRETRY_CATEGORY, VERSION), 'user-agent-007'), Metric( 'https://example.com', 'POST', '{0}&cm2=1&ea=ValueError&ec={1}&el={2}&ev=0'.format( GLOBAL_DIMENSIONS_URL_PARAMS, metrics._GA_ERRORRETRY_CATEGORY, VERSION), 'user-agent-007'), Metric(