Skip to content

Commit

Permalink
Support opentelemetry logs
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed May 12, 2024
1 parent 183ea12 commit 36141d6
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/AgileConfig.Server.Apisite/Appsettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ public class Appsettings
/// SSO button text
/// </summary>
public static string SsoButtonText => Global.Config["SSO:loginButtonText"];

public static string OtlpLogsEndpoint => Global.Config["otlp:logs:endpoint"];

public static string OtlpLogsProtocol => Global.Config["otlp:logs:protocol"];
}
}
48 changes: 43 additions & 5 deletions src/AgileConfig.Server.Apisite/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Resources;
using OpenTelemetry.Logs;
using OpenTelemetry.Exporter;

namespace AgileConfig.Server.Apisite
{
public class Program
{
//public static IRemoteServerNodeProxy RemoteServerNodeProxy { get; private set; }
public const string AppName = "AgileConfig Server";

public static void Main(string[] args)
{
Expand All @@ -17,26 +21,60 @@ public static void Main(string[] args)
var builder = new ConfigurationBuilder()
.SetBasePath(basePath);
#if DEBUG
Global.Config =
Global.Config =
builder
.AddJsonFile("appsettings.Development.json")
.AddEnvironmentVariables()
.Build();
#else
Global.Config = builder.AddJsonFile("appsettings.json").AddEnvironmentVariables().Build();
#endif

var host = CreateWebHostBuilder(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
private static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
return WebHost.CreateDefaultBuilder(args).ConfigureLogging(
(context, builder) =>
{
AddOtlpLogging(builder);
}
)
.UseConfiguration(Global.Config)
.UseStartup<Startup>();
}


private static void AddOtlpLogging(ILoggingBuilder builder)
{
if (string.IsNullOrEmpty(Appsettings.OtlpLogsEndpoint))
{
return;
}

builder.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(Program.AppName));
options
.AddOtlpExporter(expOp =>
{
if (Appsettings.OtlpLogsProtocol == "grpc")
{
expOp.Protocol = OtlpExportProtocol.Grpc;
}
else
{
expOp.Protocol = OtlpExportProtocol.HttpProtobuf;
}

expOp.Endpoint = new Uri(Appsettings.OtlpLogsEndpoint);
})
;
});
}

}
}
1 change: 1 addition & 0 deletions src/AgileConfig.Server.Apisite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public IConfiguration Configuration
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOtlp();
services.AddDefaultHttpClient(IsTrustSSL(Configuration));
services.AddRestClient();

Expand Down
33 changes: 32 additions & 1 deletion src/AgileConfig.Server.Apisite/StartupExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using AgileConfig.Server.Common;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using System.Net.Http;
using OpenTelemetry.Trace;
using OpenTelemetry.Logs;
using OpenTelemetry.Exporter;

