Skip to content

Commit

Permalink
Fix segment is null when record log (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhaoyang authored Sep 18, 2022
1 parent 738fe6c commit 3e88e01
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
Expand All @@ -17,10 +17,11 @@
*/

using SkyApm.Tracing.Segments;
namespace SkyApm.Transport

namespace SkyApm.Tracing
{
public interface ILoggerContextContextMapper
public interface ISegmentContextAccessor
{
LoggerRequest Map(LoggerContext loggerContext);
SegmentContext Context { get; }
}
}
}
32 changes: 0 additions & 32 deletions src/SkyApm.Abstractions/Tracing/Segments/LoggerContext.cs

This file was deleted.

3 changes: 1 addition & 2 deletions src/SkyApm.Abstractions/Transport/ISkyApmLogDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
*
*/

using SkyApm.Tracing.Segments;
using System.Threading;
using System.Threading.Tasks;

namespace SkyApm.Transport
{
public interface ISkyApmLogDispatcher
{
bool Dispatch(LoggerContext loggerContext);
bool Dispatch(LoggerRequest loggerRequest);

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

Expand Down
15 changes: 12 additions & 3 deletions src/SkyApm.Abstractions/Transport/LoggerRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@
*
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace SkyApm.Transport
{
public class LoggerRequest
{
public Dictionary<string, object> Logs { get; set; }

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

public long Date { get; set; }
}

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

public string TraceId { get; set; }

public string ServiceId { get; set; }

public string ServiceInstanceId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ private static IServiceCollection AddTracing(this IServiceCollection services)
public static IServiceCollection AddSkyApmLogger(this IServiceCollection services)
{
services.AddSingleton<ISkyApmLogDispatcher, AsyncQueueSkyApmLogDispatcher>();
services.AddSingleton<ILoggerContextContextMapper, LoggerContextContextMapper>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, SkyApmLoggerProvider>());
return services;
}
Expand Down
42 changes: 42 additions & 0 deletions src/SkyApm.Core/Tracing/SegmentContextAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The SkyAPM licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using SkyApm.Tracing.Segments;

namespace SkyApm.Tracing
{
public class SegmentContextAccessor : ISegmentContextAccessor
{
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor;
private readonly ILocalSegmentContextAccessor _localSegmentContextAccessor;
private readonly IExitSegmentContextAccessor _exitSegmentContextAccessor;

public SegmentContextAccessor(IEntrySegmentContextAccessor entrySegmentContextAccessor,
ILocalSegmentContextAccessor localSegmentContextAccessor,
IExitSegmentContextAccessor exitSegmentContextAccessor)
{
_entrySegmentContextAccessor = entrySegmentContextAccessor;
_localSegmentContextAccessor = localSegmentContextAccessor;
_exitSegmentContextAccessor = exitSegmentContextAccessor;
}

public SegmentContext Context =>
_entrySegmentContextAccessor.Context ??
_localSegmentContextAccessor.Context ?? _entrySegmentContextAccessor.Context;
}
}
31 changes: 11 additions & 20 deletions src/SkyApm.Core/Transport/AsyncQueueSkyApmLogDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using SkyApm.Config;
using SkyApm.Logging;
using SkyApm.Tracing.Segments;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -36,61 +35,53 @@ public class AsyncQueueSkyApmLogDispatcher : ISkyApmLogDispatcher
private readonly IRuntimeEnvironment _runtimeEnvironment;

private readonly ILoggerReporter _loggerReporter;

private readonly ILoggerContextContextMapper _loggerContextContextMapper;


private readonly TransportConfig _config;

private int _offset;

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

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

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

var segment = _loggerContextContextMapper.Map(loggerContext);

if (segment == null)
return false;

_segmentQueue.Enqueue(segment);

_segmentQueue.Enqueue(loggerRequest);

Interlocked.Increment(ref _offset);

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

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

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

Interlocked.Exchange(ref _offset, _segmentQueue.Count);
Expand Down
43 changes: 0 additions & 43 deletions src/SkyApm.Core/Transport/LoggerContextContextMapper.cs

This file was deleted.

51 changes: 41 additions & 10 deletions src/SkyApm.Diagnostics.MSLogging/SkyApmLogger.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Licensed to the SkyAPM under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The SkyAPM licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
Expand All @@ -11,25 +29,38 @@ public class SkyApmLogger : ILogger
{
private readonly string _categoryName;
private readonly ISkyApmLogDispatcher _skyApmLogDispatcher;
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor;
private readonly ISegmentContextAccessor _segmentContextAccessor;

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

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
var logs = new Dictionary<string, object>();
logs.Add("className", _categoryName);
logs.Add("Level", logLevel);
logs.Add("logMessage", state.ToString() ?? "");
var logContext = new LoggerContext()
var logs = new Dictionary<string, object>
{
{ "className", _categoryName },
{ "Level", logLevel },
{ "logMessage", state.ToString() ?? "" }
};
SegmentContext segmentContext = _segmentContextAccessor.Context;
var logContext = new LoggerRequest()
{
Logs = logs,
SegmentContext = _entrySegmentContextAccessor.Context,
SegmentReference = segmentContext == null
? null
: new LoggerSegmentReference()
{
TraceId = segmentContext.TraceId,
SegmentId = segmentContext.SegmentId,
ServiceId = segmentContext.ServiceId,
ServiceInstanceId = segmentContext.ServiceInstanceId
},
};
_skyApmLogDispatcher.Dispatch(logContext);
}
Expand Down
Loading

0 comments on commit 3e88e01

Please sign in to comment.