Skip to content

Commit

Permalink
can now retrieve metrics (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Dec 27, 2022
1 parent 384f6a1 commit 3374ad9
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ var listSynonyms = await typesenseClient.ListSynonyms("Addresses");
var deleteSynonym = await typesenseClient.DeleteSynonym("Addresses", "Address_Synonym");
```

## Metrics

Get current RAM, CPU, Disk & Network usage metrics.

```c#
var metrics = await typesenseClient.RetrieveMetrics();
```

### Typesense API Errors

Typesense API exceptions in the [Typesense-api-errors](https://typesense.org/docs/0.23.0/api/api-errors.html) spec.
Expand Down
9 changes: 9 additions & 0 deletions examples/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ async static Task Main(string[] args)

// Example delete collection
await ExampleDeleteCollection(typesenseClient);

// Example retrieve metrics
await ExampleRetrieveMetrics(typesenseClient);
}

private static async Task ExampleCreateCollection(ITypesenseClient typesenseClient)
Expand Down Expand Up @@ -435,4 +438,10 @@ private static async Task ExampleDeleteCollection(ITypesenseClient typesenseClie
var deleteCollectionResult = await typesenseClient.DeleteCollection("Addresses");
Console.WriteLine($"Deleted collection: {JsonSerializer.Serialize(deleteCollectionResult)}");
}

private static async Task ExampleRetrieveMetrics(ITypesenseClient typesenseClient)
{
var metrics = await typesenseClient.RetrieveMetrics();
Console.WriteLine($"Retrieved metrics: {JsonSerializer.Serialize(metrics)}");
}
}
10 changes: 10 additions & 0 deletions src/Typesense/ITypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,14 @@ public interface ITypesenseClient
/// <exception cref="TypesenseApiNotFoundException"></exception>
/// <exception cref="TypesenseApiServiceUnavailableException"></exception>
Task<DeleteSynonymResponse> DeleteSynonym(string collection, string synonym);

/// <summary>
/// Get current RAM, CPU, Disk & Network usage metrics.
/// </summary>
/// <returns>
/// Response containing current RAM, CPU, Disk & Network usage metrics.
/// </returns>
/// <exception cref="TypesenseApiException"></exception>
/// <exception cref="TypesenseApiServiceUnavailableException"></exception>
Task<MetricsResponse> RetrieveMetrics();
}
96 changes: 96 additions & 0 deletions src/Typesense/MetricsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Text.Json.Serialization;

namespace Typesense;

public sealed record MetricsResponse
{
[JsonPropertyName("system_cpu1_active_percentage")]
public string SystemCPU1ActivePercentage { get; init; }

[JsonPropertyName("system_cpu2_active_percentage")]
public string SystemCPU2ActivePercentage { get; init; }

[JsonPropertyName("system_cpu3_active_percentage")]
public string SystemCPU3ActivePercentage { get; init; }

[JsonPropertyName("system_cpu4_active_percentage")]
public string SystemCPU4ActivePercentage { get; init; }

[JsonPropertyName("system_cpu_active_percentage")]
public string SystemCPUActivePercentage { get; init; }

[JsonPropertyName("system_disk_total_bytes")]
public string SystemDiskTotalBytes { get; init; }

[JsonPropertyName("system_disk_used_bytes")]
public string SystemDiskUsedBytes { get; init; }

[JsonPropertyName("system_memory_total_bytes")]
public string SystemMemoryTotalBytes { get; init; }

[JsonPropertyName("system_network_received_bytes")]
public string SystemNetworkReceivedBytes { get; init; }

[JsonPropertyName("system_network_sent_bytes")]
public string SystemNetworkSentBytes { get; init; }

[JsonPropertyName("typesense_memory_active_bytes")]
public string TypesenseMemoryActiveBytes { get; init; }

[JsonPropertyName("typesense_memory_allocated_bytes")]
public string TypesenseMemoryAllocatedbytes { get; init; }

[JsonPropertyName("typesense_memory_fragmentation_ratio")]
public string TypesenseMemoryFragmentationRatio { get; init; }

[JsonPropertyName("typesense_memory_mapped_bytes")]
public string TypesenseMemoryMappedBytes { get; init; }

[JsonPropertyName("typesense_memory_metadata_bytes")]
public string TypesenseMemoryMetadataBytes { get; init; }

[JsonPropertyName("typesense_memory_resident_bytes")]
public string TypesenseMemoryResidentBytes { get; init; }

[JsonPropertyName("typesense_memory_retained_bytes")]
public string TypenseMemoryRetainedBytes { get; init; }

[JsonConstructor]
public MetricsResponse(
string systemCPU1ActivePercentage,
string systemCPU2ActivePercentage,
string systemCPU3ActivePercentage,
string systemCPU4ActivePercentage,
string systemCPUActivePercentage,
string systemDiskTotalBytes,
string systemDiskUsedBytes,
string systemMemoryTotalBytes,
string systemnetworkReceivedBytes,
string systemNetworkSentBytes,
string typesenseMemoryActiveBytes,
string typesenseMemoryAllocatedbytes,
string typesenseMemoryFragmentationRatio,
string typesenseMemoryMappedBytes,
string typesenseMemoryMetadataBytes,
string typesenseMemoryResidentBytes,
string typenseMemoryRetainedBytes)
{
SystemCPU1ActivePercentage = systemCPU1ActivePercentage;
SystemCPU2ActivePercentage = systemCPU2ActivePercentage;
SystemCPU3ActivePercentage = systemCPU3ActivePercentage;
SystemCPU4ActivePercentage = systemCPU4ActivePercentage;
SystemCPUActivePercentage = systemCPUActivePercentage;
SystemDiskTotalBytes = systemDiskTotalBytes;
SystemDiskUsedBytes = systemDiskUsedBytes;
SystemMemoryTotalBytes = systemMemoryTotalBytes;
SystemNetworkReceivedBytes = systemnetworkReceivedBytes;
SystemNetworkSentBytes = systemNetworkSentBytes;
TypesenseMemoryActiveBytes = typesenseMemoryActiveBytes;
TypesenseMemoryAllocatedbytes = typesenseMemoryAllocatedbytes;
TypesenseMemoryFragmentationRatio = typesenseMemoryFragmentationRatio;
TypesenseMemoryMappedBytes = typesenseMemoryMappedBytes;
TypesenseMemoryMetadataBytes = typesenseMemoryMetadataBytes;
TypesenseMemoryResidentBytes = typesenseMemoryResidentBytes;
TypenseMemoryRetainedBytes = typenseMemoryRetainedBytes;
}
}
6 changes: 6 additions & 0 deletions src/Typesense/TypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ public async Task<DeleteSynonymResponse> DeleteSynonym(string collection, string
return HandleEmptyStringJsonSerialize<DeleteSynonymResponse>(response, _jsonNameCaseInsentiveTrue);
}

public async Task<MetricsResponse> RetrieveMetrics()
{
var response = await Get("/metrics.json").ConfigureAwait(false);
return HandleEmptyStringJsonSerialize<MetricsResponse>(response);
}

private static string CreateUrlSearchParameters(SearchParameters searchParameters)
{
var urlParameters = "";
Expand Down
27 changes: 27 additions & 0 deletions test/Typesense.Tests/TypesenseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,33 @@ public async Task Delete_synonym()
response.Should().Be(expected);
}

[Fact, TestPriority(30)]
public async Task Can_retrieve_metrics()
{
var response = await _client.RetrieveMetrics();

using (var scope = new AssertionScope())
{
response.Should().NotBeNull();
response.SystemCPU1ActivePercentage.Should().NotBeNullOrWhiteSpace();
response.SystemCPU2ActivePercentage.Should().NotBeNull();
response.SystemCPU3ActivePercentage.Should().NotBeNull();
response.SystemCPU4ActivePercentage.Should().NotBeNull();
response.SystemCPUActivePercentage.Should().NotBeNullOrWhiteSpace();
response.SystemDiskTotalBytes.Should().NotBeNullOrWhiteSpace();
response.SystemDiskUsedBytes.Should().NotBeNullOrWhiteSpace();
response.SystemMemoryTotalBytes.Should().NotBeNullOrWhiteSpace();
response.SystemNetworkReceivedBytes.Should().NotBeNullOrWhiteSpace();
response.TypesenseMemoryActiveBytes.Should().NotBeNullOrWhiteSpace();
response.TypesenseMemoryAllocatedbytes.Should().NotBeNullOrWhiteSpace();
response.TypesenseMemoryFragmentationRatio.Should().NotBeNullOrWhiteSpace();
response.TypesenseMemoryMappedBytes.Should().NotBeNullOrWhiteSpace();
response.TypesenseMemoryMetadataBytes.Should().NotBeNullOrWhiteSpace();
response.TypesenseMemoryResidentBytes.Should().NotBeNullOrWhiteSpace();
response.TypenseMemoryRetainedBytes.Should().NotBeNullOrWhiteSpace();
}
}

private async Task CreateCompanyCollection()
{
var schema = new Schema(
Expand Down

0 comments on commit 3374ad9

Please sign in to comment.