From 66a042abfa679a67aa8c448cb74ac0d8b11b8096 Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Tue, 24 Dec 2024 14:03:12 +0800 Subject: [PATCH] feat: add blazor server route match --- .gitignore | 3 +- Directory.Build.props | 21 +++-- .../Model/ThirdPartyIdpModel.cs | 12 +-- .../Models/DbModel.cs | 8 +- .../Models/ElasticModel.cs | 8 +- .../Models/RedisModel.cs | 4 +- .../GetSchedulerJobByIdentityRequest.cs | 4 +- .../Masa.Contrib.StackSdks.Auth/AuthClient.cs | 2 +- .../ServiceCollectionExtensions.cs | 2 +- .../ServiceCollectionExtensions.cs | 28 +++---- .../EnvironmentModel.cs | 2 +- .../MasaConfigurationBuilderExtensions.cs | 2 +- .../MasaConfigurationIsolationProvider.cs | 2 +- .../Blazor/BlazorRouteData.cs | 33 ++++++++ .../Blazor/BlazorRouteManager.cs | 82 +++++++++++++++++++ .../Blazor/BlazorRouteParamKey.cs | 12 +++ .../Extensions/ServiceExtensions.cs | 8 +- .../Middleware/HttpResponseMiddleware.cs | 2 +- .../Options/MasaObservableOptions.cs | 12 +-- .../AspNetCoreInstrumentationHandler.cs | 15 ++++ .../OpenTelemetryInstrumentationOptions.cs | 8 +- .../_Imports.cs | 4 +- 22 files changed, 216 insertions(+), 58 deletions(-) create mode 100644 src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteData.cs create mode 100644 src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteManager.cs create mode 100644 src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteParamKey.cs diff --git a/.gitignore b/.gitignore index 211acf2..149bdef 100644 --- a/.gitignore +++ b/.gitignore @@ -224,4 +224,5 @@ coverage.opencover.xml *.pdb # Run Tests -BenchmarkDotNet.Artifacts/ \ No newline at end of file +BenchmarkDotNet.Artifacts/ +TestResults/ \ No newline at end of file diff --git a/Directory.Build.props b/Directory.Build.props index b8a20b6..6e77fa2 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,20 +1,29 @@ - net6.0; + net6.0;net7.0;net8.0; 6.0.0 7.0.0 8.0.0 - 1.2.0-preview.5 + 2.2.3 + 2.2.3 + 3.6.2 + + 16.9.4 + 16.9.4 + 17.12.0 + + 3.0.2 + 3.0.2 + 3.1.2 + + 1.2.0-preview.5 1.9.0 1.9.0 - 11.1.0 6.0.0 + 7.17.4 4.16.1 - 2.2.3 - 3.0.2 - 16.9.4 diff --git a/src/BuildingBlocks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/ThirdPartyIdpModel.cs b/src/BuildingBlocks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/ThirdPartyIdpModel.cs index 315bdd1..bc2e6a2 100644 --- a/src/BuildingBlocks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/ThirdPartyIdpModel.cs +++ b/src/BuildingBlocks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/ThirdPartyIdpModel.cs @@ -5,19 +5,19 @@ namespace Masa.BuildingBlocks.StackSdks.Auth.Contracts.Model; public class ThirdPartyIdpModel { - public string ClientId { get; set; } + public string ClientId { get; set; } = default!; - public string ClientSecret { get; set; } + public string ClientSecret { get; set; } = default!; - public string Url { get; set; } + public string Url { get; set; } = default!; public AuthenticationTypes VerifyType { get; set; } - public string Name { get; set; } + public string Name { get; set; } = default!; - public string DisplayName { get; set; } + public string DisplayName { get; set; } = default!; - public string Icon { get; set; } + public string Icon { get; set; } = default!; public ThirdPartyIdpTypes ThirdPartyIdpType { get; set; } diff --git a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/DbModel.cs b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/DbModel.cs index ca8d3e8..23b4d96 100644 --- a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/DbModel.cs +++ b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/DbModel.cs @@ -5,15 +5,15 @@ namespace Masa.BuildingBlocks.StackSdks.Config.Models; public class DbModel { - public string Server { get; set; } + public string Server { get; set; } = default!; public int Port { get; set; } = 1433; - public string Database { get; set; } + public string Database { get; set; } = default!; - public string UserId { get; set; } + public string UserId { get; set; } = default!; - public string Password { get; set; } + public string Password { get; set; } = default!; public string? DbType { get; set; } diff --git a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/ElasticModel.cs b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/ElasticModel.cs index a5fdbbe..318c346 100644 --- a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/ElasticModel.cs +++ b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/ElasticModel.cs @@ -5,11 +5,11 @@ namespace Masa.BuildingBlocks.StackSdks.Config.Models; public class ElasticModel { - public List Nodes { get; set; } + public List Nodes { get; set; } = default!; - public string Index { get; set; } + public string Index { get; set; } = default!; - public string UserName { get; set; } + public string UserName { get; set; } = default!; - public string Password { get; set; } + public string Password { get; set; } = default!; } diff --git a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/RedisModel.cs b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/RedisModel.cs index a076fce..660be5f 100644 --- a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/RedisModel.cs +++ b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Config/Models/RedisModel.cs @@ -5,11 +5,11 @@ namespace Masa.BuildingBlocks.StackSdks.Config.Models; public class RedisModel { - public string RedisHost { get; set; } + public string RedisHost { get; set; } = default!; public int RedisPort { get; set; } public int RedisDb { get; set; } - public string RedisPassword { get; set; } + public string RedisPassword { get; set; } = default!; } diff --git a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Scheduler/Request/GetSchedulerJobByIdentityRequest.cs b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Scheduler/Request/GetSchedulerJobByIdentityRequest.cs index 2381ff1..2be38eb 100644 --- a/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Scheduler/Request/GetSchedulerJobByIdentityRequest.cs +++ b/src/BuildingBlocks/Masa.BuildingBlocks.StackSdks.Scheduler/Request/GetSchedulerJobByIdentityRequest.cs @@ -5,7 +5,7 @@ namespace Masa.BuildingBlocks.StackSdks.Scheduler.Request; public class GetSchedulerJobByIdentityRequest { - public string JobIdentity { get; set; } + public string JobIdentity { get; set; } = default!; - public string ProjectIdentity { get; set; } + public string ProjectIdentity { get; set; } = default!; } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Auth/AuthClient.cs b/src/Contrib/Masa.Contrib.StackSdks.Auth/AuthClient.cs index ef28a49..1cb2ca1 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Auth/AuthClient.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Auth/AuthClient.cs @@ -30,6 +30,6 @@ public AuthClient(ICaller caller, IMultiEnvironmentUserContext userContext, IMul public IThirdPartyIdpService ThirdPartyIdpService { get; } - public IOssService OssService { get; } + public IOssService OssService { get; } = default!; } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs b/src/Contrib/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs index 894c535..62c8523 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs @@ -12,7 +12,7 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services, { var redisOptions = configuration.GetSection("$public.RedisConfig").Get(); authServiceBaseAddress ??= configuration.GetValue("$public.AppSettings:AuthClient:Url"); - services.AddAuthClient(authServiceBaseAddress, redisOptions); + services.AddAuthClient(authServiceBaseAddress!, redisOptions!); return services; } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs b/src/Contrib/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs index 73d1c64..11ed175 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs @@ -93,25 +93,25 @@ private static Dictionary GetConfigMap(IServiceCollection servic var serviceProvider = services.BuildServiceProvider(); var configuration = serviceProvider.GetRequiredService(); - string environment = configuration.GetValue(MasaStackConfigConstant.ENVIRONMENT); - environment = string.IsNullOrWhiteSpace(environment) ? configuration["ASPNETCORE_ENVIRONMENT"] : environment; + string environment = configuration.GetValue(MasaStackConfigConstant.ENVIRONMENT)!; + environment = string.IsNullOrWhiteSpace(environment) ? configuration["ASPNETCORE_ENVIRONMENT"]! : environment; var configs = new Dictionary() { - { MasaStackConfigConstant.VERSION, configuration.GetValue(MasaStackConfigConstant.VERSION) }, + { MasaStackConfigConstant.VERSION, configuration.GetValue(MasaStackConfigConstant.VERSION)! }, { MasaStackConfigConstant.IS_DEMO, configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString() }, - { MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue(MasaStackConfigConstant.DOMAIN_NAME) }, - { MasaStackConfigConstant.NAMESPACE, configuration.GetValue(MasaStackConfigConstant.NAMESPACE) }, - { MasaStackConfigConstant.CLUSTER, configuration.GetValue(MasaStackConfigConstant.CLUSTER) }, - { MasaStackConfigConstant.OTLP_URL, configuration.GetValue(MasaStackConfigConstant.OTLP_URL) }, - { MasaStackConfigConstant.REDIS, configuration.GetValue(MasaStackConfigConstant.REDIS) }, - { MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue(MasaStackConfigConstant.CONNECTIONSTRING) }, - { MasaStackConfigConstant.MASA_STACK, configuration.GetValue(MasaStackConfigConstant.MASA_STACK) }, - { MasaStackConfigConstant.ELASTIC, configuration.GetValue(MasaStackConfigConstant.ELASTIC) }, + { MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue(MasaStackConfigConstant.DOMAIN_NAME)! }, + { MasaStackConfigConstant.NAMESPACE, configuration.GetValue(MasaStackConfigConstant.NAMESPACE)! }, + { MasaStackConfigConstant.CLUSTER, configuration.GetValue(MasaStackConfigConstant.CLUSTER)! }, + { MasaStackConfigConstant.OTLP_URL, configuration.GetValue < string >(MasaStackConfigConstant.OTLP_URL)! }, + { MasaStackConfigConstant.REDIS, configuration.GetValue(MasaStackConfigConstant.REDIS)! }, + { MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue(MasaStackConfigConstant.CONNECTIONSTRING)! }, + { MasaStackConfigConstant.MASA_STACK, configuration.GetValue(MasaStackConfigConstant.MASA_STACK)! }, + { MasaStackConfigConstant.ELASTIC, configuration.GetValue(MasaStackConfigConstant.ELASTIC)! }, { MasaStackConfigConstant.ENVIRONMENT, environment }, - { MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue(MasaStackConfigConstant.ADMIN_PWD) }, - { MasaStackConfigConstant.DCC_SECRET, configuration.GetValue(MasaStackConfigConstant.DCC_SECRET) }, - { MasaStackConfigConstant.SUFFIX_IDENTITY, configuration.GetValue(MasaStackConfigConstant.SUFFIX_IDENTITY) } + { MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue(MasaStackConfigConstant.ADMIN_PWD)! }, + { MasaStackConfigConstant.DCC_SECRET, configuration.GetValue(MasaStackConfigConstant.DCC_SECRET)! }, + { MasaStackConfigConstant.SUFFIX_IDENTITY, configuration.GetValue(MasaStackConfigConstant.SUFFIX_IDENTITY)! } }; return configs; diff --git a/src/Contrib/Masa.Contrib.StackSdks.Isolation/EnvironmentModel.cs b/src/Contrib/Masa.Contrib.StackSdks.Isolation/EnvironmentModel.cs index 2b4b3b4..6531e9c 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Isolation/EnvironmentModel.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Isolation/EnvironmentModel.cs @@ -5,5 +5,5 @@ namespace Masa.Contrib.StackSdks.Isolation; internal class EnvironmentModel : IEnvironmentModel { - public string Environment { get; set; } + public string Environment { get; set; } = default!; } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationBuilderExtensions.cs b/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationBuilderExtensions.cs index 2e2fc3d..32d14ce 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationBuilderExtensions.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationBuilderExtensions.cs @@ -18,7 +18,7 @@ public static IMasaConfigurationBuilder UseDccIsolation(this IMasaConfigurationB configurationApiClient, loggerFactory)); var configurationBuilder = serviceProvider.GetService() as IConfigurationBuilder; - configurationBuilder.AddConfiguration(builder.Add(source).Build()); + configurationBuilder!.AddConfiguration(builder.Add(source).Build()); return builder; } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationIsolationProvider.cs b/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationIsolationProvider.cs index 2553bab..7932dd6 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationIsolationProvider.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Isolation/MasaConfigurationIsolationProvider.cs @@ -36,7 +36,7 @@ void SetData() { Data = _data.SelectMany(kv => kv.Value.GetPropertyNames().Select(key => - new KeyValuePair($"{kv.Key}{ConfigurationPath.KeyDelimiter}{key}", kv.Value.GetProperty(key)!) + new KeyValuePair($"{kv.Key}{ConfigurationPath.KeyDelimiter}{key}", kv.Value.GetProperty(key)!) ) ).ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase); } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteData.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteData.cs new file mode 100644 index 0000000..563d4a1 --- /dev/null +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteData.cs @@ -0,0 +1,33 @@ +namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Blazor; + +internal class BlazorRouteData +{ + public string Template { get; set; } = default!; + + public Type Type { get; set; } = default!; + + public List RouteKeies { get; set; } = new(); + + public int MinCount => RouteKeies.Count(k => !k.CanNull); + + public int MaxCount => RouteKeies.Count; + + public bool IsMatch(string url) + { + if (string.IsNullOrEmpty(url)) + return false; + var paths = url[1..].Split('/'); + if (!(MinCount - paths.Length == 0 || MaxCount - paths.Length == 0)) + return false; + var index = 0; + foreach (var param in paths) + { + if (RouteKeies[index].IsParamter) + continue; + if (!string.Equals(RouteKeies[index].Value, param, StringComparison.CurrentCultureIgnoreCase)) + return false; + index++; + } + return true; + } +} \ No newline at end of file diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteManager.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteManager.cs new file mode 100644 index 0000000..de3a125 --- /dev/null +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteManager.cs @@ -0,0 +1,82 @@ +namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Blazor; + +public sealed class BlazorRouteManager +{ + private BlazorRouteManager() { } + + private static Dictionary> _routes = new(); + + internal static bool EnableBlazorRoute { get; private set; } + + public static void InitRoute(params Assembly[] assemblies) + { + if (assemblies == null || assemblies.Length == 0) + throw new ArgumentNullException(nameof(assemblies)); + + EnableBlazorRoute = true; + foreach (var assembly in assemblies) + { + InitRoute(assembly); + } + } + + private static void InitRoute(Assembly assembly) + { + if (assembly == null) + return; + + var typeRoutes = assembly.GetTypes().Select(type => new { type, routes = type.GetCustomAttributes().ToList() }).Where(item => item.routes.Count > 0).ToList(); + + foreach (var item in typeRoutes) + { + if (_routes.ContainsKey(item.type)) + continue; + var routeDatas = item.routes.Select(route => + { + var values = (route.Template.StartsWith('/') ? route.Template[1..] : route.Template).Split('/'); + return new BlazorRouteData + { + Template = route.Template, + Type = item.type, + RouteKeies = values.Select((value, index) => new BlazorRouteParamKey + { + Value = value, + Index = index, + IsParamter = IsParamter(value), + CanNull = CanNull(value, index == values.Length - 1) + } + ).ToList() + }; + }).ToList(); + _routes.Add(item.type, routeDatas); + } + } + + internal static bool TryGetUrlRoute(string url, out BlazorRouteData? route) + { + route = default; + foreach (var type in _routes.Keys) + { + var routes = _routes[type]; + var matches = routes.Where(route => route.IsMatch(url)).OrderByDescending(item => item.MaxCount); + if (matches.Any()) + { + route = matches.First(); + return true; + } + } + return false; + } + + private static bool IsParamter(string value) + { + if (string.IsNullOrEmpty(value)) + throw new ArgumentNullException(nameof(value)); + return Regex.IsMatch(value, @"^\{\w+:?\w+\??\}$"); + } + + private static bool CanNull(string value, bool isLast) + { + return isLast && value.Contains('?'); + } +} \ No newline at end of file diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteParamKey.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteParamKey.cs new file mode 100644 index 0000000..5da17fc --- /dev/null +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Blazor/BlazorRouteParamKey.cs @@ -0,0 +1,12 @@ +namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Blazor; + +internal class BlazorRouteParamKey +{ + public string Value { get; set; } = default!; + + public int Index { get; set; } + + public bool IsParamter { get; set; } + + public bool CanNull { get; set; } +} diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Extensions/ServiceExtensions.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Extensions/ServiceExtensions.cs index 2bae6f8..665f663 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Extensions/ServiceExtensions.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Extensions/ServiceExtensions.cs @@ -42,7 +42,8 @@ public static IServiceCollection AddObservable(this IServiceCollection services, string? otlpUrl = null, bool isBlazor = false, bool isInterruptSignalRTracing = true, - IEnumerable? activitySources = default) + IEnumerable? activitySources = default, + IEnumerable? blazorRouteAssemblies = default) { ArgumentNullException.ThrowIfNull(option); @@ -69,7 +70,10 @@ public static IServiceCollection AddObservable(this IServiceCollection services, builder.SetResourceBuilder(resources); builder.AddOtlpExporter(otlp => otlp.Endpoint = uri!); }); - + if (blazorRouteAssemblies != null && blazorRouteAssemblies.Any()) + { + BlazorRouteManager.InitRoute(blazorRouteAssemblies.ToArray()); + } return services; } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Middleware/HttpResponseMiddleware.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Middleware/HttpResponseMiddleware.cs index f57cb32..6c371fa 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Middleware/HttpResponseMiddleware.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Middleware/HttpResponseMiddleware.cs @@ -31,7 +31,7 @@ public async Task InvokeAsync(HttpContext httpContext) } else { - OpenTelemetryInstrumentationOptions.Logger.LogInformation("response length: {Length}, context: {Context}", responseResult.Length, responseResult); + OpenTelemetryInstrumentationOptions.Logger?.LogInformation("response length: {Length}, context: {Context}", responseResult.Length, responseResult); } } } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Options/MasaObservableOptions.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Options/MasaObservableOptions.cs index 68952f8..2734e90 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Options/MasaObservableOptions.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Options/MasaObservableOptions.cs @@ -5,15 +5,15 @@ namespace Masa.Contrib.StackSdks.Tsc; public class MasaObservableOptions { - public string ProjectName { get; set; } + public string ProjectName { get; set; } = default!; - public string ServiceName { get; set; } + public string ServiceName { get; set; } = default!; - public string ServiceNameSpace { get; set; } + public string ServiceNameSpace { get; set; } = default!; - public string ServiceInstanceId { get; set; } + public string ServiceInstanceId { get; set; } = default!; - public string ServiceVersion { get; set; } + public string ServiceVersion { get; set; } = default!; - public string Layer { get; set; } + public string Layer { get; set; } = default!; } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/Handler/AspNetCoreInstrumentationHandler.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/Handler/AspNetCoreInstrumentationHandler.cs index fafa5b0..579a116 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/Handler/AspNetCoreInstrumentationHandler.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/Handler/AspNetCoreInstrumentationHandler.cs @@ -1,6 +1,8 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +using Microsoft.AspNetCore.Http; + namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Tracing.Handler; internal class AspNetCoreInstrumentationHandler : ExceptionHandler @@ -8,12 +10,14 @@ internal class AspNetCoreInstrumentationHandler : ExceptionHandler public virtual void OnHttpRequest(Activity activity, HttpRequest httpRequest) { AddMasaHttpRequest(activity, httpRequest); + AddBlazorServerRoute(activity, httpRequest); HttpMetricProviders.AddHttpRequestMetric(httpRequest); } public virtual void OnHttpResponse(Activity activity, HttpResponse httpResponse) { AddMasaHttpResponse(activity, httpResponse); + HttpMetricProviders.AddHttpResponseMetric(httpResponse); } @@ -53,4 +57,15 @@ public static void AddMasaHttpResponse(Activity activity, HttpResponse httpRespo } activity.SetStatus(GetStatusResult(httpResponse.StatusCode)); } + + public static void AddBlazorServerRoute(Activity activity, HttpRequest httpRequest) + { + if (!BlazorFilterExtenistion.IsBlazorFilter(httpRequest.HttpContext)) + return; + if (BlazorRouteManager.EnableBlazorRoute && BlazorRouteManager.TryGetUrlRoute(httpRequest.Path, out var route)) + { + activity?.SetTag("http.route", route!.Template); + activity?.SetTag("blazor.server.type", route!.Type); + } + } } diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/OpenTelemetryInstrumentationOptions.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/OpenTelemetryInstrumentationOptions.cs index 41b1f10..0f58d5b 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/OpenTelemetryInstrumentationOptions.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Traceing/OpenTelemetryInstrumentationOptions.cs @@ -17,7 +17,7 @@ public OpenTelemetryInstrumentationOptions(IServiceProvider serviceProvider) private readonly static AspNetCoreInstrumentationHandler aspNetCoreInstrumentationHandler = new(); private readonly static HttpClientInstrumentHandler httpClientInstrumentHandler = new(); - internal static ILogger Logger { get; private set; } + internal static ILogger? Logger { get; private set; } internal static long MaxBodySize { get; private set; } = 200 * 1 << 10; /// @@ -55,14 +55,14 @@ public OpenTelemetryInstrumentationOptions(IServiceProvider serviceProvider) options.ParseAndFormatRequest = true; }; - public Func ConnectionMultiplexerOptions { get; set; } + public Func? ConnectionMultiplexerOptions { get; set; } - public Action StackExchangeRedisInstrumentationOptions { get; set; } + public Action? StackExchangeRedisInstrumentationOptions { get; set; } /// /// Build trace callback, allow to supplement the build process /// - public Action BuildTraceCallback { get; set; } + public Action? BuildTraceCallback { get; set; } public static void SetMaxBodySize(string maxValue) { diff --git a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/_Imports.cs b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/_Imports.cs index 3f067ae..da49d18 100644 --- a/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/_Imports.cs +++ b/src/Contrib/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/_Imports.cs @@ -4,10 +4,12 @@ global using Masa.BuildingBlocks.StackSdks.Config.Consts; global using Masa.Contrib.StackSdks.Tsc; global using Masa.Contrib.StackSdks.Tsc.OpenTelemetry; +global using Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Blazor; global using Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Metric.Instrumentation.Http; global using Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Traceing.Filters; global using Masa.Contrib.StackSdks.Tsc.OpenTelemetry.Tracing.Handler; global using Microsoft.AspNetCore.Builder; +global using Microsoft.AspNetCore.Components; global using Microsoft.AspNetCore.Http; global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; @@ -38,4 +40,4 @@ global using System.Security.Claims; global using System.Text; global using System.Text.RegularExpressions; -global using System.Threading.Tasks; \ No newline at end of file +global using System.Threading.Tasks;