我尝试在使用 WCF 的应用程序中将 OpenTelemetry 与控制台导出器集成。问题是,我当前的跟踪 idSystem.Diagnostics.DiagnosticSource.Activity
没有传播到另一端。因此,另一方面,父信息丢失并且跟踪被破坏。
我确实在检查日志时打开了 WCF 中的 ActivityPropagation。
问题是,我在另一边的活动没有/错误的活动 ID。
目前我不知道我还能做些什么来传播 System.Diagnostics.DiagnosticSource.Activity.Id
WCF
- 如何正确设置 OpenTelemetry 或 WCF 以传播 Activity Id/Context?
代码基本上只有 microsoft WCF Calculator Tutorial。我尝试将 OpenTelemetry 集成到其中。
这些是我用于 OpenTelemetry 的部分
客户:
public class Program
{
private static readonly ActivitySource MyActivitySource = new ActivitySource("MyCompany.MyProduct.MyClient");
public static void Main(string[] args)
{
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyClient")
.AddConsoleExporter()
.Build();
using (var activity_wcf_session = MyActivitySource.StartActivity("WCF_Session"))
{
activity_wcf_session?.SetTag("index", 0);
//Step 1: Create an instance of the WCF proxy.
CalculatorClient client = new CalculatorClient();
// Step 2: Call the service operations.
// Call the Add service operation.
using (var activity = MyActivitySource.StartActivity("Client_call_add()"))
{
activity?.SetTag("msg", "foo");
double result = client.Add(100.00D, 15.99D);
}
// Step 3: Close the client to gracefully close the connection and clean up resources.
Console.WriteLine("\nPress <Enter> to terminate the wcf client.");
Console.ReadLine();
client.Close();
}
}
}
服务:
public class CalculatorService : ICalculator
{
private static readonly ActivitySource MyActivitySource = new ActivitySource(
"MyCompany.MyProduct.CalculatorService");
private TracerProvider tracerProvider;
public CalculatorService()
{
tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.CalculatorService")
.AddConsoleExporter()
.Build();
}
public double Add(double n1, double n2)
{
Console.WriteLine("Activity.Current is null: " + (Activity.Current == null)); // is always null
using (var activity = MyActivitySource.StartActivity("CalculatorService_add()", ActivityKind.Server))
{
// activity.parent is not set
double result = n1 + n2;
return result;
}
}
// ...
}
}
这就是我在项目中激活活动传播的方式(主机相同)
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information,ActivityTracing" propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\logs\GettingStarted_Client.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="xml" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>