mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Added Application Insights Telemetry
This commit is contained in:
parent
33af7cf391
commit
0449c3a43f
5 changed files with 52 additions and 10 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace ServerManager.WebApplication.Controllers;
|
namespace ServerManager.WebApplication.Controllers.ApiVersion1;
|
||||||
|
|
||||||
[Route("api/plugin")]
|
[Route("api/plugin")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
|
|
@ -9,8 +10,12 @@ namespace ServerManager.WebApplication.Controllers;
|
||||||
[Produces("application/json")]
|
[Produces("application/json")]
|
||||||
public class PluginController : ControllerBase
|
public class PluginController : ControllerBase
|
||||||
{
|
{
|
||||||
public PluginController()
|
private readonly ILogger<PluginController> _logger;
|
||||||
|
|
||||||
|
public PluginController(
|
||||||
|
ILogger<PluginController> logger)
|
||||||
{
|
{
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/plugin/call/00000000-0000-0000-0000-000000000000/192.168.1.1
|
// GET: api/plugin/call/00000000-0000-0000-0000-000000000000/192.168.1.1
|
||||||
|
|
@ -19,10 +24,11 @@ public class PluginController : ControllerBase
|
||||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
public ActionResult<bool> PluginCall_V1([FromRoute] string pluginCode, [FromRoute] string ipString)
|
public ActionResult<bool> PluginCall([FromRoute] string pluginCode, [FromRoute] string ipString)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Plugin call request made {pluginCode}; {ipString}", pluginCode, ipString);
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using ServerManager.WebApplication.Models;
|
using ServerManager.WebApplication.Models;
|
||||||
using ServerManager.WebApplication.Models.ApiVersion1;
|
using ServerManager.WebApplication.Models.ApiVersion1;
|
||||||
using ServerManager.WebApplication.Services;
|
using ServerManager.WebApplication.Services;
|
||||||
|
|
||||||
namespace ServerManager.WebApplication.Controllers;
|
namespace ServerManager.WebApplication.Controllers.ApiVersion1;
|
||||||
|
|
||||||
[Route("api/server")]
|
[Route("api/server")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
|
|
@ -15,10 +17,14 @@ namespace ServerManager.WebApplication.Controllers;
|
||||||
public class ServerController : ControllerBase
|
public class ServerController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IServerQueryService _serverQueryService;
|
private readonly IServerQueryService _serverQueryService;
|
||||||
|
private readonly ILogger<ServerController> _logger;
|
||||||
|
|
||||||
public ServerController(IServerQueryService serverQueryService)
|
public ServerController(
|
||||||
|
IServerQueryService serverQueryService,
|
||||||
|
ILogger<ServerController> logger)
|
||||||
{
|
{
|
||||||
_serverQueryService = serverQueryService;
|
_serverQueryService = serverQueryService;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/server/call/00000000-0000-0000-0000-000000000000/192.168.1.1
|
// GET: api/server/call/00000000-0000-0000-0000-000000000000/192.168.1.1
|
||||||
|
|
@ -31,6 +37,7 @@ public class ServerController : ControllerBase
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Server call request made {pluginCode}; {ipString}", managerCode, ipString);
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
|
@ -52,24 +59,47 @@ public class ServerController : ControllerBase
|
||||||
// check for valid service
|
// check for valid service
|
||||||
if (_serverQueryService == null)
|
if (_serverQueryService == null)
|
||||||
{
|
{
|
||||||
var response = new ErrorResponse { Errors = new List<string> { "Server query service not available." } };
|
var response = new ErrorResponse
|
||||||
|
{
|
||||||
|
Errors = new List<string>
|
||||||
|
{
|
||||||
|
"Server query service not available."
|
||||||
|
}
|
||||||
|
};
|
||||||
return StatusCode(StatusCodes.Status503ServiceUnavailable, response);
|
return StatusCode(StatusCodes.Status503ServiceUnavailable, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var stopWatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
var result = _serverQueryService.CheckServerStatus(managerCode, managerVersion, ipString, port);
|
var result = _serverQueryService.CheckServerStatus(managerCode, managerVersion, ipString, port);
|
||||||
var response = new ServerStatusResponse { Available = result.ToString() };
|
var response = new ServerStatusResponse
|
||||||
|
{
|
||||||
|
Available = result.ToString()
|
||||||
|
};
|
||||||
|
|
||||||
|
stopWatch.Stop();
|
||||||
|
_logger.LogInformation("Server status request made {managerCode}; {managerVersion}; {ipString}; {port}; {timeTaken}", managerCode, managerVersion, ipString, port, stopWatch.Elapsed);
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
catch (ServerManagerApiException ex)
|
catch (ServerManagerApiException ex)
|
||||||
{
|
{
|
||||||
var response = new ErrorResponse { Errors = ex.Messages };
|
var response = new ErrorResponse
|
||||||
|
{
|
||||||
|
Errors = ex.Messages
|
||||||
|
};
|
||||||
return StatusCode(ex.StatusCode, response);
|
return StatusCode(ex.StatusCode, response);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var response = new ErrorResponse { Errors = new List<string> { ex.Message } };
|
var response = new ErrorResponse
|
||||||
|
{
|
||||||
|
Errors = new List<string>
|
||||||
|
{
|
||||||
|
ex.Message
|
||||||
|
}
|
||||||
|
};
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, response);
|
return StatusCode(StatusCodes.Status500InternalServerError, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ public class Startup
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddHealthChecks();
|
services.AddHealthChecks();
|
||||||
|
services.AddApplicationInsightsTelemetry(Configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
|
@ -67,7 +68,6 @@ public class Startup
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var enableSwagger = Configuration.GetValue<bool>("EnableSwagger");
|
var enableSwagger = Configuration.GetValue<bool>("EnableSwagger");
|
||||||
if (enableSwagger)
|
if (enableSwagger)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,11 @@
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft": "Warning",
|
"Microsoft": "Warning",
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
|
},
|
||||||
|
"ApplicationInsights": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue