/// <summary>
        /// Initializes a new instance of the <see cref="TelemetryDataClientBase"/> class.
        /// </summary>
        /// <param name="tracer">The tracer</param>
        /// <param name="httpClientWrapper">The HTTP client wrapper</param>
        /// <param name="credentialsFactory">The credentials factory</param>
        /// <param name="queryUri">The query URI</param>
        /// <param name="queryTimeout">The query timeout</param>
        /// <param name="telemetryDbType">The type of telemetry DB that this data client accesses</param>
        /// <param name="mainTelemetryDbId">The main telemetry DB ID - the ID of the DB on which all queries will run</param>
        /// <param name="telemetryResourceIds">the telemetry resource IDs - the IDs of the resources that store the telemetry that this data client accesses</param>
        protected TelemetryDataClientBase(
            ITracer tracer,
            IHttpClientWrapper httpClientWrapper,
            ICredentialsFactory credentialsFactory,
            Uri queryUri,
            TimeSpan queryTimeout,
            TelemetryDbType telemetryDbType,
            string mainTelemetryDbId,
            IEnumerable <string> telemetryResourceIds)
        {
            this.tracer               = Diagnostics.EnsureArgumentNotNull(() => tracer);
            this.httpClientWrapper    = Diagnostics.EnsureArgumentNotNull(() => httpClientWrapper);
            this.Timeout              = Diagnostics.EnsureArgumentInRange(() => queryTimeout, TimeSpan.FromMinutes(0), TimeSpan.FromHours(2));
            this.queryUri             = queryUri;
            this.TelemetryDbType      = telemetryDbType;
            this.MainTelemetryDbId    = Diagnostics.EnsureStringNotNullOrWhiteSpace(() => mainTelemetryDbId);
            this.TelemetryResourceIds = telemetryResourceIds?.ToList() ?? new List <string>();
            this.retryPolicy          = PolicyExtensions.CreateDefaultPolicy(this.tracer, this.TelemetryDbType.ToString());

            // Extract the host part of the URI as the credentials resource
            UriBuilder builder = new UriBuilder()
            {
                Scheme = this.queryUri.Scheme,
                Host   = this.queryUri.Host
            };

            Diagnostics.EnsureArgumentNotNull(() => credentialsFactory);
            this.credentials = credentialsFactory.Create(builder.Uri.ToString());
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MsiCredentials"/> class.
 /// </summary>
 /// <param name="httpClientWrapper">The HTTP client wrapper</param>
 /// <param name="resource">The resource name - the URL for which these credentials will be used</param>
 /// <param name="tracer">The tracer</param>
 public MsiCredentials(IHttpClientWrapper httpClientWrapper, string resource, ITracer tracer)
 {
     this.resource          = resource;
     this.httpClientWrapper = httpClientWrapper;
     this.tracer            = tracer;
     this.retryPolicy       = PolicyExtensions.CreateDefaultPolicy(this.tracer, DependencyName);
 }
        public void HandleHttpRequests_ReturnsPolicyBuilder()
        {
            var builder = PolicyExtensions.HandleHttpRequests();

            Assert.IsNotNull(builder);
            Assert.IsInstanceOfType(builder, typeof(PolicyBuilder <HttpResponseMessage>));
        }
Example #4
0
        /// <summary>
        /// Report download progress through the database if necessary.
        /// </summary>
        /// <param name="innerState">
        /// The inner State.
        /// </param>
        private void ReportProgress(InnerState innerState)
        {
            long now = PolicyExtensions.GetCurrentMilliseconds();

            if (innerState.BytesSoFar - innerState.BytesNotified > DownloaderService.MinimumProgressStep &&
                now - innerState.TimeLastNotification > DownloaderService.MinimumProgressTime)
            {
                // we store progress updates to the database here
                this.downloadInfo.CurrentBytes = innerState.BytesSoFar;
                DownloadsDatabase.UpdateDownloadCurrentBytes(this.downloadInfo);

                innerState.BytesNotified        = innerState.BytesSoFar;
                innerState.TimeLastNotification = now;

                long totalBytesSoFar = innerState.BytesThisSession + this.downloaderService.BytesSoFar;

                Debug.WriteLine(
                    "DownloadThread : downloaded {0} out of {1}",
                    this.downloadInfo.CurrentBytes,
                    this.downloadInfo.TotalBytes);
                Debug.WriteLine(
                    "DownloadThread :      total {0} out of {1}", totalBytesSoFar, this.downloaderService.TotalLength);

                this.downloaderService.NotifyUpdateBytes(totalBytesSoFar);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MetricClient"/> class
        /// </summary>
        /// <param name="tracer">The tracer</param>
        /// <param name="subscriptionId">The subscription Id</param>
        /// <param name="monitorManagementClient">Monitor management client to use to fetch metric data</param>
        public MetricClient(IExtendedTracer tracer, string subscriptionId, IMonitorManagementClient monitorManagementClient)
        {
            this.tracer = Diagnostics.EnsureArgumentNotNull(() => tracer);

            this.monitorManagementClient = monitorManagementClient;
            this.monitorManagementClient.SubscriptionId = subscriptionId;
            this.tracer      = tracer;
            this.retryPolicy = PolicyExtensions.CreateDefaultPolicy(this.tracer, DependencyName);
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ActivityLogClient"/> class.
        /// </summary>
        /// <param name="credentialsFactory">The credentials factory</param>
        /// <param name="httpClientWrapper">The HTTP client wrapper</param>
        /// <param name="tracer">The tracer</param>
        public ActivityLogClient(ICredentialsFactory credentialsFactory, IHttpClientWrapper httpClientWrapper, IExtendedTracer tracer)
        {
            this.httpClientWrapper = Diagnostics.EnsureArgumentNotNull(() => httpClientWrapper);
            this.tracer            = Diagnostics.EnsureArgumentNotNull(() => tracer);
            this.baseUri           = new Uri(ConfigurationManager.AppSettings["ResourceManagerBaseUri"] ?? "https://management.azure.com/");
            Diagnostics.EnsureArgumentNotNull(() => credentialsFactory);
            this.credentials = credentialsFactory.Create(ConfigurationManager.AppSettings["ResourceManagerCredentialsResource"] ?? "https://management.azure.com/");

            this.httpRetryPolicy = PolicyExtensions.CreateTransientHttpErrorPolicy(this.tracer, DependencyName);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AnalysisExecuter"/> class.
        /// </summary>
        /// <param name="httpClientWrapper">The HTTP client wrapper</param>
        /// <param name="tracer">Log wrapper</param>
        public AnalysisExecuter(IHttpClientWrapper httpClientWrapper, ITracer tracer)
        {
            this.tracer            = Diagnostics.EnsureArgumentNotNull(() => tracer);
            this.httpClientWrapper = Diagnostics.EnsureArgumentNotNull(() => httpClientWrapper);
            this.retryPolicy       = PolicyExtensions.CreateDefaultPolicy(this.tracer, DependencyName);

            var functionAppBaseUrl = ConfigurationReader.ReadConfig("FunctionBaseUrl", true);

            this.analysisUrl = $"{functionAppBaseUrl}/api/Analyze";
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MdmClient"/> class
        /// </summary>
        /// <param name="tracer">The tracer</param>
        /// <param name="credentialsFactory">The credentials factory</param>
        /// <param name="resourceIdentifier">The resource for which we want to fetch data from MDM</param>
        /// /// <param name="monitorManagementClient">Monitor management client to use to fetch data from MDM</param>
        public MdmClient(ITracer tracer, ICredentialsFactory credentialsFactory, ResourceIdentifier resourceIdentifier, IMonitorManagementClient monitorManagementClient)
        {
            this.tracer = Diagnostics.EnsureArgumentNotNull(() => tracer);
            Diagnostics.EnsureArgumentNotNull(() => credentialsFactory);
            this.resourceIdentifier = resourceIdentifier;

            this.credentials             = credentialsFactory.Create("https://management.azure.com/");
            this.monitorManagementClient = monitorManagementClient;
            this.monitorManagementClient.SubscriptionId = resourceIdentifier.SubscriptionId;
            this.tracer      = tracer;
            this.retryPolicy = PolicyExtensions.CreateDefaultPolicy(this.tracer, DependencyName);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LogAnalyticsClient"/> class.
        /// </summary>
        /// <param name="tracer">The tracer</param>
        /// <param name="httpClientWrapper">The HTTP client wrapper</param>
        /// <param name="credentialsFactory">The credentials factory</param>
        /// <param name="queryUri">The URI to use for querying telemetry - this should already include the resource to query</param>
        /// <param name="queryTimeout">The query timeout</param>
        public LogAnalyticsClient(
            ITracer tracer,
            IHttpClientWrapper httpClientWrapper,
            ICredentialsFactory credentialsFactory,
            Uri queryUri,
            TimeSpan queryTimeout)
        {
            this.tracer             = Diagnostics.EnsureArgumentNotNull(() => tracer);
            this.httpClientWrapper  = Diagnostics.EnsureArgumentNotNull(() => httpClientWrapper);
            this.credentialsFactory = Diagnostics.EnsureArgumentNotNull(() => credentialsFactory);
            this.QueryUri           = Diagnostics.EnsureArgumentNotNull(() => queryUri);
            this.Timeout            = Diagnostics.EnsureArgumentInRange(() => queryTimeout, TimeSpan.FromMinutes(0), TimeSpan.FromHours(2));

            this.retryPolicy = PolicyExtensions.CreateTransientHttpErrorPolicy(this.tracer, "LogAnalytics");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MetricClient"/> class
        /// </summary>
        /// <param name="tracer">The tracer</param>
        /// <param name="monitorManagementClient">Monitor management client to use to fetch metric data</param>
        public MetricClient(ITracer tracer, IMonitorManagementClient monitorManagementClient)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException(nameof(tracer));
            }

            if (monitorManagementClient == null)
            {
                throw new ArgumentNullException(nameof(monitorManagementClient));
            }

            this.tracer = tracer;
            this.monitorManagementClient = monitorManagementClient;
            this.retryPolicy             = PolicyExtensions.CreateDefaultPolicy(this.tracer, DependencyName);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ExtendedAzureResourceManagerClient"/> class
 /// </summary>
 /// <param name="httpClientWrapper">The HTTP client wrapper</param>
 /// <param name="credentialsFactory">The credentials factory</param>
 /// <param name="tracer">The tracer</param>
 public ExtendedAzureResourceManagerClient(IHttpClientWrapper httpClientWrapper, ICredentialsFactory credentialsFactory, ITracer tracer)
 {
     this.httpClientWrapper = Diagnostics.EnsureArgumentNotNull(() => httpClientWrapper);
     Diagnostics.EnsureArgumentNotNull(() => credentialsFactory);
     this.baseUri     = new Uri(ConfigurationManager.AppSettings["ResourceManagerBaseUri"] ?? "https://management.azure.com/");
     this.credentials = credentialsFactory.CreateAzureCredentials(ConfigurationManager.AppSettings["ResourceManagerCredentialsResource"] ?? "https://management.azure.com/");
     this.tracer      = Diagnostics.EnsureArgumentNotNull(() => tracer);
     this.tracer      = tracer;
     this.retryPolicy = Policy
                        .Handle <CloudException>(ex => ex.Request != null && (ex.Response.StatusCode >= HttpStatusCode.InternalServerError || ex.Response.StatusCode == HttpStatusCode.RequestTimeout))
                        .WaitAndRetryAsync(
         3,
         (i) => TimeSpan.FromSeconds(Math.Pow(2, i)),
         (exception, span, context) => tracer.TraceError($"Failed accessing DependencyName on {exception.Message}, retry {Math.Log(span.Seconds, 2)} out of 3"));
     this.httpRetryPolicy = PolicyExtensions.CreateTransientHttpErrorPolicy(this.tracer, DependencyName);
 }
        async Task SubscribeTo(string topicArn, string topicName, string queueUrl)
        {
            var sqsQueueArn = await queueCache.GetQueueArn(queueUrl).ConfigureAwait(false);

            var permissionStatement = PolicyExtensions.CreateSQSPermissionStatement(topicArn, sqsQueueArn);
            var addPolicyStatement  = new PolicyStatement(topicName, topicArn, permissionStatement, sqsQueueArn);

            if (endpointStartingMode)
            {
                preparedPolicyStatements.Add(addPolicyStatement);
                return;
            }

            var addPolicyStatements = new List <PolicyStatement> {
                addPolicyStatement
            };

            await SettlePolicy(queueUrl, addPolicyStatements).ConfigureAwait(false);
        }
Example #13
0
        /// <summary>
        /// The update download database.
        /// </summary>
        /// <param name="status">
        /// The status.
        /// </param>
        /// <param name="countRetry">
        /// The count retry.
        /// </param>
        /// <param name="retryAfter">
        /// The retry after.
        /// </param>
        /// <param name="redirectCount">
        /// The redirect count.
        /// </param>
        /// <param name="gotData">
        /// The got data.
        /// </param>
        private void UpdateDownloadDatabase(
            ExpansionDownloadStatus status, bool countRetry, int retryAfter, int redirectCount, bool gotData)
        {
            this.downloadInfo.Status        = status;
            this.downloadInfo.RetryAfter    = retryAfter;
            this.downloadInfo.RedirectCount = redirectCount;
            this.downloadInfo.LastModified  = PolicyExtensions.GetCurrentMilliseconds();
            if (!countRetry)
            {
                this.downloadInfo.FailedCount = 0;
            }
            else if (gotData)
            {
                this.downloadInfo.FailedCount = 1;
            }
            else
            {
                this.downloadInfo.FailedCount++;
            }

            DownloadsDatabase.UpdateDownload(this.downloadInfo);
        }
Example #14
0
        protected TelemetryDataClientBase(
            ITracer tracer,
            IHttpClientWrapper httpClientWrapper,
            ICredentialsFactory credentialsFactory,
            IExtendedAzureResourceManagerClient azureResourceManagerClient,
            string queryUriFormat,
            TimeSpan queryTimeout,
            string telemetryDbType,
            IEnumerable <string> telemetryResourceIds)
        {
            this.tracer                     = Diagnostics.EnsureArgumentNotNull(() => tracer);
            this.httpClientWrapper          = Diagnostics.EnsureArgumentNotNull(() => httpClientWrapper);
            this.credentialsFactory         = Diagnostics.EnsureArgumentNotNull(() => credentialsFactory);
            this.AzureResourceManagerClient = Diagnostics.EnsureArgumentNotNull(() => azureResourceManagerClient);
            this.queryUriFormat             = Diagnostics.EnsureStringNotNullOrWhiteSpace(() => queryUriFormat);
            this.Timeout                    = Diagnostics.EnsureArgumentInRange(() => queryTimeout, TimeSpan.FromMinutes(0), TimeSpan.FromHours(2));
            this.telemetryDbType            = telemetryDbType;

            int maximumNumberOfTelemetryResources = int.Parse(ConfigurationManager.AppSettings["MaximumNumberOfTelemetryResources"] ?? "300", CultureInfo.InvariantCulture);

            this.TelemetryResourceIds = telemetryResourceIds?.Take(maximumNumberOfTelemetryResources).ToList() ?? new List <string>();

            this.retryPolicy = PolicyExtensions.CreateTransientHttpErrorPolicy(this.tracer, this.telemetryDbType);
        }
Example #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AuthorizationManagementClient"/> class.
 /// </summary>
 /// <param name="tracer">Log wrapper</param>
 /// <param name="httpClientWrapper">The HTTP client wrapper</param>
 public AuthorizationManagementClient(ITracer tracer, IHttpClientWrapper httpClientWrapper)
 {
     this.tracer            = Diagnostics.EnsureArgumentNotNull(() => tracer);
     this.httpClientWrapper = Diagnostics.EnsureArgumentNotNull(() => httpClientWrapper);
     this.retryPolicy       = PolicyExtensions.CreateDefaultPolicy(this.tracer, DependencyName);
 }
Example #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AzureResourceManagerClient"/> class
 /// </summary>
 /// <param name="credentialsFactory">The credentials factory</param>
 /// <param name="tracer">The tracer</param>
 public AzureResourceManagerClient(ICredentialsFactory credentialsFactory, ITracer tracer)
 {
     this.credentials = credentialsFactory.Create("https://management.azure.com/");
     this.tracer      = tracer;
     this.retryPolicy = PolicyExtensions.CreateDefaultPolicy(this.tracer, DependencyName);
 }