diff --git a/Masa.Contrib.sln b/Masa.Contrib.sln index 4896f1af8..00cdf2d4f 100644 --- a/Masa.Contrib.sln +++ b/Masa.Contrib.sln @@ -170,7 +170,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Storage.Object EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Storage", "Storage", "{165391A5-034E-4894-8084-8DF7D4AA7518}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Configuration.AutoMap.NoArgumentConstructor.Tests", "test\Masa.Contrib.Configuration.AutoMap.NoArgumentConstructor.Tests\Masa.Contrib.Configuration.AutoMap.NoArgumentConstructor.Tests.csproj", "{B8358ED1-C95A-4EC0-9756-FB32C931F204}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Configuration.AutoMap.NoArgumentConstructor.Tests", "test\Masa.Contrib.Configuration.AutoMap.NoArgumentConstructor.Tests\Masa.Contrib.Configuration.AutoMap.NoArgumentConstructor.Tests.csproj", "{B8358ED1-C95A-4EC0-9756-FB32C931F204}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.Observability", "src\BuildingBlocks\MASA.BuildingBlocks\src\Observability\Masa.BuildingBlocks.Observability\Masa.BuildingBlocks.Observability.csproj", "{E4BBDD80-010C-4A4F-8D71-54012C0B237E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Observability.Opentelemetry.Logging", "src\Observability\Masa.Contrib.Observability.Opentelemetry.Logging\Masa.Contrib.Observability.Opentelemetry.Logging.csproj", "{86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -676,6 +680,22 @@ Global {B8358ED1-C95A-4EC0-9756-FB32C931F204}.Release|Any CPU.Build.0 = Release|Any CPU {B8358ED1-C95A-4EC0-9756-FB32C931F204}.Release|x64.ActiveCfg = Release|Any CPU {B8358ED1-C95A-4EC0-9756-FB32C931F204}.Release|x64.Build.0 = Release|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Debug|x64.ActiveCfg = Debug|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Debug|x64.Build.0 = Debug|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Release|Any CPU.Build.0 = Release|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Release|x64.ActiveCfg = Release|Any CPU + {E4BBDD80-010C-4A4F-8D71-54012C0B237E}.Release|x64.Build.0 = Release|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Debug|x64.ActiveCfg = Debug|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Debug|x64.Build.0 = Debug|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Release|Any CPU.Build.0 = Release|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Release|x64.ActiveCfg = Release|Any CPU + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -761,6 +781,8 @@ Global {97532A33-A591-4DF5-A2C0-72527B78ED82} = {38E6C400-90C0-493E-9266-C1602E229F1B} {165391A5-034E-4894-8084-8DF7D4AA7518} = {42DF7AAC-362C-48F4-B76A-BDEEEFF17CC9} {B8358ED1-C95A-4EC0-9756-FB32C931F204} = {9EEE31DA-3165-4CB3-AAE9-27CC3A4DE669} + {E4BBDD80-010C-4A4F-8D71-54012C0B237E} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1} + {86BC5A1E-FE3F-4A53-A522-FE0E4515BB8E} = {75050CBC-A0F2-408A-A582-54EF37450B29} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03} diff --git a/src/BuildingBlocks/MASA.BuildingBlocks b/src/BuildingBlocks/MASA.BuildingBlocks index 8b7188d25..00ec10314 160000 --- a/src/BuildingBlocks/MASA.BuildingBlocks +++ b/src/BuildingBlocks/MASA.BuildingBlocks @@ -1 +1 @@ -Subproject commit 8b7188d25edb0b8111624cde7d98e5f054b35e1e +Subproject commit 00ec10314adb7a433ebcb10ea2d8af8bd26481fc diff --git a/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/Masa.Contrib.Observability.Opentelemetry.Logging.csproj b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/Masa.Contrib.Observability.Opentelemetry.Logging.csproj new file mode 100644 index 000000000..2f60baeb7 --- /dev/null +++ b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/Masa.Contrib.Observability.Opentelemetry.Logging.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/MasaOpenTelemetryLogOptions.cs b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/MasaOpenTelemetryLogOptions.cs new file mode 100644 index 000000000..3fa032ae3 --- /dev/null +++ b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/MasaOpenTelemetryLogOptions.cs @@ -0,0 +1,9 @@ +using Masa.BuildingBlocks.Observability.Logging; + +namespace Masa.Contrib.Observability.Opentelemetry.Logging +{ + public class MasaOpenTelemetryLogOptions: MasaLoggingOptions + { + + } +} diff --git a/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/MasaResourceSemanticConventions.cs b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/MasaResourceSemanticConventions.cs new file mode 100644 index 000000000..410cf1171 --- /dev/null +++ b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/MasaResourceSemanticConventions.cs @@ -0,0 +1,7 @@ +namespace OpenTelemetry.Resources +{ + internal class MasaResourceSemanticConventions + { + public const string AttributeServiceProjectName = "service.project.name"; + } +} diff --git a/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/OpenTelemetryLoggingExtensions.cs b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/OpenTelemetryLoggingExtensions.cs new file mode 100644 index 000000000..479c67a20 --- /dev/null +++ b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/OpenTelemetryLoggingExtensions.cs @@ -0,0 +1,21 @@ +using OpenTelemetry.Exporter; +using OpenTelemetry.Logs; + +namespace Microsoft.Extensions.Logging +{ + public static class OpenTelemetryLoggingExtensions + { + public static ILoggingBuilder AddMasaOpenTelemetry(this ILoggingBuilder builder, Action configure, Action? exportConfigure = null) + { + builder = builder.AddOpenTelemetry(options => + { + options.IncludeScopes = true; + if (configure != null) + configure.Invoke(options); + options.AddOtlpExporter(exportConfigure); + }); + + return builder; + } + } +} diff --git a/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/README.md b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/README.md new file mode 100644 index 000000000..514d7839a --- /dev/null +++ b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/README.md @@ -0,0 +1,82 @@ +[中](README.zh-CN.md) | EN + +## Opentelemetry.Logging + +Example: + +1. loggingadd project support,added based on [Openteletry specification](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/common)contracted project support; +2. Added by default[OtlpExporter](https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlpexporter)export,And log data is stored in Elasticsearch by default. If you need to modify the data storage medium, please refer to the configuration to modify. + + +```C# +Install-Package Masa.Contrib.Observability.Opentelemetry.Logging +``` + +1. Project support: + +```C# +using OpenTelemetry.Resources; +using OpenTelemetry.Logs; + +var builder = WebApplication.CreateBuilder(args); + +builder.Logging.AddOpenTelemetry(options => +{ + options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddMasaService( + serviceName: "service.example1", + serviceProjectId: "masa.stack.auth", + serviceVersion: "1.0.0" + )); + options.AddConsoleExporter(); +}); + +var app = builder.Build(); + +app.MapGet("/", (ILogger logger) => +{ + logger.LogInformation("Hello World!"); + return "Hello World!"; +}); + +app.Run(); + +partial class Program { } +``` + +After running, you can see the project configuration information just now in the console output: +``` +info: Program[0] + Hello World! +LogRecord.TraceId: 6678d6ff3b922948e0d6de47b1beaf80 +LogRecord.SpanId: 3c04a4585635b5ec +LogRecord.Timestamp: 2022-04-24T06:36:13.2218113Z +LogRecord.EventId: 0 +LogRecord.EventName: +LogRecord.CategoryName: Program +LogRecord.LogLevel: Information +LogRecord.TraceFlags: None +LogRecord.State: Hello World! +Resource associated with LogRecord: + service.project.id: masa.stack.auth + service.name: service.example1 + service.version: 1.0.0 + service.instance.id: 2871cd69-a78b-4169-8b72-b30fb17c17f3 +``` + + +2. OTLP Expoter Configration + +```C# +builder.Logging.AddMasaOpenTelemetry(options => +{ + options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddMasaService( + serviceName: "service.example1", + serviceProjectId: "masa.stack.auth", + serviceVersion: "1.0.0" + )); +}, exportConfigure => { + exportConfigure.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf; + exportConfigure.Endpoint = new Uri("http://localhost:4317"); +}); +``` +Detailed configuration reference[Opentelemetry Exporters](https://opentelemetry.io/docs/collector/configuration/#exporters). \ No newline at end of file diff --git a/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/README.zh-CN.md b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/README.zh-CN.md new file mode 100644 index 000000000..baf2a02eb --- /dev/null +++ b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/README.zh-CN.md @@ -0,0 +1,83 @@ +中 | [EN](README.md) + +## Opentelemetry.Logging + +用例: + +1. logging添加项目支持,添加了基于 [Openteletry specification](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/common)约定的项目支持; +2. 默认添加了[OtlpExporter](https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlpexporter)导出,并且日志数据默认存储到Elasticsearch,如果需要修改数据存储介质,请参考配置进行修改。 + + + +```C# +Install-Package Masa.Contrib.Observability.Opentelemetry.Logging +``` + +1. 项目支持: + +```C# +using OpenTelemetry.Resources; +using OpenTelemetry.Logs; + +var builder = WebApplication.CreateBuilder(args); + +builder.Logging.AddOpenTelemetry(options => +{ + options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddMasaService( + serviceName: "service.example1", + serviceProjectId: "masa.stack.auth", + serviceVersion: "1.0.0" + )); + options.AddConsoleExporter(); +}); + +var app = builder.Build(); + +app.MapGet("/", (ILogger logger) => +{ + logger.LogInformation("Hello World!"); + return "Hello World!"; +}); + +app.Run(); + +partial class Program { } +``` + +运行后可以在控制台输出中看到刚才项目配置信息: +``` +info: Program[0] + Hello World! +LogRecord.TraceId: 6678d6ff3b922948e0d6de47b1beaf80 +LogRecord.SpanId: 3c04a4585635b5ec +LogRecord.Timestamp: 2022-04-24T06:36:13.2218113Z +LogRecord.EventId: 0 +LogRecord.EventName: +LogRecord.CategoryName: Program +LogRecord.LogLevel: Information +LogRecord.TraceFlags: None +LogRecord.State: Hello World! +Resource associated with LogRecord: + service.project.id: masa.stack.auth + service.name: service.example1 + service.version: 1.0.0 + service.instance.id: 2871cd69-a78b-4169-8b72-b30fb17c17f3 +``` + + +2. OTLP Expoter配置 + +```C# +builder.Logging.AddMasaOpenTelemetry(options => +{ + options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddMasaService( + serviceName: "service.example1", + serviceProjectId: "masa.stack.auth", + serviceVersion: "1.0.0" + )); +}, exportConfigure => { + exportConfigure.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf; + exportConfigure.Endpoint = new Uri("http://localhost:4317"); +}); +``` +详细配置参考[Opentelemetry Exporters](https://opentelemetry.io/docs/collector/configuration/#exporters)。 diff --git a/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/ResourceBuilderExtenstions.cs b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/ResourceBuilderExtenstions.cs new file mode 100644 index 000000000..eada7496f --- /dev/null +++ b/src/Observability/Masa.Contrib.Observability.Opentelemetry.Logging/ResourceBuilderExtenstions.cs @@ -0,0 +1,25 @@ +using Masa.Contrib.Observability.Opentelemetry.Logging; + +namespace OpenTelemetry.Resources +{ + public static class ResourceBuilderExtenstions + { + public static ResourceBuilder AddMasaService( + this ResourceBuilder resourceBuilder, + MasaOpenTelemetryLogOptions options) + { + if (options == null) + { + throw new ArgumentException("Must not be null or empty", nameof(options)); + } + + resourceBuilder = resourceBuilder.AddService(options.ServiceName, options.ServiceNameSpace, options.ServerVersion, true, options.ServiceInstanceId); + + if (!string.IsNullOrEmpty(options.ProjectName)) + { + resourceBuilder.AddAttributes(new KeyValuePair[] { new KeyValuePair(MasaResourceSemanticConventions.AttributeServiceProjectName, options.ProjectName) }); + } + return resourceBuilder; + } + } +} diff --git a/src/Observability/Opentelemetry/MASA.Utils.Observability.Opentelemetry.Logging/Masa.Contrib.Observability.Opentelemetry.Logging.csproj b/src/Observability/Opentelemetry/MASA.Utils.Observability.Opentelemetry.Logging/Masa.Contrib.Observability.Opentelemetry.Logging.csproj new file mode 100644 index 000000000..d6e4bd0c5 --- /dev/null +++ b/src/Observability/Opentelemetry/MASA.Utils.Observability.Opentelemetry.Logging/Masa.Contrib.Observability.Opentelemetry.Logging.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + +