Skip to content

Commit

Permalink
feat(openapi): add query system metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Jul 30, 2022
1 parent 4b11429 commit 30d9b1c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
9 changes: 9 additions & 0 deletions samples/App3/Controllers/OpenApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,14 @@ public async Task<string> NamespaceDelete(string i)

return flag.ToString();
}

// GET o/metrics
[HttpGet("metrics")]
public async Task<string> GetMetrics()
{
var flag = await _api.GetMetricsAsync(false).ConfigureAwait(false);

return flag.ToJsonString();
}
}
}
20 changes: 20 additions & 0 deletions src/Nacos/OpenApi/DefaultNacosOpenApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class DefaultNacosOpenApi : INacosOpenApi
{
private const string _namespacePath = "nacos/v1/console/namespaces";
private const string _metricsPath = "nacos/v1/ns/operator/metrics";

private readonly IHttpClientFactory _httpClientFactory;
private readonly Nacos.V2.NacosSdkOptions _options;
Expand Down Expand Up @@ -66,6 +67,25 @@ public async Task<bool> DeleteNamespaceAsync(string namespaceId)
}
}

public async Task<NacosMetrics> GetMetricsAsync(bool onlyStatus)
{
var client = _httpClientFactory.CreateClient(Constants.HttpClientName);

var req = new HttpRequestMessage(HttpMethod.Get, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_metricsPath}?onlyStatus={onlyStatus.ToString().ToLower()}");

var resp = await client.SendAsync(req).ConfigureAwait(false);

if (resp.IsSuccessStatusCode)
{
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
return res.ToObj<NacosMetrics>();
}
else
{
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "GetMetricsAsync exception");
}
}

public async Task<List<NacosNamespace>> GetNamespacesAsync()
{
var client = _httpClientFactory.CreateClient(Constants.HttpClientName);
Expand Down
7 changes: 7 additions & 0 deletions src/Nacos/OpenApi/INacosOpenApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,12 @@ public interface INacosOpenApi
/// <param name="namespaceId">ID of namespace</param>
/// <returns>Deleted or not</returns>
Task<bool> DeleteNamespaceAsync(string namespaceId);

/// <summary>
/// Query system metrics
/// </summary>
/// <param name="onlyStatus">only status info or not</param>
/// <returns>system metrics</returns>
Task<NacosMetrics> GetMetricsAsync(bool onlyStatus);
}
}
50 changes: 50 additions & 0 deletions src/Nacos/OpenApi/Models/NacosMetrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace Nacos.OpenApi
{
public class NacosMetrics
{
[Newtonsoft.Json.JsonProperty("status")]
public string Status { get; set; }

[Newtonsoft.Json.JsonProperty("serviceCount")]
public int ServiceCount { get; set; }

[Newtonsoft.Json.JsonProperty("instanceCount")]
public int InstanceCount { get; set; }

[Newtonsoft.Json.JsonProperty("subscribeCount")]
public int SubscribeCount { get; set; }

[Newtonsoft.Json.JsonProperty("raftNotifyTaskCount")]
public int RaftNotifyTaskCount { get; set; }

[Newtonsoft.Json.JsonProperty("responsibleServiceCount")]
public int ResponsibleServiceCount { get; set; }

[Newtonsoft.Json.JsonProperty("responsibleInstanceCount")]
public int ResponsibleInstanceCount { get; set; }

[Newtonsoft.Json.JsonProperty("clientCount")]
public int ClientCount { get; set; }

[Newtonsoft.Json.JsonProperty("connectionBasedClientCount")]
public int ConnectionBasedClientCount { get; set; }

[Newtonsoft.Json.JsonProperty("ephemeralIpPortClientCount")]
public int EphemeralIpPortClientCount { get; set; }

[Newtonsoft.Json.JsonProperty("persistentIpPortClientCount")]
public int PersistentIpPortClientCount { get; set; }

[Newtonsoft.Json.JsonProperty("responsibleClientCount")]
public int ResponsibleClientCount { get; set; }

[Newtonsoft.Json.JsonProperty("cpu")]
public float Cpu { get; set; }

[Newtonsoft.Json.JsonProperty("load")]
public float Load { get; set; }

[Newtonsoft.Json.JsonProperty("mem")]
public float Mem { get; set; }
}
}

0 comments on commit 30d9b1c

Please sign in to comment.