浏览代码

Fixed Activity is created as null when ASP.NET Core instrument is not enabled.

master
Savorboard 2 年前
父节点
当前提交
9c8baeccf7
共有 1 个文件被更改,包括 12 次插入9 次删除
  1. +12
    -9
      src/DotNetCore.CAP.OpenTelemetry/DiagnosticListener.cs

+ 12
- 9
src/DotNetCore.CAP.OpenTelemetry/DiagnosticListener.cs 查看文件

@@ -26,7 +26,7 @@ namespace DotNetCore.CAP.OpenTelemetry
private const string OperateNamePrefix = "CAP/"; private const string OperateNamePrefix = "CAP/";
private const string ProducerOperateNameSuffix = "/Publisher"; private const string ProducerOperateNameSuffix = "/Publisher";
private const string ConsumerOperateNameSuffix = "/Subscriber"; private const string ConsumerOperateNameSuffix = "/Subscriber";
public void OnCompleted() public void OnCompleted()
{ {
} }
@@ -42,22 +42,25 @@ namespace DotNetCore.CAP.OpenTelemetry
case CapEvents.BeforePublishMessageStore: case CapEvents.BeforePublishMessageStore:
{ {
var eventData = (CapEventDataPubStore)evt.Value!; var eventData = (CapEventDataPubStore)evt.Value!;
var useParent = false;
if (Activity.Current != null)
ActivityContext parentContext = default;

if (Activity.Current != null && Activity.Current.Source.Name == "OpenTelemetry.Instrumentation.AspNetCore")
{ {
if (_contexts.TryAdd(eventData.Message.GetId(), Activity.Current.Context))
{
useParent = true;
}
_contexts.TryAdd(eventData.Message.GetId(), parentContext = Activity.Current.Context);
} }
var activity = ActivitySource.StartActivity("Event Persistence: " + eventData.Operation);
else
{
Activity.Current = null;
}

var activity = ActivitySource.StartActivity("Event Persistence: " + eventData.Operation, ActivityKind.Internal, parentContext);
if (activity != null) if (activity != null)
{ {
activity.SetTag("message.name", eventData.Operation); activity.SetTag("message.name", eventData.Operation);
activity.AddEvent(new ActivityEvent("CAP message persistence start...", activity.AddEvent(new ActivityEvent("CAP message persistence start...",
DateTimeOffset.FromUnixTimeMilliseconds(eventData.OperationTimestamp!.Value))); DateTimeOffset.FromUnixTimeMilliseconds(eventData.OperationTimestamp!.Value)));


if (!useParent)
if (parentContext != default)
{ {
_contexts[eventData.Message.GetId()] = Activity.Current!.Context; _contexts[eventData.Message.GetId()] = Activity.Current!.Context;
} }


正在加载...
取消
保存