Skip to content

Instantly share code, notes, and snippets.

@wullemsb
Created June 27, 2025 07:55
Show Gist options
  • Save wullemsb/67d3db12af36f203d91ebbbad5beefaf to your computer and use it in GitHub Desktop.
Save wullemsb/67d3db12af36f203d91ebbbad5beefaf to your computer and use it in GitHub Desktop.
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly IFeatureManager _featureManager;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IFeatureManager featureManager)
{
_logger = logger;
_featureManager = featureManager;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IEnumerable<WeatherForecast>> GetWeatherForecast()
{
if (await _featureManager.IsEnabledAsync("NewFunctionality"))
{
_logger.LogInformation("Getting weather forecast for V2 API version");
this.HttpContext.Response.Headers.Append("X-API-Version", "2.0");
//Adding this extra tag allows us to track which functionality is used
Activity.Current?.SetTag("weatherforecast.api_version", "2.0");
}
else {
_logger.LogInformation("Getting weather forecast for V1 API version");
this.HttpContext.Response.Headers.Append("X-API-Version", "1.0");
//Adding this extra tag allows us to track which functionality is used
Activity.Current?.SetTag("weatherforecast.api_version", "1.0");
}
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment