Skip to content

Commit

Permalink
Only expose when there is a URI & provide docs for legacy approach (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkerkhove authored May 24, 2020
1 parent f731a69 commit f29318a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
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

0 comments on commit f29318a

Please sign in to comment.