From 36d4b69a9cb5e61d18ec5851976105860ea27e9e Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Fri, 24 Jun 2022 16:53:48 +0800 Subject: [PATCH 01/12] chore: rebase resolved --- Masa.Contrib.sln | 11 ++++++ .../Masa.Contrib.BasicAbility.Tsc.csproj | 5 +++ .../Service/LogService.cs | 35 +++++++++++++++++++ .../Service/MetricService.cs | 15 ++++++++ .../TscClient.cs | 12 +++++++ .../Masa.Contrib.BasicAbility.Tsc/_Imports.cs | 1 + 6 files changed, 79 insertions(+) create mode 100644 src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs create mode 100644 src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs create mode 100644 src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs diff --git a/Masa.Contrib.sln b/Masa.Contrib.sln index 05a8f51b2..a1b9b6397 100644 --- a/Masa.Contrib.sln +++ b/Masa.Contrib.sln @@ -290,6 +290,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.Distribut EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.DistributedLocking.Medallion.Tests", "test\Masa.Contrib.Data.DistributedLocking.Medallion.Tests\Masa.Contrib.Data.DistributedLocking.Medallion.Tests.csproj", "{FD35BEFA-512C-4483-B5C0-ADCA35680315}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.BasicAbility.Tsc", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj", "{2B3D13B3-9EA1-4EFF-BF71-50109AF10742}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1194,6 +1196,14 @@ Global {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|Any CPU.Build.0 = Release|Any CPU {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.ActiveCfg = Release|Any CPU {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.Build.0 = Release|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|x64.ActiveCfg = Debug|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|x64.Build.0 = Debug|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|Any CPU.Build.0 = Release|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|x64.ActiveCfg = Release|Any CPU + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1338,6 +1348,7 @@ Global {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} = {38E6C400-90C0-493E-9266-C1602E229F1B} {D91C3145-C31C-4301-A493-96F94C903CF2} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} {FD35BEFA-512C-4483-B5C0-ADCA35680315} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} + {2B3D13B3-9EA1-4EFF-BF71-50109AF10742} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj index ac29386c6..637065399 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj @@ -7,6 +7,7 @@ + @@ -19,4 +20,8 @@ + + + + diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs new file mode 100644 index 000000000..1cc6c71ff --- /dev/null +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs @@ -0,0 +1,35 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +using Masa.BuildingBlocks.BasicAbility.Tsc.Model; +using Masa.Utils.Caller.Core; + +namespace Masa.Contrib.BasicAbility.Tsc.Service; + +public class LogService : ILogService +{ + private const string AGGREGATION_URI = "/api/log/aggregation"; + private const string LATEST_URI = "/api/log/latest"; + private const string FIELD_URI = "/api/log/field"; + private readonly ICallerProvider _caller; + + public LogService(ICallerProvider caller) + { + _caller = caller; + } + + public async Task>> GetAggregationAsync(RequestLogAggregationModel query) + { + return (await _caller.GetAsync>>(AGGREGATION_URI, query)) ?? default!; + } + + public async Task> GetFieldsAsync() + { + return (await _caller.GetAsync>(FIELD_URI)) ?? default!; + } + + public async Task GetLatestAsync(RequestLogAggregationModel query) + { + return (await _caller.GetAsync(LATEST_URI, query)) ?? default!; + } +} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs new file mode 100644 index 000000000..038bb05b7 --- /dev/null +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -0,0 +1,15 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Masa.Contrib.BasicAbility.Tsc.Service +{ + internal class MetricService + { + } +} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs new file mode 100644 index 000000000..79260e337 --- /dev/null +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs @@ -0,0 +1,12 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +using Masa.BuildingBlocks.BasicAbility.Tsc; + +namespace Masa.Contrib.BasicAbility.Tsc; + +internal class TscClient : ITscClient +{ + public ILogService LogService { get; private set; } + public IMetricService MetricService { get; private set; } +} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs index ba5bb2a67..4285a25e1 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs @@ -16,3 +16,4 @@ global using System.Net; global using System.Net.Http.Headers; global using System.Text; +global using Masa.BuildingBlocks.BasicAbility.Tsc.Service; From 8f166e996657864141a3c156bd05b509aba53ab3 Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Fri, 24 Jun 2022 16:05:06 +0800 Subject: [PATCH 02/12] feat: add tsc log and metric sdk --- Masa.Contrib.sln | 12 ++++++ .../Extensions/CallerProviderExtensions.cs | 17 +++++++++ .../Extensions/ServiceExtensions.cs | 35 ++++++++++++++++++ .../StreamExtensions.cs} | 2 +- .../Masa.Contrib.BasicAbility.Tsc.csproj | 3 +- .../Service/LogService.cs | 7 +--- .../Service/MetricService.cs | 37 +++++++++++++++---- .../TscClient.cs | 13 +++++-- .../Masa.Contrib.BasicAbility.Tsc/_Imports.cs | 8 +++- ...Masa.Contrib.BasicAbility.Tsc.Tests.csproj | 23 ++++++++++++ .../_Imports.cs | 4 ++ 11 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs create mode 100644 src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs rename src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/{StreamExtenistions.cs => Extensions/StreamExtensions.cs} (97%) create mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj create mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs diff --git a/Masa.Contrib.sln b/Masa.Contrib.sln index a1b9b6397..e926c143b 100644 --- a/Masa.Contrib.sln +++ b/Masa.Contrib.sln @@ -291,6 +291,9 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.DistributedLocking.Medallion.Tests", "test\Masa.Contrib.Data.DistributedLocking.Medallion.Tests\Masa.Contrib.Data.DistributedLocking.Medallion.Tests.csproj", "{FD35BEFA-512C-4483-B5C0-ADCA35680315}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.BasicAbility.Tsc", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj", "{2B3D13B3-9EA1-4EFF-BF71-50109AF10742}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.BasicAbility.Tsc", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj", "{2B3D13B3-9EA1-4EFF-BF71-50109AF10742}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.BasicAbility.Tsc.Tests", "test\Masa.Contrib.BasicAbility.Tsc.Test\Masa.Contrib.BasicAbility.Tsc.Tests.csproj", "{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -1204,6 +1207,14 @@ Global {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|Any CPU.Build.0 = Release|Any CPU {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|x64.ActiveCfg = Release|Any CPU {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|x64.Build.0 = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.ActiveCfg = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.Build.0 = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.Build.0 = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.ActiveCfg = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1349,6 +1360,7 @@ Global {D91C3145-C31C-4301-A493-96F94C903CF2} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} {FD35BEFA-512C-4483-B5C0-ADCA35680315} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} {2B3D13B3-9EA1-4EFF-BF71-50109AF10742} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1} + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22} = {38E6C400-90C0-493E-9266-C1602E229F1B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs new file mode 100644 index 000000000..f21554fd5 --- /dev/null +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs @@ -0,0 +1,17 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Utils.Caller.Core; + +internal static class CallerProviderExtensions +{ + public static async Task GetByBodyAsync(this ICallerProvider caller, string url, object body) where TResult : class + { + var request = new HttpRequestMessage(HttpMethod.Get, url); + if (body != null) + { + request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); + } + return (await caller.SendAsync(request)) ?? default!; + } +} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs new file mode 100644 index 000000000..2016f0015 --- /dev/null +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs @@ -0,0 +1,35 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.BasicAbility.Tsc; + +public static class ServiceExtensions +{ + private const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.tsc"; + + public static IServiceCollection AddTscClient(this IServiceCollection services, string tscServiceBaseUri) + { + ArgumentNullException.ThrowIfNull(tscServiceBaseUri, nameof(tscServiceBaseUri)); + + if (services.Any(service => service.ServiceType == typeof(ITscClient))) + return services; + + services.AddCaller(builder => + { + builder.UseHttpClient(options => + { + options.BaseAddress = tscServiceBaseUri; + options.Name = DEFAULT_CLIENT_NAME; + }); + }); + + services.AddSingleton(serviceProvider => + { + var caller = serviceProvider.GetRequiredService().CreateClient(DEFAULT_CLIENT_NAME); + var pmCaching = new TscClient(caller); + return pmCaching; + }); + + return services; + } +} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/StreamExtenistions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/StreamExtensions.cs similarity index 97% rename from src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/StreamExtenistions.cs rename to src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/StreamExtensions.cs index da016dfc8..27da597ef 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/StreamExtenistions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/StreamExtensions.cs @@ -3,7 +3,7 @@ namespace System.IO; -public static class StreamExtenistions +public static class StreamExtensions { private static readonly Encoding _defaultEncoding = Encoding.UTF8; diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj index 637065399..3a178726c 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj @@ -7,7 +7,8 @@ - + + diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs index 1cc6c71ff..830af0052 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs @@ -1,9 +1,6 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. -using Masa.BuildingBlocks.BasicAbility.Tsc.Model; -using Masa.Utils.Caller.Core; - namespace Masa.Contrib.BasicAbility.Tsc.Service; public class LogService : ILogService @@ -18,7 +15,7 @@ public LogService(ICallerProvider caller) _caller = caller; } - public async Task>> GetAggregationAsync(RequestLogAggregationModel query) + public async Task>> GetAggregationAsync(LogAggregationRequest query) { return (await _caller.GetAsync>>(AGGREGATION_URI, query)) ?? default!; } @@ -28,7 +25,7 @@ public async Task> GetFieldsAsync() return (await _caller.GetAsync>(FIELD_URI)) ?? default!; } - public async Task GetLatestAsync(RequestLogAggregationModel query) + public async Task GetLatestAsync(LogAggregationRequest query) { return (await _caller.GetAsync(LATEST_URI, query)) ?? default!; } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index 038bb05b7..bd3900d8c 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -1,15 +1,36 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Masa.Contrib.BasicAbility.Tsc.Service; -namespace Masa.Contrib.BasicAbility.Tsc.Service +internal class MetricService : IMetricService { - internal class MetricService + private readonly ICallerProvider _caller; + private const string AGGREGATION_URI = "/api/metric/aggregation"; + private const string METRIC_URI = "/api/metric/all"; + private const string LABELVALUES_URI = "/api/metric/label-values"; + + public MetricService(ICallerProvider caller) + { + _caller = caller; + } + + public async Task> GetMetricsAsync(IEnumerable? match) + { + return (await _caller.GetAsync>(METRIC_URI, new { match })) ?? default!; + } + + public async Task>> GetLabelAndValuesAsync(MetricLableValuesRequest query) + { + var data = await _caller.GetByBodyAsync>>>(LABELVALUES_URI, query); + if (data == null || !data.ContainsKey(query.Match)) + return default!; + + return data[query.Match]; + } + + public async Task GetMetricAggAsync(MetricAggRequest query) { + return (await _caller.GetByBodyAsync(AGGREGATION_URI, query)) ?? default!; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs index 79260e337..791a67ac4 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs @@ -1,12 +1,17 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. -using Masa.BuildingBlocks.BasicAbility.Tsc; - namespace Masa.Contrib.BasicAbility.Tsc; internal class TscClient : ITscClient { - public ILogService LogService { get; private set; } - public IMetricService MetricService { get; private set; } + public TscClient(ICallerProvider callerProvider) + { + LogService = new LogService(callerProvider); + MetricService = new MetricService(callerProvider); + } + + public ILogService LogService { get; } + + public IMetricService MetricService { get; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs index 4285a25e1..c9ff9d7ec 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs @@ -1,8 +1,15 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the Apache License. See LICENSE.txt in the project root for license information. +global using Masa.BuildingBlocks.BasicAbility.Tsc; +global using Masa.BuildingBlocks.BasicAbility.Tsc.Model; +global using Masa.BuildingBlocks.BasicAbility.Tsc.Service; global using Masa.Contrib.BasicAbility.Tsc; +global using Masa.Contrib.BasicAbility.Tsc.Service; +global using Masa.Utils.Caller.Core; +global using Masa.Utils.Caller.HttpClient; global using Microsoft.AspNetCore.Http; +global using Microsoft.Extensions.DependencyInjection; global using OpenTelemetry.Contrib.Instrumentation.ElasticsearchClient; global using OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore; global using OpenTelemetry.Instrumentation.AspNetCore; @@ -16,4 +23,3 @@ global using System.Net; global using System.Net.Http.Headers; global using System.Text; -global using Masa.BuildingBlocks.BasicAbility.Tsc.Service; diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj b/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj new file mode 100644 index 000000000..774d241b2 --- /dev/null +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + + + diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs new file mode 100644 index 000000000..1109a8e81 --- /dev/null +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs @@ -0,0 +1,4 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +global using Microsoft.VisualStudio.TestTools.UnitTesting; From a254f7603112ed6c467f69518e81da26527860e8 Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Fri, 24 Jun 2022 17:19:26 +0800 Subject: [PATCH 03/12] chore: update project --- Masa.Contrib.sln | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/Masa.Contrib.sln b/Masa.Contrib.sln index e926c143b..5d269f415 100644 --- a/Masa.Contrib.sln +++ b/Masa.Contrib.sln @@ -286,14 +286,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Identity.Ident EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DistributedLocking", "DistributedLocking", "{DB2B4DA3-EEF2-49AA-93A4-B00C25210A68}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.DistributedLocking.Local.Tests", "test\Masa.Contrib.Data.DistributedLocking.Local.Tests\Masa.Contrib.Data.DistributedLocking.Local.Tests.csproj", "{D91C3145-C31C-4301-A493-96F94C903CF2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.DistributedLocking.Local.Tests", "test\Masa.Contrib.Data.DistributedLocking.Local.Tests\Masa.Contrib.Data.DistributedLocking.Local.Tests.csproj", "{D91C3145-C31C-4301-A493-96F94C903CF2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.DistributedLocking.Medallion.Tests", "test\Masa.Contrib.Data.DistributedLocking.Medallion.Tests\Masa.Contrib.Data.DistributedLocking.Medallion.Tests.csproj", "{FD35BEFA-512C-4483-B5C0-ADCA35680315}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.DistributedLocking.Medallion.Tests", "test\Masa.Contrib.Data.DistributedLocking.Medallion.Tests\Masa.Contrib.Data.DistributedLocking.Medallion.Tests.csproj", "{FD35BEFA-512C-4483-B5C0-ADCA35680315}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.BasicAbility.Tsc", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj", "{2B3D13B3-9EA1-4EFF-BF71-50109AF10742}" -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.BasicAbility.Tsc", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj", "{2B3D13B3-9EA1-4EFF-BF71-50109AF10742}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.Tsc.Tests", "test\Masa.Contrib.BasicAbility.Tsc.Test\Masa.Contrib.BasicAbility.Tsc.Tests.csproj", "{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.BasicAbility.Tsc.Tests", "test\Masa.Contrib.BasicAbility.Tsc.Test\Masa.Contrib.BasicAbility.Tsc.Tests.csproj", "{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.BasicAbility.Tsc", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj", "{C265268A-F311-4B6A-915E-C1AF9D1EB624}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -1199,14 +1198,6 @@ Global {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|Any CPU.Build.0 = Release|Any CPU {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.ActiveCfg = Release|Any CPU {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.Build.0 = Release|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|x64.ActiveCfg = Debug|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Debug|x64.Build.0 = Debug|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|Any CPU.Build.0 = Release|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|x64.ActiveCfg = Release|Any CPU - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742}.Release|x64.Build.0 = Release|Any CPU {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.Build.0 = Debug|Any CPU {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -1215,6 +1206,14 @@ Global {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.Build.0 = Release|Any CPU {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.ActiveCfg = Release|Any CPU {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.Build.0 = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.ActiveCfg = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.Build.0 = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.Build.0 = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.ActiveCfg = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1359,8 +1358,8 @@ Global {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} = {38E6C400-90C0-493E-9266-C1602E229F1B} {D91C3145-C31C-4301-A493-96F94C903CF2} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} {FD35BEFA-512C-4483-B5C0-ADCA35680315} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} - {2B3D13B3-9EA1-4EFF-BF71-50109AF10742} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1} {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22} = {38E6C400-90C0-493E-9266-C1602E229F1B} + {C265268A-F311-4B6A-915E-C1AF9D1EB624} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03} From aafd4c5423216a1ee1c8cf8feb7766448372b4f6 Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Mon, 4 Jul 2022 11:58:08 +0800 Subject: [PATCH 04/12] feat: model names update and tests add --- .../Service/LogService.cs | 8 +- .../Service/MetricService.cs | 14 +-- .../TscClient.cs | 1 + .../Masa.Contrib.BasicAbility.Tsc/_Imports.cs | 1 + src/BuildingBlocks/MASA.BuildingBlocks | 2 +- ...Masa.Contrib.BasicAbility.Tsc.Tests.csproj | 3 +- .../Service/LogServiceTests.cs | 95 +++++++++++++++++++ .../Service/MetricServiceTests.cs | 74 +++++++++++++++ .../_Imports.cs | 10 ++ 9 files changed, 194 insertions(+), 14 deletions(-) create mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs create mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs index 830af0052..567217282 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs @@ -5,10 +5,10 @@ namespace Masa.Contrib.BasicAbility.Tsc.Service; public class LogService : ILogService { - private const string AGGREGATION_URI = "/api/log/aggregation"; - private const string LATEST_URI = "/api/log/latest"; - private const string FIELD_URI = "/api/log/field"; private readonly ICallerProvider _caller; + internal const string AGGREGATION_URI = "/api/log/aggregation"; + internal const string LATEST_URI = "/api/log/latest"; + internal const string FIELD_URI = "/api/log/field"; public LogService(ICallerProvider caller) { @@ -25,7 +25,7 @@ public async Task> GetFieldsAsync() return (await _caller.GetAsync>(FIELD_URI)) ?? default!; } - public async Task GetLatestAsync(LogAggregationRequest query) + public async Task GetLatestAsync(LogLatestRequest query) { return (await _caller.GetAsync(LATEST_URI, query)) ?? default!; } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index bd3900d8c..66122e9cd 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -6,18 +6,18 @@ namespace Masa.Contrib.BasicAbility.Tsc.Service; internal class MetricService : IMetricService { private readonly ICallerProvider _caller; - private const string AGGREGATION_URI = "/api/metric/aggregation"; - private const string METRIC_URI = "/api/metric/all"; - private const string LABELVALUES_URI = "/api/metric/label-values"; + internal const string RANGEVALUES_URL = "/api/metric/range-values"; + internal const string NAMES_URI = "/api/metric/names"; + internal const string LABELVALUES_URI = "/api/metric/label-values"; public MetricService(ICallerProvider caller) { _caller = caller; } - public async Task> GetMetricsAsync(IEnumerable? match) + public async Task> GetMetricNamesAsync(IEnumerable? match) { - return (await _caller.GetAsync>(METRIC_URI, new { match })) ?? default!; + return (await _caller.GetAsync>(NAMES_URI, new { match })) ?? default!; } public async Task>> GetLabelAndValuesAsync(MetricLableValuesRequest query) @@ -29,8 +29,8 @@ public async Task>> GetLabelAndValuesAsync(Metri return data[query.Match]; } - public async Task GetMetricAggAsync(MetricAggRequest query) + public async Task GetMetricValuesAsync(MetricRangeValueRequest query) { - return (await _caller.GetByBodyAsync(AGGREGATION_URI, query)) ?? default!; + return (await _caller.GetByBodyAsync(RANGEVALUES_URL, query)) ?? default!; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs index 791a67ac4..c0acf6ee4 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs @@ -1,6 +1,7 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +[assembly: InternalsVisibleTo("Masa.Contrib.BasicAbility.Tsc.Tests")] namespace Masa.Contrib.BasicAbility.Tsc; internal class TscClient : ITscClient diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs index c9ff9d7ec..cbb809b1e 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs @@ -22,4 +22,5 @@ global using System.Diagnostics; global using System.Net; global using System.Net.Http.Headers; +global using System.Runtime.CompilerServices; global using System.Text; diff --git a/src/BuildingBlocks/MASA.BuildingBlocks b/src/BuildingBlocks/MASA.BuildingBlocks index 415ed143e..fdb9ea87d 160000 --- a/src/BuildingBlocks/MASA.BuildingBlocks +++ b/src/BuildingBlocks/MASA.BuildingBlocks @@ -1 +1 @@ -Subproject commit 415ed143e5bf0db31b11da720ae0902f0d61e7e2 +Subproject commit fdb9ea87d100da01ff54f45928bf0932e5100976 diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj b/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj index 774d241b2..64a140519 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj @@ -7,8 +7,7 @@ false - - + diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs new file mode 100644 index 000000000..1d51d38ed --- /dev/null +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs @@ -0,0 +1,95 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.BasicAbility.Tsc.Tests; + +[TestClass] +public class LogServiceTests +{ + [TestMethod] + public async Task GetFieldsAsyncTest() + { + var callerProvider = new Mock(); + + var time = DateTime.Now; + var query = new MetricLableValuesRequest + { + Match = "up", + Start = time.AddMinutes(-15), + End = time + }; + var data = new string[] { + "@timestamp", + "container.instance.id", + "container.instance.name", + "Id" + }; + callerProvider.Setup(provider => provider.GetAsync>(LogService.FIELD_URI, default)).ReturnsAsync(data).Verifiable(); + var client = new TscClient(callerProvider.Object); + + var result = await client.LogService.GetFieldsAsync(); + Assert.IsNull(result); + } + + [TestMethod] + public async Task GetAggregationAsyncTest() + { + var callerProvider = new Mock(); + + var time = DateTime.Now; + var query = new LogAggregationRequest + { + Start = time.AddMinutes(-15), + End = time, + FieldMaps = new FieldAggregationRequest[] { + new FieldAggregationRequest{ + Name="container.instance.id", + AggType= LogAggTypes.Count, + Alias="count1" + }, + new FieldAggregationRequest{ + Name="container.instance.name", + AggType= LogAggTypes.Count, + Alias="count2" + } + } + }; + var data = new Dictionary { + {"count1","0" }, + { "count2","0"} + }; + callerProvider.Setup(provider => provider.GetAsync>>(LogService.FIELD_URI, default)).ReturnsAsync(data).Verifiable(); + var client = new TscClient(callerProvider.Object); + + var result = await client.LogService.GetAggregationAsync(query); + Assert.IsNull(result); + } + + [TestMethod] + public async Task GetLatestAsyncTest() + { + var callerProvider = new Mock(); + + var time = DateTime.Now; + var query = new LogLatestRequest + { + Start = time.AddMinutes(-15), + End = time, + IsDesc = true, + Query = "\"term\": {\"Resource.service.name\": \"masa.tsc.api\"}" + }; + + var str = "{\"@timestamp\":\"2022-06-15T09:09:05.972899500Z\",\"Attributes.ProcessorName\":\"Masa.Contrib.Dispatcher.IntegrationEvents.Dapr.Processor.RetryByDataProcessor\",\"Attributes.exception.message\":\"SQLite Error 1: 'no such table: IntegrationEventLog'.\",\"Attributes.exception.type\":\"SqliteException\",\"Attributes.{OriginalFormat}\":\"Processor '{ProcessorName}' failed\",\"Body\":\"Processor 'Masa.Contrib.Dispatcher.IntegrationEvents.Dapr.Processor.RetryByDataProcessor' failed\",\"Resource.service.instance.id\":\"5d9d00e3-5bb0-40bc-bbb8-ef0b210f739d\",\"Resource.service.name\":\"masa.tsc.api\",\"Resource.service.namespace\":\"Development\",\"Resource.service.version\":\"0.1.0\",\"Resource.telemetry.sdk.language\":\"dotnet\",\"Resource.telemetry.sdk.name\":\"opentelemetry\",\"Resource.telemetry.sdk.version\":\"1.3.0.470\",\"SeverityNumber\":13,\"SeverityText\":\"Warning\",\"TraceFlags\":0}"; + var options = new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true, + }; + options.Converters.Add(new JsonStringEnumConverter()); + var data = JsonSerializer.Deserialize(str, options); + callerProvider.Setup(provider => provider.GetAsync(LogService.LATEST_URI, default)).ReturnsAsync(data).Verifiable(); + var client = new TscClient(callerProvider.Object); + + var result = await client.LogService.GetLatestAsync(query); + Assert.IsNull(result); + } +} diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs new file mode 100644 index 000000000..b8d7ce1fa --- /dev/null +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs @@ -0,0 +1,74 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.BasicAbility.Tsc.Tests; + +[TestClass] +public class MetricServiceTests +{ + [TestMethod] + public async Task GetMetricNamesAsyncTest() + { + var callerProvider = new Mock(); + IEnumerable data = new string[] { "up", "http_request_total" }; + callerProvider.Setup(provider => provider.GetAsync>(MetricService.NAMES_URI, default)).ReturnsAsync(data).Verifiable(); + IEnumerable query = new string[] { "up" }; + callerProvider.Setup(provider => provider.GetAsync>(MetricService.NAMES_URI, query, default)).ReturnsAsync(query).Verifiable(); + IEnumerable queryNotExists = new string[] { "up1" }; + callerProvider.Setup(provider => provider.GetAsync>(MetricService.NAMES_URI, queryNotExists, default)).ReturnsAsync(default(IEnumerable)).Verifiable(); + var client = new TscClient(callerProvider.Object); + + var result = await client.MetricService.GetMetricNamesAsync(default); + Assert.IsNotNull(result); + + result = await client.MetricService.GetMetricNamesAsync(query); + Assert.IsNotNull(result); + + result = await client.MetricService.GetMetricNamesAsync(queryNotExists); + Assert.IsNull(result); + } + + [TestMethod] + public async Task GetLabelAndValuesAsyncTest() + { + var callerProvider = new Mock(); + + var time = DateTime.Now; + var query = new MetricLableValuesRequest + { + Match = "up", + Start = time.AddMinutes(-15), + End = time + }; + var data = new Dictionary> + { + {"job",new List{"prometheus"} }, + { "container",new List{ "config-reloader", "coredns" } } + }; + callerProvider.Setup(provider => provider.GetAsync>>(MetricService.LABELVALUES_URI, query, default)).ReturnsAsync(data).Verifiable(); + var client = new TscClient(callerProvider.Object); + + var result = await client.MetricService.GetLabelAndValuesAsync(query); + Assert.IsNull(result); + } + + [TestMethod] + public async Task GetMetricValuesAsyncTest() + { + var callerProvider = new Mock(); + + var time = DateTime.Now; + var query = new MetricRangeValueRequest + { + Match = "up", + Start = time.AddMinutes(-15), + End = time + }; + var data = "100"; + callerProvider.Setup(provider => provider.GetAsync(MetricService.RANGEVALUES_URL, query, default)).ReturnsAsync(data).Verifiable(); + var client = new TscClient(callerProvider.Object); + + var result = await client.MetricService.GetMetricValuesAsync(query); + Assert.IsNull(result); + } +} diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs index 1109a8e81..ddf51babe 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs @@ -1,4 +1,14 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +global using Masa.BuildingBlocks.BasicAbility.Tsc.Enums; +global using Masa.BuildingBlocks.BasicAbility.Tsc.Model; +global using Masa.Contrib.BasicAbility.Tsc.Service; +global using Masa.Utils.Caller.Core; global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using Moq; +global using System; +global using System.Collections.Generic; +global using System.Text.Json; +global using System.Text.Json.Serialization; +global using System.Threading.Tasks; From 51f22f5e5446a54f985712e6ce1506147385c90c Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Mon, 4 Jul 2022 16:49:05 +0800 Subject: [PATCH 05/12] feat : api update --- .../Extensions/CallerProviderExtensions.cs | 3 +- .../Extensions/ServiceExtensions.cs | 1 + .../Service/MetricService.cs | 2 +- .../Common.cs | 21 ++++++ .../CallerProviderExtensionsTests.cs | 21 ++++++ ...Masa.Contrib.BasicAbility.Tsc.Tests.csproj | 5 +- .../Service/LogServiceTests.cs | 28 +++---- .../Service/MetricServiceTests.cs | 74 ------------------- .../_Imports.cs | 1 + 9 files changed, 65 insertions(+), 91 deletions(-) create mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs create mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs delete mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs index f21554fd5..f091b3a5f 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +[assembly: InternalsVisibleTo("Masa.Contrib.BasicAbility.Tsc.Tests")] namespace Masa.Utils.Caller.Core; internal static class CallerProviderExtensions @@ -12,6 +13,6 @@ public static async Task GetByBodyAsync(this ICallerProvider c { request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); } - return (await caller.SendAsync(request)) ?? default!; + return (await caller.SendAsync(request,default)) ?? default!; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs index 2016f0015..d0b496224 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +[assembly: InternalsVisibleTo("Masa.Contrib.BasicAbility.Tsc.Tests")] namespace Masa.Contrib.BasicAbility.Tsc; public static class ServiceExtensions diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index 66122e9cd..8d84b7d74 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -15,7 +15,7 @@ public MetricService(ICallerProvider caller) _caller = caller; } - public async Task> GetMetricNamesAsync(IEnumerable? match) + public async Task> GetMetricNamesAsync(IEnumerable? match = default) { return (await _caller.GetAsync>(NAMES_URI, new { match })) ?? default!; } diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs new file mode 100644 index 000000000..d59af4c0e --- /dev/null +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs @@ -0,0 +1,21 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +using System.Net.Http; +using System.Text; + +namespace Masa.Contrib.BasicAbility.Tsc.Tests; + +internal class Common +{ + + public static HttpRequestMessage CreateMessage(string url, object body) + { + var request = new HttpRequestMessage(HttpMethod.Get, url); + if (body != null) + { + request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); + } + return request; + } +} diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs new file mode 100644 index 000000000..d19f2500b --- /dev/null +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs @@ -0,0 +1,21 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.BasicAbility.Tsc.Tests.Extensions; + +[TestClass] +public class CallerProviderExtensionsTests +{ + [TestMethod] + public async Task GetByBodyAsyncTest() + { + var callerProvider = new Mock(); + string url = "http://locahost:80/test"; + var param = new { name = "name" }; + var result = "ok"; + callerProvider.Setup(provider => provider.SendAsync(It.IsAny(), default)).ReturnsAsync(result); + var str = await callerProvider.Object.GetByBodyAsync(url, "name"); + Assert.IsNotNull(str); + Assert.AreEqual(result, str); + } +} diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj b/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj index 64a140519..2c1ac968e 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Masa.Contrib.BasicAbility.Tsc.Tests.csproj @@ -7,11 +7,12 @@ false - + + - + diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs index 1d51d38ed..ecaf2b4d0 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs @@ -18,7 +18,8 @@ public async Task GetFieldsAsyncTest() Start = time.AddMinutes(-15), End = time }; - var data = new string[] { + var data = new string[] + { "@timestamp", "container.instance.id", "container.instance.name", @@ -26,9 +27,8 @@ public async Task GetFieldsAsyncTest() }; callerProvider.Setup(provider => provider.GetAsync>(LogService.FIELD_URI, default)).ReturnsAsync(data).Verifiable(); var client = new TscClient(callerProvider.Object); - var result = await client.LogService.GetFieldsAsync(); - Assert.IsNull(result); + Assert.IsNotNull(result); } [TestMethod] @@ -41,28 +41,30 @@ public async Task GetAggregationAsyncTest() { Start = time.AddMinutes(-15), End = time, - FieldMaps = new FieldAggregationRequest[] { + FieldMaps = new FieldAggregationRequest[] + { new FieldAggregationRequest{ Name="container.instance.id", - AggType= LogAggTypes.Count, - Alias="count1" + AggType= LogAggTypes.Count, + Alias="count1" }, new FieldAggregationRequest{ Name="container.instance.name", - AggType= LogAggTypes.Count, - Alias="count2" + AggType= LogAggTypes.Count, + Alias="count2" } } }; - var data = new Dictionary { + var data = new Dictionary + { {"count1","0" }, { "count2","0"} }; - callerProvider.Setup(provider => provider.GetAsync>>(LogService.FIELD_URI, default)).ReturnsAsync(data).Verifiable(); + callerProvider.Setup(provider => provider.GetAsync>>(LogService.FIELD_URI, query, default)).ReturnsAsync(data).Verifiable(); var client = new TscClient(callerProvider.Object); var result = await client.LogService.GetAggregationAsync(query); - Assert.IsNull(result); + Assert.IsNotNull(result); } [TestMethod] @@ -86,10 +88,10 @@ public async Task GetLatestAsyncTest() }; options.Converters.Add(new JsonStringEnumConverter()); var data = JsonSerializer.Deserialize(str, options); - callerProvider.Setup(provider => provider.GetAsync(LogService.LATEST_URI, default)).ReturnsAsync(data).Verifiable(); + callerProvider.Setup(provider => provider.GetAsync(LogService.LATEST_URI, query, default)).ReturnsAsync(data).Verifiable(); var client = new TscClient(callerProvider.Object); var result = await client.LogService.GetLatestAsync(query); - Assert.IsNull(result); + Assert.IsNotNull(result); } } diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs deleted file mode 100644 index b8d7ce1fa..000000000 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) MASA Stack All rights reserved. -// Licensed under the MIT License. See LICENSE.txt in the project root for license information. - -namespace Masa.Contrib.BasicAbility.Tsc.Tests; - -[TestClass] -public class MetricServiceTests -{ - [TestMethod] - public async Task GetMetricNamesAsyncTest() - { - var callerProvider = new Mock(); - IEnumerable data = new string[] { "up", "http_request_total" }; - callerProvider.Setup(provider => provider.GetAsync>(MetricService.NAMES_URI, default)).ReturnsAsync(data).Verifiable(); - IEnumerable query = new string[] { "up" }; - callerProvider.Setup(provider => provider.GetAsync>(MetricService.NAMES_URI, query, default)).ReturnsAsync(query).Verifiable(); - IEnumerable queryNotExists = new string[] { "up1" }; - callerProvider.Setup(provider => provider.GetAsync>(MetricService.NAMES_URI, queryNotExists, default)).ReturnsAsync(default(IEnumerable)).Verifiable(); - var client = new TscClient(callerProvider.Object); - - var result = await client.MetricService.GetMetricNamesAsync(default); - Assert.IsNotNull(result); - - result = await client.MetricService.GetMetricNamesAsync(query); - Assert.IsNotNull(result); - - result = await client.MetricService.GetMetricNamesAsync(queryNotExists); - Assert.IsNull(result); - } - - [TestMethod] - public async Task GetLabelAndValuesAsyncTest() - { - var callerProvider = new Mock(); - - var time = DateTime.Now; - var query = new MetricLableValuesRequest - { - Match = "up", - Start = time.AddMinutes(-15), - End = time - }; - var data = new Dictionary> - { - {"job",new List{"prometheus"} }, - { "container",new List{ "config-reloader", "coredns" } } - }; - callerProvider.Setup(provider => provider.GetAsync>>(MetricService.LABELVALUES_URI, query, default)).ReturnsAsync(data).Verifiable(); - var client = new TscClient(callerProvider.Object); - - var result = await client.MetricService.GetLabelAndValuesAsync(query); - Assert.IsNull(result); - } - - [TestMethod] - public async Task GetMetricValuesAsyncTest() - { - var callerProvider = new Mock(); - - var time = DateTime.Now; - var query = new MetricRangeValueRequest - { - Match = "up", - Start = time.AddMinutes(-15), - End = time - }; - var data = "100"; - callerProvider.Setup(provider => provider.GetAsync(MetricService.RANGEVALUES_URL, query, default)).ReturnsAsync(data).Verifiable(); - var client = new TscClient(callerProvider.Object); - - var result = await client.MetricService.GetMetricValuesAsync(query); - Assert.IsNull(result); - } -} diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs index ddf51babe..39dff925d 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs @@ -9,6 +9,7 @@ global using Moq; global using System; global using System.Collections.Generic; +global using System.Net.Http; global using System.Text.Json; global using System.Text.Json.Serialization; global using System.Threading.Tasks; From 0daba91c465d4141a8118f2f2f44a3f3b5bb97ec Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Tue, 5 Jul 2022 11:11:39 +0800 Subject: [PATCH 06/12] feat: test and update --- .../Service/MetricService.cs | 12 ++- .../Service/MetricServiceTests.cs | 75 +++++++++++++++++++ .../_Imports.cs | 2 + 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index 8d84b7d74..634003cc8 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -17,7 +17,12 @@ public MetricService(ICallerProvider caller) public async Task> GetMetricNamesAsync(IEnumerable? match = default) { - return (await _caller.GetAsync>(NAMES_URI, new { match })) ?? default!; + string param = default!; + if (match != null && match.Any(s => !string.IsNullOrEmpty(s))) + { + param = string.Join(',', match.Where(s => !string.IsNullOrEmpty(s))); + } + return (await _caller.GetAsync>(NAMES_URI, new { match = param })) ?? default!; } public async Task>> GetLabelAndValuesAsync(MetricLableValuesRequest query) @@ -31,6 +36,11 @@ public async Task>> GetLabelAndValuesAsync(Metri public async Task GetMetricValuesAsync(MetricRangeValueRequest query) { + if (query.Lables != null && !string.IsNullOrEmpty(query.Match)) + { + query.Match = $"{query.Match}{{{string.Join(',',query.Lables)}}}"; + } + return (await _caller.GetByBodyAsync(RANGEVALUES_URL, query)) ?? default!; } } diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs new file mode 100644 index 000000000..e508cbea9 --- /dev/null +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs @@ -0,0 +1,75 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +using System.Linq; + +namespace Masa.Contrib.BasicAbility.Tsc.Tests.Service; + +[TestClass] +public class MetricServiceTests +{ + private ITscClient _client; + + [TestInitialize] + public void Initialize() + { + IServiceCollection service = new ServiceCollection(); + service.AddTscClient("https://localhost:6324/"); + _client = service.BuildServiceProvider().GetService() ?? default!; + } + + [TestMethod] + [DataRow(null)] + [DataRow(new string[] { "up", "prometheus_http_requests_total", "prometheus_http_request_duration_seconds_count" })] + [DataRow(new string[] { "not_exists", "up" })] + //[DataRow(new string[] { "not_exists" })] + public async Task GetMetricNamesAsyncTest(IEnumerable match) + { + var result = await _client.MetricService.GetMetricNamesAsync(match); + if (match == null) + { + Assert.IsNotNull(result); + } + else if (match.Count() - 1 > 0) + { + Assert.IsNotNull(result); + } + else + { + Assert.IsNull(result); + } + } + + [TestMethod] + [DataRow("up", "2022-07-01T09:00:00.000Z", "2022-07-05T22:00:00.000Z")] + public async Task GetLabelAndValuesAsyncTest(string match, string start, string end) + { + DateTime startDateTime = DateTime.Parse(start); + DateTime endDateTime = DateTime.Parse(end); + var result = await _client.MetricService.GetLabelAndValuesAsync(new MetricLableValuesRequest + { + Match = match, + Start = startDateTime, + End = endDateTime + }); + + Assert.IsNotNull(result); + Assert.IsTrue(result.Any()); + } + + [TestMethod] + [DataRow("up", null, "2022-07-01T09:00:00.000Z", "2022-07-05T22:00:00.000Z")] + public async Task GetMetricValuesAsyncTest(string match, IEnumerable labels, string start, string end) + { + DateTime startDateTime = DateTime.Parse(start); + DateTime endDateTime = DateTime.Parse(end); + var result = await _client.MetricService.GetMetricValuesAsync(new MetricRangeValueRequest + { + Match = match, + Lables = labels, + End = endDateTime, + Start = startDateTime + }); + Assert.IsNotNull(result); + } +} diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs index 39dff925d..d217dca5b 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs @@ -1,10 +1,12 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +global using Masa.BuildingBlocks.BasicAbility.Tsc; global using Masa.BuildingBlocks.BasicAbility.Tsc.Enums; global using Masa.BuildingBlocks.BasicAbility.Tsc.Model; global using Masa.Contrib.BasicAbility.Tsc.Service; global using Masa.Utils.Caller.Core; +global using Microsoft.Extensions.DependencyInjection; global using Microsoft.VisualStudio.TestTools.UnitTesting; global using Moq; global using System; From e6d43ede0ee8b1ae872c551894fe282c1e313530 Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Wed, 6 Jul 2022 10:50:23 +0800 Subject: [PATCH 07/12] chore: tests update --- src/BuildingBlocks/MASA.BuildingBlocks | 2 +- .../Service/MetricServiceTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BuildingBlocks/MASA.BuildingBlocks b/src/BuildingBlocks/MASA.BuildingBlocks index 23b1b97ed..59842a18b 160000 --- a/src/BuildingBlocks/MASA.BuildingBlocks +++ b/src/BuildingBlocks/MASA.BuildingBlocks @@ -1 +1 @@ -Subproject commit 23b1b97edb56f5b11ee7e7473f3772962961ebbc +Subproject commit 59842a18b3d1787c9a920906e9927921f4a0010d diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs index e508cbea9..fa94e8a8e 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs @@ -22,7 +22,7 @@ public void Initialize() [DataRow(null)] [DataRow(new string[] { "up", "prometheus_http_requests_total", "prometheus_http_request_duration_seconds_count" })] [DataRow(new string[] { "not_exists", "up" })] - //[DataRow(new string[] { "not_exists" })] + [DataRow(new string[] { "not_exists" })] public async Task GetMetricNamesAsyncTest(IEnumerable match) { var result = await _client.MetricService.GetMetricNamesAsync(match); @@ -30,7 +30,7 @@ public async Task GetMetricNamesAsyncTest(IEnumerable match) { Assert.IsNotNull(result); } - else if (match.Count() - 1 > 0) + else if (match.Count() > 0) { Assert.IsNotNull(result); } From fee7de7f9afb3e9073e3d44d6f1342c8dfbe9bbd Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Mon, 11 Jul 2022 09:00:46 +0800 Subject: [PATCH 08/12] feat: names update --- Masa.Contrib.sln | 36 +++++++++---------- .../Service/MetricService.cs | 12 +++---- src/BuildingBlocks/MASA.BuildingBlocks | 2 +- .../Service/LogServiceTests.cs | 2 +- .../Service/MetricServiceTests.cs | 12 +++---- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/Masa.Contrib.sln b/Masa.Contrib.sln index cd43c08b9..00df05c83 100644 --- a/Masa.Contrib.sln +++ b/Masa.Contrib.sln @@ -1202,22 +1202,6 @@ Global {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|Any CPU.Build.0 = Release|Any CPU {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.ActiveCfg = Release|Any CPU {FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.Build.0 = Release|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.ActiveCfg = Debug|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.Build.0 = Debug|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.Build.0 = Release|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.ActiveCfg = Release|Any CPU - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.Build.0 = Release|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.ActiveCfg = Debug|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.Build.0 = Debug|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.Build.0 = Release|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.ActiveCfg = Release|Any CPU - {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.Build.0 = Release|Any CPU {23582B50-BAEA-4A2D-82A4-345A3C30124E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {23582B50-BAEA-4A2D-82A4-345A3C30124E}.Debug|Any CPU.Build.0 = Debug|Any CPU {23582B50-BAEA-4A2D-82A4-345A3C30124E}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -1346,6 +1330,22 @@ Global {FC4E526A-DBFC-406A-8ED3-64983B67F688}.Release|Any CPU.Build.0 = Release|Any CPU {FC4E526A-DBFC-406A-8ED3-64983B67F688}.Release|x64.ActiveCfg = Release|Any CPU {FC4E526A-DBFC-406A-8ED3-64983B67F688}.Release|x64.Build.0 = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.ActiveCfg = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.Build.0 = Debug|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.Build.0 = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.ActiveCfg = Release|Any CPU + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.Build.0 = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.ActiveCfg = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.Build.0 = Debug|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.Build.0 = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.ActiveCfg = Release|Any CPU + {C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1487,8 +1487,6 @@ Global {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} = {38E6C400-90C0-493E-9266-C1602E229F1B} {D91C3145-C31C-4301-A493-96F94C903CF2} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} {FD35BEFA-512C-4483-B5C0-ADCA35680315} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} - {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22} = {38E6C400-90C0-493E-9266-C1602E229F1B} - {C265268A-F311-4B6A-915E-C1AF9D1EB624} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1} {23582B50-BAEA-4A2D-82A4-345A3C30124E} = {59DA3D5F-9E39-4173-8C31-126967CC189F} {1371F22B-FDD7-43B0-8E7D-E68DA31F07F3} = {38E6C400-90C0-493E-9266-C1602E229F1B} {FF3FD53D-D23E-48AC-98B6-3B028B4DE8AF} = {0D34A7F0-DC77-4789-A136-93089CBD15C3} @@ -1506,6 +1504,8 @@ Global {DA816A33-F164-4456-92DD-A672BAD1A6B1} = {0D34A7F0-DC77-4789-A136-93089CBD15C3} {23633E49-F11A-4D14-899A-E2599C8182CE} = {38E6C400-90C0-493E-9266-C1602E229F1B} {FC4E526A-DBFC-406A-8ED3-64983B67F688} = {E33ADF54-4D35-49B7-BDA6-412587CA39FF} + {FED315CF-7CA2-4653-AD37-BC0C1D7FFD22} = {38E6C400-90C0-493E-9266-C1602E229F1B} + {C265268A-F311-4B6A-915E-C1AF9D1EB624} = {0D34A7F0-DC77-4789-A136-93089CBD15C3} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03} diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index 634003cc8..dfa522010 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -15,17 +15,17 @@ public MetricService(ICallerProvider caller) _caller = caller; } - public async Task> GetMetricNamesAsync(IEnumerable? match = default) + public async Task> GetNamesAsync(IEnumerable? matches = default) { string param = default!; - if (match != null && match.Any(s => !string.IsNullOrEmpty(s))) + if (matches != null && matches.Any(s => !string.IsNullOrEmpty(s))) { - param = string.Join(',', match.Where(s => !string.IsNullOrEmpty(s))); + param = string.Join(',', matches.Where(s => !string.IsNullOrEmpty(s))); } return (await _caller.GetAsync>(NAMES_URI, new { match = param })) ?? default!; } - public async Task>> GetLabelAndValuesAsync(MetricLableValuesRequest query) + public async Task>> GetLabelValuesAsync(LableValuesRequest query) { var data = await _caller.GetByBodyAsync>>>(LABELVALUES_URI, query); if (data == null || !data.ContainsKey(query.Match)) @@ -34,11 +34,11 @@ public async Task>> GetLabelAndValuesAsync(Metri return data[query.Match]; } - public async Task GetMetricValuesAsync(MetricRangeValueRequest query) + public async Task GetValuesAsync(ValuesRequest query) { if (query.Lables != null && !string.IsNullOrEmpty(query.Match)) { - query.Match = $"{query.Match}{{{string.Join(',',query.Lables)}}}"; + query.Match = $"{query.Match}{{{string.Join(',', query.Lables)}}}"; } return (await _caller.GetByBodyAsync(RANGEVALUES_URL, query)) ?? default!; diff --git a/src/BuildingBlocks/MASA.BuildingBlocks b/src/BuildingBlocks/MASA.BuildingBlocks index 59842a18b..1de9a2fba 160000 --- a/src/BuildingBlocks/MASA.BuildingBlocks +++ b/src/BuildingBlocks/MASA.BuildingBlocks @@ -1 +1 @@ -Subproject commit 59842a18b3d1787c9a920906e9927921f4a0010d +Subproject commit 1de9a2fba47e79ca264956edb382b6452aadc4c3 diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs index ecaf2b4d0..b461478e9 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs @@ -12,7 +12,7 @@ public async Task GetFieldsAsyncTest() var callerProvider = new Mock(); var time = DateTime.Now; - var query = new MetricLableValuesRequest + var query = new LableValuesRequest { Match = "up", Start = time.AddMinutes(-15), diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs index fa94e8a8e..90d170fe2 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs @@ -23,9 +23,9 @@ public void Initialize() [DataRow(new string[] { "up", "prometheus_http_requests_total", "prometheus_http_request_duration_seconds_count" })] [DataRow(new string[] { "not_exists", "up" })] [DataRow(new string[] { "not_exists" })] - public async Task GetMetricNamesAsyncTest(IEnumerable match) + public async Task GetNamesAsyncTest(IEnumerable match) { - var result = await _client.MetricService.GetMetricNamesAsync(match); + var result = await _client.MetricService.GetNamesAsync(match); if (match == null) { Assert.IsNotNull(result); @@ -42,11 +42,11 @@ public async Task GetMetricNamesAsyncTest(IEnumerable match) [TestMethod] [DataRow("up", "2022-07-01T09:00:00.000Z", "2022-07-05T22:00:00.000Z")] - public async Task GetLabelAndValuesAsyncTest(string match, string start, string end) + public async Task GetLabelValuesAsyncTest(string match, string start, string end) { DateTime startDateTime = DateTime.Parse(start); DateTime endDateTime = DateTime.Parse(end); - var result = await _client.MetricService.GetLabelAndValuesAsync(new MetricLableValuesRequest + var result = await _client.MetricService.GetLabelValuesAsync(new LableValuesRequest { Match = match, Start = startDateTime, @@ -59,11 +59,11 @@ public async Task GetLabelAndValuesAsyncTest(string match, string start, string [TestMethod] [DataRow("up", null, "2022-07-01T09:00:00.000Z", "2022-07-05T22:00:00.000Z")] - public async Task GetMetricValuesAsyncTest(string match, IEnumerable labels, string start, string end) + public async Task GetValuesAsyncTest(string match, IEnumerable labels, string start, string end) { DateTime startDateTime = DateTime.Parse(start); DateTime endDateTime = DateTime.Parse(end); - var result = await _client.MetricService.GetMetricValuesAsync(new MetricRangeValueRequest + var result = await _client.MetricService.GetValuesAsync(new ValuesRequest { Match = match, Lables = labels, From a4356e7737137ecbe2b64d5e71927d2a80757f1a Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Fri, 15 Jul 2022 15:35:17 +0800 Subject: [PATCH 09/12] feat: fix trace repeat add otlpexporter and code style update --- Directory.Build.props | 4 ++++ .../Extensions/CallerProviderExtensions.cs | 2 +- .../Extensions/ServiceExtensions.cs | 2 +- .../Extensions/StreamExtensions.cs | 7 +++--- .../Masa.Contrib.BasicAbility.Tsc.csproj | 22 +++++++++---------- .../Service/LogService.cs | 6 ++--- .../Service/MetricService.cs | 4 ++-- ...NetCoreInstrumentationOptionsExtensions.cs | 1 + .../Tracing/MasaServiceExtensions.cs | 2 -- src/BuildingBlocks/MASA.BuildingBlocks | 2 +- .../Common.cs | 21 ------------------ .../CallerProviderExtensionsTests.cs | 2 +- .../Service/LogServiceTests.cs | 2 +- .../Service/MetricServiceTests.cs | 4 +--- .../_Imports.cs | 1 + 15 files changed, 31 insertions(+), 51 deletions(-) delete mode 100644 test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs diff --git a/Directory.Build.props b/Directory.Build.props index 57c9fe7df..75266c7a4 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -10,6 +10,10 @@ 7.3.0 1.5.0 1.0.4 + + 1.3.0 + 1.0.0-beta2 + 1.0.0-rc9.3 $(AssemblyName) packageIcon.png diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs index f091b3a5f..386a6e3be 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs @@ -13,6 +13,6 @@ public static async Task GetByBodyAsync(this ICallerProvider c { request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); } - return (await caller.SendAsync(request,default)) ?? default!; + return (await caller.SendAsync(request))!; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs index d0b496224..4195c5521 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/ServiceExtensions.cs @@ -30,7 +30,7 @@ public static IServiceCollection AddTscClient(this IServiceCollection services, var pmCaching = new TscClient(caller); return pmCaching; }); - + return services; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/StreamExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/StreamExtensions.cs index 27da597ef..b8d3a5fc5 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/StreamExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/StreamExtensions.cs @@ -12,12 +12,12 @@ public static class StreamExtensions if (stream == null) return null; - if(!stream.CanRead) + if (!stream.CanRead) return "cann't read"; if (!stream.CanSeek) return "cann't seek"; - + var start = (int)stream.Position; List data = new(); var buffer = new byte[1024]; @@ -34,7 +34,7 @@ public static class StreamExtensions { data.AddRange(buffer[0..count]); break; - } + } } while (true); if (data.Count > 0) @@ -46,4 +46,3 @@ public static class StreamExtensions return null; } } - diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj index 3a178726c..cf843b888 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Masa.Contrib.BasicAbility.Tsc.csproj @@ -7,18 +7,18 @@ - - - - - - - - - - + + + + + + + + + + - + diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs index 567217282..6377d136d 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/LogService.cs @@ -17,16 +17,16 @@ public LogService(ICallerProvider caller) public async Task>> GetAggregationAsync(LogAggregationRequest query) { - return (await _caller.GetAsync>>(AGGREGATION_URI, query)) ?? default!; + return (await _caller.GetAsync>>(AGGREGATION_URI, query))!; } public async Task> GetFieldsAsync() { - return (await _caller.GetAsync>(FIELD_URI)) ?? default!; + return (await _caller.GetAsync>(FIELD_URI))!; } public async Task GetLatestAsync(LogLatestRequest query) { - return (await _caller.GetAsync(LATEST_URI, query)) ?? default!; + return (await _caller.GetAsync(LATEST_URI, query))!; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index dfa522010..137306d83 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -22,7 +22,7 @@ public async Task> GetNamesAsync(IEnumerable? matche { param = string.Join(',', matches.Where(s => !string.IsNullOrEmpty(s))); } - return (await _caller.GetAsync>(NAMES_URI, new { match = param })) ?? default!; + return (await _caller.GetAsync>(NAMES_URI, new { match = param }))!; } public async Task>> GetLabelValuesAsync(LableValuesRequest query) @@ -41,6 +41,6 @@ public async Task GetValuesAsync(ValuesRequest query) query.Match = $"{query.Match}{{{string.Join(',', query.Lables)}}}"; } - return (await _caller.GetByBodyAsync(RANGEVALUES_URL, query)) ?? default!; + return (await _caller.GetByBodyAsync(RANGEVALUES_URL, query))!; } } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/AspNetCoreInstrumentationOptionsExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/AspNetCoreInstrumentationOptionsExtensions.cs index cb829561b..1cdf430d4 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/AspNetCoreInstrumentationOptionsExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/AspNetCoreInstrumentationOptionsExtensions.cs @@ -27,6 +27,7 @@ public static class AspNetCoreInstrumentationOptionsExtensions ".ico", ".png", ".woff", + ".icon" }; /// diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs index 35bf08763..7f80612bc 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs @@ -30,8 +30,6 @@ public static IServiceCollection AddMasaTracing(this IServiceCollection services builder.AddRedisInstrumentation(option.Connection, option.StackExchangeRedisCallsInstrumentationOptions); option?.BuildTraceCallback?.Invoke(builder); - - builder.AddOtlpExporter(); }); return services; diff --git a/src/BuildingBlocks/MASA.BuildingBlocks b/src/BuildingBlocks/MASA.BuildingBlocks index 1de9a2fba..953712d74 160000 --- a/src/BuildingBlocks/MASA.BuildingBlocks +++ b/src/BuildingBlocks/MASA.BuildingBlocks @@ -1 +1 @@ -Subproject commit 1de9a2fba47e79ca264956edb382b6452aadc4c3 +Subproject commit 953712d742dbe6f22730492dca53d8fd773f95b9 diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs deleted file mode 100644 index d59af4c0e..000000000 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) MASA Stack All rights reserved. -// Licensed under the MIT License. See LICENSE.txt in the project root for license information. - -using System.Net.Http; -using System.Text; - -namespace Masa.Contrib.BasicAbility.Tsc.Tests; - -internal class Common -{ - - public static HttpRequestMessage CreateMessage(string url, object body) - { - var request = new HttpRequestMessage(HttpMethod.Get, url); - if (body != null) - { - request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); - } - return request; - } -} diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs index d19f2500b..49564a7b3 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Extensions/CallerProviderExtensionsTests.cs @@ -7,7 +7,7 @@ namespace Masa.Contrib.BasicAbility.Tsc.Tests.Extensions; public class CallerProviderExtensionsTests { [TestMethod] - public async Task GetByBodyAsyncTest() + public async Task SendGetRequestWithBodyParameterAsyncTest() { var callerProvider = new Mock(); string url = "http://locahost:80/test"; diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs index b461478e9..6f76f3562 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs @@ -7,7 +7,7 @@ namespace Masa.Contrib.BasicAbility.Tsc.Tests; public class LogServiceTests { [TestMethod] - public async Task GetFieldsAsyncTest() + public async Task GetMappingFieldsAsyncTest() { var callerProvider = new Mock(); diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs index 90d170fe2..46480170e 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs @@ -1,8 +1,6 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. -using System.Linq; - namespace Masa.Contrib.BasicAbility.Tsc.Tests.Service; [TestClass] @@ -15,7 +13,7 @@ public void Initialize() { IServiceCollection service = new ServiceCollection(); service.AddTscClient("https://localhost:6324/"); - _client = service.BuildServiceProvider().GetService() ?? default!; + _client = service.BuildServiceProvider().GetRequiredService(); } [TestMethod] diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs index d217dca5b..15b778434 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs @@ -11,6 +11,7 @@ global using Moq; global using System; global using System.Collections.Generic; +global using System.Linq; global using System.Net.Http; global using System.Text.Json; global using System.Text.Json.Serialization; From 5b2d585891086c2cbca5c51605f9c13a037b298e Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Fri, 15 Jul 2022 15:40:11 +0800 Subject: [PATCH 10/12] feat: code style update --- .../Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index 137306d83..e1c89b9b5 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -18,9 +18,9 @@ public MetricService(ICallerProvider caller) public async Task> GetNamesAsync(IEnumerable? matches = default) { string param = default!; - if (matches != null && matches.Any(s => !string.IsNullOrEmpty(s))) + if (matches != null && matches.Any()) { - param = string.Join(',', matches.Where(s => !string.IsNullOrEmpty(s))); + param = string.Join(',', matches); } return (await _caller.GetAsync>(NAMES_URI, new { match = param }))!; } From edd811e5ab8a87a3adf9d39a5c0627aa083a12d0 Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Mon, 18 Jul 2022 16:37:41 +0800 Subject: [PATCH 11/12] chore: tests update --- .../Service/MetricService.cs | 2 +- .../Tracing/MasaServiceExtensions.cs | 12 ++--- src/BuildingBlocks/MASA.BuildingBlocks | 2 +- .../Service/LogServiceTests.cs | 4 +- .../Service/MetricServiceTests.cs | 52 +++++++++++-------- .../_Imports.cs | 3 +- 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index e1c89b9b5..b0c67c5ed 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -22,7 +22,7 @@ public async Task> GetNamesAsync(IEnumerable? matche { param = string.Join(',', matches); } - return (await _caller.GetAsync>(NAMES_URI, new { match = param }))!; + return (await _caller.GetAsync>(NAMES_URI, new Dictionary { { "match", param } }))!; } public async Task>> GetLabelValuesAsync(LableValuesRequest query) diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs index 7f80612bc..b752e8606 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Tracing/MasaServiceExtensions.cs @@ -14,22 +14,22 @@ public static IServiceCollection AddMasaTracing(this IServiceCollection services if (configure != null) configure.Invoke(option); - if (option?.AspNetCoreInstrumentationOptions != null) + if (option.AspNetCoreInstrumentationOptions != null) builder.AddAspNetCoreInstrumentation(option.AspNetCoreInstrumentationOptions); - if (option?.HttpClientInstrumentationOptions != null) + if (option.HttpClientInstrumentationOptions != null) builder.AddHttpClientInstrumentation(option.HttpClientInstrumentationOptions); - if (option?.EntityFrameworkInstrumentationOptions != null) + if (option.EntityFrameworkInstrumentationOptions != null) builder.AddEntityFrameworkCoreInstrumentation(option.EntityFrameworkInstrumentationOptions); - if (option?.ElasticsearchClientInstrumentationOptions != null) + if (option.ElasticsearchClientInstrumentationOptions != null) builder.AddElasticsearchClientInstrumentation(option.ElasticsearchClientInstrumentationOptions); - if (option?.StackExchangeRedisCallsInstrumentationOptions != null && option?.Connection != null) + if (option.StackExchangeRedisCallsInstrumentationOptions != null && option.Connection != null) builder.AddRedisInstrumentation(option.Connection, option.StackExchangeRedisCallsInstrumentationOptions); - option?.BuildTraceCallback?.Invoke(builder); + option.BuildTraceCallback?.Invoke(builder); }); return services; diff --git a/src/BuildingBlocks/MASA.BuildingBlocks b/src/BuildingBlocks/MASA.BuildingBlocks index 953712d74..ec0327550 160000 --- a/src/BuildingBlocks/MASA.BuildingBlocks +++ b/src/BuildingBlocks/MASA.BuildingBlocks @@ -1 +1 @@ -Subproject commit 953712d742dbe6f22730492dca53d8fd773f95b9 +Subproject commit ec0327550fd69e5837c0f4fd6a2be761a5e376e5 diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs index 6f76f3562..f21102398 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/LogServiceTests.cs @@ -45,12 +45,12 @@ public async Task GetAggregationAsyncTest() { new FieldAggregationRequest{ Name="container.instance.id", - AggType= LogAggTypes.Count, + AggregationType= AggregationTypes.Count, Alias="count1" }, new FieldAggregationRequest{ Name="container.instance.name", - AggType= LogAggTypes.Count, + AggregationType= AggregationTypes.Count, Alias="count2" } } diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs index 46480170e..e9a227745 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/Service/MetricServiceTests.cs @@ -6,35 +6,32 @@ namespace Masa.Contrib.BasicAbility.Tsc.Tests.Service; [TestClass] public class MetricServiceTests { - private ITscClient _client; - - [TestInitialize] - public void Initialize() - { - IServiceCollection service = new ServiceCollection(); - service.AddTscClient("https://localhost:6324/"); - _client = service.BuildServiceProvider().GetRequiredService(); - } - [TestMethod] [DataRow(null)] [DataRow(new string[] { "up", "prometheus_http_requests_total", "prometheus_http_request_duration_seconds_count" })] - [DataRow(new string[] { "not_exists", "up" })] - [DataRow(new string[] { "not_exists" })] + [DataRow(new string[] { "not_exists_test" })] public async Task GetNamesAsyncTest(IEnumerable match) { - var result = await _client.MetricService.GetNamesAsync(match); - if (match == null) + var data = new string[] { "up", "prometheus_http_requests_total", "prometheus_http_request_duration_seconds_count" }; + var caller = new Mock(); + caller.Setup(provider => provider.GetAsync?>(MetricService.NAMES_URI, It.Is>(dic => dic == null || dic.ContainsKey("match")), default)) + .ReturnsAsync((string? url, Dictionary param, CancellationToken token) => { - Assert.IsNotNull(result); - } - else if (match.Count() > 0) + if (param == null || !param.ContainsKey("match") || param["match"] is null || !param["match"].Contains("not_exists_test")) + return data; + return default; + }).Verifiable(); + var client = new TscClient(caller.Object); + var result = await client.MetricService.GetNamesAsync(match); + + if (match != null && match.Any(s => s == "not_exists_test")) { - Assert.IsNotNull(result); + Assert.IsNull(result); } else { - Assert.IsNull(result); + Assert.IsNotNull(result); + Assert.IsTrue(result.Any()); } } @@ -42,9 +39,18 @@ public async Task GetNamesAsyncTest(IEnumerable match) [DataRow("up", "2022-07-01T09:00:00.000Z", "2022-07-05T22:00:00.000Z")] public async Task GetLabelValuesAsyncTest(string match, string start, string end) { + var caller = new Mock(); + caller.Setup(provider => provider.SendAsync>>>(It.IsNotNull(), default)) + .ReturnsAsync(new Dictionary>> { + {"up",new Dictionary>{ + {"name",new List{"name1","name2"} } + } } + }); + var client = new TscClient(caller.Object); + DateTime startDateTime = DateTime.Parse(start); DateTime endDateTime = DateTime.Parse(end); - var result = await _client.MetricService.GetLabelValuesAsync(new LableValuesRequest + var result = await client.MetricService.GetLabelValuesAsync(new LableValuesRequest { Match = match, Start = startDateTime, @@ -59,9 +65,13 @@ public async Task GetLabelValuesAsyncTest(string match, string start, string end [DataRow("up", null, "2022-07-01T09:00:00.000Z", "2022-07-05T22:00:00.000Z")] public async Task GetValuesAsyncTest(string match, IEnumerable labels, string start, string end) { + var caller = new Mock(); + caller.Setup(provider => provider.SendAsync(It.IsNotNull(), default)).ReturnsAsync("1.0"); + var client = new TscClient(caller.Object); + DateTime startDateTime = DateTime.Parse(start); DateTime endDateTime = DateTime.Parse(end); - var result = await _client.MetricService.GetValuesAsync(new ValuesRequest + var result = await client.MetricService.GetValuesAsync(new ValuesRequest { Match = match, Lables = labels, diff --git a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs index 15b778434..88e3b09ee 100644 --- a/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs +++ b/test/Masa.Contrib.BasicAbility.Tsc.Test/_Imports.cs @@ -1,12 +1,10 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. -global using Masa.BuildingBlocks.BasicAbility.Tsc; global using Masa.BuildingBlocks.BasicAbility.Tsc.Enums; global using Masa.BuildingBlocks.BasicAbility.Tsc.Model; global using Masa.Contrib.BasicAbility.Tsc.Service; global using Masa.Utils.Caller.Core; -global using Microsoft.Extensions.DependencyInjection; global using Microsoft.VisualStudio.TestTools.UnitTesting; global using Moq; global using System; @@ -15,4 +13,5 @@ global using System.Net.Http; global using System.Text.Json; global using System.Text.Json.Serialization; +global using System.Threading; global using System.Threading.Tasks; From 2f96007172e58708afc25d6c9511403ee615d895 Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Tue, 19 Jul 2022 10:53:14 +0800 Subject: [PATCH 12/12] feat: change value can be null --- .../Extensions/CallerProviderExtensions.cs | 4 ++-- .../Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs index 386a6e3be..ea44b36bd 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Extensions/CallerProviderExtensions.cs @@ -6,10 +6,10 @@ namespace Masa.Utils.Caller.Core; internal static class CallerProviderExtensions { - public static async Task GetByBodyAsync(this ICallerProvider caller, string url, object body) where TResult : class + public static async Task GetByBodyAsync(this ICallerProvider caller, string url, object? body) where TResult : class { var request = new HttpRequestMessage(HttpMethod.Get, url); - if (body != null) + if (body is not null) { request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); } diff --git a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs index b0c67c5ed..5bbc83185 100644 --- a/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs +++ b/src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/Service/MetricService.cs @@ -29,7 +29,7 @@ public async Task>> GetLabelValuesAsync(LableVal { var data = await _caller.GetByBodyAsync>>>(LABELVALUES_URI, query); if (data == null || !data.ContainsKey(query.Match)) - return default!; + return new Dictionary>(); return data[query.Match]; }