namespace AgileConfig.Server.Apisite
{
Expand All @@ -9,12 +14,38 @@ public static class StartupExtension
public static void AddDefaultHttpClient(this IServiceCollection services, bool isTrustSSL)
{
services.AddHttpClient(Global.DefaultHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => {
.ConfigurePrimaryHttpMessageHandler(() =>
{
return NewMessageHandler(isTrustSSL);
})
;
}

public static void AddOtlp(this IServiceCollection services)
{
//services.AddOpenTelemetry()
// .ConfigureResource(resource => resource.AddService(Program.AppName))
// .WithTracing(tracing => tracing
// .AddAspNetCoreInstrumentation()
// .AddHttpClientInstrumentation()
// .AddOtlpExporter(op =>
// {
// op.Protocol = OtlpExportProtocol.HttpProtobuf;
// op.Endpoint = new System.Uri(Global.Config["otlp:traces:endpoint"]);
// })
// )
//.WithMetrics(metrics => metrics
// .AddRuntimeInstrumentation()
// .AddAspNetCoreInstrumentation()
// .AddOtlpExporter(op =>
// {
// op.Protocol = OtlpExportProtocol.HttpProtobuf;
// op.Endpoint = new System.Uri(Global.Config["otlp:trace:endpoint"]);
// })
// )
;
}

static HttpMessageHandler NewMessageHandler(bool alwaysTrustSsl)
{
var handler = new HttpClientHandler();
Expand Down
14 changes: 13 additions & 1 deletion src/AgileConfig.Server.Apisite/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
"Microsoft": "Information"
}
},
"otlp": {
"logs": {
"protocol": "http", // http grpc
"endpoint": "http://192.168.0.201:5341/ingest/otlp/v1/logs"
},
"traces": {
"endpoint": "http://" // 暂时不支持
},
"metrics": {
"endpoint": "http://" // 暂时不支持
}
},
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
"serviceHealthCheckInterval": 15, // 服务健康检测的间隔时间,单位:秒
"serviceUnhealthInterval": 30, // 判断服务不健康的间隔,超出这个时间没响应过则认为不健康,单位:秒
Expand Down Expand Up @@ -45,7 +57,7 @@

"SSO": {
"enabled": true, // 是否启用 SSO
"loginButtonText": "SSO",// 自定义 SSO 跳转按钮的文字
"loginButtonText": "SSO", // 自定义 SSO 跳转按钮的文字
"OIDC": {
"clientId": "2bb823b7-f1ad-48c7-a9a1-713e9a885a5d", // 应用程序ID
"clientSecret": "", // 应用程序密钥
Expand Down
12 changes: 12 additions & 0 deletions src/AgileConfig.Server.Apisite/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
"Microsoft": "Warning"
}
},
"otlp": {
"logs": {
"protocol": "http", // http grpc
"endpoint": ""
},
"traces": {
"endpoint": "" // 暂时不支持
},
"metrics": {
"endpoint": "" // 暂时不支持
}
},
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
"serviceHealthCheckInterval": 15, // 服务健康检测的间隔时间,单位:秒
"serviceUnhealthInterval": 60, // 判断服务不健康的间隔,超出这个时间没响应过则认为不健康,默认60,单位:秒
Expand Down
7 changes: 6 additions & 1 deletion src/AgileConfig.Server.Data.Entity/SysLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public SysLog()
public string Id { get; set; }

[Column(Name = "app_id", StringLength = 36)]
public string AppId { get; set; }
public string AppId { get; set; } = "";

[Column(Name = "log_type")]
public SysLogType LogType { get; set; }
Expand All @@ -35,5 +35,10 @@ public SysLog()

[Column(Name = "log_text", StringLength = 2000)]
public string LogText { get; set; }

public override string ToString()
{
return $"Id:{Id}, AppId:{AppId}, LogType:{LogType}, LogTime:{LogTime}, LogText:{LogText}";
}
}
}
1 change: 0 additions & 1 deletion src/AgileConfig.Server.Service/RegisterCenterService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.IService;
using System;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using AgileConfig.Server.Data.Abstraction;
Expand Down
10 changes: 7 additions & 3 deletions src/AgileConfig.Server.Service/ServiceHealthCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Task StartCheckAsync()
{
continue;
}

var lstHeartBeat = service.LastHeartBeat;
if (!lstHeartBeat.HasValue)
{
Expand Down Expand Up @@ -198,8 +198,12 @@ private async Task<bool> CheckAService(ServiceInfo service)
int istatus = ((int)resp.StatusCode - 200);
result = istatus >= 0 && istatus < 100; // 200 段都认为是正常的

_logger.LogInformation("check service health {0} {1} {2} result:{3}", service.CheckUrl, service.ServiceId,
service.ServiceName, result ? "up" : "down");
if (!result)
{
_logger.LogInformation("check service health {0} {1} {2} result:{3}", service.CheckUrl, service.ServiceId,
service.ServiceName, "down");
}

return result;
}
catch (Exception e)
Expand Down
16 changes: 14 additions & 2 deletions src/AgileConfig.Server.Service/SysLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,40 @@
using System.Linq.Expressions;
using System.Threading.Tasks;
using AgileConfig.Server.Data.Abstraction;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.Extensions.Logging;
using static FreeSql.Internal.GlobalFilter;

namespace AgileConfig.Server.Service
{
public class SysLogService : ISysLogService
{
private readonly ISysLogRepository _sysLogRepository;
private readonly ILogger<SysLogService> _logger;

public SysLogService(ISysLogRepository sysLogRepository)
public SysLogService(ISysLogRepository sysLogRepository, ILogger<SysLogService> logger)
{
_sysLogRepository = sysLogRepository;
_logger = logger;
}

public async Task<bool> AddRangeAsync(IEnumerable<SysLog> logs)
{
await _sysLogRepository.InsertAsync(logs.ToList());

foreach (var item in logs)
{
_logger.LogInformation("{AppId} {LogType} {LogTime} {LogText}", item.AppId, item.LogType, item.LogTime, item.LogText);
}

return true;
}

public async Task<bool> AddSysLogAsync(SysLog log)
{
await _sysLogRepository.InsertAsync(log);

_logger.LogInformation("{AppId} {LogType} {LogTime} {LogText}", log.AppId, log.LogType, log.LogTime, log.LogText);

return true;
}

Expand Down

0 comments on commit 36141d6

Please sign in to comment.