Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only expose when there is a URI & provide docs for legacy approach #1048

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ public static IApplicationBuilder UseMetricSinks(this IApplicationBuilder app, I
/// <param name="scrapeEndpointPath">Path where the scrape endpoint will be exposed</param>
public static IApplicationBuilder AddPrometheusScraperMetricSink(this IApplicationBuilder app, string scrapeEndpointPath)
{
app.UsePrometheusServer(prometheusOptions =>
if (string.IsNullOrWhiteSpace(scrapeEndpointPath) == false)
{
prometheusOptions.MapPath = scrapeEndpointPath;
prometheusOptions.UseDefaultCollectors = false;
});
app.UsePrometheusServer(prometheusOptions =>
{
prometheusOptions.MapPath = scrapeEndpointPath;
prometheusOptions.UseDefaultCollectors = false;
});
}

return app;
}
Expand Down
20 changes: 12 additions & 8 deletions src/Promitor.Agents.Scraper/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Promitor.Agents.Scraper.Validation;
using Promitor.Core.Scraping.Configuration.Serialization.v1.Mapping;
using Promitor.Integrations.AzureMonitor.Logging;
using Promitor.Integrations.Sinks.Prometheus.Configuration;
using Serilog;
using Swashbuckle.AspNetCore.SwaggerUI;

Expand All @@ -24,19 +23,19 @@ public class Startup : AgentStartup
{
private const string ApiName = "Promitor - Scraper API";
private const string ComponentName = "Promitor Scraper";
private readonly string _prometheusBaseUriPath;
private readonly string _legacyPrometheusUriPath;

public Startup(IConfiguration configuration)
: base(configuration)
{
var scrapeEndpointConfiguration = configuration.GetSection("prometheus:scrapeEndpoint").Get<ScrapeEndpointConfiguration>();
_prometheusBaseUriPath = scrapeEndpointConfiguration.BaseUriPath;
var runtimeConfiguration = configuration.Get<ScraperRuntimeConfiguration>();
_legacyPrometheusUriPath = runtimeConfiguration?.Prometheus?.ScrapeEndpoint?.BaseUriPath;
}

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string openApiDescription = BuildOpenApiDescription(Configuration);
string openApiDescription = BuildOpenApiDescription(Configuration, _legacyPrometheusUriPath);
services.UseWebApi()
.AddHttpCorrelation()
.AddAutoMapper(typeof(V1MappingProfile).Assembly)
Expand Down Expand Up @@ -65,7 +64,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
.UseHttpCorrelation()
.UseRouting()
.UseMetricSinks(Configuration)
.AddPrometheusScraperMetricSink(_prometheusBaseUriPath) // Deprecated and will be gone in 2.0
.AddPrometheusScraperMetricSink(_legacyPrometheusUriPath) // Deprecated and will be gone in 2.0
.ExposeOpenApiUi() // New Swagger UI
.ExposeOpenApiUi(ApiName, swaggerUiOptions =>
{
Expand Down Expand Up @@ -100,14 +99,19 @@ protected override LoggerConfiguration FilterTelemetry(LoggerConfiguration logge
return standardConfiguration;
}

private string BuildOpenApiDescription(IConfiguration configuration)
private string BuildOpenApiDescription(IConfiguration configuration, string legacyPrometheusUriPath)
{
var metricSinkConfiguration = configuration.GetSection("metricSinks").Get<MetricSinkConfiguration>();

var openApiDescriptionBuilder = new StringBuilder();
openApiDescriptionBuilder.Append("Collection of APIs to manage the Promitor Scraper.\r\n\r\n");
openApiDescriptionBuilder.AppendLine("Configured metric sinks are:\r\n");

if (string.IsNullOrWhiteSpace(legacyPrometheusUriPath) == false)
{
openApiDescriptionBuilder.AppendLine($"<li>Legacy Prometheus scrape endpoint is exposed at <a href=\"./../..{legacyPrometheusUriPath}\" target=\"_blank\">{legacyPrometheusUriPath}</a></li>");
}

if (metricSinkConfiguration.PrometheusScrapingEndpoint != null)
{
var prometheusScrapingBaseUri = metricSinkConfiguration.PrometheusScrapingEndpoint.BaseUriPath;
Expand All @@ -118,7 +122,7 @@ private string BuildOpenApiDescription(IConfiguration configuration)
{
openApiDescriptionBuilder.AppendLine($"<li>StatsD server located on {metricSinkConfiguration.Statsd.Host}:{metricSinkConfiguration.Statsd.Port}</li>");
}

return openApiDescriptionBuilder.ToString();
}
}
Expand Down