db620d33df
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
56 lines
2.6 KiB
C#
56 lines
2.6 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
#pragma warning disable VSTHRD002 // Synchronous waits are required by OpenTelemetry enrichment callbacks.
|
|
|
|
using OpenTelemetry;
|
|
using OpenTelemetry.Trace;
|
|
|
|
namespace Harness.Shared.Console;
|
|
|
|
/// <summary>
|
|
/// Provides factory methods for creating pre-configured OpenTelemetry tracing for harness samples.
|
|
/// </summary>
|
|
public static class HarnessTracing
|
|
{
|
|
/// <summary>
|
|
/// Creates a <see cref="TracerProvider"/> that captures spans from the specified source and HTTP client activity,
|
|
/// enriching HTTP spans with full request/response headers and bodies, and exports all spans to a timestamped
|
|
/// text file in the application base directory.
|
|
/// </summary>
|
|
/// <param name="sourceName">The activity source name to subscribe to (e.g., "Harness.Research").</param>
|
|
/// <returns>A configured <see cref="TracerProvider"/>, or <see langword="null"/> if the builder returns null.</returns>
|
|
public static TracerProvider? CreateFileTracerProvider(string sourceName)
|
|
{
|
|
var traceLogPath = Path.Combine(AppContext.BaseDirectory, $"traces_{DateTime.UtcNow:yyyyMMdd_HHmmss}_{Guid.NewGuid()}.log");
|
|
|
|
return Sdk.CreateTracerProviderBuilder()
|
|
.AddSource(sourceName)
|
|
.AddHttpClientInstrumentation((options) =>
|
|
{
|
|
options.EnrichWithHttpRequestMessage = (activity, request) =>
|
|
{
|
|
activity.SetTag("http.request.headers", request.Headers.ToString());
|
|
if (request.Content != null)
|
|
{
|
|
activity.SetTag("http.request.content.headers", request.Content.Headers.ToString());
|
|
var content = request.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
activity.SetTag("http.request.content.body", content);
|
|
}
|
|
};
|
|
|
|
options.EnrichWithHttpResponseMessage = (activity, response) =>
|
|
{
|
|
activity.SetTag("http.response.headers", response.Headers.ToString());
|
|
if (response.Content != null)
|
|
{
|
|
activity.SetTag("http.response.content.headers", response.Content.Headers.ToString());
|
|
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
activity.SetTag("http.response.content.body", content);
|
|
}
|
|
};
|
|
})
|
|
.AddProcessor(new SimpleActivityExportProcessor(new FileSpanExporter(traceLogPath)))
|
|
.Build();
|
|
}
|
|
}
|