【问题标题】:Service Fabric QoS loggingService Fabric QoS 日志记录
【发布时间】:2023-04-06 11:20:01
【问题描述】:

我有一个微服务应用程序,我想在其中记录服务质量 (QoS) 日志 - 每个服务调用一个日志行,无论服务调用是什么。 我还希望每次调用都有详细的调用,这些调用在 QoS 日志和详细日志中具有相同的 ID。

我不想更改服务方法的签名以获取更多参数。相反,我想以某种方式更改远程处理堆栈,以便涵盖所有当前和未来的服务调用。

我跟着answer 写了一个IServiceRemotingClient,它使用AsyncLocal 来存储上下文信息。例如,这不允许我检查响应类型和记录异常堆栈跟踪。

我的问题是 - 有没有办法将 SF 的调用包装到我自己的方法中,更靠近我的服务方法,在那里我可以记录异常和结果,而不是在远程级别,一切都在已经打包成黑盒了

public class LoggingServiceRemotingClient: IServiceRemotingClient
{
   IServiceRemotingClient standardClient;
   ...
   public async Task<IServiceRemotingResponseMessage> RequestResponseAsync(IServiceRemotingRequestMessage requestMessage)
   {
     var callId = Guid.NewGuid();
     try{
       SetCallIdInAsyncLocal(callId); // trimmed down pseudo code
       var response = await this.standardClient.RequestResponseAsync(requestMessage);
       Log.Ok(callId); // trimmed down pseudo code
     } catch (Exception x){
       // this will never be hit because the response body was already created with an exception serialized in it.
       // how do I catch such exceptions across all calls?
       // also some calls a due to client errors, others are internal - a single place to differentiate would be nice.
       Log.Fail(callId, x);
     }
   }
}

服务本身也会使用callId 来记录自己的进程:

public class MyService: StatelessService, IMyService
{
  public async Task<string> GetMeAString(string prefix)
  {
      Debug.Assert(prefix!=null); // this exception is a caller fault
      Guid callId = GetFromAsyncLocal();
      Log(callId, "Got a call here")
      string result = prefix+": "+Guid.NewGuid().ToString();
      Log(callId, "returning "+result);
      return result;
  }
}

我不明白为什么AsyncLocal 可以在这里工作,但显然可以。

【问题讨论】:

  • 请看here。该 repo 也允许消息拦截,请参阅the doc。您可以在方法发送之前或接收之后拦截它们。

标签: .net azure-service-fabric


【解决方案1】:

我认为您正在寻找来自 Microsoft Application Insights for Service Fabric 包的 Trace Correlation with Service Remoting

它将所有依赖跟踪信息发送到应用洞察,不需要对代码进行太多更改,还可以与其他应用洞察监控功能相结合。

如果您想从头开始或不使用 AppInsights,Peter Bons 提到的选项也是有效的。

【讨论】:

  • 我无法使用 Application Insights。最终它可能会监听 ETW,这是我不想做的另一件事
  • 它是开源的,您可以抓取代码并替换不符合您需求的位。
猜你喜欢
  • 2019-09-27
  • 2017-12-09
  • 2018-09-07
  • 2016-09-29
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 2023-03-02
相关资源
最近更新 更多