public static Func <string, string> Create(IConfiguration bootstrapConfiguration)
        {
            string appConfigurationUri = bootstrapConfiguration[ConfigurationConstants.AppConfigurationUriConfigurationKey];

            if (appConfigurationUri == null)
            {
                return(value => value);
            }

            TelemetryClient telemetry   = ConfigMapper.GetTelemetryClient(bootstrapConfiguration);
            TokenCredential credentials = ServiceConfigurationExtensions.GetAzureTokenCredential(bootstrapConfiguration);
            var             client      = new ConfigurationClient(new Uri(appConfigurationUri), credentials);

            void TrackEvent(string name, string key, Exception?error)
            {
                telemetry.TrackEvent(name, new Dictionary <string, string>
                {
                    ["name"] = key,
                    ["appConfigurationUri"] = appConfigurationUri,
                    ["success"]             = error == null ? "true" : "false",
                });
                if (error != null)
                {
                    telemetry.TrackException(error);
                }
            }

            return(RegexConfigMapper.Create(ConfigReferenceRegex, key =>
            {
                const string featureManagementPrefix = "FeatureManagement:";
                if (key.StartsWith(featureManagementPrefix))
                {
                    key = key.Substring(featureManagementPrefix.Length);
                    var(result, error) = GetFeatureFlagValue(client, key);
                    TrackEvent("AppConfigurationFeatureFlagAccess", key, error);
                    return result;
                }
                else
                {
                    var(result, error) = GetConfigurationSetting(client, key);
                    TrackEvent("AppConfigurationSettingAccess", key, error);
                    return result;
                }
            }));
        }
예제 #2
0
        public static Func <string, string> Create(IConfiguration bootstrapConfiguration)
        {
            TelemetryClient telemetry   = ConfigMapper.GetTelemetryClient(bootstrapConfiguration);
            string          keyVaultUri = bootstrapConfiguration[ConfigurationConstants.KeyVaultUriConfigurationKey];
            var             credentials = ServiceConfigurationExtensions.GetAzureTokenCredential(bootstrapConfiguration);
            var             client      = new SecretClient(new Uri(keyVaultUri), credentials);

            void TrackEvent(string key, Exception?error)
            {
                telemetry.TrackEvent("KeyVaultSecretAccess", new Dictionary <string, string>
                {
                    ["secretKey"]   = key,
                    ["keyVaultUri"] = keyVaultUri,
                    ["success"]     = error == null ? "true" : "false",
                });
                if (error != null)
                {
                    telemetry.TrackException(error);
                }
            }

            return(RegexConfigMapper.Create(VaultReferenceRegex, key =>
            {
                try
                {
                    var value = client.GetSecret(key).Value.Value;
                    TrackEvent(key, null);
                    return value;
                }
                catch (RequestFailedException ex)
                {
                    TrackEvent(key, ex);
                    return "";
                }
            }));
        }