Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
fix(Prometheus-client): Return error message when using invalid query (
Browse files Browse the repository at this point in the history
…#81)

* feat: add promethus http api query client

* feat: add test project

* feat: api query add

* feat: update code and add tests

* docs: add docs

* feat: add convert json value to object

* feat: fix enume and comments

* feat: code styles and other specifications update

* feat: update callerprovider inject

* feat: update jsonelement convert to obj

* feat: update extensions

* feat: update internal to public

* feat: update directory

* feat: test refrence update

* chore: comments and filename update

* chore: remove not exists project id

* chore: update docs and code style

* chore: enums update

* chore: doc code style update

* chore: dosc update

* chore: docs update

* feat: add promethus http api query client

* feat: add test project

* feat: api query add

* feat: update code and add tests

* docs: add docs

* feat: add convert json value to object

* feat: fix enume and comments

* feat: code styles and other specifications update

* feat: update callerprovider inject

* feat: update jsonelement convert to obj

* feat: update extensions

* feat: update internal to public

* feat: update directory

* feat: test refrence update

* chore: comments and filename update

* chore: remove not exists project id

* chore: update docs and code style

* chore: enums update

* chore: doc code style update

* chore: dosc update

* chore: docs update

* chore: name update and remove no used files

* chore: folder name update

* chore: file rename

* chore: class split

* feat: close thorw friendly exception

* chore: code format
  • Loading branch information
Qinyouzeng authored Jul 19, 2022
1 parent e373859 commit 43415bc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class CallerProviderExtensions
public static async Task<string> GetAsync(this ICallerProvider caller, string url, object data)
{
var request = new HttpRequestMessage(HttpMethod.Get, $"{url}?{data.ToUrlParam()}");
var response = await caller.SendAsync(request);
var response = await caller.SendAsync(request, autoThrowUserFriendlyException: false);
return await response.Content.ReadAsStringAsync();
}
}
54 changes: 46 additions & 8 deletions test/Masa.Utils.Data.Prometheus.Test/MasaPrometheusClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,41 @@ public class MasaPrometheusClientTests
{
private IMasaPrometheusClient _client;

[TestInitialize]
public void Initialize()
public MasaPrometheusClientTests()
{
IServiceCollection service = new ServiceCollection();
service.AddPrometheusClient("http://localhost:9090");
_client = service.BuildServiceProvider().GetService<IMasaPrometheusClient>();
_client = service.BuildServiceProvider().GetService<IMasaPrometheusClient>() ?? default!;
}

[TestMethod]
public async Task TestQueryAsync()
[DataRow(null)]
[DataRow("up")]
[DataRow("not_exists")]
[DataRow("error data")]
public async Task TestQueryAsync(string query)
{
var result = await _client.QueryAsync(new QueryRequest
{
Query = "up"
Query = query
});

Assert.IsNotNull(result);
Assert.AreEqual(result.Status, ResultStatuses.Success);
Assert.IsNotNull(result.Data);
if (string.IsNullOrEmpty(query) || query.Contains(' '))
{
Assert.AreEqual(result.Status, ResultStatuses.Error);
}
else
{
if (query == "not_exists")
{
Assert.IsFalse(result.Data?.Result?.Any());
}
else
{
Assert.IsTrue(result.Data?.Result?.Any());
}
}
}

[TestMethod]
Expand Down Expand Up @@ -84,11 +101,32 @@ public async Task TestSeriesQueryAsync()
}

[TestMethod]
public async Task TestLabelsQueryAsync()
[DataRow(null)]
[DataRow(new string[] { "up" })]
[DataRow(new string[] { "not_exists" })]
[DataRow(new string[] { "error data" })]
public async Task TestLabelsQueryAsync(IEnumerable<string> matches)
{
var result = await _client.LabelsQueryAsync(new MetaDataQueryRequest { Match = matches });
if (matches != null && matches.Any(s => s.Contains(' ')))
{
Assert.AreEqual(result.Status, ResultStatuses.Error);
Assert.IsNotNull(result.Error);
}
else
{
var result = await _client.LabelsQueryAsync(default!);
Assert.IsNotNull(result);
Assert.AreEqual(result.Status, ResultStatuses.Success);
if (matches == null || matches.Any(s => s == "up"))
{
Assert.IsTrue(result.Data?.Any());
}
else
{
Assert.IsFalse(result.Data?.Any());
}
}
}

[TestMethod]
Expand Down

0 comments on commit 43415bc

Please sign in to comment.