Example #1
0
        private JToken Evaluate(string featureKey, User user, JToken defaultValue, JTokenType?expectedType)
        {
            if (!Initialized())
            {
                Logger.LogWarning("LaunchDarkly client has not yet been initialized. Returning default");
                return(defaultValue);
            }
            if (user == null || user.Key == null)
            {
                Logger.LogWarning("Feature flag evaluation called with null user or null user key. Returning default");
                sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, null);
                return(defaultValue);
            }

            try
            {
                var featureFlag = _featureStore.Get(featureKey);
                if (featureFlag == null)
                {
                    Logger.LogWarning("Unknown feature flag " + featureKey + "; returning default value: ");
                    sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, null);
                    return(defaultValue);
                }

                FeatureFlag.EvalResult evalResult = featureFlag.Evaluate(user, _featureStore);
                if (!IsOffline())
                {
                    foreach (var prereqEvent in evalResult.PrerequisiteEvents)
                    {
                        _eventStore.Add(prereqEvent);
                    }
                }
                if (evalResult.Result != null)
                {
                    if (expectedType != null && !evalResult.Result.Type.Equals(expectedType))
                    {
                        Logger.LogError("Expected type: " + expectedType + " but got " + evalResult.GetType() +
                                        " when evaluating FeatureFlag: " + featureKey + ". Returning default");
                        sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, featureFlag.Version);
                        return(defaultValue);
                    }
                    sendFlagRequestEvent(featureKey, user, evalResult.Result, defaultValue, featureFlag.Version);
                    return(evalResult.Result);
                }
            }
            catch (Exception e)
            {
                Logger.LogError(
                    String.Format(
                        "Encountered exception in LaunchDarkly client: {0} when evaluating feature key: {1} for user key: {2}",
                        e.Message, featureKey, user.Key));
                Logger.LogDebug(e.ToString());
            }
            sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, null);
            return(defaultValue);
        }
Example #2
0
        private JToken Evaluate(string featureKey, User user, JToken defaultValue, JTokenType?expectedType)
        {
            if (!Initialized())
            {
                Log.Warn("LaunchDarkly client has not yet been initialized. Returning default");
                return(defaultValue);
            }
            if (user == null || user.Key == null)
            {
                Log.Warn("Feature flag evaluation called with null user or null user key. Returning default");
                sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, null);
                return(defaultValue);
            }

            try
            {
                var featureFlag = _featureStore.Get(VersionedDataKind.Features, featureKey);
                if (featureFlag == null)
                {
                    Log.InfoFormat("Unknown feature flag {0}; returning default value",
                                   featureKey);

                    sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, null);
                    return(defaultValue);
                }

                FeatureFlag.EvalResult evalResult = featureFlag.Evaluate(user, _featureStore, _configuration);
                if (!IsOffline())
                {
                    foreach (var prereqEvent in evalResult.PrerequisiteEvents)
                    {
                        _eventStore.Add(prereqEvent);
                    }
                }
                if (evalResult.Result != null)
                {
                    if (expectedType != null && !evalResult.Result.Type.Equals(expectedType))
                    {
                        Log.ErrorFormat("Expected type: {0} but got {1} when evaluating FeatureFlag: {2}. Returning default",
                                        expectedType,
                                        evalResult.GetType(),
                                        featureKey);

                        sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, featureFlag.Version);
                        return(defaultValue);
                    }
                    sendFlagRequestEvent(featureKey, user, evalResult.Result, defaultValue, featureFlag.Version);
                    return(evalResult.Result);
                }
            }
            catch (Exception e)
            {
                Log.ErrorFormat("Encountered exception in LaunchDarkly client: {0} when evaluating feature key: {1} for user key: {2}",
                                e,
                                Util.ExceptionMessage(e),
                                featureKey,
                                user.Key);

                Log.Debug("{0}", e);
            }
            sendFlagRequestEvent(featureKey, user, defaultValue, defaultValue, null);
            return(defaultValue);
        }