Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bad names for logs of observability #555

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

namespace SkyApm.Transport
{
public interface ILoggerReporter
public interface ILogReporter
{
Task ReportAsync(IReadOnlyCollection<LoggerRequest> loggerRequests,
Task ReportAsync(IReadOnlyCollection<LogRequest> logRequests,
CancellationToken cancellationToken = default(CancellationToken));
}
}
2 changes: 1 addition & 1 deletion src/SkyApm.Abstractions/Transport/ISkyApmLogDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace SkyApm.Transport
{
public interface ISkyApmLogDispatcher
{
bool Dispatch(LoggerRequest loggerRequest);
bool Dispatch(LogRequest logRequest);

Task Flush(CancellationToken token = default(CancellationToken));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@

namespace SkyApm.Transport
{
public class LoggerRequest
public class LogRequest
{
public string Message { get; set; }

public Dictionary<string, object> Tags { get; set; }

public LoggerSegmentReference SegmentReference { get; set; }
public LogSegmentReference SegmentReference { get; set; }

public long Date { get; set; }

public string Endpoint { get; set; }
}
public class LoggerSegmentReference

public class LogSegmentReference
{
public string SegmentId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static IServiceCollection AddSampling(this IServiceCollection services)
private static IServiceCollection AddGrpcTransport(this IServiceCollection services)
{
services.AddSingleton<ISegmentReporter, SegmentReporter>();
services.AddSingleton<ILoggerReporter, LoggerReporter>();
services.AddSingleton<ILogReporter, LogReporter>();
services.AddSingleton<ICLRStatsReporter, CLRStatsReporter>();
services.AddSingleton<ConnectionManager>();
services.AddSingleton<IPingCaller, PingCaller>();
Expand Down
27 changes: 14 additions & 13 deletions src/SkyApm.Core/Transport/AsyncQueueSkyApmLogDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,60 +28,61 @@ namespace SkyApm.Transport
public class AsyncQueueSkyApmLogDispatcher : ISkyApmLogDispatcher
{
private readonly ILogger _logger;

private readonly CancellationTokenSource _cancellation;

private readonly ConcurrentQueue<LoggerRequest> _segmentQueue;
private readonly ConcurrentQueue<LogRequest> _segmentQueue;

private readonly IRuntimeEnvironment _runtimeEnvironment;

private readonly ILoggerReporter _loggerReporter;
private readonly ILogReporter _logReporter;

private readonly TransportConfig _config;

private int _offset;

public AsyncQueueSkyApmLogDispatcher(IConfigAccessor configAccessor, ILoggerFactory loggerFactory, ILoggerReporter loggerReporter, IRuntimeEnvironment runtimeEnvironment)
public AsyncQueueSkyApmLogDispatcher(IConfigAccessor configAccessor, ILoggerFactory loggerFactory, ILogReporter logReporter, IRuntimeEnvironment runtimeEnvironment)
{
_logger = loggerFactory.CreateLogger(typeof(AsyncQueueSkyApmLogDispatcher));
_config = configAccessor.Get<TransportConfig>();
_runtimeEnvironment = runtimeEnvironment;
_segmentQueue = new ConcurrentQueue<LoggerRequest>();
_segmentQueue = new ConcurrentQueue<LogRequest>();
_cancellation = new CancellationTokenSource();
_loggerReporter= loggerReporter;
_logReporter= logReporter;
}

public bool Dispatch(LoggerRequest loggerRequest)
public bool Dispatch(LogRequest logRequest)
{
if (!_runtimeEnvironment.Initialized || loggerRequest == null)
if (!_runtimeEnvironment.Initialized || logRequest == null)
return false;

// todo performance optimization for ConcurrentQueue
if (_config.QueueSize < _offset || _cancellation.IsCancellationRequested)
return false;

_segmentQueue.Enqueue(loggerRequest);
_segmentQueue.Enqueue(logRequest);

Interlocked.Increment(ref _offset);

_logger.Debug($"Dispatch trace segment. [SegmentId]={loggerRequest.SegmentReference?.SegmentId}.");
_logger.Debug($"Dispatch trace segment. [SegmentId]={logRequest.SegmentReference?.SegmentId}.");
return true;
}

public Task Flush(CancellationToken token = default)
{
var limit = _config.BatchSize;
var index = 0;
var loggers = new List<LoggerRequest>(limit);
var logs = new List<LogRequest>(limit);
while (index++ < limit && _segmentQueue.TryDequeue(out var request))
{
loggers.Add(request);
logs.Add(request);
Interlocked.Decrement(ref _offset);
}

// send async
if (loggers.Count > 0)
if (logs.Count > 0)
{
_loggerReporter.ReportAsync(loggers, token);
_logReporter.ReportAsync(logs, token);
}

return Task.CompletedTask;
Expand Down
4 changes: 2 additions & 2 deletions src/SkyApm.Diagnostics.MSLogging/SkyApmLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
message += "\r\n" + (exception.HasInnerExceptions() ? exception.ToDemystifiedString(_tracingConfig.ExceptionMaxDepth) : exception.ToString());
}
SegmentContext segmentContext = _segmentContextAccessor.Context;
var logContext = new LoggerRequest()
var logContext = new LogRequest()
{
Message = message ?? string.Empty,
Tags = tags,
SegmentReference = segmentContext == null
? null
: new LoggerSegmentReference()
: new LogSegmentReference()
{
TraceId = segmentContext.TraceId,
SegmentId = segmentContext.SegmentId,
Expand Down
2 changes: 1 addition & 1 deletion src/SkyApm.Transport.Grpc/SkyApm.Transport.Grpc.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />

<PropertyGroup>
<Description>$(Product) gRPC data transmitter.</Description>
<AssemblyTitle>$(PackagePrefix).Transport.Grpc</AssemblyTitle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@

namespace SkyApm.Transport.Grpc
{
public class LoggerReporter : ILoggerReporter
public class LogReporter : ILogReporter
{
private readonly ConnectionManager _connectionManager;
private readonly ILogger _logger;
private readonly GrpcConfig _grpcConfig;
private readonly InstrumentConfig _instrumentConfig;

public LoggerReporter(ConnectionManager connectionManager, IConfigAccessor configAccessor,
public LogReporter(ConnectionManager connectionManager, IConfigAccessor configAccessor,
ILoggerFactory loggerFactory)
{
_connectionManager = connectionManager;
_grpcConfig = configAccessor.Get<GrpcConfig>();
_instrumentConfig = configAccessor.Get<InstrumentConfig>();
_logger = loggerFactory.CreateLogger(typeof(SegmentReporter));
_logger = loggerFactory.CreateLogger(typeof(LogReporter));
}

public async Task ReportAsync(IReadOnlyCollection<LoggerRequest> loggerRequests,
public async Task ReportAsync(IReadOnlyCollection<LogRequest> logRequests,
CancellationToken cancellationToken = default)
{
if (!_connectionManager.Ready)
Expand All @@ -62,33 +62,33 @@ public async Task ReportAsync(IReadOnlyCollection<LoggerRequest> loggerRequests,
using (var asyncClientStreamingCall = client.collect(_grpcConfig.GetMeta(),
_grpcConfig.GetReportTimeout(), cancellationToken))
{
foreach (var loggerRequest in loggerRequests)
foreach (var logRequest in logRequests)
{
var logBody = new LogData()
{
Timestamp = loggerRequest.Date,
Timestamp = logRequest.Date,
Service = _instrumentConfig.ServiceName,
ServiceInstance = _instrumentConfig.ServiceInstanceName,
Endpoint = loggerRequest.Endpoint,
Endpoint = logRequest.Endpoint,
Body = new LogDataBody()
{
Type = "text",
Text = new TextLog()
{
Text = loggerRequest.Message,
Text = logRequest.Message,
},
},
Tags = new LogTags(),
};
if (loggerRequest.SegmentReference != null)
if (logRequest.SegmentReference != null)
{
logBody.TraceContext = new TraceContext()
{
TraceId = loggerRequest.SegmentReference.TraceId,
TraceSegmentId = loggerRequest.SegmentReference.SegmentId,
TraceId = logRequest.SegmentReference.TraceId,
TraceSegmentId = logRequest.SegmentReference.SegmentId,
};
}
foreach (var tag in loggerRequest.Tags)
foreach (var tag in logRequest.Tags)
{
logBody.Tags.Data.Add(new KeyStringValuePair()
{
Expand All @@ -103,17 +103,17 @@ public async Task ReportAsync(IReadOnlyCollection<LoggerRequest> loggerRequests,
await asyncClientStreamingCall.ResponseAsync;

stopwatch.Stop();
_logger.Information($"Report {loggerRequests.Count} logs. cost: {stopwatch.Elapsed}s");
_logger.Information($"Report {logRequests.Count} logs. cost: {stopwatch.Elapsed}s");
}
}
catch (IOException ex)
{
_logger.Error("Report trace segment fail.", ex);
_logger.Error("Report log fail.", ex);
_connectionManager.Failure(ex);
}
catch (Exception ex)
{
_logger.Error("Report trace segment fail.", ex);
_logger.Error("Report log fail.", ex);
}
}
}
Expand Down