def _evaluate_internal(self, key, user, default, include_reasons_in_events): default = self._config.get_default(key, default) if self._config.offline: return EvaluationDetail(default, None, error_reason('CLIENT_NOT_READY')) if user is not None: self._sanitize_user(user) def send_event(value, variation=None, flag=None, reason=None): self._send_event({'kind': 'feature', 'key': key, 'user': user, 'value': value, 'variation': variation, 'default': default, 'version': flag.get('version') if flag else None, 'trackEvents': flag.get('trackEvents') if flag else None, 'debugEventsUntilDate': flag.get('debugEventsUntilDate') if flag else None, 'reason': reason if include_reasons_in_events else None}) if not self.is_initialized(): if self._store.initialized: log.warn("Feature Flag evaluation attempted before client has initialized - using last known values from feature store for feature key: " + key) else: log.warn("Feature Flag evaluation attempted before client has initialized! Feature store unavailable - returning default: " + str(default) + " for feature key: " + key) reason = error_reason('CLIENT_NOT_READY') send_event(default, None, None, reason) return EvaluationDetail(default, None, reason) if user is not None and user.get('key', "") == "": log.warn("User key is blank. Flag evaluation will proceed, but the user will not be stored in LaunchDarkly.") flag = self._store.get(FEATURES, key, lambda x: x) if not flag: reason = error_reason('FLAG_NOT_FOUND') send_event(default, None, None, reason) return EvaluationDetail(default, None, reason) else: if user is None or user.get('key') is None: reason = error_reason('USER_NOT_SPECIFIED') send_event(default, None, flag, reason) return EvaluationDetail(default, None, reason) try: result = evaluate(flag, user, self._store, include_reasons_in_events) for event in result.events or []: self._send_event(event) detail = result.detail if detail.is_default_value(): detail = EvaluationDetail(default, None, detail.reason) send_event(detail.value, detail.variation_index, flag, detail.reason) return detail except Exception as e: log.error("Unexpected error while evaluating feature flag \"%s\": %s" % (key, e)) log.debug(traceback.format_exc()) reason = error_reason('EXCEPTION') send_event(default, None, flag, reason) return EvaluationDetail(default, None, reason)
def _evaluate_internal(self, key, user, default, event_factory): default = self._config.get_default(key, default) if self._config.offline: return EvaluationDetail(default, None, error_reason('CLIENT_NOT_READY')) if not self.is_initialized(): if self._store.initialized: log.warning( "Feature Flag evaluation attempted before client has initialized - using last known values from feature store for feature key: " + key) else: log.warning( "Feature Flag evaluation attempted before client has initialized! Feature store unavailable - returning default: " + str(default) + " for feature key: " + key) reason = error_reason('CLIENT_NOT_READY') self._send_event( event_factory.new_unknown_flag_event( key, user, default, reason)) return EvaluationDetail(default, None, reason) if user is not None and user.get('key', "") == "": log.warning( "User key is blank. Flag evaluation will proceed, but the user will not be stored in LaunchDarkly." ) try: flag = self._store.get(FEATURES, key, lambda x: x) except Exception as e: log.error( "Unexpected error while retrieving feature flag \"%s\": %s" % (key, repr(e))) log.debug(traceback.format_exc()) reason = error_reason('EXCEPTION') self._send_event( event_factory.new_unknown_flag_event(key, user, default, reason)) return EvaluationDetail(default, None, reason) if not flag: reason = error_reason('FLAG_NOT_FOUND') self._send_event( event_factory.new_unknown_flag_event(key, user, default, reason)) return EvaluationDetail(default, None, reason) else: if user is None or user.get('key') is None: reason = error_reason('USER_NOT_SPECIFIED') self._send_event( event_factory.new_default_event(flag, user, default, reason)) return EvaluationDetail(default, None, reason) try: result = evaluate(flag, user, self._store, event_factory) for event in result.events or []: self._send_event(event) detail = result.detail if detail.is_default_value(): detail = EvaluationDetail(default, None, detail.reason) self._send_event( event_factory.new_eval_event(flag, user, detail, default)) return detail except Exception as e: log.error( "Unexpected error while evaluating feature flag \"%s\": %s" % (key, repr(e))) log.debug(traceback.format_exc()) reason = error_reason('EXCEPTION') self._send_event( event_factory.new_default_event(flag, user, default, reason)) return EvaluationDetail(default, None, reason)