Removing prometheus-net dependency (#1526)
* Update README.md * Update KubernetesClient.csproj * Delete examples/prometheus directory * Delete src/KubernetesClient/PrometheusHandler.cs
This commit is contained in:
11
README.md
11
README.md
@@ -26,14 +26,11 @@ You should also be able to authenticate with the in-cluster service
|
|||||||
account using the `InClusterConfig` function shown below.
|
account using the `InClusterConfig` function shown below.
|
||||||
|
|
||||||
## Monitoring
|
## Monitoring
|
||||||
There is optional built-in metric generation for prometheus client metrics.
|
Metrics are built in to HttpClient using System.Diagnostics.DiagnosticsSource.
|
||||||
The exported metrics are:
|
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/built-in-metrics-system-net
|
||||||
|
|
||||||
* `k8s_dotnet_request_total` - Counter of request, broken down by HTTP Method
|
There are many ways these metrics can be consumed/exposed but that decision is up to the application, not KubernetesClient itself.
|
||||||
* `k8s_dotnet_response_code_total` - Counter of responses, broken down by HTTP Method and response code
|
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-collection
|
||||||
* `k8s_request_latency_seconds` - Latency histograms broken down by method, api group, api version and resource kind
|
|
||||||
|
|
||||||
There is an example integrating these monitors in the examples/prometheus directory.
|
|
||||||
|
|
||||||
## Sample Code
|
## Sample Code
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
using k8s;
|
|
||||||
using Prometheus;
|
|
||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace prom
|
|
||||||
{
|
|
||||||
internal class Prometheus
|
|
||||||
{
|
|
||||||
private static void Main(string[] args)
|
|
||||||
{
|
|
||||||
var config = KubernetesClientConfiguration.BuildDefaultConfig();
|
|
||||||
var handler = new PrometheusHandler();
|
|
||||||
IKubernetes client = new Kubernetes(config, handler);
|
|
||||||
|
|
||||||
var server = new MetricServer(hostname: "localhost", port: 1234);
|
|
||||||
server.Start();
|
|
||||||
|
|
||||||
Console.WriteLine("Making requests!");
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
client.CoreV1.ListNamespacedPod("default");
|
|
||||||
client.CoreV1.ListNode();
|
|
||||||
client.AppsV1.ListNamespacedDeployment("default");
|
|
||||||
Thread.Sleep(1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<OutputType>Exe</OutputType>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="prometheus-net" Version="8.2.1" />
|
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
|
||||||
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
|
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
|
||||||
<PackageReference Include="Fractions" Version="7.3.0" />
|
<PackageReference Include="Fractions" Version="7.3.0" />
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
using Prometheus;
|
|
||||||
using System.Net.Http;
|
|
||||||
|
|
||||||
namespace k8s
|
|
||||||
{
|
|
||||||
public class PrometheusHandler : DelegatingHandler
|
|
||||||
{
|
|
||||||
private const string PREFIX = "k8s_dotnet";
|
|
||||||
private readonly Counter requests = Metrics.CreateCounter(
|
|
||||||
$"{PREFIX}_request_total", "Number of requests sent by this client",
|
|
||||||
new CounterConfiguration
|
|
||||||
{
|
|
||||||
LabelNames = new[] { "method" },
|
|
||||||
});
|
|
||||||
|
|
||||||
private readonly Histogram requestLatency = Metrics.CreateHistogram(
|
|
||||||
$"{PREFIX}_request_latency_seconds", "Latency of requests sent by this client",
|
|
||||||
new HistogramConfiguration
|
|
||||||
{
|
|
||||||
LabelNames = new[] { "verb", "group", "version", "kind" },
|
|
||||||
});
|
|
||||||
|
|
||||||
private readonly Counter responseCodes = Metrics.CreateCounter(
|
|
||||||
$"{PREFIX}_response_code_total", "Number of response codes received by the client",
|
|
||||||
new CounterConfiguration
|
|
||||||
{
|
|
||||||
LabelNames = new[] { "method", "code" },
|
|
||||||
});
|
|
||||||
|
|
||||||
private readonly Gauge activeRequests = Metrics.CreateGauge(
|
|
||||||
$"{PREFIX}_active_requests", "Number of requests currently in progress",
|
|
||||||
new GaugeConfiguration
|
|
||||||
{
|
|
||||||
LabelNames = new[] { "method" },
|
|
||||||
});
|
|
||||||
|
|
||||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
if (request == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(request));
|
|
||||||
}
|
|
||||||
|
|
||||||
var digest = KubernetesRequestDigest.Parse(request);
|
|
||||||
requests.WithLabels(digest.Verb).Inc();
|
|
||||||
using (activeRequests.WithLabels(digest.Verb).TrackInProgress())
|
|
||||||
using (requestLatency.WithLabels(digest.Verb, digest.ApiGroup, digest.ApiVersion, digest.Kind).NewTimer())
|
|
||||||
{
|
|
||||||
var resp = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
|
|
||||||
responseCodes.WithLabels(request.Method.ToString(), ((int)resp.StatusCode).ToString()).Inc();
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user