Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            string agentVersion = Version.Get();

            var    promitorUserAgent  = UserAgent.Generate("Scraper", agentVersion);
            string openApiDescription = BuildOpenApiDescription(Configuration);

            services.UseWebApi()
            .AddMemoryCache()
            .AddResourceDiscoveryClient(promitorUserAgent)
            .AddAtlassianStatuspageClient(promitorUserAgent, Configuration)
            .AddUsability()
            .AddHttpCorrelation(options => options.UpstreamService.ExtractFromRequest = true)
            .AddAutoMapper(typeof(V1MappingProfile).Assembly)
            .DefineDependencies()
            .AddValidationRules()
            .ConfigureYamlConfiguration(Configuration)
            .UseOpenApiSpecifications("Promitor - Scraper API v1", openApiDescription, 1);

            services.AddHealthChecks()
            .AddResourceDiscoveryHealthCheck(Configuration);

            services.UseMetricSinks(Configuration, _logger)
            .AddScrapingMutex(Configuration)
            .ScheduleMetricScraping();
        }
Exemplo n.º 2
0
        private async Task <ResourceGraphClient> CreateClientAsync()
        {
            var azureEnvironment   = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureEnvironment();
            var azureAuthorityHost = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureAuthorityHost();

            var credentials = await AzureAuthenticationFactory.GetTokenCredentialsAsync(azureEnvironment.ManagementEndpoint, TenantId, _azureAuthenticationInfo, azureAuthorityHost);

            var resourceManagerBaseUri = new Uri(azureEnvironment.ResourceManagerEndpoint);
            var appId = DetermineApplicationId(_azureAuthenticationInfo);

            var metricLabels = new Dictionary <string, string>
            {
                { "tenant_id", TenantId },
                { "cloud", azureEnvironment.GetDisplayName() },
                { "app_id", appId },
                { "auth_mode", _azureAuthenticationInfo.Mode.ToString() },
            };
            var resourceGraphClient = new ResourceGraphClient(resourceManagerBaseUri, credentials, new AzureResourceGraphThrottlingRequestHandler(_prometheusMetricsCollector, metricLabels, _logger));

            var version           = Promitor.Core.Version.Get();
            var promitorUserAgent = UserAgent.Generate("Resource-Discovery", version);

            resourceGraphClient.UserAgent.Clear();
            resourceGraphClient.UserAgent.TryParseAdd(promitorUserAgent);

            return(resourceGraphClient);
        }
Exemplo n.º 3
0
        static async Task RunSample3Async(AppConfig appConfig)
        {
            int    accountId    = 999999999;
            string clientId     = "YOUR_CLIENT_ID";
            string clientSecret = "YOUR_CLIENT_SECRET";
            string redirectUrl  = "YOUR_REDIRECT_URL";
            string refreshToken = "YOUR_REFRESH_TOKEN";

            accountId    = appConfig.AccountId;
            clientId     = appConfig.ClientId;
            clientSecret = appConfig.ClientSecret;
            redirectUrl  = appConfig.RedirectUrl;
            refreshToken = appConfig.RefreshToken;

            var accessTokenSource = new AccessTokenSource(
                clientId: clientId,
                clientSecret: clientSecret,
                redirectUrl: redirectUrl,
                refreshToken: refreshToken
                );
            var basecampClientOptions = new BasecampClientOptions(accountId, accessTokenSource)
            {
                UserAgent            = UserAgent.Generate("YOUR_APPLICATION", "YOUR_CONTACT_ADDRESS"),
                RateLimiter          = new ConstantRateLimiter(delayMs: 500),
                ResponseMessageCache = new SingleResponseMessageCache(),
            };
            var basecampClient = new BasecampClient(basecampClientOptions);
        }
Exemplo n.º 4
0
        public void Generate_NoVersionProvided_ThrowsException(string version)
        {
            // Arrange
            var agentName = "Scraper";

            // Act & Assert
            Assert.Throws <ArgumentException>(() => UserAgent.Generate(agentName, version));
        }
Exemplo n.º 5
0
        public void Generate_NoNameProvided_ThrowsException(string agentName)
        {
            // Arrange
            var version = "1.2.3";

            // Act & Assert
            Assert.Throws <ArgumentException>(() => UserAgent.Generate(agentName, version));
        }
        protected override HttpRequestMessage BeforeSendingRequest(HttpRequestMessage request)
        {
            string agentVersion      = Version.Get();
            var    promitorUserAgent = UserAgent.Generate("Scraper", agentVersion);

            request.Headers.UserAgent.Clear();
            request.Headers.UserAgent.TryParseAdd(promitorUserAgent);

            return(request);
        }
Exemplo n.º 7
0
        public void Generate_ValidInput_ThrowsException()
        {
            // Arrange
            var agentName         = "Scraper";
            var version           = "1.2.3";
            var expectedUserAgent = $"App/Promitor Agent/{agentName} Version/{version}";

            // Act
            var generatedUserAgent = UserAgent.Generate(agentName, version);

            // Assert
            Assert.Equal(expectedUserAgent, generatedUserAgent);
        }
Exemplo n.º 8
0
        private async Task <ResourceGraphClient> CreateClientAsync()
        {
            var azureEnvironment = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureEnvironment();
            var credentials      = await Authentication.GetServiceClientCredentialsAsync(azureEnvironment.ManagementEndpoint, QueryApplicationId, _queryApplicationSecret, TenantId);

            var resourceGraphClient = new ResourceGraphClient(credentials);

            var version           = Promitor.Core.Version.Get();
            var promitorUserAgent = UserAgent.Generate("Resource-Discovery", version);

            resourceGraphClient.UserAgent.TryParseAdd(promitorUserAgent);

            return(resourceGraphClient);
        }
Exemplo n.º 9
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            string agentVersion      = Version.Get();
            var    promitorUserAgent = UserAgent.Generate("Scraper", agentVersion);

            request.Headers.UserAgent.TryParseAdd(promitorUserAgent);

            var response = await base.SendAsync(request, cancellationToken);

            await MeasureArmRateLimitingAsync(response);

            if ((int)response.StatusCode == 429)
            {
                LogArmThrottling();
            }

            return(response);
        }