Skip to content

Commit

Permalink
#518. Change log message format. (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
inversionhourglass authored Mar 9, 2023
1 parent 7bd426b commit 466754f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
6 changes: 5 additions & 1 deletion src/SkyApm.Abstractions/Transport/LoggerRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ namespace SkyApm.Transport
{
public class LoggerRequest
{
public Dictionary<string, object> Logs { get; set; }
public string Message { get; set; }

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

public LoggerSegmentReference SegmentReference { get; set; }

public long Date { get; set; }

public string Endpoint { get; set; }
}

public class LoggerSegmentReference
Expand Down
4 changes: 2 additions & 2 deletions src/SkyApm.Core/Tracing/SegmentContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public SegmentContextAccessor(IEntrySegmentContextAccessor entrySegmentContextAc
}

public SegmentContext Context =>
_entrySegmentContextAccessor.Context ??
_localSegmentContextAccessor.Context ?? _entrySegmentContextAccessor.Context;
_localSegmentContextAccessor.Context ??
_entrySegmentContextAccessor.Context ?? _entrySegmentContextAccessor.Context;
}
}
25 changes: 19 additions & 6 deletions src/SkyApm.Diagnostics.MSLogging/SkyApmLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;
using SkyApm.Tracing;
using SkyApm.Tracing.Segments;
Expand All @@ -30,36 +31,48 @@ public class SkyApmLogger : ILogger
private readonly string _categoryName;
private readonly ISkyApmLogDispatcher _skyApmLogDispatcher;
private readonly ISegmentContextAccessor _segmentContextAccessor;
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor;

public SkyApmLogger(string categoryName, ISkyApmLogDispatcher skyApmLogDispatcher,
ISegmentContextAccessor segmentContextAccessor)
ISegmentContextAccessor segmentContextAccessor,
IEntrySegmentContextAccessor entrySegmentContextAccessor)
{
_categoryName = categoryName;
_skyApmLogDispatcher = skyApmLogDispatcher;
_segmentContextAccessor = segmentContextAccessor;
_entrySegmentContextAccessor = entrySegmentContextAccessor;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
var logs = new Dictionary<string, object>
var tags = new Dictionary<string, object>
{
{ "className", _categoryName },
{ "logger", _categoryName },
{ "Level", logLevel },
{ "logMessage", state.ToString() ?? "" }
{ "thread", Thread.CurrentThread.ManagedThreadId },
};
if (exception != null)
{
tags["errorType"] = exception.GetType().ToString();
}
SegmentContext segmentContext = _segmentContextAccessor.Context;
var logContext = new LoggerRequest()
{
Logs = logs,
Message = state.ToString() ?? string.Empty,
Tags = tags,
SegmentReference = segmentContext == null
? null
: new LoggerSegmentReference()
{
TraceId = segmentContext.TraceId,
SegmentId = segmentContext.SegmentId
SegmentId = segmentContext.SegmentId,
},
};
if (_entrySegmentContextAccessor.Context != null)
{
logContext.Endpoint = _entrySegmentContextAccessor.Context.Span.OperationName.ToString();
}
_skyApmLogDispatcher.Dispatch(logContext);
}

Expand Down
7 changes: 5 additions & 2 deletions src/SkyApm.Diagnostics.MSLogging/SkyApmLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public class SkyApmLoggerProvider : ILoggerProvider
private readonly ConcurrentDictionary<string, SkyApmLogger> _doveLoggers = new ConcurrentDictionary<string, SkyApmLogger>();
private readonly ISkyApmLogDispatcher _skyApmLogDispatcher;
private readonly ISegmentContextAccessor _segmentContextAccessor;
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor;

public SkyApmLoggerProvider(ISkyApmLogDispatcher skyApmLogDispatcher,
ISegmentContextAccessor segmentContextAccessor)
ISegmentContextAccessor segmentContextAccessor,
IEntrySegmentContextAccessor entrySegmentContextAccessor)
{
_skyApmLogDispatcher = skyApmLogDispatcher;
_segmentContextAccessor = segmentContextAccessor;
_entrySegmentContextAccessor = entrySegmentContextAccessor;
}

public void Dispose()
Expand All @@ -43,7 +46,7 @@ public void Dispose()
public ILogger CreateLogger(string categoryName)
{
return _doveLoggers.GetOrAdd(categoryName,
_ => new SkyApmLogger(categoryName, _skyApmLogDispatcher, _segmentContextAccessor));
_ => new SkyApmLogger(categoryName, _skyApmLogDispatcher, _segmentContextAccessor, _entrySegmentContextAccessor));
}
}
}
33 changes: 19 additions & 14 deletions src/SkyApm.Transport.Grpc/V8/LoggerReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,38 @@ public async Task ReportAsync(IReadOnlyCollection<LoggerRequest> loggerRequests,
{
foreach (var loggerRequest in loggerRequests)
{
var logMessage = new StringBuilder();
foreach (var log in loggerRequest.Logs)
{
logMessage.Append($"\r\n{log.Key} : {log.Value}");
}

var logBody = new LogData()
{
TraceContext = new TraceContext()
{
TraceId = loggerRequest.SegmentReference?.TraceId ?? string.Empty,
TraceSegmentId = loggerRequest.SegmentReference?.SegmentId ?? string.Empty,
//SpanId=item.Segment
},
Timestamp = loggerRequest.Date,
Service = _instrumentConfig.ServiceName,
ServiceInstance = _instrumentConfig.ServiceInstanceName,
Endpoint = "",
Endpoint = loggerRequest.Endpoint,
Body = new LogDataBody()
{
Type = "text",
Text = new TextLog()
{
Text = logMessage.ToString(),
Text = loggerRequest.Message,
},
},
Tags = new LogTags(),
};
if (loggerRequest.SegmentReference != null)
{
logBody.TraceContext = new TraceContext()
{
TraceId = loggerRequest.SegmentReference.TraceId,
TraceSegmentId = loggerRequest.SegmentReference.SegmentId,
};
}
foreach (var tag in loggerRequest.Tags)
{
logBody.Tags.Data.Add(new KeyStringValuePair()
{
Key = tag.Key,
Value = tag.Value.ToString(),
});
}
await asyncClientStreamingCall.RequestStream.WriteAsync(logBody);
}

Expand Down

0 comments on commit 466754f

Please sign in to comment.