-
-
Notifications
You must be signed in to change notification settings - Fork 75
RE1-T102 Added healthcheck #279
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using System; | ||
| using System.Net.Http; | ||
| using System.Reflection; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Resgrid.Config; | ||
| using Resgrid.Web.Mcp.Infrastructure; | ||
| using Resgrid.Web.Mcp.Models; | ||
|
|
||
| namespace Resgrid.Web.Mcp.Controllers | ||
| { | ||
| /// <summary> | ||
| /// Health Check system to get information and health status of the MCP Server | ||
| /// </summary> | ||
| [AllowAnonymous] | ||
| [Route("health")] | ||
| public sealed class HealthController : Controller | ||
| { | ||
| private readonly McpToolRegistry _toolRegistry; | ||
| private readonly IResponseCache _responseCache; | ||
| private readonly IHttpClientFactory _httpClientFactory; | ||
|
|
||
| public HealthController( | ||
| McpToolRegistry toolRegistry, | ||
| IResponseCache responseCache, | ||
| IHttpClientFactory httpClientFactory) | ||
| { | ||
| _toolRegistry = toolRegistry; | ||
| _responseCache = responseCache; | ||
| _httpClientFactory = httpClientFactory; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the current health status of the MCP Server | ||
| /// </summary> | ||
| /// <returns>HealthResult object with the server health status</returns> | ||
| [HttpGet("current")] | ||
| public async Task<IActionResult> GetCurrent() | ||
| { | ||
| var result = new HealthResult | ||
| { | ||
| ServerVersion = Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "Unknown", | ||
| ServerName = McpConfig.ServerName ?? "Resgrid MCP Server", | ||
| SiteId = "0", | ||
| ToolCount = _toolRegistry.GetToolCount(), | ||
| ServerRunning = true | ||
| }; | ||
|
|
||
| // Check cache connectivity with real probe | ||
| result.CacheOnline = await ProbeCacheConnectivityAsync(); | ||
|
|
||
| // Check API connectivity with real probe | ||
| result.ApiOnline = await ProbeApiConnectivityAsync(); | ||
|
|
||
| return Json(result); | ||
| } | ||
|
|
||
| private async Task<bool> ProbeCacheConnectivityAsync() | ||
| { | ||
| try | ||
| { | ||
| const string sentinelKey = "_healthcheck_sentinel"; | ||
| var sentinelValue = Guid.NewGuid().ToString(); | ||
| var ttl = TimeSpan.FromSeconds(5); | ||
|
|
||
| // Attempt to set and retrieve a sentinel value | ||
| var retrieved = await _responseCache.GetOrCreateAsync( | ||
| sentinelKey, | ||
| () => Task.FromResult(sentinelValue), | ||
| ttl); | ||
|
|
||
| // Verify the value matches and clean up | ||
| var success = retrieved == sentinelValue; | ||
| _responseCache.Remove(sentinelKey); | ||
|
|
||
| return success; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private async Task<bool> ProbeApiConnectivityAsync() | ||
| { | ||
| try | ||
| { | ||
| var apiBaseUrl = SystemBehaviorConfig.ResgridApiBaseUrl; | ||
| if (string.IsNullOrWhiteSpace(apiBaseUrl)) | ||
| return false; | ||
|
|
||
| using var httpClient = _httpClientFactory.CreateClient("ResgridApi"); | ||
| using var request = new HttpRequestMessage(HttpMethod.Head, "/"); | ||
| using var response = await httpClient.SendAsync(request); | ||
|
|
||
| return response.IsSuccessStatusCode; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| namespace Resgrid.Web.Mcp.Models | ||
| { | ||
| /// <summary> | ||
| /// Response for getting the health of the Resgrid MCP Server. | ||
| /// </summary> | ||
| public sealed class HealthResult | ||
| { | ||
| /// <summary> | ||
| /// Site\Location of this MCP Server | ||
| /// </summary> | ||
| public string SiteId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The Version of the MCP Server | ||
| /// </summary> | ||
| public string ServerVersion { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The name of the MCP Server | ||
| /// </summary> | ||
| public string ServerName { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Number of registered tools | ||
| /// </summary> | ||
| public int ToolCount { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Can the MCP Server talk to the Resgrid API | ||
| /// </summary> | ||
| public bool ApiOnline { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Can the MCP Server talk to the cache | ||
| /// </summary> | ||
| public bool CacheOnline { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Is the MCP Server running | ||
| /// </summary> | ||
| public bool ServerRunning { get; set; } | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Route mismatch: TracesSampler excludes wrong paths for this endpoint.
The actual health endpoint route is
/health/current(Line 17[Route("health")]+ Line 38[HttpGet("current")]). However, inProgram.csLines 74-76, theTracesSamplerchecks for/health/getcurrentand/api/health/getcurrent— neither matches the real route. Health check probes will be sampled and sent to Sentry, adding noise.Fix the paths in
Program.cs:🤖 Prompt for AI Agents