Skip to content

Commit 93951b9

Browse files
committed
Merge branch 'main' of https://github.com/masastack/MASA.Framework into feature/i18n
2 parents 85f6841 + fa84220 commit 93951b9

File tree

31 files changed

+649
-160
lines changed

31 files changed

+649
-160
lines changed

src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdk.Tsc/Model/Metric/LableValuesRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Masa.BuildingBlocks.StackSdks.Tsc.Model;
55

66
public class LableValuesRequest
77
{
8-
private string[] _match = new string[1];
8+
private readonly string[] _match = new string[1];
99

1010
/// <summary>
1111
/// it for the parameter `match` whitch is IEnumerable<string>

src/Contrib/Data/Orm/EFCore/Masa.Contrib.Data.EFCore/Filters/SaveChangeFilter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public void OnExecuting(ChangeTracker changeTracker)
3131
{
3232
if (entity.State == EntityState.Added)
3333
{
34-
entity.CurrentValues[nameof(IAuditEntity<TUserId>.CreationTime)] =
35-
DateTime.UtcNow; //The current time to change to localization after waiting for localization
34+
entity.CurrentValues[nameof(IAuditEntity<TUserId>.Creator)] = userId;
3635
}
3736
else
3837
{

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/Extensions/ServiceExtensions.cs

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public static class ServiceExtensions
99
{
1010
private const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.tsc";
1111

12-
public static IServiceCollection AddTscClient(this IServiceCollection services, string tscServiceBaseUri)
12+
public static IServiceCollection AddTscClient(this IServiceCollection services, string tscServiceBaseUrl)
1313
{
14-
ArgumentNullException.ThrowIfNull(tscServiceBaseUri, nameof(tscServiceBaseUri));
14+
ArgumentNullException.ThrowIfNull(tscServiceBaseUrl);
1515

1616
if (services.Any(service => service.ServiceType == typeof(ITscClient)))
1717
return services;
@@ -20,7 +20,7 @@ public static IServiceCollection AddTscClient(this IServiceCollection services,
2020
{
2121
builder.UseHttpClient(DEFAULT_CLIENT_NAME, options =>
2222
{
23-
options.BaseAddress = tscServiceBaseUri;
23+
options.BaseAddress = tscServiceBaseUrl;
2424
});
2525
builder.DisableAutoRegistration = true;
2626
});
@@ -35,4 +35,82 @@ public static IServiceCollection AddTscClient(this IServiceCollection services,
3535
MasaApp.TrySetServiceCollection(services);
3636
return services;
3737
}
38+
39+
public static IServiceCollection AddObservable(this IServiceCollection services,
40+
ILoggingBuilder loggingBuilder,
41+
IConfiguration configuration,
42+
bool isBlazor = false,
43+
bool isInterruptSignalRTracing = true)
44+
{
45+
return services.AddObservable(loggingBuilder,
46+
configuration.GetSection("Masa:Observable").Get<MasaObservableOptions>(),
47+
configuration.GetSection("Masa:Observable:OtlpUrl").Get<string>(),
48+
isBlazor,
49+
isInterruptSignalRTracing);
50+
}
51+
52+
public static IServiceCollection AddObservable(this IServiceCollection services,
53+
ILoggingBuilder loggingBuilder,
54+
Func<MasaObservableOptions> optionsConfigure,
55+
Func<string>? otlpUrlConfigure = null,
56+
bool isBlazor = false,
57+
bool isInterruptSignalRTracing = true)
58+
{
59+
ArgumentNullException.ThrowIfNull(optionsConfigure);
60+
var options = optionsConfigure();
61+
var otlpUrl = otlpUrlConfigure?.Invoke() ?? string.Empty;
62+
return services.AddObservable(loggingBuilder, options, otlpUrl, isBlazor, isInterruptSignalRTracing);
63+
}
64+
65+
public static IServiceCollection AddObservable(this IServiceCollection services,
66+
ILoggingBuilder loggingBuilder,
67+
MasaObservableOptions option,
68+
string? otlpUrl = null,
69+
bool isBlazor = false,
70+
bool isInterruptSignalRTracing = true)
71+
{
72+
ArgumentNullException.ThrowIfNull(option);
73+
var resources = ResourceBuilder.CreateDefault().AddMasaService(option);
74+
Uri? uri = null;
75+
if (!string.IsNullOrEmpty(otlpUrl) && !Uri.TryCreate(otlpUrl, UriKind.Absolute, out uri))
76+
throw new UriFormatException($"{nameof(otlpUrl)}:{otlpUrl} is invalid url");
77+
78+
loggingBuilder.AddMasaOpenTelemetry(builder =>
79+
{
80+
builder.SetResourceBuilder(resources);
81+
builder.AddOtlpExporter(options =>
82+
{
83+
if (uri != null)
84+
options.Endpoint = uri;
85+
});
86+
});
87+
88+
services.AddMasaMetrics(builder =>
89+
{
90+
builder.SetResourceBuilder(resources);
91+
builder.AddOtlpExporter(options =>
92+
{
93+
if (uri != null)
94+
options.Endpoint = uri;
95+
});
96+
});
97+
98+
return services.AddMasaTracing(builder =>
99+
{
100+
builder.AspNetCoreInstrumentationOptions.AppendDefaultFilter(builder, isInterruptSignalRTracing);
101+
102+
if (isBlazor)
103+
builder.AspNetCoreInstrumentationOptions.AppendBlazorFilter(builder);
104+
105+
builder.BuildTraceCallback = options =>
106+
{
107+
options.SetResourceBuilder(resources);
108+
options.AddOtlpExporter(options =>
109+
{
110+
if (uri != null)
111+
options.Endpoint = uri;
112+
});
113+
};
114+
});
115+
}
38116
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/Extensions/StreamExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class StreamExtensions
88
{
99
private static readonly Encoding _defaultEncoding = Encoding.UTF8;
1010

11-
public static async Task<string?> ReadAsStringAsync(this Stream stream, Encoding? encoding = null)
11+
public static async Task<string?> ReadAsStringAsync(this Stream stream, Encoding? encoding = null,int bufferSize=1024)
1212
{
1313
if (stream == null)
1414
return null;
@@ -21,7 +21,7 @@ public static class StreamExtensions
2121

2222
var start = (int)stream.Position;
2323
List<byte> data = new();
24-
var buffer = new byte[1024];
24+
var buffer = new byte[bufferSize];
2525
do
2626
{
2727
var count = await stream.ReadAsync(buffer, 0, buffer.Length);

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/Logging/OpenTelemetryLoggingExtensions.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ public static class OpenTelemetryLoggingExtensions
77
{
88
public static ILoggingBuilder AddMasaOpenTelemetry(this ILoggingBuilder builder, Action<OpenTelemetryLoggerOptions> configure)
99
{
10-
builder.AddOpenTelemetry(options =>
11-
{
12-
options.IncludeScopes = true;
13-
options.IncludeFormattedMessage = true;
14-
options.ParseStateValues = true;
15-
if (configure != null)
16-
configure.Invoke(options);
17-
});
18-
19-
return builder;
10+
return builder.AddOpenTelemetry(options =>
11+
{
12+
options.IncludeScopes = true;
13+
options.IncludeFormattedMessage = true;
14+
options.ParseStateValues = true;
15+
if (configure != null)
16+
configure.Invoke(options);
17+
});
2018
}
2119
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/OpenTelemetryAttributeName.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace Masa.Contrib.StackSdks.Tsc;
77
/// Constants for semantic attribute names outlined by the OpenTelemetry specifications.
88
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/README.md"/>.
99
/// </summary>
10-
internal sealed class OpenTelemetryAttributeName
10+
internal static class OpenTelemetryAttributeName
1111
{
1212
/// <summary>
1313
/// Constants for deployment semantic attribute names outlined by the OpenTelemetry specifications.
1414
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/11cc73939a32e3a2e6f11bdeab843c61cf8594e9/specification/resource/semantic_conventions/deployment_environment.md"/>.
1515
/// </summary>
16-
internal class Deployment
16+
internal static class Deployment
1717
{
1818
/// <summary>
1919
/// The name of the deployment environment (aka deployment tier).
@@ -26,7 +26,7 @@ internal class Deployment
2626
/// Constants for end user semantic attribute names outlined by the OpenTelemetry specifications.
2727
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/span-general.md"/>.
2828
/// </summary>
29-
internal class EndUser
29+
internal static class EndUser
3030
{
3131
/// <summary>
3232
/// Username or client_id extracted from the access token or Authorization header in the inbound request from outside the system.
@@ -57,7 +57,7 @@ internal class EndUser
5757
/// Constants for HTTP semantic attribute names outlined by the OpenTelemetry specifications.
5858
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md"/>.
5959
/// </summary>
60-
internal class Http
60+
internal static class Http
6161
{
6262
/// <summary>
6363
/// The URI scheme identifying the used protocol.
@@ -134,7 +134,7 @@ internal static class Host
134134
/// Constants for service semantic attribute names outlined by the OpenTelemetry specifications.
135135
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md"/>.
136136
/// </summary>
137-
internal class Service
137+
internal static class Service
138138
{
139139
public const string NAME = "service.name";
140140

@@ -143,4 +143,13 @@ internal class Service
143143
/// </summary>
144144
public const string PROJECT_NAME = "service.project.name";
145145
}
146+
147+
internal static class Exception
148+
{
149+
public const string TYPE = "exception.type";
150+
151+
public const string MESSAGE = "exception.message";
152+
153+
public const string STACKTRACE = "exception.stacktrace";
154+
}
146155
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/OpenTemetry/ResourceBuilderExtenstions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static ResourceBuilder AddMasaService(
99
this ResourceBuilder resourceBuilder,
1010
MasaObservableOptions options, Action<ResourceBuilder>? action = null)
1111
{
12-
ArgumentNullException.ThrowIfNull(options, nameof(options));
12+
ArgumentNullException.ThrowIfNull(options);
1313

1414
resourceBuilder = resourceBuilder.AddService(options.ServiceName, options.ServiceNameSpace, options.ServiceVersion, true, options.ServiceInstanceId);
1515

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/Service/LogService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Masa.Contrib.StackSdks.Tsc.Service;
66
public class LogService : ILogService
77
{
88
private readonly ICaller _caller;
9-
internal const string AGGREGATION_URI = "/api/log/aggregation";
9+
internal const string AGGREGATION_URI = "/api/log/aggregate";
1010
internal const string LATEST_URI = "/api/log/latest";
11-
internal const string FIELD_URI = "/api/log/field";
11+
internal const string FIELD_URI = "/api/log/mapping";
1212

1313
public LogService(ICaller caller)
1414
{

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/Service/MetricService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public MetricService(ICaller caller)
1515
_caller = caller;
1616
}
1717

18-
public async Task<IEnumerable<string>> GetNamesAsync(IEnumerable<string>? matches = default)
18+
public async Task<IEnumerable<string>> GetNamesAsync(IEnumerable<string>? match = null)
1919
{
2020
string param = default!;
21-
if (matches != null && matches.Any())
21+
if (match != null && match.Any())
2222
{
23-
param = string.Join(',', matches);
23+
param = string.Join(',', match);
2424
}
2525
return (await _caller.GetAsync<IEnumerable<string>>(NAMES_URI, new Dictionary<string, string> { { "match", param } }))!;
2626
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc/Tracing/ActivityExtensions.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ namespace System.Diagnostics;
55

66
public static class ActivityExtension
77
{
8-
public static async Task<Activity?> AddMasaSupplement(this Activity activity, HttpRequest httpRequest)
8+
public static async Task<Activity> AddMasaSupplement(this Activity activity, HttpRequest httpRequest)
99
{
10-
if (activity is null) return null;
11-
1210
activity.SetTag(OpenTelemetryAttributeName.Http.FLAVOR, httpRequest.Protocol);
1311
activity.SetTag(OpenTelemetryAttributeName.Http.SCHEME, httpRequest.Scheme);
14-
activity.SetTag(OpenTelemetryAttributeName.Http.CLIENT_IP, httpRequest.HttpContext?.Connection.RemoteIpAddress);
12+
activity.SetTag(OpenTelemetryAttributeName.Http.CLIENT_IP, httpRequest.HttpContext?.Connection?.RemoteIpAddress);
1513
activity.SetTag(OpenTelemetryAttributeName.Http.REQUEST_CONTENT_LENGTH, httpRequest.ContentLength);
1614
activity.SetTag(OpenTelemetryAttributeName.Http.REQUEST_CONTENT_TYPE, httpRequest.ContentType);
1715
if (httpRequest.Body != null)
@@ -29,22 +27,19 @@ public static class ActivityExtension
2927
{
3028
if (httpRequest.Body != null)
3129
{
32-
var contentType = httpRequest.HttpContext.Request.ContentType;
33-
if (!string.IsNullOrEmpty(contentType))
34-
{
35-
var attr = MediaTypeHeaderValue.Parse(contentType);
36-
if (attr != null && !string.IsNullOrEmpty(attr.CharSet))
37-
return Encoding.GetEncoding(attr.CharSet);
38-
}
30+
var contentType = httpRequest.ContentType;
31+
if (!string.IsNullOrEmpty(contentType)
32+
&& MediaTypeHeaderValue.TryParse(contentType, out var attr)
33+
&& attr != null
34+
&& !string.IsNullOrEmpty(attr.CharSet))
35+
return Encoding.GetEncoding(attr.CharSet);
3936
}
4037

4138
return null;
4239
}
4340

44-
public static Activity? AddMasaSupplement(this Activity activity, HttpResponse httpResponse)
41+
public static Activity AddMasaSupplement(this Activity activity, HttpResponse httpResponse)
4542
{
46-
if (activity is null) return null;
47-
4843
activity.SetTag(OpenTelemetryAttributeName.Http.RESPONSE_CONTENT_LENGTH, httpResponse.ContentLength);
4944
activity.SetTag(OpenTelemetryAttributeName.Http.RESPONSE_CONTENT_TYPE, httpResponse.ContentType);
5045
activity.SetTag(OpenTelemetryAttributeName.Host.NAME, Dns.GetHostName());
@@ -58,10 +53,8 @@ public static class ActivityExtension
5853
return activity;
5954
}
6055

61-
public static async Task<Activity?> AddMasaSupplement(this Activity activity, HttpRequestMessage httpRequest)
56+
public static async Task<Activity> AddMasaSupplement(this Activity activity, HttpRequestMessage httpRequest)
6257
{
63-
if (activity is null) return null;
64-
6558
activity.SetTag(OpenTelemetryAttributeName.Http.SCHEME, httpRequest.RequestUri?.Scheme);
6659
activity.SetTag(OpenTelemetryAttributeName.Host.NAME, Dns.GetHostName());
6760

@@ -89,12 +82,9 @@ public static class ActivityExtension
8982
return null;
9083
}
9184

92-
public static Activity? AddMasaSupplement(this Activity activity, HttpResponseMessage httpResponse)
85+
public static Activity AddMasaSupplement(this Activity activity, HttpResponseMessage httpResponse)
9386
{
94-
if (activity is null) return null;
95-
9687
activity.SetTag(OpenTelemetryAttributeName.Host.NAME, Dns.GetHostName());
97-
9888
return activity;
9989
}
10090

0 commit comments

Comments
 (0)