Пример #1
0
        private void ValidateConfigurationParameters()
        {
            if (!_isOffline)
            {
                if (string.IsNullOrEmpty(_sdkKey))
                {
                    // User didn't supply SDK key, try reading from environment variable
                    var s = Environment.GetEnvironmentVariable(UnlaunchConstants.SdkKeyEnvVariableName);
                    if (string.IsNullOrEmpty(s))
                    {
                        throw new ArgumentException($"sdkKey cannot be null or empty. Must be supplied to the builder or set as an environment variable. {UnlaunchConstants.GetSdkKeyHelpMessage()}");
                    }

                    Logger.Info("Setting SDK Key read from environment variable");
                    _sdkKey = s;
                }

                if (string.IsNullOrEmpty(_baseUrl))
                {
                    throw new ArgumentException("hostname cannot be null or empty. Must point to a valid Unlaunch Service host");
                }
            }

            Contract.EnsuresOnThrow <ArgumentException>(_pollingInterval >= MinPollInterval,
                                                        $"pollingInterval must be great than {MinPollInterval.TotalSeconds} seconds");
            Contract.EnsuresOnThrow <ArgumentException>(_connectionTimeout >= MinConnectionTimeout,
                                                        $"connectionTimeOut must be at least {_connectionTimeout.TotalMilliseconds} milliseconds ");
            Contract.EnsuresOnThrow <ArgumentException>(_connectionTimeout.TotalMilliseconds < int.MaxValue,
                                                        "connectionTimeOut must be less than int.MaxValue={int.MaxValue} milliseconds");
            Contract.EnsuresOnThrow <ArgumentException>(_metricsFlushInterval >= MinMetricsFlushInterval,
                                                        $"metricsFlushInterval must be great than {MinMetricsFlushInterval.TotalSeconds} seconds");
            Contract.EnsuresOnThrow <ArgumentException>(_eventsFlushInterval >= MinEventsFlushInterval,
                                                        $"eventsFlushInterval must be great than {MinEventsFlushInterval.TotalSeconds} seconds");
            Contract.EnsuresOnThrow <ArgumentException>(_eventsQueueSize >= MinEventsQueueSize,
                                                        $"eventsQueue must be at least {MinEventsQueueSize}");
            Contract.EnsuresOnThrow <ArgumentException>(_metricsQueueSize >= MinMetricsQueueSize,
                                                        $"metricsQueueSize must be at least {MinMetricsQueueSize}");
        }
Пример #2
0
        private UnlaunchFeature Evaluate(string flagKey, string identity, IEnumerable <UnlaunchAttribute> attributes)
        {
            if (string.IsNullOrEmpty(flagKey))
            {
                throw new ArgumentException("flagKey must not be null or empty");
            }

            if (string.IsNullOrWhiteSpace(identity))
            {
                throw new ArgumentException($"userId must be a string and cannot contain any whitespace characters: {identity}");
            }

            if (_shutdownInitiated.Get())
            {
                Logger.Debug($"Asked to evaluate flag {flagKey} but shutdown already initiated on the client");
                return(UnlaunchConstants.GetControlFeatureByName(flagKey));
            }

            if (!IsReady())
            {
                Logger.Warn("the SDK is not ready. Returning the SDK default 'control' as variation which may not give the right result");
                return(UnlaunchConstants.GetControlFeatureByName(flagKey));
            }

            var user = attributes == null?UnlaunchUser.Create(identity) : UnlaunchUser.CreateWithAttributes(identity, attributes);

            FeatureFlag flag;

            try
            {
                flag = _dataStore.GetFlag(flagKey);
            }
            catch (Exception e)
            {
                return(new UnlaunchFeature(flagKey, UnlaunchConstants.FlagDefaultReturnType,
                                           null, $"there was an error fetching flag: {e.Message}"));
            }

            if (flag == null)
            {
                Logger.Warn($"UnlaunchFeature '{flagKey}' not found in the data store. Returning 'control' variation");
                return(new UnlaunchFeature(flagKey, UnlaunchConstants.FlagDefaultReturnType,
                                           null, "flag was not found in the in-memory cache"));
            }

            var result     = _evaluator.Evaluate(flag, user);
            var impression = new Impression
            {
                type             = UnlaunchConstants.EventTypeForImpressionEvents,
                key              = flag.Key,
                secondaryKey     = result.GetVariation(),
                flagKey          = flag.Key,
                flagStatus       = flag.Enabled.ToString(),
                evaluationReason = result.GetEvaluationReason(),
                userId           = user.GetId(),
                variationKey     = result.GetVariation()
            };

            Track(impression);

            return(result);
        }