Esempio n. 1
0
        public void Setup()
        {
            this.httpClient = new FixedHttpClient();

            GitHubHttpClient githubHttpClient = new GitHubHttpClient(this.httpClient, new NoopRateLimiter(), new NoopCache <ConditionalRequestTableEntity>(), new NoopTelemetryClient());

            this.recordWriter = new InMemoryRecordWriter();

            IAuthentication      authentication = new BasicAuthentication("Identity", "PersonalAccessToken");
            List <IRecordWriter> recordWriters  = new List <IRecordWriter>();

            recordWriters.Add(this.recordWriter);
            this.collector = new GitHubCollector(githubHttpClient, authentication, new NoopTelemetryClient(), recordWriters);
        }
Esempio n. 2
0
        private async Task ExecuteTrafficCollector(ExecutionContext executionContext, ILogger logger)
        {
            DateTime functionStartDate = DateTime.UtcNow;
            string   sessionId         = Guid.NewGuid().ToString();
            string   identifier        = "TrafficTimer";

            CloudQueue trafficCloudQueue = await AzureHelpers.GetStorageQueueAsync("traffic").ConfigureAwait(false);

            IQueue trafficQueue = new CloudQueueWrapper(trafficCloudQueue);

            FunctionContext context = new FunctionContext()
            {
                CollectorType     = CollectorType.TrafficTimer.ToString(),
                FunctionStartDate = functionStartDate,
                SessionId         = sessionId,
                InvocationId      = executionContext.InvocationId.ToString(),
            };

            StatsTracker     statsTracker    = null;
            bool             success         = false;
            ITelemetryClient telemetryClient = new GitHubApplicationInsightsTelemetryClient(this.telemetryClient, context, logger);

            try
            {
                telemetryClient.TrackEvent("SessionStart", GetCollectorCommonSessionStartEventProperties(context, identifier));

                ICache <RateLimitTableEntity> rateLimiterCache = new AzureTableCache <RateLimitTableEntity>(telemetryClient, "ratelimiter");
                await rateLimiterCache.InitializeAsync().ConfigureAwait(false);

                ICache <ConditionalRequestTableEntity> requestsCache = new AzureTableCache <ConditionalRequestTableEntity>(telemetryClient, "requests");
                await requestsCache.InitializeAsync().ConfigureAwait(false);

                string organizations = await AzureHelpers.GetBlobContentAsync("github-settings", "organizations.json").ConfigureAwait(false);

                JArray organizationsArray = JArray.Parse(organizations);
                foreach (JToken organizationToken in organizationsArray)
                {
                    JObject organization      = (JObject)organizationToken;
                    string  organizationLogin = organization.SelectToken("$.OrganizationLogin").Value <string>();
                    long    organizationId    = organization.SelectToken("$.OrganizationId").Value <long>();

                    IRateLimiter     rateLimiter = new GitHubRateLimiter(this.configManager.UsesGitHubAuth(context.CollectorType) ? organizationLogin : "******", rateLimiterCache, this.httpClient, telemetryClient, maxUsageBeforeDelayStarts: 80.0, this.apiDomain);
                    GitHubHttpClient httpClient  = new GitHubHttpClient(this.httpClient, rateLimiter, requestsCache, telemetryClient);

                    statsTracker = new StatsTracker(telemetryClient, httpClient, StatsTrackerRefreshFrequency);

                    IAuthentication authentication = this.configManager.GetAuthentication(CollectorType.TrafficTimer, httpClient, organizationLogin, this.apiDomain);
                    CollectorBase <GitHubCollectionNode> collector = new GitHubCollector(httpClient, authentication, telemetryClient, new List <IRecordWriter>());

                    GitHubCollectionNode repositoriesNode = new GitHubCollectionNode()
                    {
                        RecordType         = DataContract.RepositoryInstanceRecordType,
                        ApiName            = DataContract.RepositoriesApiName,
                        GetInitialUrl      = additionalMetadata => OnboardingProcessor.InitialRepositoriesUrl(organizationLogin, this.apiDomain),
                        ProcessRecordAsync = async record =>
                        {
                            string repositoryName = record.SelectToken("$.name").Value <string>();
                            long   repositoryId   = record.SelectToken("$.id").Value <long>();

                            Repository repository = new Repository(organizationId, repositoryId, organizationLogin, repositoryName);
                            await trafficQueue.PutObjectAsJsonStringAsync(repository, TimeSpan.MaxValue).ConfigureAwait(false);

                            return(new List <RecordWithContext>());
                        },
                    };

                    await collector.ProcessAsync(repositoriesNode).ConfigureAwait(false);
                }

                success = true;
            }
            catch (Exception exception) when(!(exception is FatalException))
            {
                telemetryClient.TrackException(exception, "TrafficTimer failed.");
                throw exception;
            }
            finally
            {
                SendSessionEndEvent(telemetryClient, context.FunctionStartDate, outputPaths: string.Empty, GetCollectorCommonSessionStartEventProperties(context, identifier), success);
                statsTracker?.Stop();
            }
        }