def recv_log(self, message=None, level='info'): if message: source = get_request_meta().get('source', 'unknown') source_environment = get_request_meta().get('source_environment', DEFAULT_ENVIRONMENT) request_id = get_request_meta().get('request_id') or '-' getattr(self.log, level)( "[%s_%s] %s %s" % (source_environment, source, request_id, message)) return True return False
def _get_current_environment(self): # first, try to get environment from request_meta, # then from server_meta, then use default return get_request_meta().get( 'environment', get_server_meta().get('environment', DEFAULT_ENVIRONMENT))
def set_endpoint(self, environment=DEFAULT_ENVIRONMENT, service=None, endpoint_parts=None): endpoint = "{scheme}://{host}:{port}/{path}".format( scheme=endpoint_parts.get('scheme', 'http'), host=endpoint_parts.get('host', get_request_meta().get('environ')['REMOTE_ADDR']), port=endpoint_parts['port'], path=endpoint_parts['path']) self.log.info("Registering endpoint %s:%s = %s" % ( environment, service, endpoint)) dict_set(self.service_endpoints, [environment, service], endpoint) return endpoint
def set_custom_environment(self, query_dict): if "environment" in query_dict: # environments set in the request should be # stored in request_meta, not server_meta. # Storing environment in request_meta will # propagate it to other services as well. environment = query_dict['environment'][-1] get_request_meta()['environment'] = environment self.log.info('Using custom environment: %s' % environment) return environment
def set_custom_endpoints(self, query_dict): # custom enpoint information travels on in request_meta custom_endpoints = {} for key in query_dict.keys(): if key.startswith(ENDPOINT_OVERRIDE_PREFIX): endpoint = key[(len(ENDPOINT_OVERRIDE_PREFIX)):] custom_endpoints[endpoint] = query_dict[key][-1] if custom_endpoints: get_request_meta()['custom_endpoints'] = custom_endpoints self.log.info('Using custom endpoints: %s' % str(custom_endpoints)) return custom_endpoints
def _get_endpoint_with_overrides(self, environment, service): is_custom_endpoint = False endpoint = self.get_endpoint(environment, service) custom_endpoint = get_request_meta().get('custom_endpoints', {}).get(service, None) if custom_endpoint: is_custom_endpoint = True # custom_endpoint could be an environment name # or an endpoint url # TEMPORARILY DISABLE # if custom_endpoint in self.get_environments(): # endpoint = self.endpoints[custom_endpoint].get(service) #else: endpoint = custom_endpoint return endpoint, is_custom_endpoint